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