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