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