Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1 | /* File object implementation */ |
| 2 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 3 | #define PY_SSIZE_T_CLEAN |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4 | #include "Python.h" |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 5 | #include "structmember.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6 | |
Martin v. Löwis | 0e8bd7e | 2006-06-10 12:23:46 +0000 | [diff] [blame] | 7 | #ifdef HAVE_SYS_TYPES_H |
Guido van Rossum | 4149843 | 1999-01-07 22:09:51 +0000 | [diff] [blame] | 8 | #include <sys/types.h> |
Martin v. Löwis | 0e8bd7e | 2006-06-10 12:23:46 +0000 | [diff] [blame] | 9 | #endif /* HAVE_SYS_TYPES_H */ |
Guido van Rossum | 4149843 | 1999-01-07 22:09:51 +0000 | [diff] [blame] | 10 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 11 | #ifdef MS_WINDOWS |
Guido van Rossum | b819914 | 1997-05-06 15:23:24 +0000 | [diff] [blame] | 12 | #define fileno _fileno |
Tim Peters | fb05db2 | 2002-03-11 00:24:00 +0000 | [diff] [blame] | 13 | /* can simulate truncate with Win32 API functions; see file_truncate */ |
Guido van Rossum | b819914 | 1997-05-06 15:23:24 +0000 | [diff] [blame] | 14 | #define HAVE_FTRUNCATE |
Tim Peters | 7a1f917 | 2002-07-14 22:14:19 +0000 | [diff] [blame] | 15 | #define WIN32_LEAN_AND_MEAN |
Tim Peters | fb05db2 | 2002-03-11 00:24:00 +0000 | [diff] [blame] | 16 | #include <windows.h> |
Guido van Rossum | b819914 | 1997-05-06 15:23:24 +0000 | [diff] [blame] | 17 | #endif |
| 18 | |
Andrew MacIntyre | c487439 | 2002-02-26 11:36:35 +0000 | [diff] [blame] | 19 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 20 | #include <io.h> |
| 21 | #endif |
| 22 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 23 | #define BUF(v) PyString_AS_STRING((PyStringObject *)v) |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 24 | |
Andrew M. Kuchling | 00b6a5c | 2010-02-22 23:10:52 +0000 | [diff] [blame] | 25 | #ifdef HAVE_ERRNO_H |
Guido van Rossum | f1dc566 | 1993-07-05 10:31:29 +0000 | [diff] [blame] | 26 | #include <errno.h> |
Guido van Rossum | ff7e83d | 1999-08-27 20:39:37 +0000 | [diff] [blame] | 27 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 28 | |
Jack Jansen | 7b8c754 | 2002-04-14 20:12:41 +0000 | [diff] [blame] | 29 | #ifdef HAVE_GETC_UNLOCKED |
| 30 | #define GETC(f) getc_unlocked(f) |
| 31 | #define FLOCKFILE(f) flockfile(f) |
| 32 | #define FUNLOCKFILE(f) funlockfile(f) |
| 33 | #else |
| 34 | #define GETC(f) getc(f) |
| 35 | #define FLOCKFILE(f) |
| 36 | #define FUNLOCKFILE(f) |
| 37 | #endif |
| 38 | |
Jack Jansen | 7b8c754 | 2002-04-14 20:12:41 +0000 | [diff] [blame] | 39 | /* Bits in f_newlinetypes */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 40 | #define NEWLINE_UNKNOWN 0 /* No newline seen, yet */ |
| 41 | #define NEWLINE_CR 1 /* \r newline seen */ |
| 42 | #define NEWLINE_LF 2 /* \n newline seen */ |
| 43 | #define NEWLINE_CRLF 4 /* \r\n newline seen */ |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 44 | |
Gregory P. Smith | aa63d0d | 2008-04-06 23:11:17 +0000 | [diff] [blame] | 45 | /* |
| 46 | * These macros release the GIL while preventing the f_close() function being |
| 47 | * called in the interval between them. For that purpose, a running total of |
| 48 | * the number of currently running unlocked code sections is kept in |
| 49 | * the unlocked_count field of the PyFileObject. The close() method raises |
| 50 | * an IOError if that field is non-zero. See issue #815646, #595601. |
| 51 | */ |
| 52 | |
| 53 | #define FILE_BEGIN_ALLOW_THREADS(fobj) \ |
| 54 | { \ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 55 | fobj->unlocked_count++; \ |
| 56 | Py_BEGIN_ALLOW_THREADS |
Gregory P. Smith | aa63d0d | 2008-04-06 23:11:17 +0000 | [diff] [blame] | 57 | |
| 58 | #define FILE_END_ALLOW_THREADS(fobj) \ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 59 | Py_END_ALLOW_THREADS \ |
| 60 | fobj->unlocked_count--; \ |
| 61 | assert(fobj->unlocked_count >= 0); \ |
Gregory P. Smith | aa63d0d | 2008-04-06 23:11:17 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | #define FILE_ABORT_ALLOW_THREADS(fobj) \ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 65 | Py_BLOCK_THREADS \ |
| 66 | fobj->unlocked_count--; \ |
| 67 | assert(fobj->unlocked_count >= 0); |
Gregory P. Smith | aa63d0d | 2008-04-06 23:11:17 +0000 | [diff] [blame] | 68 | |
Anthony Baxter | ac6bd46 | 2006-04-13 02:06:09 +0000 | [diff] [blame] | 69 | #ifdef __cplusplus |
| 70 | extern "C" { |
| 71 | #endif |
| 72 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 73 | FILE * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 74 | PyFile_AsFile(PyObject *f) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 75 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 76 | if (f == NULL || !PyFile_Check(f)) |
| 77 | return NULL; |
| 78 | else |
| 79 | return ((PyFileObject *)f)->f_fp; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Gregory P. Smith | aa63d0d | 2008-04-06 23:11:17 +0000 | [diff] [blame] | 82 | void PyFile_IncUseCount(PyFileObject *fobj) |
| 83 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 84 | fobj->unlocked_count++; |
Gregory P. Smith | aa63d0d | 2008-04-06 23:11:17 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | void PyFile_DecUseCount(PyFileObject *fobj) |
| 88 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 89 | fobj->unlocked_count--; |
| 90 | assert(fobj->unlocked_count >= 0); |
Gregory P. Smith | aa63d0d | 2008-04-06 23:11:17 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 93 | PyObject * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 94 | PyFile_Name(PyObject *f) |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 95 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 96 | if (f == NULL || !PyFile_Check(f)) |
| 97 | return NULL; |
| 98 | else |
| 99 | return ((PyFileObject *)f)->f_name; |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Gregory P. Smith | aa63d0d | 2008-04-06 23:11:17 +0000 | [diff] [blame] | 102 | /* This is a safe wrapper around PyObject_Print to print to the FILE |
| 103 | of a PyFileObject. PyObject_Print releases the GIL but knows nothing |
| 104 | about PyFileObject. */ |
| 105 | static int |
| 106 | file_PyObject_Print(PyObject *op, PyFileObject *f, int flags) |
| 107 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 108 | int result; |
| 109 | PyFile_IncUseCount(f); |
| 110 | result = PyObject_Print(op, f->f_fp, flags); |
| 111 | PyFile_DecUseCount(f); |
| 112 | return result; |
Gregory P. Smith | aa63d0d | 2008-04-06 23:11:17 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Neil Schemenauer | ed19b88 | 2002-03-23 02:06:50 +0000 | [diff] [blame] | 115 | /* On Unix, fopen will succeed for directories. |
| 116 | In Python, there should be no file objects referring to |
| 117 | directories, so we need a check. */ |
| 118 | |
| 119 | static PyFileObject* |
| 120 | dircheck(PyFileObject* f) |
| 121 | { |
| 122 | #if defined(HAVE_FSTAT) && defined(S_IFDIR) && defined(EISDIR) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 123 | struct stat buf; |
| 124 | if (f->f_fp == NULL) |
| 125 | return f; |
| 126 | if (fstat(fileno(f->f_fp), &buf) == 0 && |
| 127 | S_ISDIR(buf.st_mode)) { |
| 128 | char *msg = strerror(EISDIR); |
| 129 | PyObject *exc = PyObject_CallFunction(PyExc_IOError, "(isO)", |
| 130 | EISDIR, msg, f->f_name); |
| 131 | PyErr_SetObject(PyExc_IOError, exc); |
| 132 | Py_XDECREF(exc); |
| 133 | return NULL; |
| 134 | } |
Neil Schemenauer | ed19b88 | 2002-03-23 02:06:50 +0000 | [diff] [blame] | 135 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 136 | return f; |
Neil Schemenauer | ed19b88 | 2002-03-23 02:06:50 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Tim Peters | 59c9a64 | 2001-09-13 05:38:56 +0000 | [diff] [blame] | 139 | |
| 140 | static PyObject * |
Nicholas Bastin | abce8a6 | 2004-03-21 20:24:07 +0000 | [diff] [blame] | 141 | fill_file_fields(PyFileObject *f, FILE *fp, PyObject *name, char *mode, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 142 | int (*close)(FILE *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 143 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 144 | assert(name != NULL); |
| 145 | assert(f != NULL); |
| 146 | assert(PyFile_Check(f)); |
| 147 | assert(f->f_fp == NULL); |
Tim Peters | 4441001 | 2001-09-14 03:26:08 +0000 | [diff] [blame] | 148 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 149 | Py_DECREF(f->f_name); |
| 150 | Py_DECREF(f->f_mode); |
| 151 | Py_DECREF(f->f_encoding); |
| 152 | Py_DECREF(f->f_errors); |
Nicholas Bastin | abce8a6 | 2004-03-21 20:24:07 +0000 | [diff] [blame] | 153 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 154 | Py_INCREF(name); |
| 155 | f->f_name = name; |
Nicholas Bastin | abce8a6 | 2004-03-21 20:24:07 +0000 | [diff] [blame] | 156 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 157 | f->f_mode = PyString_FromString(mode); |
Tim Peters | 4441001 | 2001-09-14 03:26:08 +0000 | [diff] [blame] | 158 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 159 | f->f_close = close; |
| 160 | f->f_softspace = 0; |
| 161 | f->f_binary = strchr(mode,'b') != NULL; |
| 162 | f->f_buf = NULL; |
| 163 | f->f_univ_newline = (strchr(mode, 'U') != NULL); |
| 164 | f->f_newlinetypes = NEWLINE_UNKNOWN; |
| 165 | f->f_skipnextlf = 0; |
| 166 | Py_INCREF(Py_None); |
| 167 | f->f_encoding = Py_None; |
| 168 | Py_INCREF(Py_None); |
| 169 | f->f_errors = Py_None; |
| 170 | f->readable = f->writable = 0; |
| 171 | if (strchr(mode, 'r') != NULL || f->f_univ_newline) |
| 172 | f->readable = 1; |
| 173 | if (strchr(mode, 'w') != NULL || strchr(mode, 'a') != NULL) |
| 174 | f->writable = 1; |
| 175 | if (strchr(mode, '+') != NULL) |
| 176 | f->readable = f->writable = 1; |
Tim Peters | f1827cf | 2003-09-07 03:30:18 +0000 | [diff] [blame] | 177 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 178 | if (f->f_mode == NULL) |
| 179 | return NULL; |
| 180 | f->f_fp = fp; |
| 181 | f = dircheck(f); |
| 182 | return (PyObject *) f; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 183 | } |
| 184 | |
Kristján Valur Jónsson | fd4c872 | 2009-02-04 10:05:25 +0000 | [diff] [blame] | 185 | #if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__) |
| 186 | #define Py_VERIFY_WINNT |
| 187 | /* The CRT on windows compiled with Visual Studio 2005 and higher may |
| 188 | * assert if given invalid mode strings. This is all fine and well |
| 189 | * in static languages like C where the mode string is typcially hard |
| 190 | * coded. But in Python, were we pass in the mode string from the user, |
| 191 | * we need to verify it first manually |
| 192 | */ |
| 193 | static int _PyVerify_Mode_WINNT(const char *mode) |
| 194 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 195 | /* See if mode string is valid on Windows to avoid hard assertions */ |
| 196 | /* remove leading spacese */ |
| 197 | int singles = 0; |
| 198 | int pairs = 0; |
| 199 | int encoding = 0; |
| 200 | const char *s, *c; |
Kristján Valur Jónsson | fd4c872 | 2009-02-04 10:05:25 +0000 | [diff] [blame] | 201 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 202 | while(*mode == ' ') /* strip initial spaces */ |
| 203 | ++mode; |
| 204 | if (!strchr("rwa", *mode)) /* must start with one of these */ |
| 205 | return 0; |
| 206 | while (*++mode) { |
| 207 | if (*mode == ' ' || *mode == 'N') /* ignore spaces and N */ |
| 208 | continue; |
| 209 | s = "+TD"; /* each of this can appear only once */ |
| 210 | c = strchr(s, *mode); |
| 211 | if (c) { |
| 212 | ptrdiff_t idx = s-c; |
| 213 | if (singles & (1<<idx)) |
| 214 | return 0; |
| 215 | singles |= (1<<idx); |
| 216 | continue; |
| 217 | } |
| 218 | s = "btcnSR"; /* only one of each letter in the pairs allowed */ |
| 219 | c = strchr(s, *mode); |
| 220 | if (c) { |
| 221 | ptrdiff_t idx = (s-c)/2; |
| 222 | if (pairs & (1<<idx)) |
| 223 | return 0; |
| 224 | pairs |= (1<<idx); |
| 225 | continue; |
| 226 | } |
| 227 | if (*mode == ',') { |
| 228 | encoding = 1; |
| 229 | break; |
| 230 | } |
| 231 | return 0; /* found an invalid char */ |
| 232 | } |
Kristján Valur Jónsson | fd4c872 | 2009-02-04 10:05:25 +0000 | [diff] [blame] | 233 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 234 | if (encoding) { |
| 235 | char *e[] = {"UTF-8", "UTF-16LE", "UNICODE"}; |
| 236 | while (*mode == ' ') |
| 237 | ++mode; |
| 238 | /* find 'ccs =' */ |
| 239 | if (strncmp(mode, "ccs", 3)) |
| 240 | return 0; |
| 241 | mode += 3; |
| 242 | while (*mode == ' ') |
| 243 | ++mode; |
| 244 | if (*mode != '=') |
| 245 | return 0; |
| 246 | while (*mode == ' ') |
| 247 | ++mode; |
| 248 | for(encoding = 0; encoding<_countof(e); ++encoding) { |
| 249 | size_t l = strlen(e[encoding]); |
| 250 | if (!strncmp(mode, e[encoding], l)) { |
| 251 | mode += l; /* found a valid encoding */ |
| 252 | break; |
| 253 | } |
| 254 | } |
| 255 | if (encoding == _countof(e)) |
| 256 | return 0; |
| 257 | } |
| 258 | /* skip trailing spaces */ |
| 259 | while (*mode == ' ') |
| 260 | ++mode; |
Kristján Valur Jónsson | fd4c872 | 2009-02-04 10:05:25 +0000 | [diff] [blame] | 261 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 262 | return *mode == '\0'; /* must be at the end of the string */ |
Kristján Valur Jónsson | fd4c872 | 2009-02-04 10:05:25 +0000 | [diff] [blame] | 263 | } |
| 264 | #endif |
| 265 | |
Skip Montanaro | bbf12ba | 2005-05-20 03:07:06 +0000 | [diff] [blame] | 266 | /* check for known incorrect mode strings - problem is, platforms are |
| 267 | free to accept any mode characters they like and are supposed to |
| 268 | ignore stuff they don't understand... write or append mode with |
Georg Brandl | 7b90e16 | 2006-05-18 07:01:27 +0000 | [diff] [blame] | 269 | universal newline support is expressly forbidden by PEP 278. |
| 270 | Additionally, remove the 'U' from the mode string as platforms |
Kristján Valur Jónsson | 0a440d4 | 2007-04-26 09:15:08 +0000 | [diff] [blame] | 271 | won't know what it is. Non-zero return signals an exception */ |
| 272 | int |
| 273 | _PyFile_SanitizeMode(char *mode) |
Skip Montanaro | bbf12ba | 2005-05-20 03:07:06 +0000 | [diff] [blame] | 274 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 275 | char *upos; |
| 276 | size_t len = strlen(mode); |
Skip Montanaro | bbf12ba | 2005-05-20 03:07:06 +0000 | [diff] [blame] | 277 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 278 | if (!len) { |
| 279 | PyErr_SetString(PyExc_ValueError, "empty mode string"); |
| 280 | return -1; |
| 281 | } |
Skip Montanaro | bbf12ba | 2005-05-20 03:07:06 +0000 | [diff] [blame] | 282 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 283 | upos = strchr(mode, 'U'); |
| 284 | if (upos) { |
| 285 | memmove(upos, upos+1, len-(upos-mode)); /* incl null char */ |
Skip Montanaro | bbf12ba | 2005-05-20 03:07:06 +0000 | [diff] [blame] | 286 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 287 | if (mode[0] == 'w' || mode[0] == 'a') { |
| 288 | PyErr_Format(PyExc_ValueError, "universal newline " |
| 289 | "mode can only be used with modes " |
| 290 | "starting with 'r'"); |
| 291 | return -1; |
| 292 | } |
Georg Brandl | 7b90e16 | 2006-05-18 07:01:27 +0000 | [diff] [blame] | 293 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 294 | if (mode[0] != 'r') { |
| 295 | memmove(mode+1, mode, strlen(mode)+1); |
| 296 | mode[0] = 'r'; |
| 297 | } |
Georg Brandl | 7b90e16 | 2006-05-18 07:01:27 +0000 | [diff] [blame] | 298 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 299 | if (!strchr(mode, 'b')) { |
| 300 | memmove(mode+2, mode+1, strlen(mode)); |
| 301 | mode[1] = 'b'; |
| 302 | } |
| 303 | } else if (mode[0] != 'r' && mode[0] != 'w' && mode[0] != 'a') { |
| 304 | PyErr_Format(PyExc_ValueError, "mode string must begin with " |
| 305 | "one of 'r', 'w', 'a' or 'U', not '%.200s'", mode); |
| 306 | return -1; |
| 307 | } |
Kristján Valur Jónsson | fd4c872 | 2009-02-04 10:05:25 +0000 | [diff] [blame] | 308 | #ifdef Py_VERIFY_WINNT |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 309 | /* additional checks on NT with visual studio 2005 and higher */ |
| 310 | if (!_PyVerify_Mode_WINNT(mode)) { |
| 311 | PyErr_Format(PyExc_ValueError, "Invalid mode ('%.50s')", mode); |
| 312 | return -1; |
| 313 | } |
Kristján Valur Jónsson | fd4c872 | 2009-02-04 10:05:25 +0000 | [diff] [blame] | 314 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 315 | return 0; |
Skip Montanaro | bbf12ba | 2005-05-20 03:07:06 +0000 | [diff] [blame] | 316 | } |
| 317 | |
Tim Peters | 59c9a64 | 2001-09-13 05:38:56 +0000 | [diff] [blame] | 318 | static PyObject * |
| 319 | open_the_file(PyFileObject *f, char *name, char *mode) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 320 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 321 | char *newmode; |
| 322 | assert(f != NULL); |
| 323 | assert(PyFile_Check(f)); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 324 | #ifdef MS_WINDOWS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 325 | /* windows ignores the passed name in order to support Unicode */ |
| 326 | assert(f->f_name != NULL); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 327 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 328 | assert(name != NULL); |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 329 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 330 | assert(mode != NULL); |
| 331 | assert(f->f_fp == NULL); |
Tim Peters | 59c9a64 | 2001-09-13 05:38:56 +0000 | [diff] [blame] | 332 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 333 | /* probably need to replace 'U' by 'rb' */ |
| 334 | newmode = PyMem_MALLOC(strlen(mode) + 3); |
| 335 | if (!newmode) { |
| 336 | PyErr_NoMemory(); |
| 337 | return NULL; |
| 338 | } |
| 339 | strcpy(newmode, mode); |
Georg Brandl | 7b90e16 | 2006-05-18 07:01:27 +0000 | [diff] [blame] | 340 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 341 | if (_PyFile_SanitizeMode(newmode)) { |
| 342 | f = NULL; |
| 343 | goto cleanup; |
| 344 | } |
Skip Montanaro | bbf12ba | 2005-05-20 03:07:06 +0000 | [diff] [blame] | 345 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 346 | /* rexec.py can't stop a user from getting the file() constructor -- |
| 347 | all they have to do is get *any* file object f, and then do |
| 348 | type(f). Here we prevent them from doing damage with it. */ |
| 349 | if (PyEval_GetRestricted()) { |
| 350 | PyErr_SetString(PyExc_IOError, |
| 351 | "file() constructor not accessible in restricted mode"); |
| 352 | f = NULL; |
| 353 | goto cleanup; |
| 354 | } |
| 355 | errno = 0; |
Skip Montanaro | 51ffac6 | 2004-06-11 04:49:03 +0000 | [diff] [blame] | 356 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 357 | #ifdef MS_WINDOWS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 358 | if (PyUnicode_Check(f->f_name)) { |
| 359 | PyObject *wmode; |
| 360 | wmode = PyUnicode_DecodeASCII(newmode, strlen(newmode), NULL); |
| 361 | if (f->f_name && wmode) { |
| 362 | FILE_BEGIN_ALLOW_THREADS(f) |
| 363 | /* PyUnicode_AS_UNICODE OK without thread |
| 364 | lock as it is a simple dereference. */ |
| 365 | f->f_fp = _wfopen(PyUnicode_AS_UNICODE(f->f_name), |
| 366 | PyUnicode_AS_UNICODE(wmode)); |
| 367 | FILE_END_ALLOW_THREADS(f) |
| 368 | } |
| 369 | Py_XDECREF(wmode); |
| 370 | } |
Skip Montanaro | 51ffac6 | 2004-06-11 04:49:03 +0000 | [diff] [blame] | 371 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 372 | if (NULL == f->f_fp && NULL != name) { |
| 373 | FILE_BEGIN_ALLOW_THREADS(f) |
| 374 | f->f_fp = fopen(name, newmode); |
| 375 | FILE_END_ALLOW_THREADS(f) |
| 376 | } |
Skip Montanaro | 51ffac6 | 2004-06-11 04:49:03 +0000 | [diff] [blame] | 377 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 378 | if (f->f_fp == NULL) { |
Kristján Valur Jónsson | 74c3ea0 | 2006-07-03 14:59:05 +0000 | [diff] [blame] | 379 | #if defined _MSC_VER && (_MSC_VER < 1400 || !defined(__STDC_SECURE_LIB__)) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 380 | /* MSVC 6 (Microsoft) leaves errno at 0 for bad mode strings, |
| 381 | * across all Windows flavors. When it sets EINVAL varies |
| 382 | * across Windows flavors, the exact conditions aren't |
| 383 | * documented, and the answer lies in the OS's implementation |
| 384 | * of Win32's CreateFile function (whose source is secret). |
| 385 | * Seems the best we can do is map EINVAL to ENOENT. |
| 386 | * Starting with Visual Studio .NET 2005, EINVAL is correctly |
| 387 | * set by our CRT error handler (set in exceptions.c.) |
| 388 | */ |
| 389 | if (errno == 0) /* bad mode string */ |
| 390 | errno = EINVAL; |
| 391 | else if (errno == EINVAL) /* unknown, but not a mode string */ |
| 392 | errno = ENOENT; |
Tim Peters | 2ea9111 | 2002-04-08 04:13:12 +0000 | [diff] [blame] | 393 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 394 | /* EINVAL is returned when an invalid filename or |
| 395 | * an invalid mode is supplied. */ |
| 396 | if (errno == EINVAL) { |
| 397 | PyObject *v; |
| 398 | char message[100]; |
| 399 | PyOS_snprintf(message, 100, |
| 400 | "invalid mode ('%.50s') or filename", mode); |
| 401 | v = Py_BuildValue("(isO)", errno, message, f->f_name); |
| 402 | if (v != NULL) { |
| 403 | PyErr_SetObject(PyExc_IOError, v); |
| 404 | Py_DECREF(v); |
| 405 | } |
| 406 | } |
| 407 | else |
| 408 | PyErr_SetFromErrnoWithFilenameObject(PyExc_IOError, f->f_name); |
| 409 | f = NULL; |
| 410 | } |
| 411 | if (f != NULL) |
| 412 | f = dircheck(f); |
Georg Brandl | 7b90e16 | 2006-05-18 07:01:27 +0000 | [diff] [blame] | 413 | |
| 414 | cleanup: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 415 | PyMem_FREE(newmode); |
Georg Brandl | 7b90e16 | 2006-05-18 07:01:27 +0000 | [diff] [blame] | 416 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 417 | return (PyObject *)f; |
Tim Peters | 59c9a64 | 2001-09-13 05:38:56 +0000 | [diff] [blame] | 418 | } |
| 419 | |
Gregory P. Smith | aa63d0d | 2008-04-06 23:11:17 +0000 | [diff] [blame] | 420 | static PyObject * |
| 421 | close_the_file(PyFileObject *f) |
| 422 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 423 | int sts = 0; |
| 424 | int (*local_close)(FILE *); |
| 425 | FILE *local_fp = f->f_fp; |
Antoine Pitrou | 638cee6 | 2010-10-28 14:50:57 +0000 | [diff] [blame] | 426 | char *local_setbuf = f->f_setbuf; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 427 | if (local_fp != NULL) { |
| 428 | local_close = f->f_close; |
| 429 | if (local_close != NULL && f->unlocked_count > 0) { |
Benjamin Peterson | a72d15c | 2017-09-13 21:20:29 -0700 | [diff] [blame^] | 430 | if (Py_REFCNT(f) > 0) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 431 | PyErr_SetString(PyExc_IOError, |
| 432 | "close() called during concurrent " |
| 433 | "operation on the same file object."); |
| 434 | } else { |
| 435 | /* This should not happen unless someone is |
| 436 | * carelessly playing with the PyFileObject |
| 437 | * struct fields and/or its associated FILE |
| 438 | * pointer. */ |
| 439 | PyErr_SetString(PyExc_SystemError, |
| 440 | "PyFileObject locking error in " |
| 441 | "destructor (refcnt <= 0 at close)."); |
| 442 | } |
| 443 | return NULL; |
| 444 | } |
| 445 | /* NULL out the FILE pointer before releasing the GIL, because |
| 446 | * it will not be valid anymore after the close() function is |
| 447 | * called. */ |
| 448 | f->f_fp = NULL; |
| 449 | if (local_close != NULL) { |
Antoine Pitrou | 638cee6 | 2010-10-28 14:50:57 +0000 | [diff] [blame] | 450 | /* Issue #9295: must temporarily reset f_setbuf so that another |
| 451 | thread doesn't free it when running file_close() concurrently. |
| 452 | Otherwise this close() will crash when flushing the buffer. */ |
| 453 | f->f_setbuf = NULL; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 454 | Py_BEGIN_ALLOW_THREADS |
| 455 | errno = 0; |
| 456 | sts = (*local_close)(local_fp); |
| 457 | Py_END_ALLOW_THREADS |
Antoine Pitrou | 638cee6 | 2010-10-28 14:50:57 +0000 | [diff] [blame] | 458 | f->f_setbuf = local_setbuf; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 459 | if (sts == EOF) |
| 460 | return PyErr_SetFromErrno(PyExc_IOError); |
| 461 | if (sts != 0) |
| 462 | return PyInt_FromLong((long)sts); |
| 463 | } |
| 464 | } |
| 465 | Py_RETURN_NONE; |
Gregory P. Smith | aa63d0d | 2008-04-06 23:11:17 +0000 | [diff] [blame] | 466 | } |
| 467 | |
Tim Peters | 59c9a64 | 2001-09-13 05:38:56 +0000 | [diff] [blame] | 468 | PyObject * |
| 469 | PyFile_FromFile(FILE *fp, char *name, char *mode, int (*close)(FILE *)) |
| 470 | { |
Victor Stinner | 63c22fa | 2011-09-23 19:37:03 +0200 | [diff] [blame] | 471 | PyFileObject *f; |
| 472 | PyObject *o_name; |
| 473 | |
| 474 | f = (PyFileObject *)PyFile_Type.tp_new(&PyFile_Type, NULL, NULL); |
| 475 | if (f == NULL) |
| 476 | return NULL; |
| 477 | o_name = PyString_FromString(name); |
| 478 | if (o_name == NULL) { |
| 479 | if (close != NULL && fp != NULL) |
| 480 | close(fp); |
| 481 | Py_DECREF(f); |
| 482 | return NULL; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 483 | } |
Victor Stinner | 63c22fa | 2011-09-23 19:37:03 +0200 | [diff] [blame] | 484 | if (fill_file_fields(f, fp, o_name, mode, close) == NULL) { |
| 485 | Py_DECREF(f); |
| 486 | Py_DECREF(o_name); |
| 487 | return NULL; |
| 488 | } |
| 489 | Py_DECREF(o_name); |
| 490 | return (PyObject *)f; |
Tim Peters | 59c9a64 | 2001-09-13 05:38:56 +0000 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | PyObject * |
| 494 | PyFile_FromString(char *name, char *mode) |
| 495 | { |
Antoine Pitrou | 02a3801 | 2012-04-05 14:07:52 +0200 | [diff] [blame] | 496 | extern int fclose(FILE *); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 497 | PyFileObject *f; |
Tim Peters | 59c9a64 | 2001-09-13 05:38:56 +0000 | [diff] [blame] | 498 | |
Antoine Pitrou | 02a3801 | 2012-04-05 14:07:52 +0200 | [diff] [blame] | 499 | f = (PyFileObject *)PyFile_FromFile((FILE *)NULL, name, mode, fclose); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 500 | if (f != NULL) { |
| 501 | if (open_the_file(f, name, mode) == NULL) { |
| 502 | Py_DECREF(f); |
| 503 | f = NULL; |
| 504 | } |
| 505 | } |
| 506 | return (PyObject *)f; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 507 | } |
| 508 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 509 | void |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 510 | PyFile_SetBufSize(PyObject *f, int bufsize) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 511 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 512 | PyFileObject *file = (PyFileObject *)f; |
| 513 | if (bufsize >= 0) { |
| 514 | int type; |
| 515 | switch (bufsize) { |
| 516 | case 0: |
| 517 | type = _IONBF; |
| 518 | break; |
Martin v. Löwis | 1e3bdf6 | 2003-09-04 19:01:46 +0000 | [diff] [blame] | 519 | #ifdef HAVE_SETVBUF |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 520 | case 1: |
| 521 | type = _IOLBF; |
| 522 | bufsize = BUFSIZ; |
| 523 | break; |
Martin v. Löwis | 1e3bdf6 | 2003-09-04 19:01:46 +0000 | [diff] [blame] | 524 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 525 | default: |
| 526 | type = _IOFBF; |
Martin v. Löwis | 1e3bdf6 | 2003-09-04 19:01:46 +0000 | [diff] [blame] | 527 | #ifndef HAVE_SETVBUF |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 528 | bufsize = BUFSIZ; |
Martin v. Löwis | 1e3bdf6 | 2003-09-04 19:01:46 +0000 | [diff] [blame] | 529 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 530 | break; |
| 531 | } |
| 532 | fflush(file->f_fp); |
| 533 | if (type == _IONBF) { |
| 534 | PyMem_Free(file->f_setbuf); |
| 535 | file->f_setbuf = NULL; |
| 536 | } else { |
| 537 | file->f_setbuf = (char *)PyMem_Realloc(file->f_setbuf, |
| 538 | bufsize); |
| 539 | } |
Martin v. Löwis | 1e3bdf6 | 2003-09-04 19:01:46 +0000 | [diff] [blame] | 540 | #ifdef HAVE_SETVBUF |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 541 | setvbuf(file->f_fp, file->f_setbuf, type, bufsize); |
Guido van Rossum | f8b4de0 | 1998-03-06 15:32:40 +0000 | [diff] [blame] | 542 | #else /* !HAVE_SETVBUF */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 543 | setbuf(file->f_fp, file->f_setbuf); |
Guido van Rossum | f8b4de0 | 1998-03-06 15:32:40 +0000 | [diff] [blame] | 544 | #endif /* !HAVE_SETVBUF */ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 545 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 546 | } |
| 547 | |
Martin v. Löwis | 5467d4c | 2003-05-10 07:10:12 +0000 | [diff] [blame] | 548 | /* Set the encoding used to output Unicode strings. |
Martin v. Löwis | 9981589 | 2008-06-01 07:20:46 +0000 | [diff] [blame] | 549 | Return 1 on success, 0 on failure. */ |
Martin v. Löwis | 5467d4c | 2003-05-10 07:10:12 +0000 | [diff] [blame] | 550 | |
| 551 | int |
| 552 | PyFile_SetEncoding(PyObject *f, const char *enc) |
| 553 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 554 | return PyFile_SetEncodingAndErrors(f, enc, NULL); |
Martin v. Löwis | 9981589 | 2008-06-01 07:20:46 +0000 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | int |
| 558 | PyFile_SetEncodingAndErrors(PyObject *f, const char *enc, char* errors) |
| 559 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 560 | PyFileObject *file = (PyFileObject*)f; |
| 561 | PyObject *str, *oerrors; |
Thomas Wouters | afea529 | 2007-01-23 13:42:00 +0000 | [diff] [blame] | 562 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 563 | assert(PyFile_Check(f)); |
| 564 | str = PyString_FromString(enc); |
| 565 | if (!str) |
| 566 | return 0; |
| 567 | if (errors) { |
| 568 | oerrors = PyString_FromString(errors); |
| 569 | if (!oerrors) { |
| 570 | Py_DECREF(str); |
| 571 | return 0; |
| 572 | } |
| 573 | } else { |
| 574 | oerrors = Py_None; |
| 575 | Py_INCREF(Py_None); |
| 576 | } |
Serhiy Storchaka | 763a61c | 2016-04-10 18:05:12 +0300 | [diff] [blame] | 577 | Py_SETREF(file->f_encoding, str); |
| 578 | Py_SETREF(file->f_errors, oerrors); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 579 | return 1; |
Martin v. Löwis | 5467d4c | 2003-05-10 07:10:12 +0000 | [diff] [blame] | 580 | } |
| 581 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 582 | static PyObject * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 583 | err_closed(void) |
Guido van Rossum | d7297e6 | 1992-07-06 14:19:26 +0000 | [diff] [blame] | 584 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 585 | PyErr_SetString(PyExc_ValueError, "I/O operation on closed file"); |
| 586 | return NULL; |
Guido van Rossum | d7297e6 | 1992-07-06 14:19:26 +0000 | [diff] [blame] | 587 | } |
| 588 | |
Antoine Pitrou | bb445a1 | 2010-02-05 17:05:54 +0000 | [diff] [blame] | 589 | static PyObject * |
| 590 | err_mode(char *action) |
| 591 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 592 | PyErr_Format(PyExc_IOError, "File not open for %s", action); |
| 593 | return NULL; |
Antoine Pitrou | bb445a1 | 2010-02-05 17:05:54 +0000 | [diff] [blame] | 594 | } |
| 595 | |
Thomas Wouters | c45251a | 2006-02-12 11:53:32 +0000 | [diff] [blame] | 596 | /* Refuse regular file I/O if there's data in the iteration-buffer. |
| 597 | * Mixing them would cause data to arrive out of order, as the read* |
| 598 | * methods don't use the iteration buffer. */ |
| 599 | static PyObject * |
| 600 | err_iterbuffered(void) |
| 601 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 602 | PyErr_SetString(PyExc_ValueError, |
| 603 | "Mixing iteration and read methods would lose data"); |
| 604 | return NULL; |
Thomas Wouters | c45251a | 2006-02-12 11:53:32 +0000 | [diff] [blame] | 605 | } |
| 606 | |
Neal Norwitz | d8b995f | 2002-08-06 21:50:54 +0000 | [diff] [blame] | 607 | static void drop_readahead(PyFileObject *); |
Guido van Rossum | 7a6e959 | 2002-08-06 15:55:28 +0000 | [diff] [blame] | 608 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 609 | /* Methods */ |
| 610 | |
| 611 | static void |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 612 | file_dealloc(PyFileObject *f) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 613 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 614 | PyObject *ret; |
| 615 | if (f->weakreflist != NULL) |
| 616 | PyObject_ClearWeakRefs((PyObject *) f); |
| 617 | ret = close_the_file(f); |
| 618 | if (!ret) { |
| 619 | PySys_WriteStderr("close failed in file object destructor:\n"); |
| 620 | PyErr_Print(); |
| 621 | } |
| 622 | else { |
| 623 | Py_DECREF(ret); |
| 624 | } |
| 625 | PyMem_Free(f->f_setbuf); |
| 626 | Py_XDECREF(f->f_name); |
| 627 | Py_XDECREF(f->f_mode); |
| 628 | Py_XDECREF(f->f_encoding); |
| 629 | Py_XDECREF(f->f_errors); |
| 630 | drop_readahead(f); |
| 631 | Py_TYPE(f)->tp_free((PyObject *)f); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 632 | } |
| 633 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 634 | static PyObject * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 635 | file_repr(PyFileObject *f) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 636 | { |
Ezio Melotti | 11f8b68 | 2012-03-12 01:17:02 +0200 | [diff] [blame] | 637 | PyObject *ret = NULL; |
| 638 | PyObject *name = NULL; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 639 | if (PyUnicode_Check(f->f_name)) { |
Martin v. Löwis | 0073f2e | 2002-11-21 23:52:35 +0000 | [diff] [blame] | 640 | #ifdef Py_USING_UNICODE |
Ezio Melotti | eace3a7 | 2012-03-12 01:28:45 +0200 | [diff] [blame] | 641 | const char *name_str; |
Ezio Melotti | 11f8b68 | 2012-03-12 01:17:02 +0200 | [diff] [blame] | 642 | name = PyUnicode_AsUnicodeEscapeString(f->f_name); |
Ezio Melotti | eace3a7 | 2012-03-12 01:28:45 +0200 | [diff] [blame] | 643 | name_str = name ? PyString_AsString(name) : "?"; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 644 | ret = PyString_FromFormat("<%s file u'%s', mode '%s' at %p>", |
| 645 | f->f_fp == NULL ? "closed" : "open", |
| 646 | name_str, |
| 647 | PyString_AsString(f->f_mode), |
| 648 | f); |
| 649 | Py_XDECREF(name); |
| 650 | return ret; |
Martin v. Löwis | 0073f2e | 2002-11-21 23:52:35 +0000 | [diff] [blame] | 651 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 652 | } else { |
Ezio Melotti | 11f8b68 | 2012-03-12 01:17:02 +0200 | [diff] [blame] | 653 | name = PyObject_Repr(f->f_name); |
| 654 | if (name == NULL) |
| 655 | return NULL; |
| 656 | ret = PyString_FromFormat("<%s file %s, mode '%s' at %p>", |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 657 | f->f_fp == NULL ? "closed" : "open", |
Ezio Melotti | 11f8b68 | 2012-03-12 01:17:02 +0200 | [diff] [blame] | 658 | PyString_AsString(name), |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 659 | PyString_AsString(f->f_mode), |
| 660 | f); |
Ezio Melotti | 11f8b68 | 2012-03-12 01:17:02 +0200 | [diff] [blame] | 661 | Py_XDECREF(name); |
| 662 | return ret; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 663 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 664 | } |
| 665 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 666 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 667 | file_close(PyFileObject *f) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 668 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 669 | PyObject *sts = close_the_file(f); |
Antoine Pitrou | 83137c2 | 2010-05-17 19:56:59 +0000 | [diff] [blame] | 670 | if (sts) { |
| 671 | PyMem_Free(f->f_setbuf); |
| 672 | f->f_setbuf = NULL; |
| 673 | } |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 674 | return sts; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 675 | } |
| 676 | |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 677 | |
Guido van Rossum | b855216 | 2001-09-05 14:58:11 +0000 | [diff] [blame] | 678 | /* Our very own off_t-like type, 64-bit if possible */ |
| 679 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 680 | typedef off_t Py_off_t; |
| 681 | #elif SIZEOF_OFF_T >= 8 |
| 682 | typedef off_t Py_off_t; |
| 683 | #elif SIZEOF_FPOS_T >= 8 |
Guido van Rossum | 4f53da0 | 2001-03-01 18:26:53 +0000 | [diff] [blame] | 684 | typedef fpos_t Py_off_t; |
| 685 | #else |
Guido van Rossum | b855216 | 2001-09-05 14:58:11 +0000 | [diff] [blame] | 686 | #error "Large file support, but neither off_t nor fpos_t is large enough." |
Guido van Rossum | 4f53da0 | 2001-03-01 18:26:53 +0000 | [diff] [blame] | 687 | #endif |
| 688 | |
| 689 | |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 690 | /* a portable fseek() function |
| 691 | return 0 on success, non-zero on failure (with errno set) */ |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 692 | static int |
Guido van Rossum | 4f53da0 | 2001-03-01 18:26:53 +0000 | [diff] [blame] | 693 | _portable_fseek(FILE *fp, Py_off_t offset, int whence) |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 694 | { |
Guido van Rossum | b855216 | 2001-09-05 14:58:11 +0000 | [diff] [blame] | 695 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 696 | return fseek(fp, offset, whence); |
Guido van Rossum | b855216 | 2001-09-05 14:58:11 +0000 | [diff] [blame] | 697 | #elif defined(HAVE_FSEEKO) && SIZEOF_OFF_T >= 8 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 698 | return fseeko(fp, offset, whence); |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 699 | #elif defined(HAVE_FSEEK64) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 700 | return fseek64(fp, offset, whence); |
Fred Drake | db810ac | 2000-10-06 20:42:33 +0000 | [diff] [blame] | 701 | #elif defined(__BEOS__) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 702 | return _fseek(fp, offset, whence); |
Guido van Rossum | b855216 | 2001-09-05 14:58:11 +0000 | [diff] [blame] | 703 | #elif SIZEOF_FPOS_T >= 8 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 704 | /* lacking a 64-bit capable fseek(), use a 64-bit capable fsetpos() |
| 705 | and fgetpos() to implement fseek()*/ |
| 706 | fpos_t pos; |
| 707 | switch (whence) { |
| 708 | case SEEK_END: |
Guido van Rossum | 8b4e43e | 2001-09-10 20:43:35 +0000 | [diff] [blame] | 709 | #ifdef MS_WINDOWS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 710 | fflush(fp); |
| 711 | if (_lseeki64(fileno(fp), 0, 2) == -1) |
| 712 | return -1; |
Guido van Rossum | 8b4e43e | 2001-09-10 20:43:35 +0000 | [diff] [blame] | 713 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 714 | if (fseek(fp, 0, SEEK_END) != 0) |
| 715 | return -1; |
Guido van Rossum | 8b4e43e | 2001-09-10 20:43:35 +0000 | [diff] [blame] | 716 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 717 | /* fall through */ |
| 718 | case SEEK_CUR: |
| 719 | if (fgetpos(fp, &pos) != 0) |
| 720 | return -1; |
| 721 | offset += pos; |
| 722 | break; |
| 723 | /* case SEEK_SET: break; */ |
| 724 | } |
| 725 | return fsetpos(fp, &offset); |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 726 | #else |
Guido van Rossum | b855216 | 2001-09-05 14:58:11 +0000 | [diff] [blame] | 727 | #error "Large file support, but no way to fseek." |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 728 | #endif |
| 729 | } |
| 730 | |
| 731 | |
| 732 | /* a portable ftell() function |
| 733 | Return -1 on failure with errno set appropriately, current file |
| 734 | position on success */ |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 735 | static Py_off_t |
Fred Drake | 8ce159a | 2000-08-31 05:18:54 +0000 | [diff] [blame] | 736 | _portable_ftell(FILE* fp) |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 737 | { |
Guido van Rossum | b855216 | 2001-09-05 14:58:11 +0000 | [diff] [blame] | 738 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 739 | return ftell(fp); |
Guido van Rossum | b855216 | 2001-09-05 14:58:11 +0000 | [diff] [blame] | 740 | #elif defined(HAVE_FTELLO) && SIZEOF_OFF_T >= 8 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 741 | return ftello(fp); |
Guido van Rossum | b855216 | 2001-09-05 14:58:11 +0000 | [diff] [blame] | 742 | #elif defined(HAVE_FTELL64) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 743 | return ftell64(fp); |
Guido van Rossum | b855216 | 2001-09-05 14:58:11 +0000 | [diff] [blame] | 744 | #elif SIZEOF_FPOS_T >= 8 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 745 | fpos_t pos; |
| 746 | if (fgetpos(fp, &pos) != 0) |
| 747 | return -1; |
| 748 | return pos; |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 749 | #else |
Guido van Rossum | b855216 | 2001-09-05 14:58:11 +0000 | [diff] [blame] | 750 | #error "Large file support, but no way to ftell." |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 751 | #endif |
| 752 | } |
| 753 | |
| 754 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 755 | static PyObject * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 756 | file_seek(PyFileObject *f, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 757 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 758 | int whence; |
| 759 | int ret; |
| 760 | Py_off_t offset; |
| 761 | PyObject *offobj, *off_index; |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 762 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 763 | if (f->f_fp == NULL) |
| 764 | return err_closed(); |
| 765 | drop_readahead(f); |
| 766 | whence = 0; |
| 767 | if (!PyArg_ParseTuple(args, "O|i:seek", &offobj, &whence)) |
| 768 | return NULL; |
| 769 | off_index = PyNumber_Index(offobj); |
| 770 | if (!off_index) { |
| 771 | if (!PyFloat_Check(offobj)) |
| 772 | return NULL; |
| 773 | /* Deprecated in 2.6 */ |
| 774 | PyErr_Clear(); |
| 775 | if (PyErr_WarnEx(PyExc_DeprecationWarning, |
| 776 | "integer argument expected, got float", |
| 777 | 1) < 0) |
| 778 | return NULL; |
| 779 | off_index = offobj; |
| 780 | Py_INCREF(offobj); |
| 781 | } |
Guido van Rossum | 3c9fe0c | 1999-01-06 18:51:17 +0000 | [diff] [blame] | 782 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 783 | offset = PyInt_AsLong(off_index); |
Guido van Rossum | 3c9fe0c | 1999-01-06 18:51:17 +0000 | [diff] [blame] | 784 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 785 | offset = PyLong_Check(off_index) ? |
| 786 | PyLong_AsLongLong(off_index) : PyInt_AsLong(off_index); |
Guido van Rossum | 3c9fe0c | 1999-01-06 18:51:17 +0000 | [diff] [blame] | 787 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 788 | Py_DECREF(off_index); |
| 789 | if (PyErr_Occurred()) |
| 790 | return NULL; |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 791 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 792 | FILE_BEGIN_ALLOW_THREADS(f) |
| 793 | errno = 0; |
| 794 | ret = _portable_fseek(f->f_fp, offset, whence); |
| 795 | FILE_END_ALLOW_THREADS(f) |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 796 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 797 | if (ret != 0) { |
| 798 | PyErr_SetFromErrno(PyExc_IOError); |
| 799 | clearerr(f->f_fp); |
| 800 | return NULL; |
| 801 | } |
| 802 | f->f_skipnextlf = 0; |
| 803 | Py_INCREF(Py_None); |
| 804 | return Py_None; |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 805 | } |
| 806 | |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 807 | |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 808 | #ifdef HAVE_FTRUNCATE |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 809 | static PyObject * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 810 | file_truncate(PyFileObject *f, PyObject *args) |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 811 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 812 | Py_off_t newsize; |
| 813 | PyObject *newsizeobj = NULL; |
| 814 | Py_off_t initialpos; |
| 815 | int ret; |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 816 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 817 | if (f->f_fp == NULL) |
| 818 | return err_closed(); |
| 819 | if (!f->writable) |
| 820 | return err_mode("writing"); |
| 821 | if (!PyArg_UnpackTuple(args, "truncate", 0, 1, &newsizeobj)) |
| 822 | return NULL; |
Tim Peters | fb05db2 | 2002-03-11 00:24:00 +0000 | [diff] [blame] | 823 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 824 | /* Get current file position. If the file happens to be open for |
| 825 | * update and the last operation was an input operation, C doesn't |
| 826 | * define what the later fflush() will do, but we promise truncate() |
| 827 | * won't change the current position (and fflush() *does* change it |
| 828 | * then at least on Windows). The easiest thing is to capture |
| 829 | * current pos now and seek back to it at the end. |
| 830 | */ |
| 831 | FILE_BEGIN_ALLOW_THREADS(f) |
| 832 | errno = 0; |
| 833 | initialpos = _portable_ftell(f->f_fp); |
| 834 | FILE_END_ALLOW_THREADS(f) |
| 835 | if (initialpos == -1) |
| 836 | goto onioerror; |
Tim Peters | f1827cf | 2003-09-07 03:30:18 +0000 | [diff] [blame] | 837 | |
Martin Panter | 8d496ad | 2016-06-02 10:35:44 +0000 | [diff] [blame] | 838 | /* Set newsize to current position if newsizeobj NULL, else to the |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 839 | * specified value. |
| 840 | */ |
| 841 | if (newsizeobj != NULL) { |
Guido van Rossum | 3c9fe0c | 1999-01-06 18:51:17 +0000 | [diff] [blame] | 842 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 843 | newsize = PyInt_AsLong(newsizeobj); |
Guido van Rossum | 3c9fe0c | 1999-01-06 18:51:17 +0000 | [diff] [blame] | 844 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 845 | newsize = PyLong_Check(newsizeobj) ? |
| 846 | PyLong_AsLongLong(newsizeobj) : |
| 847 | PyInt_AsLong(newsizeobj); |
Guido van Rossum | 3c9fe0c | 1999-01-06 18:51:17 +0000 | [diff] [blame] | 848 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 849 | if (PyErr_Occurred()) |
| 850 | return NULL; |
| 851 | } |
| 852 | else /* default to current position */ |
| 853 | newsize = initialpos; |
Tim Peters | fb05db2 | 2002-03-11 00:24:00 +0000 | [diff] [blame] | 854 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 855 | /* Flush the stream. We're mixing stream-level I/O with lower-level |
| 856 | * I/O, and a flush may be necessary to synch both platform views |
| 857 | * of the current file state. |
| 858 | */ |
| 859 | FILE_BEGIN_ALLOW_THREADS(f) |
| 860 | errno = 0; |
| 861 | ret = fflush(f->f_fp); |
| 862 | FILE_END_ALLOW_THREADS(f) |
| 863 | if (ret != 0) |
| 864 | goto onioerror; |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 865 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 866 | #ifdef MS_WINDOWS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 867 | /* MS _chsize doesn't work if newsize doesn't fit in 32 bits, |
| 868 | so don't even try using it. */ |
| 869 | { |
| 870 | HANDLE hFile; |
Tim Peters | fb05db2 | 2002-03-11 00:24:00 +0000 | [diff] [blame] | 871 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 872 | /* Have to move current pos to desired endpoint on Windows. */ |
| 873 | FILE_BEGIN_ALLOW_THREADS(f) |
| 874 | errno = 0; |
| 875 | ret = _portable_fseek(f->f_fp, newsize, SEEK_SET) != 0; |
| 876 | FILE_END_ALLOW_THREADS(f) |
| 877 | if (ret) |
| 878 | goto onioerror; |
Tim Peters | fb05db2 | 2002-03-11 00:24:00 +0000 | [diff] [blame] | 879 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 880 | /* Truncate. Note that this may grow the file! */ |
| 881 | FILE_BEGIN_ALLOW_THREADS(f) |
| 882 | errno = 0; |
| 883 | hFile = (HANDLE)_get_osfhandle(fileno(f->f_fp)); |
| 884 | ret = hFile == (HANDLE)-1; |
| 885 | if (ret == 0) { |
| 886 | ret = SetEndOfFile(hFile) == 0; |
| 887 | if (ret) |
| 888 | errno = EACCES; |
| 889 | } |
| 890 | FILE_END_ALLOW_THREADS(f) |
| 891 | if (ret) |
| 892 | goto onioerror; |
| 893 | } |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 894 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 895 | FILE_BEGIN_ALLOW_THREADS(f) |
| 896 | errno = 0; |
| 897 | ret = ftruncate(fileno(f->f_fp), newsize); |
| 898 | FILE_END_ALLOW_THREADS(f) |
| 899 | if (ret != 0) |
| 900 | goto onioerror; |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 901 | #endif /* !MS_WINDOWS */ |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 902 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 903 | /* Restore original file position. */ |
| 904 | FILE_BEGIN_ALLOW_THREADS(f) |
| 905 | errno = 0; |
| 906 | ret = _portable_fseek(f->f_fp, initialpos, SEEK_SET) != 0; |
| 907 | FILE_END_ALLOW_THREADS(f) |
| 908 | if (ret) |
| 909 | goto onioerror; |
Tim Peters | f1827cf | 2003-09-07 03:30:18 +0000 | [diff] [blame] | 910 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 911 | Py_INCREF(Py_None); |
| 912 | return Py_None; |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 913 | |
| 914 | onioerror: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 915 | PyErr_SetFromErrno(PyExc_IOError); |
| 916 | clearerr(f->f_fp); |
| 917 | return NULL; |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 918 | } |
| 919 | #endif /* HAVE_FTRUNCATE */ |
| 920 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 921 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 922 | file_tell(PyFileObject *f) |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 923 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 924 | Py_off_t pos; |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 925 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 926 | if (f->f_fp == NULL) |
| 927 | return err_closed(); |
| 928 | FILE_BEGIN_ALLOW_THREADS(f) |
| 929 | errno = 0; |
| 930 | pos = _portable_ftell(f->f_fp); |
| 931 | FILE_END_ALLOW_THREADS(f) |
Gregory P. Smith | aa63d0d | 2008-04-06 23:11:17 +0000 | [diff] [blame] | 932 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 933 | if (pos == -1) { |
| 934 | PyErr_SetFromErrno(PyExc_IOError); |
| 935 | clearerr(f->f_fp); |
| 936 | return NULL; |
| 937 | } |
| 938 | if (f->f_skipnextlf) { |
| 939 | int c; |
| 940 | c = GETC(f->f_fp); |
| 941 | if (c == '\n') { |
| 942 | f->f_newlinetypes |= NEWLINE_CRLF; |
| 943 | pos++; |
| 944 | f->f_skipnextlf = 0; |
| 945 | } else if (c != EOF) ungetc(c, f->f_fp); |
| 946 | } |
Guido van Rossum | 3c9fe0c | 1999-01-06 18:51:17 +0000 | [diff] [blame] | 947 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 948 | return PyInt_FromLong(pos); |
Guido van Rossum | 3c9fe0c | 1999-01-06 18:51:17 +0000 | [diff] [blame] | 949 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 950 | return PyLong_FromLongLong(pos); |
Guido van Rossum | 3c9fe0c | 1999-01-06 18:51:17 +0000 | [diff] [blame] | 951 | #endif |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 952 | } |
| 953 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 954 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 955 | file_fileno(PyFileObject *f) |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 956 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 957 | if (f->f_fp == NULL) |
| 958 | return err_closed(); |
| 959 | return PyInt_FromLong((long) fileno(f->f_fp)); |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 960 | } |
| 961 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 962 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 963 | file_flush(PyFileObject *f) |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 964 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 965 | int res; |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 966 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 967 | if (f->f_fp == NULL) |
| 968 | return err_closed(); |
| 969 | FILE_BEGIN_ALLOW_THREADS(f) |
| 970 | errno = 0; |
| 971 | res = fflush(f->f_fp); |
| 972 | FILE_END_ALLOW_THREADS(f) |
| 973 | if (res != 0) { |
| 974 | PyErr_SetFromErrno(PyExc_IOError); |
| 975 | clearerr(f->f_fp); |
| 976 | return NULL; |
| 977 | } |
| 978 | Py_INCREF(Py_None); |
| 979 | return Py_None; |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 980 | } |
| 981 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 982 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 983 | file_isatty(PyFileObject *f) |
Guido van Rossum | a1ab7fa | 1991-06-04 19:37:39 +0000 | [diff] [blame] | 984 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 985 | long res; |
| 986 | if (f->f_fp == NULL) |
| 987 | return err_closed(); |
| 988 | FILE_BEGIN_ALLOW_THREADS(f) |
| 989 | res = isatty((int)fileno(f->f_fp)); |
| 990 | FILE_END_ALLOW_THREADS(f) |
| 991 | return PyBool_FromLong(res); |
Guido van Rossum | a1ab7fa | 1991-06-04 19:37:39 +0000 | [diff] [blame] | 992 | } |
| 993 | |
Guido van Rossum | ff7e83d | 1999-08-27 20:39:37 +0000 | [diff] [blame] | 994 | |
Guido van Rossum | 5449b6e | 1997-05-09 22:27:31 +0000 | [diff] [blame] | 995 | #if BUFSIZ < 8192 |
| 996 | #define SMALLCHUNK 8192 |
| 997 | #else |
| 998 | #define SMALLCHUNK BUFSIZ |
| 999 | #endif |
| 1000 | |
Guido van Rossum | 5449b6e | 1997-05-09 22:27:31 +0000 | [diff] [blame] | 1001 | static size_t |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 1002 | new_buffersize(PyFileObject *f, size_t currentsize) |
Guido van Rossum | 5449b6e | 1997-05-09 22:27:31 +0000 | [diff] [blame] | 1003 | { |
| 1004 | #ifdef HAVE_FSTAT |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1005 | off_t pos, end; |
| 1006 | struct stat st; |
| 1007 | if (fstat(fileno(f->f_fp), &st) == 0) { |
| 1008 | end = st.st_size; |
| 1009 | /* The following is not a bug: we really need to call lseek() |
| 1010 | *and* ftell(). The reason is that some stdio libraries |
| 1011 | mistakenly flush their buffer when ftell() is called and |
| 1012 | the lseek() call it makes fails, thereby throwing away |
| 1013 | data that cannot be recovered in any way. To avoid this, |
| 1014 | we first test lseek(), and only call ftell() if lseek() |
| 1015 | works. We can't use the lseek() value either, because we |
| 1016 | need to take the amount of buffered data into account. |
| 1017 | (Yet another reason why stdio stinks. :-) */ |
| 1018 | pos = lseek(fileno(f->f_fp), 0L, SEEK_CUR); |
| 1019 | if (pos >= 0) { |
| 1020 | pos = ftell(f->f_fp); |
| 1021 | } |
| 1022 | if (pos < 0) |
| 1023 | clearerr(f->f_fp); |
| 1024 | if (end > pos && pos >= 0) |
| 1025 | return currentsize + end - pos + 1; |
| 1026 | /* Add 1 so if the file were to grow we'd notice. */ |
| 1027 | } |
Guido van Rossum | 5449b6e | 1997-05-09 22:27:31 +0000 | [diff] [blame] | 1028 | #endif |
Nadeem Vawda | 3624815 | 2011-10-13 13:52:46 +0200 | [diff] [blame] | 1029 | /* Expand the buffer by an amount proportional to the current size, |
| 1030 | giving us amortized linear-time behavior. Use a less-than-double |
| 1031 | growth factor to avoid excessive allocation. */ |
| 1032 | return currentsize + (currentsize >> 3) + 6; |
Guido van Rossum | 5449b6e | 1997-05-09 22:27:31 +0000 | [diff] [blame] | 1033 | } |
| 1034 | |
Gustavo Niemeyer | 786ddb2 | 2002-12-16 18:12:53 +0000 | [diff] [blame] | 1035 | #if defined(EWOULDBLOCK) && defined(EAGAIN) && EWOULDBLOCK != EAGAIN |
| 1036 | #define BLOCKED_ERRNO(x) ((x) == EWOULDBLOCK || (x) == EAGAIN) |
| 1037 | #else |
| 1038 | #ifdef EWOULDBLOCK |
| 1039 | #define BLOCKED_ERRNO(x) ((x) == EWOULDBLOCK) |
| 1040 | #else |
| 1041 | #ifdef EAGAIN |
| 1042 | #define BLOCKED_ERRNO(x) ((x) == EAGAIN) |
| 1043 | #else |
| 1044 | #define BLOCKED_ERRNO(x) 0 |
| 1045 | #endif |
| 1046 | #endif |
| 1047 | #endif |
| 1048 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1049 | static PyObject * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 1050 | file_read(PyFileObject *f, PyObject *args) |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 1051 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1052 | long bytesrequested = -1; |
| 1053 | size_t bytesread, buffersize, chunksize; |
| 1054 | PyObject *v; |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 1055 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1056 | if (f->f_fp == NULL) |
| 1057 | return err_closed(); |
| 1058 | if (!f->readable) |
| 1059 | return err_mode("reading"); |
| 1060 | /* refuse to mix with f.next() */ |
| 1061 | if (f->f_buf != NULL && |
| 1062 | (f->f_bufend - f->f_bufptr) > 0 && |
| 1063 | f->f_buf[0] != '\0') |
| 1064 | return err_iterbuffered(); |
| 1065 | if (!PyArg_ParseTuple(args, "|l:read", &bytesrequested)) |
| 1066 | return NULL; |
| 1067 | if (bytesrequested < 0) |
| 1068 | buffersize = new_buffersize(f, (size_t)0); |
| 1069 | else |
| 1070 | buffersize = bytesrequested; |
| 1071 | if (buffersize > PY_SSIZE_T_MAX) { |
| 1072 | PyErr_SetString(PyExc_OverflowError, |
| 1073 | "requested number of bytes is more than a Python string can hold"); |
| 1074 | return NULL; |
| 1075 | } |
| 1076 | v = PyString_FromStringAndSize((char *)NULL, buffersize); |
| 1077 | if (v == NULL) |
| 1078 | return NULL; |
| 1079 | bytesread = 0; |
| 1080 | for (;;) { |
Gregory P. Smith | b2ac4d6 | 2012-06-25 20:57:36 -0700 | [diff] [blame] | 1081 | int interrupted; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1082 | FILE_BEGIN_ALLOW_THREADS(f) |
| 1083 | errno = 0; |
| 1084 | chunksize = Py_UniversalNewlineFread(BUF(v) + bytesread, |
| 1085 | buffersize - bytesread, f->f_fp, (PyObject *)f); |
Gregory P. Smith | b2ac4d6 | 2012-06-25 20:57:36 -0700 | [diff] [blame] | 1086 | interrupted = ferror(f->f_fp) && errno == EINTR; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1087 | FILE_END_ALLOW_THREADS(f) |
Gregory P. Smith | b2ac4d6 | 2012-06-25 20:57:36 -0700 | [diff] [blame] | 1088 | if (interrupted) { |
| 1089 | clearerr(f->f_fp); |
| 1090 | if (PyErr_CheckSignals()) { |
| 1091 | Py_DECREF(v); |
| 1092 | return NULL; |
| 1093 | } |
| 1094 | } |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1095 | if (chunksize == 0) { |
Gregory P. Smith | b2ac4d6 | 2012-06-25 20:57:36 -0700 | [diff] [blame] | 1096 | if (interrupted) |
| 1097 | continue; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1098 | if (!ferror(f->f_fp)) |
| 1099 | break; |
| 1100 | clearerr(f->f_fp); |
| 1101 | /* When in non-blocking mode, data shouldn't |
| 1102 | * be discarded if a blocking signal was |
| 1103 | * received. That will also happen if |
| 1104 | * chunksize != 0, but bytesread < buffersize. */ |
| 1105 | if (bytesread > 0 && BLOCKED_ERRNO(errno)) |
| 1106 | break; |
| 1107 | PyErr_SetFromErrno(PyExc_IOError); |
| 1108 | Py_DECREF(v); |
| 1109 | return NULL; |
| 1110 | } |
| 1111 | bytesread += chunksize; |
Gregory P. Smith | b2ac4d6 | 2012-06-25 20:57:36 -0700 | [diff] [blame] | 1112 | if (bytesread < buffersize && !interrupted) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1113 | clearerr(f->f_fp); |
| 1114 | break; |
| 1115 | } |
| 1116 | if (bytesrequested < 0) { |
| 1117 | buffersize = new_buffersize(f, buffersize); |
| 1118 | if (_PyString_Resize(&v, buffersize) < 0) |
| 1119 | return NULL; |
| 1120 | } else { |
| 1121 | /* Got what was requested. */ |
| 1122 | break; |
| 1123 | } |
| 1124 | } |
| 1125 | if (bytesread != buffersize && _PyString_Resize(&v, bytesread)) |
| 1126 | return NULL; |
| 1127 | return v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1128 | } |
| 1129 | |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 1130 | static PyObject * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 1131 | file_readinto(PyFileObject *f, PyObject *args) |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 1132 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1133 | char *ptr; |
| 1134 | Py_ssize_t ntodo; |
| 1135 | Py_ssize_t ndone, nnow; |
| 1136 | Py_buffer pbuf; |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 1137 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1138 | if (f->f_fp == NULL) |
| 1139 | return err_closed(); |
| 1140 | if (!f->readable) |
| 1141 | return err_mode("reading"); |
| 1142 | /* refuse to mix with f.next() */ |
| 1143 | if (f->f_buf != NULL && |
| 1144 | (f->f_bufend - f->f_bufptr) > 0 && |
| 1145 | f->f_buf[0] != '\0') |
| 1146 | return err_iterbuffered(); |
| 1147 | if (!PyArg_ParseTuple(args, "w*", &pbuf)) |
| 1148 | return NULL; |
| 1149 | ptr = pbuf.buf; |
| 1150 | ntodo = pbuf.len; |
| 1151 | ndone = 0; |
| 1152 | while (ntodo > 0) { |
Gregory P. Smith | b2ac4d6 | 2012-06-25 20:57:36 -0700 | [diff] [blame] | 1153 | int interrupted; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1154 | FILE_BEGIN_ALLOW_THREADS(f) |
| 1155 | errno = 0; |
| 1156 | nnow = Py_UniversalNewlineFread(ptr+ndone, ntodo, f->f_fp, |
| 1157 | (PyObject *)f); |
Gregory P. Smith | b2ac4d6 | 2012-06-25 20:57:36 -0700 | [diff] [blame] | 1158 | interrupted = ferror(f->f_fp) && errno == EINTR; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1159 | FILE_END_ALLOW_THREADS(f) |
Gregory P. Smith | b2ac4d6 | 2012-06-25 20:57:36 -0700 | [diff] [blame] | 1160 | if (interrupted) { |
| 1161 | clearerr(f->f_fp); |
| 1162 | if (PyErr_CheckSignals()) { |
| 1163 | PyBuffer_Release(&pbuf); |
| 1164 | return NULL; |
| 1165 | } |
| 1166 | } |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1167 | if (nnow == 0) { |
Gregory P. Smith | b2ac4d6 | 2012-06-25 20:57:36 -0700 | [diff] [blame] | 1168 | if (interrupted) |
| 1169 | continue; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1170 | if (!ferror(f->f_fp)) |
| 1171 | break; |
| 1172 | PyErr_SetFromErrno(PyExc_IOError); |
| 1173 | clearerr(f->f_fp); |
| 1174 | PyBuffer_Release(&pbuf); |
| 1175 | return NULL; |
| 1176 | } |
| 1177 | ndone += nnow; |
| 1178 | ntodo -= nnow; |
| 1179 | } |
| 1180 | PyBuffer_Release(&pbuf); |
| 1181 | return PyInt_FromSsize_t(ndone); |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 1182 | } |
| 1183 | |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 1184 | /************************************************************************** |
Tim Peters | f29b64d | 2001-01-15 06:33:19 +0000 | [diff] [blame] | 1185 | Routine to get next line using platform fgets(). |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 1186 | |
| 1187 | Under MSVC 6: |
| 1188 | |
Tim Peters | 1c73323 | 2001-01-08 04:02:07 +0000 | [diff] [blame] | 1189 | + MS threadsafe getc is very slow (multiple layers of function calls before+ |
| 1190 | after each character, to lock+unlock the stream). |
| 1191 | + The stream-locking functions are MS-internal -- can't access them from user |
| 1192 | code. |
| 1193 | + There's nothing Tim could find in the MS C or platform SDK libraries that |
| 1194 | can worm around this. |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 1195 | + MS fgets locks/unlocks only once per line; it's the only hook we have. |
| 1196 | |
| 1197 | So we use fgets for speed(!), despite that it's painful. |
| 1198 | |
| 1199 | MS realloc is also slow. |
| 1200 | |
Tim Peters | f29b64d | 2001-01-15 06:33:19 +0000 | [diff] [blame] | 1201 | Reports from other platforms on this method vs getc_unlocked (which MS doesn't |
| 1202 | have): |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1203 | Linux a wash |
| 1204 | Solaris a wash |
| 1205 | Tru64 Unix getline_via_fgets significantly faster |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 1206 | |
Tim Peters | f29b64d | 2001-01-15 06:33:19 +0000 | [diff] [blame] | 1207 | CAUTION: The C std isn't clear about this: in those cases where fgets |
| 1208 | writes something into the buffer, can it write into any position beyond the |
| 1209 | required trailing null byte? MSVC 6 fgets does not, and no platform is (yet) |
| 1210 | known on which it does; and it would be a strange way to code fgets. Still, |
| 1211 | getline_via_fgets may not work correctly if it does. The std test |
| 1212 | test_bufio.py should fail if platform fgets() routinely writes beyond the |
| 1213 | trailing null byte. #define DONT_USE_FGETS_IN_GETLINE to disable this code. |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 1214 | **************************************************************************/ |
| 1215 | |
Tim Peters | f29b64d | 2001-01-15 06:33:19 +0000 | [diff] [blame] | 1216 | /* Use this routine if told to, or by default on non-get_unlocked() |
| 1217 | * platforms unless told not to. Yikes! Let's spell that out: |
| 1218 | * On a platform with getc_unlocked(): |
| 1219 | * By default, use getc_unlocked(). |
| 1220 | * If you want to use fgets() instead, #define USE_FGETS_IN_GETLINE. |
| 1221 | * On a platform without getc_unlocked(): |
| 1222 | * By default, use fgets(). |
| 1223 | * If you don't want to use fgets(), #define DONT_USE_FGETS_IN_GETLINE. |
| 1224 | */ |
| 1225 | #if !defined(USE_FGETS_IN_GETLINE) && !defined(HAVE_GETC_UNLOCKED) |
| 1226 | #define USE_FGETS_IN_GETLINE |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 1227 | #endif |
| 1228 | |
Tim Peters | f29b64d | 2001-01-15 06:33:19 +0000 | [diff] [blame] | 1229 | #if defined(DONT_USE_FGETS_IN_GETLINE) && defined(USE_FGETS_IN_GETLINE) |
| 1230 | #undef USE_FGETS_IN_GETLINE |
| 1231 | #endif |
| 1232 | |
| 1233 | #ifdef USE_FGETS_IN_GETLINE |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 1234 | static PyObject* |
Gregory P. Smith | aa63d0d | 2008-04-06 23:11:17 +0000 | [diff] [blame] | 1235 | getline_via_fgets(PyFileObject *f, FILE *fp) |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 1236 | { |
Tim Peters | 15b8385 | 2001-01-08 00:53:12 +0000 | [diff] [blame] | 1237 | /* INITBUFSIZE is the maximum line length that lets us get away with the fast |
Tim Peters | 142297a | 2001-01-15 10:36:56 +0000 | [diff] [blame] | 1238 | * no-realloc, one-fgets()-call path. Boosting it isn't free, because we have |
| 1239 | * to fill this much of the buffer with a known value in order to figure out |
| 1240 | * how much of the buffer fgets() overwrites. So if INITBUFSIZE is larger |
| 1241 | * than "most" lines, we waste time filling unused buffer slots. 100 is |
| 1242 | * surely adequate for most peoples' email archives, chewing over source code, |
| 1243 | * etc -- "regular old text files". |
| 1244 | * MAXBUFSIZE is the maximum line length that lets us get away with the less |
| 1245 | * fast (but still zippy) no-realloc, two-fgets()-call path. See above for |
| 1246 | * cautions about boosting that. 300 was chosen because the worst real-life |
| 1247 | * text-crunching job reported on Python-Dev was a mail-log crawler where over |
| 1248 | * half the lines were 254 chars. |
Tim Peters | 15b8385 | 2001-01-08 00:53:12 +0000 | [diff] [blame] | 1249 | */ |
Tim Peters | 142297a | 2001-01-15 10:36:56 +0000 | [diff] [blame] | 1250 | #define INITBUFSIZE 100 |
| 1251 | #define MAXBUFSIZE 300 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1252 | char* p; /* temp */ |
| 1253 | char buf[MAXBUFSIZE]; |
| 1254 | PyObject* v; /* the string object result */ |
| 1255 | char* pvfree; /* address of next free slot */ |
| 1256 | char* pvend; /* address one beyond last free slot */ |
| 1257 | size_t nfree; /* # of free buffer slots; pvend-pvfree */ |
| 1258 | size_t total_v_size; /* total # of slots in buffer */ |
| 1259 | size_t increment; /* amount to increment the buffer */ |
| 1260 | size_t prev_v_size; |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 1261 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1262 | /* Optimize for normal case: avoid _PyString_Resize if at all |
| 1263 | * possible via first reading into stack buffer "buf". |
| 1264 | */ |
| 1265 | total_v_size = INITBUFSIZE; /* start small and pray */ |
| 1266 | pvfree = buf; |
| 1267 | for (;;) { |
| 1268 | FILE_BEGIN_ALLOW_THREADS(f) |
| 1269 | pvend = buf + total_v_size; |
| 1270 | nfree = pvend - pvfree; |
| 1271 | memset(pvfree, '\n', nfree); |
| 1272 | assert(nfree < INT_MAX); /* Should be atmost MAXBUFSIZE */ |
| 1273 | p = fgets(pvfree, (int)nfree, fp); |
| 1274 | FILE_END_ALLOW_THREADS(f) |
Tim Peters | 15b8385 | 2001-01-08 00:53:12 +0000 | [diff] [blame] | 1275 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1276 | if (p == NULL) { |
| 1277 | clearerr(fp); |
| 1278 | if (PyErr_CheckSignals()) |
| 1279 | return NULL; |
| 1280 | v = PyString_FromStringAndSize(buf, pvfree - buf); |
| 1281 | return v; |
| 1282 | } |
| 1283 | /* fgets read *something* */ |
| 1284 | p = memchr(pvfree, '\n', nfree); |
| 1285 | if (p != NULL) { |
| 1286 | /* Did the \n come from fgets or from us? |
| 1287 | * Since fgets stops at the first \n, and then writes |
| 1288 | * \0, if it's from fgets a \0 must be next. But if |
| 1289 | * that's so, it could not have come from us, since |
| 1290 | * the \n's we filled the buffer with have only more |
| 1291 | * \n's to the right. |
| 1292 | */ |
| 1293 | if (p+1 < pvend && *(p+1) == '\0') { |
| 1294 | /* It's from fgets: we win! In particular, |
| 1295 | * we haven't done any mallocs yet, and can |
| 1296 | * build the final result on the first try. |
| 1297 | */ |
| 1298 | ++p; /* include \n from fgets */ |
| 1299 | } |
| 1300 | else { |
| 1301 | /* Must be from us: fgets didn't fill the |
| 1302 | * buffer and didn't find a newline, so it |
| 1303 | * must be the last and newline-free line of |
| 1304 | * the file. |
| 1305 | */ |
| 1306 | assert(p > pvfree && *(p-1) == '\0'); |
| 1307 | --p; /* don't include \0 from fgets */ |
| 1308 | } |
| 1309 | v = PyString_FromStringAndSize(buf, p - buf); |
| 1310 | return v; |
| 1311 | } |
| 1312 | /* yuck: fgets overwrote all the newlines, i.e. the entire |
| 1313 | * buffer. So this line isn't over yet, or maybe it is but |
| 1314 | * we're exactly at EOF. If we haven't already, try using the |
| 1315 | * rest of the stack buffer. |
| 1316 | */ |
| 1317 | assert(*(pvend-1) == '\0'); |
| 1318 | if (pvfree == buf) { |
| 1319 | pvfree = pvend - 1; /* overwrite trailing null */ |
| 1320 | total_v_size = MAXBUFSIZE; |
| 1321 | } |
| 1322 | else |
| 1323 | break; |
| 1324 | } |
Tim Peters | 142297a | 2001-01-15 10:36:56 +0000 | [diff] [blame] | 1325 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1326 | /* The stack buffer isn't big enough; malloc a string object and read |
| 1327 | * into its buffer. |
| 1328 | */ |
| 1329 | total_v_size = MAXBUFSIZE << 1; |
| 1330 | v = PyString_FromStringAndSize((char*)NULL, (int)total_v_size); |
| 1331 | if (v == NULL) |
| 1332 | return v; |
| 1333 | /* copy over everything except the last null byte */ |
| 1334 | memcpy(BUF(v), buf, MAXBUFSIZE-1); |
| 1335 | pvfree = BUF(v) + MAXBUFSIZE - 1; |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 1336 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1337 | /* Keep reading stuff into v; if it ever ends successfully, break |
| 1338 | * after setting p one beyond the end of the line. The code here is |
| 1339 | * very much like the code above, except reads into v's buffer; see |
| 1340 | * the code above for detailed comments about the logic. |
| 1341 | */ |
| 1342 | for (;;) { |
| 1343 | FILE_BEGIN_ALLOW_THREADS(f) |
| 1344 | pvend = BUF(v) + total_v_size; |
| 1345 | nfree = pvend - pvfree; |
| 1346 | memset(pvfree, '\n', nfree); |
| 1347 | assert(nfree < INT_MAX); |
| 1348 | p = fgets(pvfree, (int)nfree, fp); |
| 1349 | FILE_END_ALLOW_THREADS(f) |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 1350 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1351 | if (p == NULL) { |
| 1352 | clearerr(fp); |
| 1353 | if (PyErr_CheckSignals()) { |
| 1354 | Py_DECREF(v); |
| 1355 | return NULL; |
| 1356 | } |
| 1357 | p = pvfree; |
| 1358 | break; |
| 1359 | } |
| 1360 | p = memchr(pvfree, '\n', nfree); |
| 1361 | if (p != NULL) { |
| 1362 | if (p+1 < pvend && *(p+1) == '\0') { |
| 1363 | /* \n came from fgets */ |
| 1364 | ++p; |
| 1365 | break; |
| 1366 | } |
| 1367 | /* \n came from us; last line of file, no newline */ |
| 1368 | assert(p > pvfree && *(p-1) == '\0'); |
| 1369 | --p; |
| 1370 | break; |
| 1371 | } |
| 1372 | /* expand buffer and try again */ |
| 1373 | assert(*(pvend-1) == '\0'); |
| 1374 | increment = total_v_size >> 2; /* mild exponential growth */ |
| 1375 | prev_v_size = total_v_size; |
| 1376 | total_v_size += increment; |
| 1377 | /* check for overflow */ |
| 1378 | if (total_v_size <= prev_v_size || |
| 1379 | total_v_size > PY_SSIZE_T_MAX) { |
| 1380 | PyErr_SetString(PyExc_OverflowError, |
| 1381 | "line is longer than a Python string can hold"); |
| 1382 | Py_DECREF(v); |
| 1383 | return NULL; |
| 1384 | } |
| 1385 | if (_PyString_Resize(&v, (int)total_v_size) < 0) |
| 1386 | return NULL; |
| 1387 | /* overwrite the trailing null byte */ |
| 1388 | pvfree = BUF(v) + (prev_v_size - 1); |
| 1389 | } |
| 1390 | if (BUF(v) + total_v_size != p && _PyString_Resize(&v, p - BUF(v))) |
| 1391 | return NULL; |
| 1392 | return v; |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 1393 | #undef INITBUFSIZE |
Tim Peters | 142297a | 2001-01-15 10:36:56 +0000 | [diff] [blame] | 1394 | #undef MAXBUFSIZE |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 1395 | } |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1396 | #endif /* ifdef USE_FGETS_IN_GETLINE */ |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 1397 | |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 1398 | /* Internal routine to get a line. |
| 1399 | Size argument interpretation: |
| 1400 | > 0: max length; |
Guido van Rossum | 8628206 | 2001-01-08 01:26:47 +0000 | [diff] [blame] | 1401 | <= 0: read arbitrary line |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 1402 | */ |
| 1403 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1404 | static PyObject * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 1405 | get_line(PyFileObject *f, int n) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1406 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1407 | FILE *fp = f->f_fp; |
| 1408 | int c; |
| 1409 | char *buf, *end; |
| 1410 | size_t total_v_size; /* total # of slots in buffer */ |
| 1411 | size_t used_v_size; /* # used slots in buffer */ |
| 1412 | size_t increment; /* amount to increment the buffer */ |
| 1413 | PyObject *v; |
| 1414 | int newlinetypes = f->f_newlinetypes; |
| 1415 | int skipnextlf = f->f_skipnextlf; |
| 1416 | int univ_newline = f->f_univ_newline; |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 1417 | |
Jack Jansen | 7b8c754 | 2002-04-14 20:12:41 +0000 | [diff] [blame] | 1418 | #if defined(USE_FGETS_IN_GETLINE) |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1419 | if (n <= 0 && !univ_newline ) |
| 1420 | return getline_via_fgets(f, fp); |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 1421 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1422 | total_v_size = n > 0 ? n : 100; |
| 1423 | v = PyString_FromStringAndSize((char *)NULL, total_v_size); |
| 1424 | if (v == NULL) |
| 1425 | return NULL; |
| 1426 | buf = BUF(v); |
| 1427 | end = buf + total_v_size; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1428 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1429 | for (;;) { |
| 1430 | FILE_BEGIN_ALLOW_THREADS(f) |
| 1431 | FLOCKFILE(fp); |
| 1432 | if (univ_newline) { |
| 1433 | c = 'x'; /* Shut up gcc warning */ |
| 1434 | while ( buf != end && (c = GETC(fp)) != EOF ) { |
| 1435 | if (skipnextlf ) { |
| 1436 | skipnextlf = 0; |
| 1437 | if (c == '\n') { |
| 1438 | /* Seeing a \n here with |
| 1439 | * skipnextlf true means we |
| 1440 | * saw a \r before. |
| 1441 | */ |
| 1442 | newlinetypes |= NEWLINE_CRLF; |
| 1443 | c = GETC(fp); |
| 1444 | if (c == EOF) break; |
| 1445 | } else { |
| 1446 | newlinetypes |= NEWLINE_CR; |
| 1447 | } |
| 1448 | } |
| 1449 | if (c == '\r') { |
| 1450 | skipnextlf = 1; |
| 1451 | c = '\n'; |
| 1452 | } else if ( c == '\n') |
| 1453 | newlinetypes |= NEWLINE_LF; |
| 1454 | *buf++ = c; |
| 1455 | if (c == '\n') break; |
| 1456 | } |
Gregory P. Smith | b2ac4d6 | 2012-06-25 20:57:36 -0700 | [diff] [blame] | 1457 | if (c == EOF) { |
| 1458 | if (ferror(fp) && errno == EINTR) { |
| 1459 | FUNLOCKFILE(fp); |
| 1460 | FILE_ABORT_ALLOW_THREADS(f) |
| 1461 | f->f_newlinetypes = newlinetypes; |
| 1462 | f->f_skipnextlf = skipnextlf; |
| 1463 | |
| 1464 | if (PyErr_CheckSignals()) { |
| 1465 | Py_DECREF(v); |
| 1466 | return NULL; |
| 1467 | } |
| 1468 | /* We executed Python signal handlers and got no exception. |
| 1469 | * Now back to reading the line where we left off. */ |
| 1470 | clearerr(fp); |
| 1471 | continue; |
| 1472 | } |
| 1473 | if (skipnextlf) |
| 1474 | newlinetypes |= NEWLINE_CR; |
| 1475 | } |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1476 | } else /* If not universal newlines use the normal loop */ |
| 1477 | while ((c = GETC(fp)) != EOF && |
| 1478 | (*buf++ = c) != '\n' && |
| 1479 | buf != end) |
| 1480 | ; |
| 1481 | FUNLOCKFILE(fp); |
| 1482 | FILE_END_ALLOW_THREADS(f) |
| 1483 | f->f_newlinetypes = newlinetypes; |
| 1484 | f->f_skipnextlf = skipnextlf; |
| 1485 | if (c == '\n') |
| 1486 | break; |
| 1487 | if (c == EOF) { |
| 1488 | if (ferror(fp)) { |
Gregory P. Smith | b2ac4d6 | 2012-06-25 20:57:36 -0700 | [diff] [blame] | 1489 | if (errno == EINTR) { |
| 1490 | if (PyErr_CheckSignals()) { |
| 1491 | Py_DECREF(v); |
| 1492 | return NULL; |
| 1493 | } |
| 1494 | /* We executed Python signal handlers and got no exception. |
| 1495 | * Now back to reading the line where we left off. */ |
| 1496 | clearerr(fp); |
| 1497 | continue; |
| 1498 | } |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1499 | PyErr_SetFromErrno(PyExc_IOError); |
| 1500 | clearerr(fp); |
| 1501 | Py_DECREF(v); |
| 1502 | return NULL; |
| 1503 | } |
| 1504 | clearerr(fp); |
| 1505 | if (PyErr_CheckSignals()) { |
| 1506 | Py_DECREF(v); |
| 1507 | return NULL; |
| 1508 | } |
| 1509 | break; |
| 1510 | } |
| 1511 | /* Must be because buf == end */ |
| 1512 | if (n > 0) |
| 1513 | break; |
| 1514 | used_v_size = total_v_size; |
| 1515 | increment = total_v_size >> 2; /* mild exponential growth */ |
| 1516 | total_v_size += increment; |
| 1517 | if (total_v_size > PY_SSIZE_T_MAX) { |
| 1518 | PyErr_SetString(PyExc_OverflowError, |
| 1519 | "line is longer than a Python string can hold"); |
| 1520 | Py_DECREF(v); |
| 1521 | return NULL; |
| 1522 | } |
| 1523 | if (_PyString_Resize(&v, total_v_size) < 0) |
| 1524 | return NULL; |
| 1525 | buf = BUF(v) + used_v_size; |
| 1526 | end = BUF(v) + total_v_size; |
| 1527 | } |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1528 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1529 | used_v_size = buf - BUF(v); |
| 1530 | if (used_v_size != total_v_size && _PyString_Resize(&v, used_v_size)) |
| 1531 | return NULL; |
| 1532 | return v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1533 | } |
| 1534 | |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 1535 | /* External C interface */ |
| 1536 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1537 | PyObject * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 1538 | PyFile_GetLine(PyObject *f, int n) |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 1539 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1540 | PyObject *result; |
Guido van Rossum | 4ddf0a0 | 2001-01-07 20:51:39 +0000 | [diff] [blame] | 1541 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1542 | if (f == NULL) { |
| 1543 | PyErr_BadInternalCall(); |
| 1544 | return NULL; |
| 1545 | } |
Guido van Rossum | 4ddf0a0 | 2001-01-07 20:51:39 +0000 | [diff] [blame] | 1546 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1547 | if (PyFile_Check(f)) { |
| 1548 | PyFileObject *fo = (PyFileObject *)f; |
| 1549 | if (fo->f_fp == NULL) |
| 1550 | return err_closed(); |
| 1551 | if (!fo->readable) |
| 1552 | return err_mode("reading"); |
| 1553 | /* refuse to mix with f.next() */ |
| 1554 | if (fo->f_buf != NULL && |
| 1555 | (fo->f_bufend - fo->f_bufptr) > 0 && |
| 1556 | fo->f_buf[0] != '\0') |
| 1557 | return err_iterbuffered(); |
| 1558 | result = get_line(fo, n); |
| 1559 | } |
| 1560 | else { |
| 1561 | PyObject *reader; |
| 1562 | PyObject *args; |
Guido van Rossum | 4ddf0a0 | 2001-01-07 20:51:39 +0000 | [diff] [blame] | 1563 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1564 | reader = PyObject_GetAttrString(f, "readline"); |
| 1565 | if (reader == NULL) |
| 1566 | return NULL; |
| 1567 | if (n <= 0) |
| 1568 | args = PyTuple_New(0); |
| 1569 | else |
| 1570 | args = Py_BuildValue("(i)", n); |
| 1571 | if (args == NULL) { |
| 1572 | Py_DECREF(reader); |
| 1573 | return NULL; |
| 1574 | } |
| 1575 | result = PyEval_CallObject(reader, args); |
| 1576 | Py_DECREF(reader); |
| 1577 | Py_DECREF(args); |
| 1578 | if (result != NULL && !PyString_Check(result) && |
| 1579 | !PyUnicode_Check(result)) { |
| 1580 | Py_DECREF(result); |
| 1581 | result = NULL; |
| 1582 | PyErr_SetString(PyExc_TypeError, |
| 1583 | "object.readline() returned non-string"); |
| 1584 | } |
| 1585 | } |
Guido van Rossum | 4ddf0a0 | 2001-01-07 20:51:39 +0000 | [diff] [blame] | 1586 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1587 | if (n < 0 && result != NULL && PyString_Check(result)) { |
| 1588 | char *s = PyString_AS_STRING(result); |
| 1589 | Py_ssize_t len = PyString_GET_SIZE(result); |
| 1590 | if (len == 0) { |
| 1591 | Py_DECREF(result); |
| 1592 | result = NULL; |
| 1593 | PyErr_SetString(PyExc_EOFError, |
| 1594 | "EOF when reading a line"); |
| 1595 | } |
| 1596 | else if (s[len-1] == '\n') { |
| 1597 | if (result->ob_refcnt == 1) { |
| 1598 | if (_PyString_Resize(&result, len-1)) |
| 1599 | return NULL; |
| 1600 | } |
| 1601 | else { |
| 1602 | PyObject *v; |
| 1603 | v = PyString_FromStringAndSize(s, len-1); |
| 1604 | Py_DECREF(result); |
| 1605 | result = v; |
| 1606 | } |
| 1607 | } |
| 1608 | } |
Martin v. Löwis | af6a27a | 2003-01-03 19:16:14 +0000 | [diff] [blame] | 1609 | #ifdef Py_USING_UNICODE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1610 | if (n < 0 && result != NULL && PyUnicode_Check(result)) { |
| 1611 | Py_UNICODE *s = PyUnicode_AS_UNICODE(result); |
| 1612 | Py_ssize_t len = PyUnicode_GET_SIZE(result); |
| 1613 | if (len == 0) { |
| 1614 | Py_DECREF(result); |
| 1615 | result = NULL; |
| 1616 | PyErr_SetString(PyExc_EOFError, |
| 1617 | "EOF when reading a line"); |
| 1618 | } |
| 1619 | else if (s[len-1] == '\n') { |
| 1620 | if (result->ob_refcnt == 1) |
| 1621 | PyUnicode_Resize(&result, len-1); |
| 1622 | else { |
| 1623 | PyObject *v; |
| 1624 | v = PyUnicode_FromUnicode(s, len-1); |
| 1625 | Py_DECREF(result); |
| 1626 | result = v; |
| 1627 | } |
| 1628 | } |
| 1629 | } |
Martin v. Löwis | af6a27a | 2003-01-03 19:16:14 +0000 | [diff] [blame] | 1630 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1631 | return result; |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 1632 | } |
| 1633 | |
| 1634 | /* Python method */ |
| 1635 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1636 | static PyObject * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 1637 | file_readline(PyFileObject *f, PyObject *args) |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 1638 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1639 | int n = -1; |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 1640 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1641 | if (f->f_fp == NULL) |
| 1642 | return err_closed(); |
| 1643 | if (!f->readable) |
| 1644 | return err_mode("reading"); |
| 1645 | /* refuse to mix with f.next() */ |
| 1646 | if (f->f_buf != NULL && |
| 1647 | (f->f_bufend - f->f_bufptr) > 0 && |
| 1648 | f->f_buf[0] != '\0') |
| 1649 | return err_iterbuffered(); |
| 1650 | if (!PyArg_ParseTuple(args, "|i:readline", &n)) |
| 1651 | return NULL; |
| 1652 | if (n == 0) |
| 1653 | return PyString_FromString(""); |
| 1654 | if (n < 0) |
| 1655 | n = 0; |
| 1656 | return get_line(f, n); |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 1657 | } |
| 1658 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1659 | static PyObject * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 1660 | file_readlines(PyFileObject *f, PyObject *args) |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 1661 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1662 | long sizehint = 0; |
| 1663 | PyObject *list = NULL; |
| 1664 | PyObject *line; |
| 1665 | char small_buffer[SMALLCHUNK]; |
| 1666 | char *buffer = small_buffer; |
| 1667 | size_t buffersize = SMALLCHUNK; |
| 1668 | PyObject *big_buffer = NULL; |
| 1669 | size_t nfilled = 0; |
| 1670 | size_t nread; |
| 1671 | size_t totalread = 0; |
| 1672 | char *p, *q, *end; |
| 1673 | int err; |
Gregory P. Smith | b2ac4d6 | 2012-06-25 20:57:36 -0700 | [diff] [blame] | 1674 | int shortread = 0; /* bool, did the previous read come up short? */ |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 1675 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1676 | if (f->f_fp == NULL) |
| 1677 | return err_closed(); |
| 1678 | if (!f->readable) |
| 1679 | return err_mode("reading"); |
| 1680 | /* refuse to mix with f.next() */ |
| 1681 | if (f->f_buf != NULL && |
| 1682 | (f->f_bufend - f->f_bufptr) > 0 && |
| 1683 | f->f_buf[0] != '\0') |
| 1684 | return err_iterbuffered(); |
| 1685 | if (!PyArg_ParseTuple(args, "|l:readlines", &sizehint)) |
| 1686 | return NULL; |
| 1687 | if ((list = PyList_New(0)) == NULL) |
| 1688 | return NULL; |
| 1689 | for (;;) { |
| 1690 | if (shortread) |
| 1691 | nread = 0; |
| 1692 | else { |
| 1693 | FILE_BEGIN_ALLOW_THREADS(f) |
| 1694 | errno = 0; |
| 1695 | nread = Py_UniversalNewlineFread(buffer+nfilled, |
| 1696 | buffersize-nfilled, f->f_fp, (PyObject *)f); |
| 1697 | FILE_END_ALLOW_THREADS(f) |
| 1698 | shortread = (nread < buffersize-nfilled); |
| 1699 | } |
| 1700 | if (nread == 0) { |
| 1701 | sizehint = 0; |
| 1702 | if (!ferror(f->f_fp)) |
| 1703 | break; |
Gregory P. Smith | b2ac4d6 | 2012-06-25 20:57:36 -0700 | [diff] [blame] | 1704 | if (errno == EINTR) { |
| 1705 | if (PyErr_CheckSignals()) { |
| 1706 | goto error; |
| 1707 | } |
| 1708 | clearerr(f->f_fp); |
| 1709 | shortread = 0; |
| 1710 | continue; |
| 1711 | } |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1712 | PyErr_SetFromErrno(PyExc_IOError); |
| 1713 | clearerr(f->f_fp); |
| 1714 | goto error; |
| 1715 | } |
| 1716 | totalread += nread; |
| 1717 | p = (char *)memchr(buffer+nfilled, '\n', nread); |
| 1718 | if (p == NULL) { |
| 1719 | /* Need a larger buffer to fit this line */ |
| 1720 | nfilled += nread; |
| 1721 | buffersize *= 2; |
| 1722 | if (buffersize > PY_SSIZE_T_MAX) { |
| 1723 | PyErr_SetString(PyExc_OverflowError, |
| 1724 | "line is longer than a Python string can hold"); |
| 1725 | goto error; |
| 1726 | } |
| 1727 | if (big_buffer == NULL) { |
| 1728 | /* Create the big buffer */ |
| 1729 | big_buffer = PyString_FromStringAndSize( |
| 1730 | NULL, buffersize); |
| 1731 | if (big_buffer == NULL) |
| 1732 | goto error; |
| 1733 | buffer = PyString_AS_STRING(big_buffer); |
| 1734 | memcpy(buffer, small_buffer, nfilled); |
| 1735 | } |
| 1736 | else { |
| 1737 | /* Grow the big buffer */ |
| 1738 | if ( _PyString_Resize(&big_buffer, buffersize) < 0 ) |
| 1739 | goto error; |
| 1740 | buffer = PyString_AS_STRING(big_buffer); |
| 1741 | } |
| 1742 | continue; |
| 1743 | } |
| 1744 | end = buffer+nfilled+nread; |
| 1745 | q = buffer; |
| 1746 | do { |
| 1747 | /* Process complete lines */ |
| 1748 | p++; |
| 1749 | line = PyString_FromStringAndSize(q, p-q); |
| 1750 | if (line == NULL) |
| 1751 | goto error; |
| 1752 | err = PyList_Append(list, line); |
| 1753 | Py_DECREF(line); |
| 1754 | if (err != 0) |
| 1755 | goto error; |
| 1756 | q = p; |
| 1757 | p = (char *)memchr(q, '\n', end-q); |
| 1758 | } while (p != NULL); |
| 1759 | /* Move the remaining incomplete line to the start */ |
| 1760 | nfilled = end-q; |
| 1761 | memmove(buffer, q, nfilled); |
| 1762 | if (sizehint > 0) |
| 1763 | if (totalread >= (size_t)sizehint) |
| 1764 | break; |
| 1765 | } |
| 1766 | if (nfilled != 0) { |
| 1767 | /* Partial last line */ |
| 1768 | line = PyString_FromStringAndSize(buffer, nfilled); |
| 1769 | if (line == NULL) |
| 1770 | goto error; |
| 1771 | if (sizehint > 0) { |
| 1772 | /* Need to complete the last line */ |
| 1773 | PyObject *rest = get_line(f, 0); |
| 1774 | if (rest == NULL) { |
| 1775 | Py_DECREF(line); |
| 1776 | goto error; |
| 1777 | } |
| 1778 | PyString_Concat(&line, rest); |
| 1779 | Py_DECREF(rest); |
| 1780 | if (line == NULL) |
| 1781 | goto error; |
| 1782 | } |
| 1783 | err = PyList_Append(list, line); |
| 1784 | Py_DECREF(line); |
| 1785 | if (err != 0) |
| 1786 | goto error; |
| 1787 | } |
Gregory P. Smith | aa63d0d | 2008-04-06 23:11:17 +0000 | [diff] [blame] | 1788 | |
| 1789 | cleanup: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1790 | Py_XDECREF(big_buffer); |
| 1791 | return list; |
Gregory P. Smith | aa63d0d | 2008-04-06 23:11:17 +0000 | [diff] [blame] | 1792 | |
| 1793 | error: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1794 | Py_CLEAR(list); |
| 1795 | goto cleanup; |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 1796 | } |
| 1797 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1798 | static PyObject * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 1799 | file_write(PyFileObject *f, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1800 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1801 | Py_buffer pbuf; |
Victor Stinner | caafd77 | 2010-09-08 10:51:01 +0000 | [diff] [blame] | 1802 | const char *s; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1803 | Py_ssize_t n, n2; |
Victor Stinner | caafd77 | 2010-09-08 10:51:01 +0000 | [diff] [blame] | 1804 | PyObject *encoded = NULL; |
Serhiy Storchaka | 78ad658 | 2013-12-17 17:32:20 +0200 | [diff] [blame] | 1805 | int err_flag = 0, err; |
Victor Stinner | caafd77 | 2010-09-08 10:51:01 +0000 | [diff] [blame] | 1806 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1807 | if (f->f_fp == NULL) |
| 1808 | return err_closed(); |
| 1809 | if (!f->writable) |
| 1810 | return err_mode("writing"); |
| 1811 | if (f->f_binary) { |
| 1812 | if (!PyArg_ParseTuple(args, "s*", &pbuf)) |
| 1813 | return NULL; |
| 1814 | s = pbuf.buf; |
| 1815 | n = pbuf.len; |
Victor Stinner | caafd77 | 2010-09-08 10:51:01 +0000 | [diff] [blame] | 1816 | } |
| 1817 | else { |
Victor Stinner | caafd77 | 2010-09-08 10:51:01 +0000 | [diff] [blame] | 1818 | PyObject *text; |
| 1819 | if (!PyArg_ParseTuple(args, "O", &text)) |
| 1820 | return NULL; |
| 1821 | |
| 1822 | if (PyString_Check(text)) { |
| 1823 | s = PyString_AS_STRING(text); |
| 1824 | n = PyString_GET_SIZE(text); |
Benjamin Peterson | 5ca88d2 | 2013-01-01 23:04:16 -0600 | [diff] [blame] | 1825 | #ifdef Py_USING_UNICODE |
Victor Stinner | caafd77 | 2010-09-08 10:51:01 +0000 | [diff] [blame] | 1826 | } else if (PyUnicode_Check(text)) { |
Benjamin Peterson | 5ca88d2 | 2013-01-01 23:04:16 -0600 | [diff] [blame] | 1827 | const char *encoding, *errors; |
Victor Stinner | caafd77 | 2010-09-08 10:51:01 +0000 | [diff] [blame] | 1828 | if (f->f_encoding != Py_None) |
| 1829 | encoding = PyString_AS_STRING(f->f_encoding); |
| 1830 | else |
| 1831 | encoding = PyUnicode_GetDefaultEncoding(); |
| 1832 | if (f->f_errors != Py_None) |
| 1833 | errors = PyString_AS_STRING(f->f_errors); |
| 1834 | else |
| 1835 | errors = "strict"; |
| 1836 | encoded = PyUnicode_AsEncodedString(text, encoding, errors); |
| 1837 | if (encoded == NULL) |
| 1838 | return NULL; |
| 1839 | s = PyString_AS_STRING(encoded); |
| 1840 | n = PyString_GET_SIZE(encoded); |
Benjamin Peterson | 5ca88d2 | 2013-01-01 23:04:16 -0600 | [diff] [blame] | 1841 | #endif |
Victor Stinner | caafd77 | 2010-09-08 10:51:01 +0000 | [diff] [blame] | 1842 | } else { |
| 1843 | if (PyObject_AsCharBuffer(text, &s, &n)) |
| 1844 | return NULL; |
| 1845 | } |
| 1846 | } |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1847 | f->f_softspace = 0; |
| 1848 | FILE_BEGIN_ALLOW_THREADS(f) |
| 1849 | errno = 0; |
| 1850 | n2 = fwrite(s, 1, n, f->f_fp); |
Serhiy Storchaka | 78ad658 | 2013-12-17 17:32:20 +0200 | [diff] [blame] | 1851 | if (n2 != n || ferror(f->f_fp)) { |
| 1852 | err_flag = 1; |
Serhiy Storchaka | 6d56231 | 2013-12-17 14:40:06 +0200 | [diff] [blame] | 1853 | err = errno; |
Serhiy Storchaka | 78ad658 | 2013-12-17 17:32:20 +0200 | [diff] [blame] | 1854 | } |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1855 | FILE_END_ALLOW_THREADS(f) |
Victor Stinner | caafd77 | 2010-09-08 10:51:01 +0000 | [diff] [blame] | 1856 | Py_XDECREF(encoded); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1857 | if (f->f_binary) |
| 1858 | PyBuffer_Release(&pbuf); |
Serhiy Storchaka | 78ad658 | 2013-12-17 17:32:20 +0200 | [diff] [blame] | 1859 | if (err_flag) { |
Serhiy Storchaka | 6d56231 | 2013-12-17 14:40:06 +0200 | [diff] [blame] | 1860 | errno = err; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1861 | PyErr_SetFromErrno(PyExc_IOError); |
| 1862 | clearerr(f->f_fp); |
| 1863 | return NULL; |
| 1864 | } |
| 1865 | Py_INCREF(Py_None); |
| 1866 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1867 | } |
| 1868 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1869 | static PyObject * |
Tim Peters | 2c9aa5e | 2001-09-23 04:06:05 +0000 | [diff] [blame] | 1870 | file_writelines(PyFileObject *f, PyObject *seq) |
Guido van Rossum | 5a2a683 | 1993-10-25 09:59:04 +0000 | [diff] [blame] | 1871 | { |
Guido van Rossum | ee70ad1 | 2000-03-13 16:27:06 +0000 | [diff] [blame] | 1872 | #define CHUNKSIZE 1000 |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1873 | PyObject *list, *line; |
| 1874 | PyObject *it; /* iter(seq) */ |
| 1875 | PyObject *result; |
| 1876 | int index, islist; |
| 1877 | Py_ssize_t i, j, nwritten, len; |
Guido van Rossum | ee70ad1 | 2000-03-13 16:27:06 +0000 | [diff] [blame] | 1878 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1879 | assert(seq != NULL); |
| 1880 | if (f->f_fp == NULL) |
| 1881 | return err_closed(); |
| 1882 | if (!f->writable) |
| 1883 | return err_mode("writing"); |
Tim Peters | 2c9aa5e | 2001-09-23 04:06:05 +0000 | [diff] [blame] | 1884 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1885 | result = NULL; |
| 1886 | list = NULL; |
| 1887 | islist = PyList_Check(seq); |
| 1888 | if (islist) |
| 1889 | it = NULL; |
| 1890 | else { |
| 1891 | it = PyObject_GetIter(seq); |
| 1892 | if (it == NULL) { |
| 1893 | PyErr_SetString(PyExc_TypeError, |
| 1894 | "writelines() requires an iterable argument"); |
| 1895 | return NULL; |
| 1896 | } |
| 1897 | /* From here on, fail by going to error, to reclaim "it". */ |
| 1898 | list = PyList_New(CHUNKSIZE); |
| 1899 | if (list == NULL) |
| 1900 | goto error; |
| 1901 | } |
Guido van Rossum | ee70ad1 | 2000-03-13 16:27:06 +0000 | [diff] [blame] | 1902 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1903 | /* Strategy: slurp CHUNKSIZE lines into a private list, |
| 1904 | checking that they are all strings, then write that list |
| 1905 | without holding the interpreter lock, then come back for more. */ |
| 1906 | for (index = 0; ; index += CHUNKSIZE) { |
| 1907 | if (islist) { |
| 1908 | Py_XDECREF(list); |
| 1909 | list = PyList_GetSlice(seq, index, index+CHUNKSIZE); |
| 1910 | if (list == NULL) |
| 1911 | goto error; |
| 1912 | j = PyList_GET_SIZE(list); |
| 1913 | } |
| 1914 | else { |
| 1915 | for (j = 0; j < CHUNKSIZE; j++) { |
| 1916 | line = PyIter_Next(it); |
| 1917 | if (line == NULL) { |
| 1918 | if (PyErr_Occurred()) |
| 1919 | goto error; |
| 1920 | break; |
| 1921 | } |
| 1922 | PyList_SetItem(list, j, line); |
| 1923 | } |
Benjamin Peterson | bf77554 | 2010-10-16 19:20:12 +0000 | [diff] [blame] | 1924 | /* The iterator might have closed the file on us. */ |
| 1925 | if (f->f_fp == NULL) { |
| 1926 | err_closed(); |
| 1927 | goto error; |
| 1928 | } |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1929 | } |
| 1930 | if (j == 0) |
| 1931 | break; |
Guido van Rossum | ee70ad1 | 2000-03-13 16:27:06 +0000 | [diff] [blame] | 1932 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1933 | /* Check that all entries are indeed strings. If not, |
| 1934 | apply the same rules as for file.write() and |
| 1935 | convert the results to strings. This is slow, but |
| 1936 | seems to be the only way since all conversion APIs |
| 1937 | could potentially execute Python code. */ |
| 1938 | for (i = 0; i < j; i++) { |
| 1939 | PyObject *v = PyList_GET_ITEM(list, i); |
| 1940 | if (!PyString_Check(v)) { |
| 1941 | const char *buffer; |
Antoine Pitrou | b0acc1b | 2014-05-08 19:26:04 +0200 | [diff] [blame] | 1942 | int res; |
| 1943 | if (f->f_binary) { |
| 1944 | res = PyObject_AsReadBuffer(v, (const void**)&buffer, &len); |
| 1945 | } else { |
| 1946 | res = PyObject_AsCharBuffer(v, &buffer, &len); |
| 1947 | } |
| 1948 | if (res) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1949 | PyErr_SetString(PyExc_TypeError, |
| 1950 | "writelines() argument must be a sequence of strings"); |
| 1951 | goto error; |
| 1952 | } |
| 1953 | line = PyString_FromStringAndSize(buffer, |
| 1954 | len); |
| 1955 | if (line == NULL) |
| 1956 | goto error; |
| 1957 | Py_DECREF(v); |
| 1958 | PyList_SET_ITEM(list, i, line); |
| 1959 | } |
| 1960 | } |
Marc-André Lemburg | 6ef68b5 | 2000-08-25 22:39:50 +0000 | [diff] [blame] | 1961 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1962 | /* Since we are releasing the global lock, the |
| 1963 | following code may *not* execute Python code. */ |
| 1964 | f->f_softspace = 0; |
| 1965 | FILE_BEGIN_ALLOW_THREADS(f) |
| 1966 | errno = 0; |
| 1967 | for (i = 0; i < j; i++) { |
| 1968 | line = PyList_GET_ITEM(list, i); |
| 1969 | len = PyString_GET_SIZE(line); |
| 1970 | nwritten = fwrite(PyString_AS_STRING(line), |
| 1971 | 1, len, f->f_fp); |
| 1972 | if (nwritten != len) { |
| 1973 | FILE_ABORT_ALLOW_THREADS(f) |
| 1974 | PyErr_SetFromErrno(PyExc_IOError); |
| 1975 | clearerr(f->f_fp); |
| 1976 | goto error; |
| 1977 | } |
| 1978 | } |
| 1979 | FILE_END_ALLOW_THREADS(f) |
Guido van Rossum | ee70ad1 | 2000-03-13 16:27:06 +0000 | [diff] [blame] | 1980 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1981 | if (j < CHUNKSIZE) |
| 1982 | break; |
| 1983 | } |
Guido van Rossum | ee70ad1 | 2000-03-13 16:27:06 +0000 | [diff] [blame] | 1984 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1985 | Py_INCREF(Py_None); |
| 1986 | result = Py_None; |
Guido van Rossum | ee70ad1 | 2000-03-13 16:27:06 +0000 | [diff] [blame] | 1987 | error: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1988 | Py_XDECREF(list); |
| 1989 | Py_XDECREF(it); |
| 1990 | return result; |
Tim Peters | 2c9aa5e | 2001-09-23 04:06:05 +0000 | [diff] [blame] | 1991 | #undef CHUNKSIZE |
Guido van Rossum | 5a2a683 | 1993-10-25 09:59:04 +0000 | [diff] [blame] | 1992 | } |
| 1993 | |
Guido van Rossum | 7a6e959 | 2002-08-06 15:55:28 +0000 | [diff] [blame] | 1994 | static PyObject * |
Guido van Rossum | 1a5e21e | 2006-02-28 21:57:43 +0000 | [diff] [blame] | 1995 | file_self(PyFileObject *f) |
Guido van Rossum | 7a6e959 | 2002-08-06 15:55:28 +0000 | [diff] [blame] | 1996 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1997 | if (f->f_fp == NULL) |
| 1998 | return err_closed(); |
| 1999 | Py_INCREF(f); |
| 2000 | return (PyObject *)f; |
Guido van Rossum | 7a6e959 | 2002-08-06 15:55:28 +0000 | [diff] [blame] | 2001 | } |
| 2002 | |
Georg Brandl | 98b40ad | 2006-06-08 14:50:21 +0000 | [diff] [blame] | 2003 | static PyObject * |
Georg Brandl | a9916b5 | 2008-05-17 22:11:54 +0000 | [diff] [blame] | 2004 | file_xreadlines(PyFileObject *f) |
| 2005 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2006 | if (PyErr_WarnPy3k("f.xreadlines() not supported in 3.x, " |
| 2007 | "try 'for line in f' instead", 1) < 0) |
| 2008 | return NULL; |
| 2009 | return file_self(f); |
Georg Brandl | a9916b5 | 2008-05-17 22:11:54 +0000 | [diff] [blame] | 2010 | } |
| 2011 | |
| 2012 | static PyObject * |
Georg Brandl | ad61bc8 | 2008-02-23 15:11:18 +0000 | [diff] [blame] | 2013 | file_exit(PyObject *f, PyObject *args) |
Georg Brandl | 98b40ad | 2006-06-08 14:50:21 +0000 | [diff] [blame] | 2014 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2015 | PyObject *ret = PyObject_CallMethod(f, "close", NULL); |
| 2016 | if (!ret) |
| 2017 | /* If error occurred, pass through */ |
| 2018 | return NULL; |
| 2019 | Py_DECREF(ret); |
| 2020 | /* We cannot return the result of close since a true |
| 2021 | * value will be interpreted as "yes, swallow the |
| 2022 | * exception if one was raised inside the with block". */ |
| 2023 | Py_RETURN_NONE; |
Georg Brandl | 98b40ad | 2006-06-08 14:50:21 +0000 | [diff] [blame] | 2024 | } |
| 2025 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2026 | PyDoc_STRVAR(readline_doc, |
Tim Peters | efc3a3a | 2001-09-20 07:55:22 +0000 | [diff] [blame] | 2027 | "readline([size]) -> next line from the file, as a string.\n" |
| 2028 | "\n" |
| 2029 | "Retain newline. A non-negative size argument limits the maximum\n" |
| 2030 | "number of bytes to return (an incomplete line may be returned then).\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2031 | "Return an empty string at EOF."); |
Tim Peters | efc3a3a | 2001-09-20 07:55:22 +0000 | [diff] [blame] | 2032 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2033 | PyDoc_STRVAR(read_doc, |
Tim Peters | efc3a3a | 2001-09-20 07:55:22 +0000 | [diff] [blame] | 2034 | "read([size]) -> read at most size bytes, returned as a string.\n" |
| 2035 | "\n" |
Gustavo Niemeyer | 786ddb2 | 2002-12-16 18:12:53 +0000 | [diff] [blame] | 2036 | "If the size argument is negative or omitted, read until EOF is reached.\n" |
| 2037 | "Notice that when in non-blocking mode, less data than what was requested\n" |
| 2038 | "may be returned, even if no size parameter was given."); |
Tim Peters | efc3a3a | 2001-09-20 07:55:22 +0000 | [diff] [blame] | 2039 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2040 | PyDoc_STRVAR(write_doc, |
Tim Peters | efc3a3a | 2001-09-20 07:55:22 +0000 | [diff] [blame] | 2041 | "write(str) -> None. Write string str to file.\n" |
| 2042 | "\n" |
| 2043 | "Note that due to buffering, flush() or close() may be needed before\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2044 | "the file on disk reflects the data written."); |
Tim Peters | efc3a3a | 2001-09-20 07:55:22 +0000 | [diff] [blame] | 2045 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2046 | PyDoc_STRVAR(fileno_doc, |
Tim Peters | efc3a3a | 2001-09-20 07:55:22 +0000 | [diff] [blame] | 2047 | "fileno() -> integer \"file descriptor\".\n" |
| 2048 | "\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2049 | "This is needed for lower-level file interfaces, such os.read()."); |
Tim Peters | efc3a3a | 2001-09-20 07:55:22 +0000 | [diff] [blame] | 2050 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2051 | PyDoc_STRVAR(seek_doc, |
Tim Peters | efc3a3a | 2001-09-20 07:55:22 +0000 | [diff] [blame] | 2052 | "seek(offset[, whence]) -> None. Move to new file position.\n" |
| 2053 | "\n" |
| 2054 | "Argument offset is a byte count. Optional argument whence defaults to\n" |
| 2055 | "0 (offset from start of file, offset should be >= 0); other values are 1\n" |
| 2056 | "(move relative to current position, positive or negative), and 2 (move\n" |
| 2057 | "relative to end of file, usually negative, although many platforms allow\n" |
Martin v. Löwis | 849a972 | 2003-10-18 09:38:01 +0000 | [diff] [blame] | 2058 | "seeking beyond the end of a file). If the file is opened in text mode,\n" |
| 2059 | "only offsets returned by tell() are legal. Use of other offsets causes\n" |
| 2060 | "undefined behavior." |
Tim Peters | efc3a3a | 2001-09-20 07:55:22 +0000 | [diff] [blame] | 2061 | "\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2062 | "Note that not all file objects are seekable."); |
Tim Peters | efc3a3a | 2001-09-20 07:55:22 +0000 | [diff] [blame] | 2063 | |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 2064 | #ifdef HAVE_FTRUNCATE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2065 | PyDoc_STRVAR(truncate_doc, |
Tim Peters | efc3a3a | 2001-09-20 07:55:22 +0000 | [diff] [blame] | 2066 | "truncate([size]) -> None. Truncate the file to at most size bytes.\n" |
| 2067 | "\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2068 | "Size defaults to the current file position, as returned by tell()."); |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 2069 | #endif |
Tim Peters | efc3a3a | 2001-09-20 07:55:22 +0000 | [diff] [blame] | 2070 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2071 | PyDoc_STRVAR(tell_doc, |
| 2072 | "tell() -> current file position, an integer (may be a long integer)."); |
Tim Peters | efc3a3a | 2001-09-20 07:55:22 +0000 | [diff] [blame] | 2073 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2074 | PyDoc_STRVAR(readinto_doc, |
| 2075 | "readinto() -> Undocumented. Don't use this; it may go away."); |
Tim Peters | efc3a3a | 2001-09-20 07:55:22 +0000 | [diff] [blame] | 2076 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2077 | PyDoc_STRVAR(readlines_doc, |
Tim Peters | efc3a3a | 2001-09-20 07:55:22 +0000 | [diff] [blame] | 2078 | "readlines([size]) -> list of strings, each a line from the file.\n" |
| 2079 | "\n" |
| 2080 | "Call readline() repeatedly and return a list of the lines so read.\n" |
| 2081 | "The optional size argument, if given, is an approximate bound on the\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2082 | "total number of bytes in the lines returned."); |
Tim Peters | efc3a3a | 2001-09-20 07:55:22 +0000 | [diff] [blame] | 2083 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2084 | PyDoc_STRVAR(xreadlines_doc, |
Guido van Rossum | 7a6e959 | 2002-08-06 15:55:28 +0000 | [diff] [blame] | 2085 | "xreadlines() -> returns self.\n" |
Tim Peters | efc3a3a | 2001-09-20 07:55:22 +0000 | [diff] [blame] | 2086 | "\n" |
Guido van Rossum | 7a6e959 | 2002-08-06 15:55:28 +0000 | [diff] [blame] | 2087 | "For backward compatibility. File objects now include the performance\n" |
| 2088 | "optimizations previously implemented in the xreadlines module."); |
Tim Peters | efc3a3a | 2001-09-20 07:55:22 +0000 | [diff] [blame] | 2089 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2090 | PyDoc_STRVAR(writelines_doc, |
Tim Peters | 2c9aa5e | 2001-09-23 04:06:05 +0000 | [diff] [blame] | 2091 | "writelines(sequence_of_strings) -> None. Write the strings to the file.\n" |
Tim Peters | efc3a3a | 2001-09-20 07:55:22 +0000 | [diff] [blame] | 2092 | "\n" |
Tim Peters | 2c9aa5e | 2001-09-23 04:06:05 +0000 | [diff] [blame] | 2093 | "Note that newlines are not added. The sequence can be any iterable object\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2094 | "producing strings. This is equivalent to calling write() for each string."); |
Tim Peters | efc3a3a | 2001-09-20 07:55:22 +0000 | [diff] [blame] | 2095 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2096 | PyDoc_STRVAR(flush_doc, |
| 2097 | "flush() -> None. Flush the internal I/O buffer."); |
Tim Peters | efc3a3a | 2001-09-20 07:55:22 +0000 | [diff] [blame] | 2098 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2099 | PyDoc_STRVAR(close_doc, |
Tim Peters | efc3a3a | 2001-09-20 07:55:22 +0000 | [diff] [blame] | 2100 | "close() -> None or (perhaps) an integer. Close the file.\n" |
| 2101 | "\n" |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 2102 | "Sets data attribute .closed to True. A closed file cannot be used for\n" |
Tim Peters | efc3a3a | 2001-09-20 07:55:22 +0000 | [diff] [blame] | 2103 | "further I/O operations. close() may be called more than once without\n" |
| 2104 | "error. Some kinds of file objects (for example, opened by popen())\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2105 | "may return an exit status upon closing."); |
Tim Peters | efc3a3a | 2001-09-20 07:55:22 +0000 | [diff] [blame] | 2106 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2107 | PyDoc_STRVAR(isatty_doc, |
| 2108 | "isatty() -> true or false. True if the file is connected to a tty device."); |
Tim Peters | efc3a3a | 2001-09-20 07:55:22 +0000 | [diff] [blame] | 2109 | |
Guido van Rossum | 1a5e21e | 2006-02-28 21:57:43 +0000 | [diff] [blame] | 2110 | PyDoc_STRVAR(enter_doc, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2111 | "__enter__() -> self."); |
Guido van Rossum | 1a5e21e | 2006-02-28 21:57:43 +0000 | [diff] [blame] | 2112 | |
Georg Brandl | 98b40ad | 2006-06-08 14:50:21 +0000 | [diff] [blame] | 2113 | PyDoc_STRVAR(exit_doc, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2114 | "__exit__(*excinfo) -> None. Closes the file."); |
Georg Brandl | 98b40ad | 2006-06-08 14:50:21 +0000 | [diff] [blame] | 2115 | |
Tim Peters | efc3a3a | 2001-09-20 07:55:22 +0000 | [diff] [blame] | 2116 | static PyMethodDef file_methods[] = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2117 | {"readline", (PyCFunction)file_readline, METH_VARARGS, readline_doc}, |
| 2118 | {"read", (PyCFunction)file_read, METH_VARARGS, read_doc}, |
| 2119 | {"write", (PyCFunction)file_write, METH_VARARGS, write_doc}, |
| 2120 | {"fileno", (PyCFunction)file_fileno, METH_NOARGS, fileno_doc}, |
| 2121 | {"seek", (PyCFunction)file_seek, METH_VARARGS, seek_doc}, |
Tim Peters | efc3a3a | 2001-09-20 07:55:22 +0000 | [diff] [blame] | 2122 | #ifdef HAVE_FTRUNCATE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2123 | {"truncate", (PyCFunction)file_truncate, METH_VARARGS, truncate_doc}, |
Tim Peters | efc3a3a | 2001-09-20 07:55:22 +0000 | [diff] [blame] | 2124 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2125 | {"tell", (PyCFunction)file_tell, METH_NOARGS, tell_doc}, |
| 2126 | {"readinto", (PyCFunction)file_readinto, METH_VARARGS, readinto_doc}, |
| 2127 | {"readlines", (PyCFunction)file_readlines, METH_VARARGS, readlines_doc}, |
| 2128 | {"xreadlines",(PyCFunction)file_xreadlines, METH_NOARGS, xreadlines_doc}, |
| 2129 | {"writelines",(PyCFunction)file_writelines, METH_O, writelines_doc}, |
| 2130 | {"flush", (PyCFunction)file_flush, METH_NOARGS, flush_doc}, |
| 2131 | {"close", (PyCFunction)file_close, METH_NOARGS, close_doc}, |
| 2132 | {"isatty", (PyCFunction)file_isatty, METH_NOARGS, isatty_doc}, |
| 2133 | {"__enter__", (PyCFunction)file_self, METH_NOARGS, enter_doc}, |
| 2134 | {"__exit__", (PyCFunction)file_exit, METH_VARARGS, exit_doc}, |
| 2135 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2136 | }; |
| 2137 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2138 | #define OFF(x) offsetof(PyFileObject, x) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2139 | |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 2140 | static PyMemberDef file_memberlist[] = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2141 | {"mode", T_OBJECT, OFF(f_mode), RO, |
| 2142 | "file mode ('r', 'U', 'w', 'a', possibly with 'b' or '+' added)"}, |
| 2143 | {"name", T_OBJECT, OFF(f_name), RO, |
| 2144 | "file name"}, |
| 2145 | {"encoding", T_OBJECT, OFF(f_encoding), RO, |
| 2146 | "file encoding"}, |
| 2147 | {"errors", T_OBJECT, OFF(f_errors), RO, |
| 2148 | "Unicode error handler"}, |
| 2149 | /* getattr(f, "closed") is implemented without this table */ |
| 2150 | {NULL} /* Sentinel */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2151 | }; |
| 2152 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2153 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2154 | get_closed(PyFileObject *f, void *closure) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2155 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2156 | return PyBool_FromLong((long)(f->f_fp == 0)); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2157 | } |
Jack Jansen | 7b8c754 | 2002-04-14 20:12:41 +0000 | [diff] [blame] | 2158 | static PyObject * |
| 2159 | get_newlines(PyFileObject *f, void *closure) |
| 2160 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2161 | switch (f->f_newlinetypes) { |
| 2162 | case NEWLINE_UNKNOWN: |
| 2163 | Py_INCREF(Py_None); |
| 2164 | return Py_None; |
| 2165 | case NEWLINE_CR: |
| 2166 | return PyString_FromString("\r"); |
| 2167 | case NEWLINE_LF: |
| 2168 | return PyString_FromString("\n"); |
| 2169 | case NEWLINE_CR|NEWLINE_LF: |
| 2170 | return Py_BuildValue("(ss)", "\r", "\n"); |
| 2171 | case NEWLINE_CRLF: |
| 2172 | return PyString_FromString("\r\n"); |
| 2173 | case NEWLINE_CR|NEWLINE_CRLF: |
| 2174 | return Py_BuildValue("(ss)", "\r", "\r\n"); |
| 2175 | case NEWLINE_LF|NEWLINE_CRLF: |
| 2176 | return Py_BuildValue("(ss)", "\n", "\r\n"); |
| 2177 | case NEWLINE_CR|NEWLINE_LF|NEWLINE_CRLF: |
| 2178 | return Py_BuildValue("(sss)", "\r", "\n", "\r\n"); |
| 2179 | default: |
| 2180 | PyErr_Format(PyExc_SystemError, |
| 2181 | "Unknown newlines value 0x%x\n", |
| 2182 | f->f_newlinetypes); |
| 2183 | return NULL; |
| 2184 | } |
Jack Jansen | 7b8c754 | 2002-04-14 20:12:41 +0000 | [diff] [blame] | 2185 | } |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 2186 | |
Georg Brandl | 65bb42d | 2008-03-21 20:38:24 +0000 | [diff] [blame] | 2187 | static PyObject * |
| 2188 | get_softspace(PyFileObject *f, void *closure) |
| 2189 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2190 | if (PyErr_WarnPy3k("file.softspace not supported in 3.x", 1) < 0) |
| 2191 | return NULL; |
| 2192 | return PyInt_FromLong(f->f_softspace); |
Georg Brandl | 65bb42d | 2008-03-21 20:38:24 +0000 | [diff] [blame] | 2193 | } |
| 2194 | |
| 2195 | static int |
| 2196 | set_softspace(PyFileObject *f, PyObject *value) |
| 2197 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2198 | int new; |
| 2199 | if (PyErr_WarnPy3k("file.softspace not supported in 3.x", 1) < 0) |
| 2200 | return -1; |
Georg Brandl | 65bb42d | 2008-03-21 20:38:24 +0000 | [diff] [blame] | 2201 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2202 | if (value == NULL) { |
| 2203 | PyErr_SetString(PyExc_TypeError, |
| 2204 | "can't delete softspace attribute"); |
| 2205 | return -1; |
| 2206 | } |
Georg Brandl | 65bb42d | 2008-03-21 20:38:24 +0000 | [diff] [blame] | 2207 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2208 | new = PyInt_AsLong(value); |
| 2209 | if (new == -1 && PyErr_Occurred()) |
| 2210 | return -1; |
| 2211 | f->f_softspace = new; |
| 2212 | return 0; |
Georg Brandl | 65bb42d | 2008-03-21 20:38:24 +0000 | [diff] [blame] | 2213 | } |
| 2214 | |
Guido van Rossum | 32d34c8 | 2001-09-20 21:45:26 +0000 | [diff] [blame] | 2215 | static PyGetSetDef file_getsetlist[] = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2216 | {"closed", (getter)get_closed, NULL, "True if the file is closed"}, |
| 2217 | {"newlines", (getter)get_newlines, NULL, |
| 2218 | "end-of-line convention used in this file"}, |
| 2219 | {"softspace", (getter)get_softspace, (setter)set_softspace, |
| 2220 | "flag indicating that a space needs to be printed; used by print"}, |
| 2221 | {0}, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2222 | }; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2223 | |
Neal Norwitz | d8b995f | 2002-08-06 21:50:54 +0000 | [diff] [blame] | 2224 | static void |
Guido van Rossum | 7a6e959 | 2002-08-06 15:55:28 +0000 | [diff] [blame] | 2225 | drop_readahead(PyFileObject *f) |
Guido van Rossum | 6596725 | 2001-04-21 13:20:18 +0000 | [diff] [blame] | 2226 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2227 | if (f->f_buf != NULL) { |
| 2228 | PyMem_Free(f->f_buf); |
| 2229 | f->f_buf = NULL; |
| 2230 | } |
Guido van Rossum | 6596725 | 2001-04-21 13:20:18 +0000 | [diff] [blame] | 2231 | } |
| 2232 | |
Tim Peters | f1827cf | 2003-09-07 03:30:18 +0000 | [diff] [blame] | 2233 | /* Make sure that file has a readahead buffer with at least one byte |
| 2234 | (unless at EOF) and no more than bufsize. Returns negative value on |
Georg Brandl | ed02eb6 | 2006-03-31 20:31:02 +0000 | [diff] [blame] | 2235 | error, will set MemoryError if bufsize bytes cannot be allocated. */ |
Neal Norwitz | d8b995f | 2002-08-06 21:50:54 +0000 | [diff] [blame] | 2236 | static int |
Benjamin Peterson | 95bc0e4 | 2014-09-30 21:17:15 -0400 | [diff] [blame] | 2237 | readahead(PyFileObject *f, Py_ssize_t bufsize) |
Neal Norwitz | d8b995f | 2002-08-06 21:50:54 +0000 | [diff] [blame] | 2238 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2239 | Py_ssize_t chunksize; |
Guido van Rossum | 7a6e959 | 2002-08-06 15:55:28 +0000 | [diff] [blame] | 2240 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2241 | if (f->f_buf != NULL) { |
| 2242 | if( (f->f_bufend - f->f_bufptr) >= 1) |
| 2243 | return 0; |
| 2244 | else |
| 2245 | drop_readahead(f); |
| 2246 | } |
| 2247 | if ((f->f_buf = (char *)PyMem_Malloc(bufsize)) == NULL) { |
| 2248 | PyErr_NoMemory(); |
| 2249 | return -1; |
| 2250 | } |
| 2251 | FILE_BEGIN_ALLOW_THREADS(f) |
| 2252 | errno = 0; |
| 2253 | chunksize = Py_UniversalNewlineFread( |
| 2254 | f->f_buf, bufsize, f->f_fp, (PyObject *)f); |
| 2255 | FILE_END_ALLOW_THREADS(f) |
| 2256 | if (chunksize == 0) { |
| 2257 | if (ferror(f->f_fp)) { |
| 2258 | PyErr_SetFromErrno(PyExc_IOError); |
| 2259 | clearerr(f->f_fp); |
| 2260 | drop_readahead(f); |
| 2261 | return -1; |
| 2262 | } |
| 2263 | } |
| 2264 | f->f_bufptr = f->f_buf; |
| 2265 | f->f_bufend = f->f_buf + chunksize; |
| 2266 | return 0; |
Guido van Rossum | 7a6e959 | 2002-08-06 15:55:28 +0000 | [diff] [blame] | 2267 | } |
| 2268 | |
| 2269 | /* Used by file_iternext. The returned string will start with 'skip' |
Tim Peters | f1827cf | 2003-09-07 03:30:18 +0000 | [diff] [blame] | 2270 | uninitialized bytes followed by the remainder of the line. Don't be |
| 2271 | horrified by the recursive call: maximum recursion depth is limited by |
Guido van Rossum | 7a6e959 | 2002-08-06 15:55:28 +0000 | [diff] [blame] | 2272 | logarithmic buffer growth to about 50 even when reading a 1gb line. */ |
| 2273 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2274 | static PyStringObject * |
Benjamin Peterson | 95bc0e4 | 2014-09-30 21:17:15 -0400 | [diff] [blame] | 2275 | readahead_get_line_skip(PyFileObject *f, Py_ssize_t skip, Py_ssize_t bufsize) |
Neal Norwitz | d8b995f | 2002-08-06 21:50:54 +0000 | [diff] [blame] | 2276 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2277 | PyStringObject* s; |
| 2278 | char *bufptr; |
| 2279 | char *buf; |
| 2280 | Py_ssize_t len; |
Guido van Rossum | 7a6e959 | 2002-08-06 15:55:28 +0000 | [diff] [blame] | 2281 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2282 | if (f->f_buf == NULL) |
| 2283 | if (readahead(f, bufsize) < 0) |
| 2284 | return NULL; |
Guido van Rossum | 7a6e959 | 2002-08-06 15:55:28 +0000 | [diff] [blame] | 2285 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2286 | len = f->f_bufend - f->f_bufptr; |
| 2287 | if (len == 0) |
| 2288 | return (PyStringObject *) |
| 2289 | PyString_FromStringAndSize(NULL, skip); |
| 2290 | bufptr = (char *)memchr(f->f_bufptr, '\n', len); |
| 2291 | if (bufptr != NULL) { |
| 2292 | bufptr++; /* Count the '\n' */ |
| 2293 | len = bufptr - f->f_bufptr; |
| 2294 | s = (PyStringObject *) |
Benjamin Peterson | 95bc0e4 | 2014-09-30 21:17:15 -0400 | [diff] [blame] | 2295 | PyString_FromStringAndSize(NULL, skip + len); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2296 | if (s == NULL) |
| 2297 | return NULL; |
Benjamin Peterson | 95bc0e4 | 2014-09-30 21:17:15 -0400 | [diff] [blame] | 2298 | memcpy(PyString_AS_STRING(s) + skip, f->f_bufptr, len); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2299 | f->f_bufptr = bufptr; |
| 2300 | if (bufptr == f->f_bufend) |
| 2301 | drop_readahead(f); |
| 2302 | } else { |
| 2303 | bufptr = f->f_bufptr; |
| 2304 | buf = f->f_buf; |
| 2305 | f->f_buf = NULL; /* Force new readahead buffer */ |
Benjamin Peterson | 95bc0e4 | 2014-09-30 21:17:15 -0400 | [diff] [blame] | 2306 | assert(len <= PY_SSIZE_T_MAX - skip); |
| 2307 | s = readahead_get_line_skip(f, skip + len, bufsize + (bufsize>>2)); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2308 | if (s == NULL) { |
| 2309 | PyMem_Free(buf); |
| 2310 | return NULL; |
| 2311 | } |
Benjamin Peterson | 95bc0e4 | 2014-09-30 21:17:15 -0400 | [diff] [blame] | 2312 | memcpy(PyString_AS_STRING(s) + skip, bufptr, len); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2313 | PyMem_Free(buf); |
| 2314 | } |
| 2315 | return s; |
Guido van Rossum | 7a6e959 | 2002-08-06 15:55:28 +0000 | [diff] [blame] | 2316 | } |
| 2317 | |
| 2318 | /* A larger buffer size may actually decrease performance. */ |
| 2319 | #define READAHEAD_BUFSIZE 8192 |
| 2320 | |
| 2321 | static PyObject * |
| 2322 | file_iternext(PyFileObject *f) |
| 2323 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2324 | PyStringObject* l; |
Guido van Rossum | 7a6e959 | 2002-08-06 15:55:28 +0000 | [diff] [blame] | 2325 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2326 | if (f->f_fp == NULL) |
| 2327 | return err_closed(); |
| 2328 | if (!f->readable) |
| 2329 | return err_mode("reading"); |
Guido van Rossum | 7a6e959 | 2002-08-06 15:55:28 +0000 | [diff] [blame] | 2330 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2331 | l = readahead_get_line_skip(f, 0, READAHEAD_BUFSIZE); |
| 2332 | if (l == NULL || PyString_GET_SIZE(l) == 0) { |
| 2333 | Py_XDECREF(l); |
| 2334 | return NULL; |
| 2335 | } |
| 2336 | return (PyObject *)l; |
Guido van Rossum | 7a6e959 | 2002-08-06 15:55:28 +0000 | [diff] [blame] | 2337 | } |
| 2338 | |
| 2339 | |
Tim Peters | 59c9a64 | 2001-09-13 05:38:56 +0000 | [diff] [blame] | 2340 | static PyObject * |
| 2341 | file_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 2342 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2343 | PyObject *self; |
| 2344 | static PyObject *not_yet_string; |
Tim Peters | 4441001 | 2001-09-14 03:26:08 +0000 | [diff] [blame] | 2345 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2346 | assert(type != NULL && type->tp_alloc != NULL); |
Tim Peters | 4441001 | 2001-09-14 03:26:08 +0000 | [diff] [blame] | 2347 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2348 | if (not_yet_string == NULL) { |
| 2349 | not_yet_string = PyString_InternFromString("<uninitialized file>"); |
| 2350 | if (not_yet_string == NULL) |
| 2351 | return NULL; |
| 2352 | } |
Tim Peters | 4441001 | 2001-09-14 03:26:08 +0000 | [diff] [blame] | 2353 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2354 | self = type->tp_alloc(type, 0); |
| 2355 | if (self != NULL) { |
| 2356 | /* Always fill in the name and mode, so that nobody else |
| 2357 | needs to special-case NULLs there. */ |
| 2358 | Py_INCREF(not_yet_string); |
| 2359 | ((PyFileObject *)self)->f_name = not_yet_string; |
| 2360 | Py_INCREF(not_yet_string); |
| 2361 | ((PyFileObject *)self)->f_mode = not_yet_string; |
| 2362 | Py_INCREF(Py_None); |
| 2363 | ((PyFileObject *)self)->f_encoding = Py_None; |
| 2364 | Py_INCREF(Py_None); |
| 2365 | ((PyFileObject *)self)->f_errors = Py_None; |
| 2366 | ((PyFileObject *)self)->weakreflist = NULL; |
| 2367 | ((PyFileObject *)self)->unlocked_count = 0; |
| 2368 | } |
| 2369 | return self; |
Tim Peters | 4441001 | 2001-09-14 03:26:08 +0000 | [diff] [blame] | 2370 | } |
| 2371 | |
| 2372 | static int |
| 2373 | file_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 2374 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2375 | PyFileObject *foself = (PyFileObject *)self; |
| 2376 | int ret = 0; |
| 2377 | static char *kwlist[] = {"name", "mode", "buffering", 0}; |
| 2378 | char *name = NULL; |
| 2379 | char *mode = "r"; |
| 2380 | int bufsize = -1; |
| 2381 | int wideargument = 0; |
Hirokazu Yamamoto | 5c3dd9a | 2009-06-29 15:52:21 +0000 | [diff] [blame] | 2382 | #ifdef MS_WINDOWS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2383 | PyObject *po; |
Hirokazu Yamamoto | 5c3dd9a | 2009-06-29 15:52:21 +0000 | [diff] [blame] | 2384 | #endif |
Tim Peters | 4441001 | 2001-09-14 03:26:08 +0000 | [diff] [blame] | 2385 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2386 | assert(PyFile_Check(self)); |
| 2387 | if (foself->f_fp != NULL) { |
| 2388 | /* Have to close the existing file first. */ |
| 2389 | PyObject *closeresult = file_close(foself); |
| 2390 | if (closeresult == NULL) |
| 2391 | return -1; |
| 2392 | Py_DECREF(closeresult); |
| 2393 | } |
Tim Peters | 59c9a64 | 2001-09-13 05:38:56 +0000 | [diff] [blame] | 2394 | |
Hirokazu Yamamoto | b24bb27 | 2009-05-17 02:52:09 +0000 | [diff] [blame] | 2395 | #ifdef MS_WINDOWS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2396 | if (PyArg_ParseTupleAndKeywords(args, kwds, "U|si:file", |
Serhiy Storchaka | 3c9ce74 | 2016-07-01 23:34:44 +0300 | [diff] [blame] | 2397 | kwlist, &po, &mode, &bufsize) && |
| 2398 | wcslen(PyUnicode_AS_UNICODE(po)) == (size_t)PyUnicode_GET_SIZE(po)) { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2399 | wideargument = 1; |
| 2400 | if (fill_file_fields(foself, NULL, po, mode, |
| 2401 | fclose) == NULL) |
| 2402 | goto Error; |
| 2403 | } else { |
| 2404 | /* Drop the argument parsing error as narrow |
| 2405 | strings are also valid. */ |
| 2406 | PyErr_Clear(); |
| 2407 | } |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 2408 | #endif |
| 2409 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2410 | if (!wideargument) { |
| 2411 | PyObject *o_name; |
Nicholas Bastin | abce8a6 | 2004-03-21 20:24:07 +0000 | [diff] [blame] | 2412 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2413 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "et|si:file", kwlist, |
| 2414 | Py_FileSystemDefaultEncoding, |
| 2415 | &name, |
| 2416 | &mode, &bufsize)) |
| 2417 | return -1; |
Nicholas Bastin | abce8a6 | 2004-03-21 20:24:07 +0000 | [diff] [blame] | 2418 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2419 | /* We parse again to get the name as a PyObject */ |
| 2420 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|si:file", |
| 2421 | kwlist, &o_name, &mode, |
| 2422 | &bufsize)) |
| 2423 | goto Error; |
Nicholas Bastin | abce8a6 | 2004-03-21 20:24:07 +0000 | [diff] [blame] | 2424 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2425 | if (fill_file_fields(foself, NULL, o_name, mode, |
| 2426 | fclose) == NULL) |
| 2427 | goto Error; |
| 2428 | } |
| 2429 | if (open_the_file(foself, name, mode) == NULL) |
| 2430 | goto Error; |
| 2431 | foself->f_setbuf = NULL; |
| 2432 | PyFile_SetBufSize(self, bufsize); |
| 2433 | goto Done; |
Tim Peters | 4441001 | 2001-09-14 03:26:08 +0000 | [diff] [blame] | 2434 | |
| 2435 | Error: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2436 | ret = -1; |
| 2437 | /* fall through */ |
Tim Peters | 4441001 | 2001-09-14 03:26:08 +0000 | [diff] [blame] | 2438 | Done: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2439 | PyMem_Free(name); /* free the encoded string */ |
| 2440 | return ret; |
Tim Peters | 59c9a64 | 2001-09-13 05:38:56 +0000 | [diff] [blame] | 2441 | } |
| 2442 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2443 | PyDoc_VAR(file_doc) = |
| 2444 | PyDoc_STR( |
Tim Peters | 59c9a64 | 2001-09-13 05:38:56 +0000 | [diff] [blame] | 2445 | "file(name[, mode[, buffering]]) -> file object\n" |
| 2446 | "\n" |
| 2447 | "Open a file. The mode can be 'r', 'w' or 'a' for reading (default),\n" |
| 2448 | "writing or appending. The file will be created if it doesn't exist\n" |
| 2449 | "when opened for writing or appending; it will be truncated when\n" |
| 2450 | "opened for writing. Add a 'b' to the mode for binary files.\n" |
| 2451 | "Add a '+' to the mode to allow simultaneous reading and writing.\n" |
| 2452 | "If the buffering argument is given, 0 means unbuffered, 1 means line\n" |
Skip Montanaro | 4e3ebe0 | 2007-12-08 14:37:43 +0000 | [diff] [blame] | 2453 | "buffered, and larger numbers specify the buffer size. The preferred way\n" |
| 2454 | "to open a file is with the builtin open() function.\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2455 | ) |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2456 | PyDoc_STR( |
Barry Warsaw | 4be55b5 | 2002-05-22 20:37:53 +0000 | [diff] [blame] | 2457 | "Add a 'U' to mode to open the file for input with universal newline\n" |
| 2458 | "support. Any line ending in the input file will be seen as a '\\n'\n" |
| 2459 | "in Python. Also, a file so opened gains the attribute 'newlines';\n" |
| 2460 | "the value for this attribute is one of None (no newline read yet),\n" |
| 2461 | "'\\r', '\\n', '\\r\\n' or a tuple containing all the newline types seen.\n" |
| 2462 | "\n" |
| 2463 | "'U' cannot be combined with 'w' or '+' mode.\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 2464 | ); |
Tim Peters | 59c9a64 | 2001-09-13 05:38:56 +0000 | [diff] [blame] | 2465 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2466 | PyTypeObject PyFile_Type = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2467 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 2468 | "file", |
| 2469 | sizeof(PyFileObject), |
| 2470 | 0, |
| 2471 | (destructor)file_dealloc, /* tp_dealloc */ |
| 2472 | 0, /* tp_print */ |
| 2473 | 0, /* tp_getattr */ |
| 2474 | 0, /* tp_setattr */ |
| 2475 | 0, /* tp_compare */ |
| 2476 | (reprfunc)file_repr, /* tp_repr */ |
| 2477 | 0, /* tp_as_number */ |
| 2478 | 0, /* tp_as_sequence */ |
| 2479 | 0, /* tp_as_mapping */ |
| 2480 | 0, /* tp_hash */ |
| 2481 | 0, /* tp_call */ |
| 2482 | 0, /* tp_str */ |
| 2483 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 2484 | /* softspace is writable: we must supply tp_setattro */ |
| 2485 | PyObject_GenericSetAttr, /* tp_setattro */ |
| 2486 | 0, /* tp_as_buffer */ |
| 2487 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_WEAKREFS, /* tp_flags */ |
| 2488 | file_doc, /* tp_doc */ |
| 2489 | 0, /* tp_traverse */ |
| 2490 | 0, /* tp_clear */ |
| 2491 | 0, /* tp_richcompare */ |
| 2492 | offsetof(PyFileObject, weakreflist), /* tp_weaklistoffset */ |
| 2493 | (getiterfunc)file_self, /* tp_iter */ |
| 2494 | (iternextfunc)file_iternext, /* tp_iternext */ |
| 2495 | file_methods, /* tp_methods */ |
| 2496 | file_memberlist, /* tp_members */ |
| 2497 | file_getsetlist, /* tp_getset */ |
| 2498 | 0, /* tp_base */ |
| 2499 | 0, /* tp_dict */ |
| 2500 | 0, /* tp_descr_get */ |
| 2501 | 0, /* tp_descr_set */ |
| 2502 | 0, /* tp_dictoffset */ |
| 2503 | file_init, /* tp_init */ |
| 2504 | PyType_GenericAlloc, /* tp_alloc */ |
| 2505 | file_new, /* tp_new */ |
| 2506 | PyObject_Del, /* tp_free */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2507 | }; |
Guido van Rossum | eb183da | 1991-04-04 10:44:06 +0000 | [diff] [blame] | 2508 | |
| 2509 | /* Interface for the 'soft space' between print items. */ |
| 2510 | |
| 2511 | int |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 2512 | PyFile_SoftSpace(PyObject *f, int newflag) |
Guido van Rossum | eb183da | 1991-04-04 10:44:06 +0000 | [diff] [blame] | 2513 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2514 | long oldflag = 0; |
| 2515 | if (f == NULL) { |
| 2516 | /* Do nothing */ |
| 2517 | } |
| 2518 | else if (PyFile_Check(f)) { |
| 2519 | oldflag = ((PyFileObject *)f)->f_softspace; |
| 2520 | ((PyFileObject *)f)->f_softspace = newflag; |
| 2521 | } |
| 2522 | else { |
| 2523 | PyObject *v; |
| 2524 | v = PyObject_GetAttrString(f, "softspace"); |
| 2525 | if (v == NULL) |
| 2526 | PyErr_Clear(); |
| 2527 | else { |
| 2528 | if (PyInt_Check(v)) |
| 2529 | oldflag = PyInt_AsLong(v); |
| 2530 | assert(oldflag < INT_MAX); |
| 2531 | Py_DECREF(v); |
| 2532 | } |
| 2533 | v = PyInt_FromLong((long)newflag); |
| 2534 | if (v == NULL) |
| 2535 | PyErr_Clear(); |
| 2536 | else { |
| 2537 | if (PyObject_SetAttrString(f, "softspace", v) != 0) |
| 2538 | PyErr_Clear(); |
| 2539 | Py_DECREF(v); |
| 2540 | } |
| 2541 | } |
| 2542 | return (int)oldflag; |
Guido van Rossum | eb183da | 1991-04-04 10:44:06 +0000 | [diff] [blame] | 2543 | } |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 2544 | |
| 2545 | /* Interfaces to write objects/strings to file-like objects */ |
| 2546 | |
| 2547 | int |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 2548 | PyFile_WriteObject(PyObject *v, PyObject *f, int flags) |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 2549 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2550 | PyObject *writer, *value, *args, *result; |
| 2551 | if (f == NULL) { |
| 2552 | PyErr_SetString(PyExc_TypeError, "writeobject with NULL file"); |
| 2553 | return -1; |
| 2554 | } |
| 2555 | else if (PyFile_Check(f)) { |
| 2556 | PyFileObject *fobj = (PyFileObject *) f; |
Fred Drake | 086a0f7 | 2004-03-19 15:22:36 +0000 | [diff] [blame] | 2557 | #ifdef Py_USING_UNICODE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2558 | PyObject *enc = fobj->f_encoding; |
| 2559 | int result; |
Fred Drake | 086a0f7 | 2004-03-19 15:22:36 +0000 | [diff] [blame] | 2560 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2561 | if (fobj->f_fp == NULL) { |
| 2562 | err_closed(); |
| 2563 | return -1; |
| 2564 | } |
Martin v. Löwis | 5467d4c | 2003-05-10 07:10:12 +0000 | [diff] [blame] | 2565 | #ifdef Py_USING_UNICODE |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2566 | if ((flags & Py_PRINT_RAW) && |
| 2567 | PyUnicode_Check(v) && enc != Py_None) { |
| 2568 | char *cenc = PyString_AS_STRING(enc); |
| 2569 | char *errors = fobj->f_errors == Py_None ? |
| 2570 | "strict" : PyString_AS_STRING(fobj->f_errors); |
| 2571 | value = PyUnicode_AsEncodedString(v, cenc, errors); |
| 2572 | if (value == NULL) |
| 2573 | return -1; |
| 2574 | } else { |
| 2575 | value = v; |
| 2576 | Py_INCREF(value); |
| 2577 | } |
| 2578 | result = file_PyObject_Print(value, fobj, flags); |
| 2579 | Py_DECREF(value); |
| 2580 | return result; |
Martin v. Löwis | 5467d4c | 2003-05-10 07:10:12 +0000 | [diff] [blame] | 2581 | #else |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2582 | return file_PyObject_Print(v, fobj, flags); |
Martin v. Löwis | 5467d4c | 2003-05-10 07:10:12 +0000 | [diff] [blame] | 2583 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2584 | } |
| 2585 | writer = PyObject_GetAttrString(f, "write"); |
| 2586 | if (writer == NULL) |
| 2587 | return -1; |
| 2588 | if (flags & Py_PRINT_RAW) { |
| 2589 | if (PyUnicode_Check(v)) { |
| 2590 | value = v; |
| 2591 | Py_INCREF(value); |
| 2592 | } else |
| 2593 | value = PyObject_Str(v); |
| 2594 | } |
| 2595 | else |
| 2596 | value = PyObject_Repr(v); |
| 2597 | if (value == NULL) { |
| 2598 | Py_DECREF(writer); |
| 2599 | return -1; |
| 2600 | } |
| 2601 | args = PyTuple_Pack(1, value); |
| 2602 | if (args == NULL) { |
| 2603 | Py_DECREF(value); |
| 2604 | Py_DECREF(writer); |
| 2605 | return -1; |
| 2606 | } |
| 2607 | result = PyEval_CallObject(writer, args); |
| 2608 | Py_DECREF(args); |
| 2609 | Py_DECREF(value); |
| 2610 | Py_DECREF(writer); |
| 2611 | if (result == NULL) |
| 2612 | return -1; |
| 2613 | Py_DECREF(result); |
| 2614 | return 0; |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 2615 | } |
| 2616 | |
Guido van Rossum | 27a60b1 | 1997-05-22 22:25:11 +0000 | [diff] [blame] | 2617 | int |
Tim Peters | c1bbcb8 | 2001-11-28 22:13:25 +0000 | [diff] [blame] | 2618 | PyFile_WriteString(const char *s, PyObject *f) |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 2619 | { |
Gregory P. Smith | aa63d0d | 2008-04-06 23:11:17 +0000 | [diff] [blame] | 2620 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2621 | if (f == NULL) { |
| 2622 | /* Should be caused by a pre-existing error */ |
| 2623 | if (!PyErr_Occurred()) |
| 2624 | PyErr_SetString(PyExc_SystemError, |
| 2625 | "null file for PyFile_WriteString"); |
| 2626 | return -1; |
| 2627 | } |
| 2628 | else if (PyFile_Check(f)) { |
| 2629 | PyFileObject *fobj = (PyFileObject *) f; |
| 2630 | FILE *fp = PyFile_AsFile(f); |
| 2631 | if (fp == NULL) { |
| 2632 | err_closed(); |
| 2633 | return -1; |
| 2634 | } |
| 2635 | FILE_BEGIN_ALLOW_THREADS(fobj) |
| 2636 | fputs(s, fp); |
| 2637 | FILE_END_ALLOW_THREADS(fobj) |
| 2638 | return 0; |
| 2639 | } |
| 2640 | else if (!PyErr_Occurred()) { |
| 2641 | PyObject *v = PyString_FromString(s); |
| 2642 | int err; |
| 2643 | if (v == NULL) |
| 2644 | return -1; |
| 2645 | err = PyFile_WriteObject(v, f, Py_PRINT_RAW); |
| 2646 | Py_DECREF(v); |
| 2647 | return err; |
| 2648 | } |
| 2649 | else |
| 2650 | return -1; |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 2651 | } |
Andrew M. Kuchling | 06051ed | 2000-07-13 23:56:54 +0000 | [diff] [blame] | 2652 | |
| 2653 | /* Try to get a file-descriptor from a Python object. If the object |
| 2654 | is an integer or long integer, its value is returned. If not, the |
| 2655 | object's fileno() method is called if it exists; the method must return |
| 2656 | an integer or long integer, which is returned as the file descriptor value. |
| 2657 | -1 is returned on failure. |
| 2658 | */ |
| 2659 | |
| 2660 | int PyObject_AsFileDescriptor(PyObject *o) |
| 2661 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2662 | int fd; |
| 2663 | PyObject *meth; |
Andrew M. Kuchling | 06051ed | 2000-07-13 23:56:54 +0000 | [diff] [blame] | 2664 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2665 | if (PyInt_Check(o)) { |
Serhiy Storchaka | 74f49ab | 2013-01-19 12:55:39 +0200 | [diff] [blame] | 2666 | fd = _PyInt_AsInt(o); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2667 | } |
| 2668 | else if (PyLong_Check(o)) { |
Serhiy Storchaka | 74f49ab | 2013-01-19 12:55:39 +0200 | [diff] [blame] | 2669 | fd = _PyLong_AsInt(o); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2670 | } |
| 2671 | else if ((meth = PyObject_GetAttrString(o, "fileno")) != NULL) |
| 2672 | { |
| 2673 | PyObject *fno = PyEval_CallObject(meth, NULL); |
| 2674 | Py_DECREF(meth); |
| 2675 | if (fno == NULL) |
| 2676 | return -1; |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 2677 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2678 | if (PyInt_Check(fno)) { |
Serhiy Storchaka | 74f49ab | 2013-01-19 12:55:39 +0200 | [diff] [blame] | 2679 | fd = _PyInt_AsInt(fno); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2680 | Py_DECREF(fno); |
| 2681 | } |
| 2682 | else if (PyLong_Check(fno)) { |
Serhiy Storchaka | 74f49ab | 2013-01-19 12:55:39 +0200 | [diff] [blame] | 2683 | fd = _PyLong_AsInt(fno); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2684 | Py_DECREF(fno); |
| 2685 | } |
| 2686 | else { |
| 2687 | PyErr_SetString(PyExc_TypeError, |
| 2688 | "fileno() returned a non-integer"); |
| 2689 | Py_DECREF(fno); |
| 2690 | return -1; |
| 2691 | } |
| 2692 | } |
| 2693 | else { |
| 2694 | PyErr_SetString(PyExc_TypeError, |
| 2695 | "argument must be an int, or have a fileno() method."); |
| 2696 | return -1; |
| 2697 | } |
Andrew M. Kuchling | 06051ed | 2000-07-13 23:56:54 +0000 | [diff] [blame] | 2698 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2699 | if (fd < 0) { |
| 2700 | PyErr_Format(PyExc_ValueError, |
| 2701 | "file descriptor cannot be a negative integer (%i)", |
| 2702 | fd); |
| 2703 | return -1; |
| 2704 | } |
| 2705 | return fd; |
Andrew M. Kuchling | 06051ed | 2000-07-13 23:56:54 +0000 | [diff] [blame] | 2706 | } |
Jack Jansen | 7b8c754 | 2002-04-14 20:12:41 +0000 | [diff] [blame] | 2707 | |
Jack Jansen | 7b8c754 | 2002-04-14 20:12:41 +0000 | [diff] [blame] | 2708 | /* From here on we need access to the real fgets and fread */ |
| 2709 | #undef fgets |
| 2710 | #undef fread |
| 2711 | |
| 2712 | /* |
| 2713 | ** Py_UniversalNewlineFgets is an fgets variation that understands |
| 2714 | ** all of \r, \n and \r\n conventions. |
| 2715 | ** The stream should be opened in binary mode. |
| 2716 | ** If fobj is NULL the routine always does newline conversion, and |
| 2717 | ** it may peek one char ahead to gobble the second char in \r\n. |
| 2718 | ** If fobj is non-NULL it must be a PyFileObject. In this case there |
| 2719 | ** is no readahead but in stead a flag is used to skip a following |
| 2720 | ** \n on the next read. Also, if the file is open in binary mode |
| 2721 | ** the whole conversion is skipped. Finally, the routine keeps track of |
| 2722 | ** the different types of newlines seen. |
| 2723 | ** Note that we need no error handling: fgets() treats error and eof |
| 2724 | ** identically. |
| 2725 | */ |
| 2726 | char * |
| 2727 | Py_UniversalNewlineFgets(char *buf, int n, FILE *stream, PyObject *fobj) |
| 2728 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2729 | char *p = buf; |
| 2730 | int c; |
| 2731 | int newlinetypes = 0; |
| 2732 | int skipnextlf = 0; |
| 2733 | int univ_newline = 1; |
Tim Peters | 058b141 | 2002-04-21 07:29:14 +0000 | [diff] [blame] | 2734 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2735 | if (fobj) { |
| 2736 | if (!PyFile_Check(fobj)) { |
| 2737 | errno = ENXIO; /* What can you do... */ |
| 2738 | return NULL; |
| 2739 | } |
| 2740 | univ_newline = ((PyFileObject *)fobj)->f_univ_newline; |
| 2741 | if ( !univ_newline ) |
| 2742 | return fgets(buf, n, stream); |
| 2743 | newlinetypes = ((PyFileObject *)fobj)->f_newlinetypes; |
| 2744 | skipnextlf = ((PyFileObject *)fobj)->f_skipnextlf; |
| 2745 | } |
| 2746 | FLOCKFILE(stream); |
| 2747 | c = 'x'; /* Shut up gcc warning */ |
| 2748 | while (--n > 0 && (c = GETC(stream)) != EOF ) { |
| 2749 | if (skipnextlf ) { |
| 2750 | skipnextlf = 0; |
| 2751 | if (c == '\n') { |
| 2752 | /* Seeing a \n here with skipnextlf true |
| 2753 | ** means we saw a \r before. |
| 2754 | */ |
| 2755 | newlinetypes |= NEWLINE_CRLF; |
| 2756 | c = GETC(stream); |
| 2757 | if (c == EOF) break; |
| 2758 | } else { |
| 2759 | /* |
| 2760 | ** Note that c == EOF also brings us here, |
| 2761 | ** so we're okay if the last char in the file |
| 2762 | ** is a CR. |
| 2763 | */ |
| 2764 | newlinetypes |= NEWLINE_CR; |
| 2765 | } |
| 2766 | } |
| 2767 | if (c == '\r') { |
| 2768 | /* A \r is translated into a \n, and we skip |
| 2769 | ** an adjacent \n, if any. We don't set the |
| 2770 | ** newlinetypes flag until we've seen the next char. |
| 2771 | */ |
| 2772 | skipnextlf = 1; |
| 2773 | c = '\n'; |
| 2774 | } else if ( c == '\n') { |
| 2775 | newlinetypes |= NEWLINE_LF; |
| 2776 | } |
| 2777 | *p++ = c; |
| 2778 | if (c == '\n') break; |
| 2779 | } |
| 2780 | if ( c == EOF && skipnextlf ) |
| 2781 | newlinetypes |= NEWLINE_CR; |
| 2782 | FUNLOCKFILE(stream); |
| 2783 | *p = '\0'; |
| 2784 | if (fobj) { |
| 2785 | ((PyFileObject *)fobj)->f_newlinetypes = newlinetypes; |
| 2786 | ((PyFileObject *)fobj)->f_skipnextlf = skipnextlf; |
| 2787 | } else if ( skipnextlf ) { |
| 2788 | /* If we have no file object we cannot save the |
| 2789 | ** skipnextlf flag. We have to readahead, which |
| 2790 | ** will cause a pause if we're reading from an |
| 2791 | ** interactive stream, but that is very unlikely |
| 2792 | ** unless we're doing something silly like |
| 2793 | ** execfile("/dev/tty"). |
| 2794 | */ |
| 2795 | c = GETC(stream); |
| 2796 | if ( c != '\n' ) |
| 2797 | ungetc(c, stream); |
| 2798 | } |
| 2799 | if (p == buf) |
| 2800 | return NULL; |
| 2801 | return buf; |
Jack Jansen | 7b8c754 | 2002-04-14 20:12:41 +0000 | [diff] [blame] | 2802 | } |
| 2803 | |
| 2804 | /* |
| 2805 | ** Py_UniversalNewlineFread is an fread variation that understands |
| 2806 | ** all of \r, \n and \r\n conventions. |
| 2807 | ** The stream should be opened in binary mode. |
| 2808 | ** fobj must be a PyFileObject. In this case there |
| 2809 | ** is no readahead but in stead a flag is used to skip a following |
| 2810 | ** \n on the next read. Also, if the file is open in binary mode |
| 2811 | ** the whole conversion is skipped. Finally, the routine keeps track of |
| 2812 | ** the different types of newlines seen. |
| 2813 | */ |
| 2814 | size_t |
Tim Peters | 058b141 | 2002-04-21 07:29:14 +0000 | [diff] [blame] | 2815 | Py_UniversalNewlineFread(char *buf, size_t n, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2816 | FILE *stream, PyObject *fobj) |
Jack Jansen | 7b8c754 | 2002-04-14 20:12:41 +0000 | [diff] [blame] | 2817 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2818 | char *dst = buf; |
| 2819 | PyFileObject *f = (PyFileObject *)fobj; |
| 2820 | int newlinetypes, skipnextlf; |
Tim Peters | 058b141 | 2002-04-21 07:29:14 +0000 | [diff] [blame] | 2821 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2822 | assert(buf != NULL); |
| 2823 | assert(stream != NULL); |
Tim Peters | 058b141 | 2002-04-21 07:29:14 +0000 | [diff] [blame] | 2824 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2825 | if (!fobj || !PyFile_Check(fobj)) { |
| 2826 | errno = ENXIO; /* What can you do... */ |
| 2827 | return 0; |
| 2828 | } |
| 2829 | if (!f->f_univ_newline) |
| 2830 | return fread(buf, 1, n, stream); |
| 2831 | newlinetypes = f->f_newlinetypes; |
| 2832 | skipnextlf = f->f_skipnextlf; |
| 2833 | /* Invariant: n is the number of bytes remaining to be filled |
| 2834 | * in the buffer. |
| 2835 | */ |
| 2836 | while (n) { |
| 2837 | size_t nread; |
| 2838 | int shortread; |
| 2839 | char *src = dst; |
Tim Peters | 058b141 | 2002-04-21 07:29:14 +0000 | [diff] [blame] | 2840 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2841 | nread = fread(dst, 1, n, stream); |
| 2842 | assert(nread <= n); |
| 2843 | if (nread == 0) |
| 2844 | break; |
Neal Norwitz | cb3319f | 2003-02-09 01:10:02 +0000 | [diff] [blame] | 2845 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2846 | n -= nread; /* assuming 1 byte out for each in; will adjust */ |
| 2847 | shortread = n != 0; /* true iff EOF or error */ |
| 2848 | while (nread--) { |
| 2849 | char c = *src++; |
| 2850 | if (c == '\r') { |
| 2851 | /* Save as LF and set flag to skip next LF. */ |
| 2852 | *dst++ = '\n'; |
| 2853 | skipnextlf = 1; |
| 2854 | } |
| 2855 | else if (skipnextlf && c == '\n') { |
| 2856 | /* Skip LF, and remember we saw CR LF. */ |
| 2857 | skipnextlf = 0; |
| 2858 | newlinetypes |= NEWLINE_CRLF; |
| 2859 | ++n; |
| 2860 | } |
| 2861 | else { |
| 2862 | /* Normal char to be stored in buffer. Also |
| 2863 | * update the newlinetypes flag if either this |
| 2864 | * is an LF or the previous char was a CR. |
| 2865 | */ |
| 2866 | if (c == '\n') |
| 2867 | newlinetypes |= NEWLINE_LF; |
| 2868 | else if (skipnextlf) |
| 2869 | newlinetypes |= NEWLINE_CR; |
| 2870 | *dst++ = c; |
| 2871 | skipnextlf = 0; |
| 2872 | } |
| 2873 | } |
| 2874 | if (shortread) { |
| 2875 | /* If this is EOF, update type flags. */ |
| 2876 | if (skipnextlf && feof(stream)) |
| 2877 | newlinetypes |= NEWLINE_CR; |
| 2878 | break; |
| 2879 | } |
| 2880 | } |
| 2881 | f->f_newlinetypes = newlinetypes; |
| 2882 | f->f_skipnextlf = skipnextlf; |
| 2883 | return dst - buf; |
Jack Jansen | 7b8c754 | 2002-04-14 20:12:41 +0000 | [diff] [blame] | 2884 | } |
Anthony Baxter | ac6bd46 | 2006-04-13 02:06:09 +0000 | [diff] [blame] | 2885 | |
| 2886 | #ifdef __cplusplus |
| 2887 | } |
| 2888 | #endif |