Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2 | /* File object implementation */ |
| 3 | |
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 | |
Guido van Rossum | ff7e83d | 1999-08-27 20:39:37 +0000 | [diff] [blame] | 7 | #ifndef DONT_HAVE_SYS_TYPES_H |
Guido van Rossum | 4149843 | 1999-01-07 22:09:51 +0000 | [diff] [blame] | 8 | #include <sys/types.h> |
Guido van Rossum | ff7e83d | 1999-08-27 20:39:37 +0000 | [diff] [blame] | 9 | #endif /* DONT_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 | |
Guido van Rossum | f2044e1 | 1998-04-28 16:05:59 +0000 | [diff] [blame] | 19 | #ifdef macintosh |
| 20 | #ifdef USE_GUSI |
| 21 | #define HAVE_FTRUNCATE |
| 22 | #endif |
| 23 | #endif |
| 24 | |
Jack Jansen | e08dea19 | 1995-04-23 22:12:47 +0000 | [diff] [blame] | 25 | #ifdef __MWERKS__ |
| 26 | /* Mwerks fopen() doesn't always set errno */ |
| 27 | #define NO_FOPEN_ERRNO |
| 28 | #endif |
Guido van Rossum | 295d171 | 1995-02-19 15:55:19 +0000 | [diff] [blame] | 29 | |
Andrew MacIntyre | c487439 | 2002-02-26 11:36:35 +0000 | [diff] [blame] | 30 | #if defined(PYOS_OS2) && defined(PYCC_GCC) |
| 31 | #include <io.h> |
| 32 | #endif |
| 33 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 34 | #define BUF(v) PyString_AS_STRING((PyStringObject *)v) |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 35 | |
Guido van Rossum | ff7e83d | 1999-08-27 20:39:37 +0000 | [diff] [blame] | 36 | #ifndef DONT_HAVE_ERRNO_H |
Guido van Rossum | f1dc566 | 1993-07-05 10:31:29 +0000 | [diff] [blame] | 37 | #include <errno.h> |
Guido van Rossum | ff7e83d | 1999-08-27 20:39:37 +0000 | [diff] [blame] | 38 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 39 | |
Jack Jansen | 7b8c754 | 2002-04-14 20:12:41 +0000 | [diff] [blame] | 40 | #ifdef HAVE_GETC_UNLOCKED |
| 41 | #define GETC(f) getc_unlocked(f) |
| 42 | #define FLOCKFILE(f) flockfile(f) |
| 43 | #define FUNLOCKFILE(f) funlockfile(f) |
| 44 | #else |
| 45 | #define GETC(f) getc(f) |
| 46 | #define FLOCKFILE(f) |
| 47 | #define FUNLOCKFILE(f) |
| 48 | #endif |
| 49 | |
| 50 | #ifdef WITH_UNIVERSAL_NEWLINES |
| 51 | /* Bits in f_newlinetypes */ |
| 52 | #define NEWLINE_UNKNOWN 0 /* No newline seen, yet */ |
| 53 | #define NEWLINE_CR 1 /* \r newline seen */ |
| 54 | #define NEWLINE_LF 2 /* \n newline seen */ |
| 55 | #define NEWLINE_CRLF 4 /* \r\n newline seen */ |
| 56 | #endif |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 57 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 58 | FILE * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 59 | PyFile_AsFile(PyObject *f) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 60 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 61 | if (f == NULL || !PyFile_Check(f)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 62 | return NULL; |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 63 | else |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 64 | return ((PyFileObject *)f)->f_fp; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 67 | PyObject * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 68 | PyFile_Name(PyObject *f) |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 69 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 70 | if (f == NULL || !PyFile_Check(f)) |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 71 | return NULL; |
| 72 | else |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 73 | return ((PyFileObject *)f)->f_name; |
Guido van Rossum | db3165e | 1993-10-18 17:06:59 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Neil Schemenauer | ed19b88 | 2002-03-23 02:06:50 +0000 | [diff] [blame] | 76 | /* On Unix, fopen will succeed for directories. |
| 77 | In Python, there should be no file objects referring to |
| 78 | directories, so we need a check. */ |
| 79 | |
| 80 | static PyFileObject* |
| 81 | dircheck(PyFileObject* f) |
| 82 | { |
| 83 | #if defined(HAVE_FSTAT) && defined(S_IFDIR) && defined(EISDIR) |
| 84 | struct stat buf; |
| 85 | if (f->f_fp == NULL) |
| 86 | return f; |
| 87 | if (fstat(fileno(f->f_fp), &buf) == 0 && |
| 88 | S_ISDIR(buf.st_mode)) { |
| 89 | #ifdef HAVE_STRERROR |
| 90 | char *msg = strerror(EISDIR); |
| 91 | #else |
| 92 | char *msg = "Is a directory"; |
| 93 | #endif |
| 94 | PyObject *exc = PyObject_CallFunction(PyExc_IOError, "(is)", EISDIR, msg); |
| 95 | PyErr_SetObject(PyExc_IOError, exc); |
| 96 | return NULL; |
| 97 | } |
| 98 | #endif |
| 99 | return f; |
| 100 | } |
| 101 | |
Tim Peters | 59c9a64 | 2001-09-13 05:38:56 +0000 | [diff] [blame] | 102 | |
| 103 | static PyObject * |
| 104 | fill_file_fields(PyFileObject *f, FILE *fp, char *name, char *mode, |
| 105 | int (*close)(FILE *)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 106 | { |
Tim Peters | 59c9a64 | 2001-09-13 05:38:56 +0000 | [diff] [blame] | 107 | assert(f != NULL); |
| 108 | assert(PyFile_Check(f)); |
Tim Peters | 4441001 | 2001-09-14 03:26:08 +0000 | [diff] [blame] | 109 | assert(f->f_fp == NULL); |
| 110 | |
| 111 | Py_DECREF(f->f_name); |
| 112 | Py_DECREF(f->f_mode); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 113 | f->f_name = PyString_FromString(name); |
| 114 | f->f_mode = PyString_FromString(mode); |
Tim Peters | 4441001 | 2001-09-14 03:26:08 +0000 | [diff] [blame] | 115 | |
Guido van Rossum | a1ab7fa | 1991-06-04 19:37:39 +0000 | [diff] [blame] | 116 | f->f_close = close; |
Guido van Rossum | eb183da | 1991-04-04 10:44:06 +0000 | [diff] [blame] | 117 | f->f_softspace = 0; |
Tim Peters | 59c9a64 | 2001-09-13 05:38:56 +0000 | [diff] [blame] | 118 | f->f_binary = strchr(mode,'b') != NULL; |
Jack Jansen | 7b8c754 | 2002-04-14 20:12:41 +0000 | [diff] [blame] | 119 | #ifdef WITH_UNIVERSAL_NEWLINES |
| 120 | f->f_univ_newline = (strchr(mode, 'U') != NULL); |
| 121 | f->f_newlinetypes = NEWLINE_UNKNOWN; |
| 122 | f->f_skipnextlf = 0; |
| 123 | #endif |
Tim Peters | 4441001 | 2001-09-14 03:26:08 +0000 | [diff] [blame] | 124 | |
Tim Peters | 59c9a64 | 2001-09-13 05:38:56 +0000 | [diff] [blame] | 125 | if (f->f_name == NULL || f->f_mode == NULL) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 126 | return NULL; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 127 | f->f_fp = fp; |
Neil Schemenauer | ed19b88 | 2002-03-23 02:06:50 +0000 | [diff] [blame] | 128 | f = dircheck(f); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 129 | return (PyObject *) f; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Tim Peters | 59c9a64 | 2001-09-13 05:38:56 +0000 | [diff] [blame] | 132 | static PyObject * |
| 133 | open_the_file(PyFileObject *f, char *name, char *mode) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 134 | { |
Tim Peters | 59c9a64 | 2001-09-13 05:38:56 +0000 | [diff] [blame] | 135 | assert(f != NULL); |
| 136 | assert(PyFile_Check(f)); |
| 137 | assert(name != NULL); |
| 138 | assert(mode != NULL); |
Tim Peters | 4441001 | 2001-09-14 03:26:08 +0000 | [diff] [blame] | 139 | assert(f->f_fp == NULL); |
Tim Peters | 59c9a64 | 2001-09-13 05:38:56 +0000 | [diff] [blame] | 140 | |
Tim Peters | 8fa4567 | 2001-09-13 21:01:29 +0000 | [diff] [blame] | 141 | /* rexec.py can't stop a user from getting the file() constructor -- |
| 142 | all they have to do is get *any* file object f, and then do |
| 143 | type(f). Here we prevent them from doing damage with it. */ |
| 144 | if (PyEval_GetRestricted()) { |
| 145 | PyErr_SetString(PyExc_IOError, |
| 146 | "file() constructor not accessible in restricted mode"); |
| 147 | return NULL; |
| 148 | } |
Tim Peters | a27a150 | 2001-11-09 20:59:14 +0000 | [diff] [blame] | 149 | errno = 0; |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 150 | #ifdef HAVE_FOPENRF |
Guido van Rossum | a08095a | 1991-02-13 23:25:27 +0000 | [diff] [blame] | 151 | if (*mode == '*') { |
| 152 | FILE *fopenRF(); |
| 153 | f->f_fp = fopenRF(name, mode+1); |
| 154 | } |
| 155 | else |
| 156 | #endif |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 157 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 158 | Py_BEGIN_ALLOW_THREADS |
Jack Jansen | 7b8c754 | 2002-04-14 20:12:41 +0000 | [diff] [blame] | 159 | #ifdef WITH_UNIVERSAL_NEWLINES |
| 160 | if (strcmp(mode, "U") == 0 || strcmp(mode, "rU") == 0) |
| 161 | mode = "rb"; |
| 162 | #else |
| 163 | /* Compatibility: specifying U in a Python without universal |
| 164 | ** newlines is allowed, and the file is opened as a normal text |
| 165 | ** file. |
| 166 | */ |
| 167 | if (strcmp(mode, "U") == 0 || strcmp(mode, "rU") == 0) |
| 168 | mode = "r"; |
| 169 | #endif |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 170 | f->f_fp = fopen(name, mode); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 171 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 172 | } |
Guido van Rossum | a08095a | 1991-02-13 23:25:27 +0000 | [diff] [blame] | 173 | if (f->f_fp == NULL) { |
Jack Jansen | e08dea19 | 1995-04-23 22:12:47 +0000 | [diff] [blame] | 174 | #ifdef NO_FOPEN_ERRNO |
Jack Jansen | b3be216 | 2001-11-30 14:16:36 +0000 | [diff] [blame] | 175 | /* Metroworks only, wich does not always sets errno */ |
Jeremy Hylton | 41c8321 | 2001-11-09 16:17:24 +0000 | [diff] [blame] | 176 | if (errno == 0) { |
Jack Jansen | b3be216 | 2001-11-30 14:16:36 +0000 | [diff] [blame] | 177 | PyObject *v; |
| 178 | v = Py_BuildValue("(is)", 0, "Cannot open file"); |
| 179 | if (v != NULL) { |
| 180 | PyErr_SetObject(PyExc_IOError, v); |
| 181 | Py_DECREF(v); |
| 182 | } |
Jack Jansen | e08dea19 | 1995-04-23 22:12:47 +0000 | [diff] [blame] | 183 | return NULL; |
| 184 | } |
| 185 | #endif |
Tim Peters | 2ea9111 | 2002-04-08 04:13:12 +0000 | [diff] [blame] | 186 | #ifdef _MSC_VER |
| 187 | /* MSVC 6 (Microsoft) leaves errno at 0 for bad mode strings, |
| 188 | * across all Windows flavors. When it sets EINVAL varies |
| 189 | * across Windows flavors, the exact conditions aren't |
| 190 | * documented, and the answer lies in the OS's implementation |
| 191 | * of Win32's CreateFile function (whose source is secret). |
| 192 | * Seems the best we can do is map EINVAL to ENOENT. |
| 193 | */ |
| 194 | if (errno == 0) /* bad mode string */ |
| 195 | errno = EINVAL; |
| 196 | else if (errno == EINVAL) /* unknown, but not a mode string */ |
| 197 | errno = ENOENT; |
| 198 | #endif |
Jeremy Hylton | 41c8321 | 2001-11-09 16:17:24 +0000 | [diff] [blame] | 199 | if (errno == EINVAL) |
Tim Peters | 2ea9111 | 2002-04-08 04:13:12 +0000 | [diff] [blame] | 200 | PyErr_Format(PyExc_IOError, "invalid mode: %s", |
Jeremy Hylton | 41c8321 | 2001-11-09 16:17:24 +0000 | [diff] [blame] | 201 | mode); |
| 202 | else |
| 203 | PyErr_SetFromErrnoWithFilename(PyExc_IOError, name); |
Tim Peters | 59c9a64 | 2001-09-13 05:38:56 +0000 | [diff] [blame] | 204 | f = NULL; |
| 205 | } |
Tim Peters | 2ea9111 | 2002-04-08 04:13:12 +0000 | [diff] [blame] | 206 | if (f != NULL) |
Neil Schemenauer | ed19b88 | 2002-03-23 02:06:50 +0000 | [diff] [blame] | 207 | f = dircheck(f); |
Tim Peters | 59c9a64 | 2001-09-13 05:38:56 +0000 | [diff] [blame] | 208 | return (PyObject *)f; |
| 209 | } |
| 210 | |
| 211 | PyObject * |
| 212 | PyFile_FromFile(FILE *fp, char *name, char *mode, int (*close)(FILE *)) |
| 213 | { |
Tim Peters | 4441001 | 2001-09-14 03:26:08 +0000 | [diff] [blame] | 214 | PyFileObject *f = (PyFileObject *)PyFile_Type.tp_new(&PyFile_Type, |
| 215 | NULL, NULL); |
Tim Peters | 59c9a64 | 2001-09-13 05:38:56 +0000 | [diff] [blame] | 216 | if (f != NULL) { |
| 217 | if (fill_file_fields(f, fp, name, mode, close) == NULL) { |
| 218 | Py_DECREF(f); |
| 219 | f = NULL; |
| 220 | } |
| 221 | } |
| 222 | return (PyObject *) f; |
| 223 | } |
| 224 | |
| 225 | PyObject * |
| 226 | PyFile_FromString(char *name, char *mode) |
| 227 | { |
| 228 | extern int fclose(FILE *); |
| 229 | PyFileObject *f; |
| 230 | |
| 231 | f = (PyFileObject *)PyFile_FromFile((FILE *)NULL, name, mode, fclose); |
| 232 | if (f != NULL) { |
| 233 | if (open_the_file(f, name, mode) == NULL) { |
| 234 | Py_DECREF(f); |
| 235 | f = NULL; |
| 236 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 237 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 238 | return (PyObject *)f; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 239 | } |
| 240 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 241 | void |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 242 | PyFile_SetBufSize(PyObject *f, int bufsize) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 243 | { |
| 244 | if (bufsize >= 0) { |
| 245 | #ifdef HAVE_SETVBUF |
| 246 | int type; |
| 247 | switch (bufsize) { |
| 248 | case 0: |
| 249 | type = _IONBF; |
| 250 | break; |
| 251 | case 1: |
| 252 | type = _IOLBF; |
| 253 | bufsize = BUFSIZ; |
| 254 | break; |
| 255 | default: |
| 256 | type = _IOFBF; |
| 257 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 258 | setvbuf(((PyFileObject *)f)->f_fp, (char *)NULL, |
| 259 | type, bufsize); |
Guido van Rossum | f8b4de0 | 1998-03-06 15:32:40 +0000 | [diff] [blame] | 260 | #else /* !HAVE_SETVBUF */ |
| 261 | if (bufsize <= 1) |
| 262 | setbuf(((PyFileObject *)f)->f_fp, (char *)NULL); |
| 263 | #endif /* !HAVE_SETVBUF */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 264 | } |
| 265 | } |
| 266 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 267 | static PyObject * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 268 | err_closed(void) |
Guido van Rossum | d7297e6 | 1992-07-06 14:19:26 +0000 | [diff] [blame] | 269 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 270 | PyErr_SetString(PyExc_ValueError, "I/O operation on closed file"); |
Guido van Rossum | d7297e6 | 1992-07-06 14:19:26 +0000 | [diff] [blame] | 271 | return NULL; |
| 272 | } |
| 273 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 274 | /* Methods */ |
| 275 | |
| 276 | static void |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 277 | file_dealloc(PyFileObject *f) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 278 | { |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 279 | if (f->f_fp != NULL && f->f_close != NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 280 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | a1ab7fa | 1991-06-04 19:37:39 +0000 | [diff] [blame] | 281 | (*f->f_close)(f->f_fp); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 282 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 283 | } |
Tim Peters | 4441001 | 2001-09-14 03:26:08 +0000 | [diff] [blame] | 284 | Py_XDECREF(f->f_name); |
| 285 | Py_XDECREF(f->f_mode); |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 286 | f->ob_type->tp_free((PyObject *)f); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 287 | } |
| 288 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 289 | static PyObject * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 290 | file_repr(PyFileObject *f) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 291 | { |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 292 | return PyString_FromFormat("<%s file '%s', mode '%s' at %p>", |
| 293 | f->f_fp == NULL ? "closed" : "open", |
| 294 | PyString_AsString(f->f_name), |
| 295 | PyString_AsString(f->f_mode), |
| 296 | f); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 297 | } |
| 298 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 299 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 300 | file_close(PyFileObject *f) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 301 | { |
Guido van Rossum | a1ab7fa | 1991-06-04 19:37:39 +0000 | [diff] [blame] | 302 | int sts = 0; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 303 | if (f->f_fp != NULL) { |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 304 | if (f->f_close != NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 305 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 306 | errno = 0; |
Guido van Rossum | a1ab7fa | 1991-06-04 19:37:39 +0000 | [diff] [blame] | 307 | sts = (*f->f_close)(f->f_fp); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 308 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 309 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 310 | f->f_fp = NULL; |
| 311 | } |
Guido van Rossum | febd551 | 1992-03-04 16:39:24 +0000 | [diff] [blame] | 312 | if (sts == EOF) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 313 | return PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | a1ab7fa | 1991-06-04 19:37:39 +0000 | [diff] [blame] | 314 | if (sts != 0) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 315 | return PyInt_FromLong((long)sts); |
| 316 | Py_INCREF(Py_None); |
| 317 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 318 | } |
| 319 | |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 320 | |
Guido van Rossum | b855216 | 2001-09-05 14:58:11 +0000 | [diff] [blame] | 321 | /* Our very own off_t-like type, 64-bit if possible */ |
| 322 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 323 | typedef off_t Py_off_t; |
| 324 | #elif SIZEOF_OFF_T >= 8 |
| 325 | typedef off_t Py_off_t; |
| 326 | #elif SIZEOF_FPOS_T >= 8 |
Guido van Rossum | 4f53da0 | 2001-03-01 18:26:53 +0000 | [diff] [blame] | 327 | typedef fpos_t Py_off_t; |
| 328 | #else |
Guido van Rossum | b855216 | 2001-09-05 14:58:11 +0000 | [diff] [blame] | 329 | #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] | 330 | #endif |
| 331 | |
| 332 | |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 333 | /* a portable fseek() function |
| 334 | return 0 on success, non-zero on failure (with errno set) */ |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 335 | static int |
Guido van Rossum | 4f53da0 | 2001-03-01 18:26:53 +0000 | [diff] [blame] | 336 | _portable_fseek(FILE *fp, Py_off_t offset, int whence) |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 337 | { |
Guido van Rossum | b855216 | 2001-09-05 14:58:11 +0000 | [diff] [blame] | 338 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 339 | return fseek(fp, offset, whence); |
| 340 | #elif defined(HAVE_FSEEKO) && SIZEOF_OFF_T >= 8 |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 341 | return fseeko(fp, offset, whence); |
| 342 | #elif defined(HAVE_FSEEK64) |
| 343 | return fseek64(fp, offset, whence); |
Fred Drake | db810ac | 2000-10-06 20:42:33 +0000 | [diff] [blame] | 344 | #elif defined(__BEOS__) |
| 345 | return _fseek(fp, offset, whence); |
Guido van Rossum | b855216 | 2001-09-05 14:58:11 +0000 | [diff] [blame] | 346 | #elif SIZEOF_FPOS_T >= 8 |
Guido van Rossum | e54e0be | 2001-01-16 20:53:31 +0000 | [diff] [blame] | 347 | /* lacking a 64-bit capable fseek(), use a 64-bit capable fsetpos() |
| 348 | and fgetpos() to implement fseek()*/ |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 349 | fpos_t pos; |
| 350 | switch (whence) { |
Guido van Rossum | e54e0be | 2001-01-16 20:53:31 +0000 | [diff] [blame] | 351 | case SEEK_END: |
Guido van Rossum | 8b4e43e | 2001-09-10 20:43:35 +0000 | [diff] [blame] | 352 | #ifdef MS_WINDOWS |
| 353 | fflush(fp); |
| 354 | if (_lseeki64(fileno(fp), 0, 2) == -1) |
| 355 | return -1; |
| 356 | #else |
Guido van Rossum | e54e0be | 2001-01-16 20:53:31 +0000 | [diff] [blame] | 357 | if (fseek(fp, 0, SEEK_END) != 0) |
| 358 | return -1; |
Guido van Rossum | 8b4e43e | 2001-09-10 20:43:35 +0000 | [diff] [blame] | 359 | #endif |
Guido van Rossum | e54e0be | 2001-01-16 20:53:31 +0000 | [diff] [blame] | 360 | /* fall through */ |
| 361 | case SEEK_CUR: |
| 362 | if (fgetpos(fp, &pos) != 0) |
| 363 | return -1; |
| 364 | offset += pos; |
| 365 | break; |
| 366 | /* case SEEK_SET: break; */ |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 367 | } |
| 368 | return fsetpos(fp, &offset); |
| 369 | #else |
Guido van Rossum | b855216 | 2001-09-05 14:58:11 +0000 | [diff] [blame] | 370 | #error "Large file support, but no way to fseek." |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 371 | #endif |
| 372 | } |
| 373 | |
| 374 | |
| 375 | /* a portable ftell() function |
| 376 | Return -1 on failure with errno set appropriately, current file |
| 377 | position on success */ |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 378 | static Py_off_t |
Fred Drake | 8ce159a | 2000-08-31 05:18:54 +0000 | [diff] [blame] | 379 | _portable_ftell(FILE* fp) |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 380 | { |
Guido van Rossum | b855216 | 2001-09-05 14:58:11 +0000 | [diff] [blame] | 381 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 382 | return ftell(fp); |
| 383 | #elif defined(HAVE_FTELLO) && SIZEOF_OFF_T >= 8 |
| 384 | return ftello(fp); |
| 385 | #elif defined(HAVE_FTELL64) |
| 386 | return ftell64(fp); |
| 387 | #elif SIZEOF_FPOS_T >= 8 |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 388 | fpos_t pos; |
| 389 | if (fgetpos(fp, &pos) != 0) |
| 390 | return -1; |
| 391 | return pos; |
| 392 | #else |
Guido van Rossum | b855216 | 2001-09-05 14:58:11 +0000 | [diff] [blame] | 393 | #error "Large file support, but no way to ftell." |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 394 | #endif |
| 395 | } |
| 396 | |
| 397 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 398 | static PyObject * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 399 | file_seek(PyFileObject *f, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 400 | { |
Guido van Rossum | d7297e6 | 1992-07-06 14:19:26 +0000 | [diff] [blame] | 401 | int whence; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 402 | int ret; |
Guido van Rossum | 4f53da0 | 2001-03-01 18:26:53 +0000 | [diff] [blame] | 403 | Py_off_t offset; |
Guido van Rossum | 3c9fe0c | 1999-01-06 18:51:17 +0000 | [diff] [blame] | 404 | PyObject *offobj; |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 405 | |
Guido van Rossum | d7297e6 | 1992-07-06 14:19:26 +0000 | [diff] [blame] | 406 | if (f->f_fp == NULL) |
| 407 | return err_closed(); |
| 408 | whence = 0; |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 409 | if (!PyArg_ParseTuple(args, "O|i:seek", &offobj, &whence)) |
Guido van Rossum | 3c9fe0c | 1999-01-06 18:51:17 +0000 | [diff] [blame] | 410 | return NULL; |
| 411 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 412 | offset = PyInt_AsLong(offobj); |
| 413 | #else |
| 414 | offset = PyLong_Check(offobj) ? |
| 415 | PyLong_AsLongLong(offobj) : PyInt_AsLong(offobj); |
| 416 | #endif |
| 417 | if (PyErr_Occurred()) |
Guido van Rossum | 8830319 | 1999-01-04 17:22:18 +0000 | [diff] [blame] | 418 | return NULL; |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 419 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 420 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 421 | errno = 0; |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 422 | ret = _portable_fseek(f->f_fp, offset, whence); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 423 | Py_END_ALLOW_THREADS |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 424 | |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 425 | if (ret != 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 426 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | febd551 | 1992-03-04 16:39:24 +0000 | [diff] [blame] | 427 | clearerr(f->f_fp); |
| 428 | return NULL; |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 429 | } |
Jack Jansen | 7b8c754 | 2002-04-14 20:12:41 +0000 | [diff] [blame] | 430 | #ifdef WITH_UNIVERSAL_NEWLINES |
| 431 | f->f_skipnextlf = 0; |
| 432 | #endif |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 433 | Py_INCREF(Py_None); |
| 434 | return Py_None; |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 435 | } |
| 436 | |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 437 | |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 438 | #ifdef HAVE_FTRUNCATE |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 439 | static PyObject * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 440 | file_truncate(PyFileObject *f, PyObject *args) |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 441 | { |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 442 | int ret; |
Guido van Rossum | 4f53da0 | 2001-03-01 18:26:53 +0000 | [diff] [blame] | 443 | Py_off_t newsize; |
Guido van Rossum | 3c9fe0c | 1999-01-06 18:51:17 +0000 | [diff] [blame] | 444 | PyObject *newsizeobj; |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 445 | |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 446 | if (f->f_fp == NULL) |
| 447 | return err_closed(); |
Guido van Rossum | 3c9fe0c | 1999-01-06 18:51:17 +0000 | [diff] [blame] | 448 | newsizeobj = NULL; |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 449 | if (!PyArg_ParseTuple(args, "|O:truncate", &newsizeobj)) |
Guido van Rossum | 8830319 | 1999-01-04 17:22:18 +0000 | [diff] [blame] | 450 | return NULL; |
Tim Peters | fb05db2 | 2002-03-11 00:24:00 +0000 | [diff] [blame] | 451 | |
| 452 | /* Set newsize to current postion if newsizeobj NULL, else to the |
| 453 | specified value. */ |
Guido van Rossum | 3c9fe0c | 1999-01-06 18:51:17 +0000 | [diff] [blame] | 454 | if (newsizeobj != NULL) { |
| 455 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
| 456 | newsize = PyInt_AsLong(newsizeobj); |
| 457 | #else |
| 458 | newsize = PyLong_Check(newsizeobj) ? |
| 459 | PyLong_AsLongLong(newsizeobj) : |
| 460 | PyInt_AsLong(newsizeobj); |
| 461 | #endif |
| 462 | if (PyErr_Occurred()) |
| 463 | return NULL; |
Tim Peters | fb05db2 | 2002-03-11 00:24:00 +0000 | [diff] [blame] | 464 | } |
| 465 | else { |
| 466 | /* Default to current position. */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 467 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 468 | errno = 0; |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 469 | newsize = _portable_ftell(f->f_fp); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 470 | Py_END_ALLOW_THREADS |
Tim Peters | fb05db2 | 2002-03-11 00:24:00 +0000 | [diff] [blame] | 471 | if (newsize == -1) |
| 472 | goto onioerror; |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 473 | } |
Tim Peters | fb05db2 | 2002-03-11 00:24:00 +0000 | [diff] [blame] | 474 | |
| 475 | /* Flush the file. */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 476 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 477 | errno = 0; |
| 478 | ret = fflush(f->f_fp); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 479 | Py_END_ALLOW_THREADS |
Tim Peters | fb05db2 | 2002-03-11 00:24:00 +0000 | [diff] [blame] | 480 | if (ret != 0) |
| 481 | goto onioerror; |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 482 | |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 483 | #ifdef MS_WINDOWS |
Tim Peters | fb05db2 | 2002-03-11 00:24:00 +0000 | [diff] [blame] | 484 | /* 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] | 485 | so don't even try using it. */ |
Tim Peters | fb05db2 | 2002-03-11 00:24:00 +0000 | [diff] [blame] | 486 | { |
Tim Peters | 8f01b68 | 2002-03-12 03:04:44 +0000 | [diff] [blame] | 487 | Py_off_t current; /* current file position */ |
Tim Peters | fb05db2 | 2002-03-11 00:24:00 +0000 | [diff] [blame] | 488 | HANDLE hFile; |
| 489 | int error; |
| 490 | |
Tim Peters | 8f01b68 | 2002-03-12 03:04:44 +0000 | [diff] [blame] | 491 | /* current <- current file postion. */ |
| 492 | if (newsizeobj == NULL) |
| 493 | current = newsize; |
| 494 | else { |
Tim Peters | fb05db2 | 2002-03-11 00:24:00 +0000 | [diff] [blame] | 495 | Py_BEGIN_ALLOW_THREADS |
| 496 | errno = 0; |
Tim Peters | 8f01b68 | 2002-03-12 03:04:44 +0000 | [diff] [blame] | 497 | current = _portable_ftell(f->f_fp); |
| 498 | Py_END_ALLOW_THREADS |
| 499 | if (current == -1) |
| 500 | goto onioerror; |
| 501 | } |
| 502 | |
| 503 | /* Move to newsize. */ |
| 504 | if (current != newsize) { |
| 505 | Py_BEGIN_ALLOW_THREADS |
| 506 | errno = 0; |
| 507 | error = _portable_fseek(f->f_fp, newsize, SEEK_SET) |
| 508 | != 0; |
Tim Peters | fb05db2 | 2002-03-11 00:24:00 +0000 | [diff] [blame] | 509 | Py_END_ALLOW_THREADS |
| 510 | if (error) |
| 511 | goto onioerror; |
| 512 | } |
| 513 | |
Tim Peters | 8f01b68 | 2002-03-12 03:04:44 +0000 | [diff] [blame] | 514 | /* Truncate. Note that this may grow the file! */ |
| 515 | Py_BEGIN_ALLOW_THREADS |
| 516 | errno = 0; |
| 517 | hFile = (HANDLE)_get_osfhandle(fileno(f->f_fp)); |
| 518 | error = hFile == (HANDLE)-1; |
| 519 | if (!error) { |
| 520 | error = SetEndOfFile(hFile) == 0; |
| 521 | if (error) |
| 522 | errno = EACCES; |
| 523 | } |
| 524 | Py_END_ALLOW_THREADS |
| 525 | if (error) |
| 526 | goto onioerror; |
| 527 | |
| 528 | /* Restore original file position. */ |
| 529 | if (current != newsize) { |
| 530 | Py_BEGIN_ALLOW_THREADS |
| 531 | errno = 0; |
| 532 | error = _portable_fseek(f->f_fp, current, SEEK_SET) |
| 533 | != 0; |
| 534 | Py_END_ALLOW_THREADS |
| 535 | if (error) |
| 536 | goto onioerror; |
| 537 | } |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 538 | } |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 539 | #else |
| 540 | Py_BEGIN_ALLOW_THREADS |
| 541 | errno = 0; |
| 542 | ret = ftruncate(fileno(f->f_fp), newsize); |
| 543 | Py_END_ALLOW_THREADS |
| 544 | if (ret != 0) goto onioerror; |
Martin v. Löwis | 6238d2b | 2002-06-30 15:26:10 +0000 | [diff] [blame] | 545 | #endif /* !MS_WINDOWS */ |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 546 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 547 | Py_INCREF(Py_None); |
| 548 | return Py_None; |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 549 | |
| 550 | onioerror: |
| 551 | PyErr_SetFromErrno(PyExc_IOError); |
| 552 | clearerr(f->f_fp); |
| 553 | return NULL; |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 554 | } |
| 555 | #endif /* HAVE_FTRUNCATE */ |
| 556 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 557 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 558 | file_tell(PyFileObject *f) |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 559 | { |
Guido van Rossum | 4f53da0 | 2001-03-01 18:26:53 +0000 | [diff] [blame] | 560 | Py_off_t pos; |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 561 | |
Guido van Rossum | d7297e6 | 1992-07-06 14:19:26 +0000 | [diff] [blame] | 562 | if (f->f_fp == NULL) |
| 563 | return err_closed(); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 564 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 565 | errno = 0; |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 566 | pos = _portable_ftell(f->f_fp); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 567 | Py_END_ALLOW_THREADS |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 568 | if (pos == -1) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 569 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | febd551 | 1992-03-04 16:39:24 +0000 | [diff] [blame] | 570 | clearerr(f->f_fp); |
| 571 | return NULL; |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 572 | } |
Jack Jansen | 7b8c754 | 2002-04-14 20:12:41 +0000 | [diff] [blame] | 573 | #ifdef WITH_UNIVERSAL_NEWLINES |
| 574 | if (f->f_skipnextlf) { |
| 575 | int c; |
| 576 | c = GETC(f->f_fp); |
| 577 | if (c == '\n') { |
| 578 | pos++; |
| 579 | f->f_skipnextlf = 0; |
| 580 | } else if (c != EOF) ungetc(c, f->f_fp); |
| 581 | } |
| 582 | #endif |
Guido van Rossum | 3c9fe0c | 1999-01-06 18:51:17 +0000 | [diff] [blame] | 583 | #if !defined(HAVE_LARGEFILE_SUPPORT) |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 584 | return PyInt_FromLong(pos); |
Guido van Rossum | 3c9fe0c | 1999-01-06 18:51:17 +0000 | [diff] [blame] | 585 | #else |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 586 | return PyLong_FromLongLong(pos); |
Guido van Rossum | 3c9fe0c | 1999-01-06 18:51:17 +0000 | [diff] [blame] | 587 | #endif |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 588 | } |
| 589 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 590 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 591 | file_fileno(PyFileObject *f) |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 592 | { |
Guido van Rossum | d7297e6 | 1992-07-06 14:19:26 +0000 | [diff] [blame] | 593 | if (f->f_fp == NULL) |
| 594 | return err_closed(); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 595 | return PyInt_FromLong((long) fileno(f->f_fp)); |
Guido van Rossum | ed233a5 | 1992-06-23 09:07:03 +0000 | [diff] [blame] | 596 | } |
| 597 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 598 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 599 | file_flush(PyFileObject *f) |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 600 | { |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 601 | int res; |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 602 | |
Guido van Rossum | d7297e6 | 1992-07-06 14:19:26 +0000 | [diff] [blame] | 603 | if (f->f_fp == NULL) |
| 604 | return err_closed(); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 605 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 606 | errno = 0; |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 607 | res = fflush(f->f_fp); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 608 | Py_END_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 609 | if (res != 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 610 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | febd551 | 1992-03-04 16:39:24 +0000 | [diff] [blame] | 611 | clearerr(f->f_fp); |
| 612 | return NULL; |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 613 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 614 | Py_INCREF(Py_None); |
| 615 | return Py_None; |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 616 | } |
| 617 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 618 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 619 | file_isatty(PyFileObject *f) |
Guido van Rossum | a1ab7fa | 1991-06-04 19:37:39 +0000 | [diff] [blame] | 620 | { |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 621 | long res; |
Guido van Rossum | d7297e6 | 1992-07-06 14:19:26 +0000 | [diff] [blame] | 622 | if (f->f_fp == NULL) |
| 623 | return err_closed(); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 624 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 625 | res = isatty((int)fileno(f->f_fp)); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 626 | Py_END_ALLOW_THREADS |
Guido van Rossum | 7f7666f | 2002-04-07 06:28:00 +0000 | [diff] [blame] | 627 | return PyBool_FromLong(res); |
Guido van Rossum | a1ab7fa | 1991-06-04 19:37:39 +0000 | [diff] [blame] | 628 | } |
| 629 | |
Guido van Rossum | ff7e83d | 1999-08-27 20:39:37 +0000 | [diff] [blame] | 630 | |
Guido van Rossum | 5449b6e | 1997-05-09 22:27:31 +0000 | [diff] [blame] | 631 | #if BUFSIZ < 8192 |
| 632 | #define SMALLCHUNK 8192 |
| 633 | #else |
| 634 | #define SMALLCHUNK BUFSIZ |
| 635 | #endif |
| 636 | |
Guido van Rossum | 3c25904 | 1999-01-14 19:00:14 +0000 | [diff] [blame] | 637 | #if SIZEOF_INT < 4 |
| 638 | #define BIGCHUNK (512 * 32) |
| 639 | #else |
| 640 | #define BIGCHUNK (512 * 1024) |
| 641 | #endif |
Guido van Rossum | 5449b6e | 1997-05-09 22:27:31 +0000 | [diff] [blame] | 642 | |
| 643 | static size_t |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 644 | new_buffersize(PyFileObject *f, size_t currentsize) |
Guido van Rossum | 5449b6e | 1997-05-09 22:27:31 +0000 | [diff] [blame] | 645 | { |
| 646 | #ifdef HAVE_FSTAT |
Fred Drake | 1bc8fab | 2001-07-19 21:49:38 +0000 | [diff] [blame] | 647 | off_t pos, end; |
Guido van Rossum | 5449b6e | 1997-05-09 22:27:31 +0000 | [diff] [blame] | 648 | struct stat st; |
| 649 | if (fstat(fileno(f->f_fp), &st) == 0) { |
| 650 | end = st.st_size; |
Guido van Rossum | cada293 | 1998-12-11 20:44:56 +0000 | [diff] [blame] | 651 | /* The following is not a bug: we really need to call lseek() |
| 652 | *and* ftell(). The reason is that some stdio libraries |
| 653 | mistakenly flush their buffer when ftell() is called and |
| 654 | the lseek() call it makes fails, thereby throwing away |
| 655 | data that cannot be recovered in any way. To avoid this, |
| 656 | we first test lseek(), and only call ftell() if lseek() |
| 657 | works. We can't use the lseek() value either, because we |
| 658 | need to take the amount of buffered data into account. |
| 659 | (Yet another reason why stdio stinks. :-) */ |
Jack Jansen | 2771b5b | 2001-10-10 22:03:27 +0000 | [diff] [blame] | 660 | #ifdef USE_GUSI2 |
| 661 | pos = lseek(fileno(f->f_fp), 1L, SEEK_CUR); |
| 662 | pos = lseek(fileno(f->f_fp), -1L, SEEK_CUR); |
| 663 | #else |
Guido van Rossum | 91aaa92 | 1998-05-05 22:21:35 +0000 | [diff] [blame] | 664 | pos = lseek(fileno(f->f_fp), 0L, SEEK_CUR); |
Jack Jansen | 2771b5b | 2001-10-10 22:03:27 +0000 | [diff] [blame] | 665 | #endif |
| 666 | if (pos >= 0) { |
Guido van Rossum | 91aaa92 | 1998-05-05 22:21:35 +0000 | [diff] [blame] | 667 | pos = ftell(f->f_fp); |
Jack Jansen | 2771b5b | 2001-10-10 22:03:27 +0000 | [diff] [blame] | 668 | } |
Guido van Rossum | d30dc0a | 1998-04-27 19:01:08 +0000 | [diff] [blame] | 669 | if (pos < 0) |
| 670 | clearerr(f->f_fp); |
Guido van Rossum | 5449b6e | 1997-05-09 22:27:31 +0000 | [diff] [blame] | 671 | if (end > pos && pos >= 0) |
Guido van Rossum | cada293 | 1998-12-11 20:44:56 +0000 | [diff] [blame] | 672 | return currentsize + end - pos + 1; |
Guido van Rossum | dcb5e7f | 1998-03-03 22:36:10 +0000 | [diff] [blame] | 673 | /* 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] | 674 | } |
| 675 | #endif |
| 676 | if (currentsize > SMALLCHUNK) { |
| 677 | /* Keep doubling until we reach BIGCHUNK; |
| 678 | then keep adding BIGCHUNK. */ |
| 679 | if (currentsize <= BIGCHUNK) |
| 680 | return currentsize + currentsize; |
| 681 | else |
| 682 | return currentsize + BIGCHUNK; |
| 683 | } |
| 684 | return currentsize + SMALLCHUNK; |
| 685 | } |
| 686 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 687 | static PyObject * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 688 | file_read(PyFileObject *f, PyObject *args) |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 689 | { |
Guido van Rossum | 789a161 | 1997-05-10 22:33:55 +0000 | [diff] [blame] | 690 | long bytesrequested = -1; |
Guido van Rossum | 5449b6e | 1997-05-09 22:27:31 +0000 | [diff] [blame] | 691 | size_t bytesread, buffersize, chunksize; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 692 | PyObject *v; |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 693 | |
Guido van Rossum | d7297e6 | 1992-07-06 14:19:26 +0000 | [diff] [blame] | 694 | if (f->f_fp == NULL) |
| 695 | return err_closed(); |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 696 | if (!PyArg_ParseTuple(args, "|l:read", &bytesrequested)) |
Guido van Rossum | 789a161 | 1997-05-10 22:33:55 +0000 | [diff] [blame] | 697 | return NULL; |
Guido van Rossum | 5449b6e | 1997-05-09 22:27:31 +0000 | [diff] [blame] | 698 | if (bytesrequested < 0) |
Guido van Rossum | ff1ccbf | 1999-04-10 15:48:23 +0000 | [diff] [blame] | 699 | buffersize = new_buffersize(f, (size_t)0); |
Guido van Rossum | 5449b6e | 1997-05-09 22:27:31 +0000 | [diff] [blame] | 700 | else |
| 701 | buffersize = bytesrequested; |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 702 | if (buffersize > INT_MAX) { |
| 703 | PyErr_SetString(PyExc_OverflowError, |
| 704 | "requested number of bytes is more than a Python string can hold"); |
| 705 | return NULL; |
| 706 | } |
Guido van Rossum | 5449b6e | 1997-05-09 22:27:31 +0000 | [diff] [blame] | 707 | v = PyString_FromStringAndSize((char *)NULL, buffersize); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 708 | if (v == NULL) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 709 | return NULL; |
Guido van Rossum | 5449b6e | 1997-05-09 22:27:31 +0000 | [diff] [blame] | 710 | bytesread = 0; |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 711 | for (;;) { |
Guido van Rossum | 6263d54 | 1997-05-10 22:07:25 +0000 | [diff] [blame] | 712 | Py_BEGIN_ALLOW_THREADS |
| 713 | errno = 0; |
Jack Jansen | 7b8c754 | 2002-04-14 20:12:41 +0000 | [diff] [blame] | 714 | chunksize = Py_UniversalNewlineFread(BUF(v) + bytesread, |
| 715 | buffersize - bytesread, f->f_fp, (PyObject *)f); |
Guido van Rossum | 6263d54 | 1997-05-10 22:07:25 +0000 | [diff] [blame] | 716 | Py_END_ALLOW_THREADS |
| 717 | if (chunksize == 0) { |
| 718 | if (!ferror(f->f_fp)) |
| 719 | break; |
| 720 | PyErr_SetFromErrno(PyExc_IOError); |
| 721 | clearerr(f->f_fp); |
| 722 | Py_DECREF(v); |
| 723 | return NULL; |
| 724 | } |
Guido van Rossum | 5449b6e | 1997-05-09 22:27:31 +0000 | [diff] [blame] | 725 | bytesread += chunksize; |
| 726 | if (bytesread < buffersize) |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 727 | break; |
Guido van Rossum | 5449b6e | 1997-05-09 22:27:31 +0000 | [diff] [blame] | 728 | if (bytesrequested < 0) { |
Guido van Rossum | cada293 | 1998-12-11 20:44:56 +0000 | [diff] [blame] | 729 | buffersize = new_buffersize(f, buffersize); |
Guido van Rossum | 5449b6e | 1997-05-09 22:27:31 +0000 | [diff] [blame] | 730 | if (_PyString_Resize(&v, buffersize) < 0) |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 731 | return NULL; |
| 732 | } |
| 733 | } |
Guido van Rossum | 5449b6e | 1997-05-09 22:27:31 +0000 | [diff] [blame] | 734 | if (bytesread != buffersize) |
| 735 | _PyString_Resize(&v, bytesread); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 736 | return v; |
| 737 | } |
| 738 | |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 739 | static PyObject * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 740 | file_readinto(PyFileObject *f, PyObject *args) |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 741 | { |
| 742 | char *ptr; |
Guido van Rossum | 00ebd46 | 2001-10-23 21:25:24 +0000 | [diff] [blame] | 743 | int ntodo; |
| 744 | size_t ndone, nnow; |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 745 | |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 746 | if (f->f_fp == NULL) |
| 747 | return err_closed(); |
Neal Norwitz | 62f5a9d | 2002-04-01 00:09:00 +0000 | [diff] [blame] | 748 | if (!PyArg_ParseTuple(args, "w#", &ptr, &ntodo)) |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 749 | return NULL; |
| 750 | ndone = 0; |
Guido van Rossum | 6263d54 | 1997-05-10 22:07:25 +0000 | [diff] [blame] | 751 | while (ntodo > 0) { |
| 752 | Py_BEGIN_ALLOW_THREADS |
| 753 | errno = 0; |
Jack Jansen | 7b8c754 | 2002-04-14 20:12:41 +0000 | [diff] [blame] | 754 | nnow = Py_UniversalNewlineFread(ptr+ndone, ntodo, f->f_fp, (PyObject *)f); |
Guido van Rossum | 6263d54 | 1997-05-10 22:07:25 +0000 | [diff] [blame] | 755 | Py_END_ALLOW_THREADS |
| 756 | if (nnow == 0) { |
| 757 | if (!ferror(f->f_fp)) |
| 758 | break; |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 759 | PyErr_SetFromErrno(PyExc_IOError); |
| 760 | clearerr(f->f_fp); |
| 761 | return NULL; |
| 762 | } |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 763 | ndone += nnow; |
| 764 | ntodo -= nnow; |
| 765 | } |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 766 | return PyInt_FromLong((long)ndone); |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 767 | } |
| 768 | |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 769 | /************************************************************************** |
Tim Peters | f29b64d | 2001-01-15 06:33:19 +0000 | [diff] [blame] | 770 | Routine to get next line using platform fgets(). |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 771 | |
| 772 | Under MSVC 6: |
| 773 | |
Tim Peters | 1c73323 | 2001-01-08 04:02:07 +0000 | [diff] [blame] | 774 | + MS threadsafe getc is very slow (multiple layers of function calls before+ |
| 775 | after each character, to lock+unlock the stream). |
| 776 | + The stream-locking functions are MS-internal -- can't access them from user |
| 777 | code. |
| 778 | + There's nothing Tim could find in the MS C or platform SDK libraries that |
| 779 | can worm around this. |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 780 | + MS fgets locks/unlocks only once per line; it's the only hook we have. |
| 781 | |
| 782 | So we use fgets for speed(!), despite that it's painful. |
| 783 | |
| 784 | MS realloc is also slow. |
| 785 | |
Tim Peters | f29b64d | 2001-01-15 06:33:19 +0000 | [diff] [blame] | 786 | Reports from other platforms on this method vs getc_unlocked (which MS doesn't |
| 787 | have): |
| 788 | Linux a wash |
| 789 | Solaris a wash |
| 790 | Tru64 Unix getline_via_fgets significantly faster |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 791 | |
Tim Peters | f29b64d | 2001-01-15 06:33:19 +0000 | [diff] [blame] | 792 | CAUTION: The C std isn't clear about this: in those cases where fgets |
| 793 | writes something into the buffer, can it write into any position beyond the |
| 794 | required trailing null byte? MSVC 6 fgets does not, and no platform is (yet) |
| 795 | known on which it does; and it would be a strange way to code fgets. Still, |
| 796 | getline_via_fgets may not work correctly if it does. The std test |
| 797 | test_bufio.py should fail if platform fgets() routinely writes beyond the |
| 798 | 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] | 799 | **************************************************************************/ |
| 800 | |
Tim Peters | f29b64d | 2001-01-15 06:33:19 +0000 | [diff] [blame] | 801 | /* Use this routine if told to, or by default on non-get_unlocked() |
| 802 | * platforms unless told not to. Yikes! Let's spell that out: |
| 803 | * On a platform with getc_unlocked(): |
| 804 | * By default, use getc_unlocked(). |
| 805 | * If you want to use fgets() instead, #define USE_FGETS_IN_GETLINE. |
| 806 | * On a platform without getc_unlocked(): |
| 807 | * By default, use fgets(). |
| 808 | * If you don't want to use fgets(), #define DONT_USE_FGETS_IN_GETLINE. |
| 809 | */ |
| 810 | #if !defined(USE_FGETS_IN_GETLINE) && !defined(HAVE_GETC_UNLOCKED) |
| 811 | #define USE_FGETS_IN_GETLINE |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 812 | #endif |
| 813 | |
Tim Peters | f29b64d | 2001-01-15 06:33:19 +0000 | [diff] [blame] | 814 | #if defined(DONT_USE_FGETS_IN_GETLINE) && defined(USE_FGETS_IN_GETLINE) |
| 815 | #undef USE_FGETS_IN_GETLINE |
| 816 | #endif |
| 817 | |
| 818 | #ifdef USE_FGETS_IN_GETLINE |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 819 | static PyObject* |
Tim Peters | f29b64d | 2001-01-15 06:33:19 +0000 | [diff] [blame] | 820 | getline_via_fgets(FILE *fp) |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 821 | { |
Tim Peters | 15b8385 | 2001-01-08 00:53:12 +0000 | [diff] [blame] | 822 | /* 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] | 823 | * no-realloc, one-fgets()-call path. Boosting it isn't free, because we have |
| 824 | * to fill this much of the buffer with a known value in order to figure out |
| 825 | * how much of the buffer fgets() overwrites. So if INITBUFSIZE is larger |
| 826 | * than "most" lines, we waste time filling unused buffer slots. 100 is |
| 827 | * surely adequate for most peoples' email archives, chewing over source code, |
| 828 | * etc -- "regular old text files". |
| 829 | * MAXBUFSIZE is the maximum line length that lets us get away with the less |
| 830 | * fast (but still zippy) no-realloc, two-fgets()-call path. See above for |
| 831 | * cautions about boosting that. 300 was chosen because the worst real-life |
| 832 | * text-crunching job reported on Python-Dev was a mail-log crawler where over |
| 833 | * half the lines were 254 chars. |
Tim Peters | 15b8385 | 2001-01-08 00:53:12 +0000 | [diff] [blame] | 834 | */ |
Tim Peters | 142297a | 2001-01-15 10:36:56 +0000 | [diff] [blame] | 835 | #define INITBUFSIZE 100 |
| 836 | #define MAXBUFSIZE 300 |
Tim Peters | 142297a | 2001-01-15 10:36:56 +0000 | [diff] [blame] | 837 | char* p; /* temp */ |
| 838 | char buf[MAXBUFSIZE]; |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 839 | PyObject* v; /* the string object result */ |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 840 | char* pvfree; /* address of next free slot */ |
| 841 | char* pvend; /* address one beyond last free slot */ |
Tim Peters | 142297a | 2001-01-15 10:36:56 +0000 | [diff] [blame] | 842 | size_t nfree; /* # of free buffer slots; pvend-pvfree */ |
| 843 | size_t total_v_size; /* total # of slots in buffer */ |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 844 | size_t increment; /* amount to increment the buffer */ |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 845 | |
Tim Peters | 15b8385 | 2001-01-08 00:53:12 +0000 | [diff] [blame] | 846 | /* Optimize for normal case: avoid _PyString_Resize if at all |
Tim Peters | 142297a | 2001-01-15 10:36:56 +0000 | [diff] [blame] | 847 | * possible via first reading into stack buffer "buf". |
Tim Peters | 15b8385 | 2001-01-08 00:53:12 +0000 | [diff] [blame] | 848 | */ |
Tim Peters | 142297a | 2001-01-15 10:36:56 +0000 | [diff] [blame] | 849 | total_v_size = INITBUFSIZE; /* start small and pray */ |
| 850 | pvfree = buf; |
| 851 | for (;;) { |
| 852 | Py_BEGIN_ALLOW_THREADS |
| 853 | pvend = buf + total_v_size; |
| 854 | nfree = pvend - pvfree; |
| 855 | memset(pvfree, '\n', nfree); |
| 856 | p = fgets(pvfree, nfree, fp); |
| 857 | Py_END_ALLOW_THREADS |
Tim Peters | 15b8385 | 2001-01-08 00:53:12 +0000 | [diff] [blame] | 858 | |
Tim Peters | 142297a | 2001-01-15 10:36:56 +0000 | [diff] [blame] | 859 | if (p == NULL) { |
| 860 | clearerr(fp); |
| 861 | if (PyErr_CheckSignals()) |
| 862 | return NULL; |
| 863 | v = PyString_FromStringAndSize(buf, pvfree - buf); |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 864 | return v; |
| 865 | } |
Tim Peters | 142297a | 2001-01-15 10:36:56 +0000 | [diff] [blame] | 866 | /* fgets read *something* */ |
| 867 | p = memchr(pvfree, '\n', nfree); |
| 868 | if (p != NULL) { |
| 869 | /* Did the \n come from fgets or from us? |
| 870 | * Since fgets stops at the first \n, and then writes |
| 871 | * \0, if it's from fgets a \0 must be next. But if |
| 872 | * that's so, it could not have come from us, since |
| 873 | * the \n's we filled the buffer with have only more |
| 874 | * \n's to the right. |
| 875 | */ |
| 876 | if (p+1 < pvend && *(p+1) == '\0') { |
| 877 | /* It's from fgets: we win! In particular, |
| 878 | * we haven't done any mallocs yet, and can |
| 879 | * build the final result on the first try. |
| 880 | */ |
| 881 | ++p; /* include \n from fgets */ |
| 882 | } |
| 883 | else { |
| 884 | /* Must be from us: fgets didn't fill the |
| 885 | * buffer and didn't find a newline, so it |
| 886 | * must be the last and newline-free line of |
| 887 | * the file. |
| 888 | */ |
| 889 | assert(p > pvfree && *(p-1) == '\0'); |
| 890 | --p; /* don't include \0 from fgets */ |
| 891 | } |
| 892 | v = PyString_FromStringAndSize(buf, p - buf); |
| 893 | return v; |
| 894 | } |
| 895 | /* yuck: fgets overwrote all the newlines, i.e. the entire |
| 896 | * buffer. So this line isn't over yet, or maybe it is but |
| 897 | * we're exactly at EOF. If we haven't already, try using the |
| 898 | * rest of the stack buffer. |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 899 | */ |
Tim Peters | 142297a | 2001-01-15 10:36:56 +0000 | [diff] [blame] | 900 | assert(*(pvend-1) == '\0'); |
| 901 | if (pvfree == buf) { |
| 902 | pvfree = pvend - 1; /* overwrite trailing null */ |
| 903 | total_v_size = MAXBUFSIZE; |
| 904 | } |
| 905 | else |
| 906 | break; |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 907 | } |
Tim Peters | 142297a | 2001-01-15 10:36:56 +0000 | [diff] [blame] | 908 | |
| 909 | /* The stack buffer isn't big enough; malloc a string object and read |
| 910 | * into its buffer. |
Tim Peters | 15b8385 | 2001-01-08 00:53:12 +0000 | [diff] [blame] | 911 | */ |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 912 | total_v_size = MAXBUFSIZE << 1; |
Tim Peters | 1c73323 | 2001-01-08 04:02:07 +0000 | [diff] [blame] | 913 | v = PyString_FromStringAndSize((char*)NULL, (int)total_v_size); |
Tim Peters | 15b8385 | 2001-01-08 00:53:12 +0000 | [diff] [blame] | 914 | if (v == NULL) |
| 915 | return v; |
| 916 | /* copy over everything except the last null byte */ |
Tim Peters | 142297a | 2001-01-15 10:36:56 +0000 | [diff] [blame] | 917 | memcpy(BUF(v), buf, MAXBUFSIZE-1); |
| 918 | pvfree = BUF(v) + MAXBUFSIZE - 1; |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 919 | |
| 920 | /* Keep reading stuff into v; if it ever ends successfully, break |
Tim Peters | 15b8385 | 2001-01-08 00:53:12 +0000 | [diff] [blame] | 921 | * after setting p one beyond the end of the line. The code here is |
| 922 | * very much like the code above, except reads into v's buffer; see |
| 923 | * the code above for detailed comments about the logic. |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 924 | */ |
| 925 | for (;;) { |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 926 | Py_BEGIN_ALLOW_THREADS |
| 927 | pvend = BUF(v) + total_v_size; |
| 928 | nfree = pvend - pvfree; |
| 929 | memset(pvfree, '\n', nfree); |
| 930 | p = fgets(pvfree, nfree, fp); |
| 931 | Py_END_ALLOW_THREADS |
| 932 | |
| 933 | if (p == NULL) { |
| 934 | clearerr(fp); |
| 935 | if (PyErr_CheckSignals()) { |
| 936 | Py_DECREF(v); |
| 937 | return NULL; |
| 938 | } |
| 939 | p = pvfree; |
| 940 | break; |
| 941 | } |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 942 | p = memchr(pvfree, '\n', nfree); |
| 943 | if (p != NULL) { |
| 944 | if (p+1 < pvend && *(p+1) == '\0') { |
| 945 | /* \n came from fgets */ |
| 946 | ++p; |
| 947 | break; |
| 948 | } |
| 949 | /* \n came from us; last line of file, no newline */ |
| 950 | assert(p > pvfree && *(p-1) == '\0'); |
| 951 | --p; |
| 952 | break; |
| 953 | } |
| 954 | /* expand buffer and try again */ |
| 955 | assert(*(pvend-1) == '\0'); |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 956 | increment = total_v_size >> 2; /* mild exponential growth */ |
| 957 | total_v_size += increment; |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 958 | if (total_v_size > INT_MAX) { |
| 959 | PyErr_SetString(PyExc_OverflowError, |
| 960 | "line is longer than a Python string can hold"); |
| 961 | Py_DECREF(v); |
| 962 | return NULL; |
| 963 | } |
| 964 | if (_PyString_Resize(&v, (int)total_v_size) < 0) |
| 965 | return NULL; |
| 966 | /* overwrite the trailing null byte */ |
Tim Peters | ddea208 | 2002-03-23 10:03:50 +0000 | [diff] [blame] | 967 | pvfree = BUF(v) + (total_v_size - increment - 1); |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 968 | } |
| 969 | if (BUF(v) + total_v_size != p) |
| 970 | _PyString_Resize(&v, p - BUF(v)); |
| 971 | return v; |
| 972 | #undef INITBUFSIZE |
Tim Peters | 142297a | 2001-01-15 10:36:56 +0000 | [diff] [blame] | 973 | #undef MAXBUFSIZE |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 974 | } |
Tim Peters | f29b64d | 2001-01-15 06:33:19 +0000 | [diff] [blame] | 975 | #endif /* ifdef USE_FGETS_IN_GETLINE */ |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 976 | |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 977 | /* Internal routine to get a line. |
| 978 | Size argument interpretation: |
| 979 | > 0: max length; |
Guido van Rossum | 8628206 | 2001-01-08 01:26:47 +0000 | [diff] [blame] | 980 | <= 0: read arbitrary line |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 981 | */ |
| 982 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 983 | static PyObject * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 984 | get_line(PyFileObject *f, int n) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 985 | { |
Guido van Rossum | 1187aa4 | 2001-01-05 14:43:05 +0000 | [diff] [blame] | 986 | FILE *fp = f->f_fp; |
| 987 | int c; |
Andrew M. Kuchling | 4b2b445 | 2000-11-29 02:53:22 +0000 | [diff] [blame] | 988 | char *buf, *end; |
Neil Schemenauer | 3a204a7 | 2002-03-23 19:41:34 +0000 | [diff] [blame] | 989 | size_t total_v_size; /* total # of slots in buffer */ |
| 990 | size_t used_v_size; /* # used slots in buffer */ |
| 991 | size_t increment; /* amount to increment the buffer */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 992 | PyObject *v; |
Jack Jansen | 7b8c754 | 2002-04-14 20:12:41 +0000 | [diff] [blame] | 993 | #ifdef WITH_UNIVERSAL_NEWLINES |
| 994 | int newlinetypes = f->f_newlinetypes; |
| 995 | int skipnextlf = f->f_skipnextlf; |
| 996 | int univ_newline = f->f_univ_newline; |
| 997 | #endif |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 998 | |
Jack Jansen | 7b8c754 | 2002-04-14 20:12:41 +0000 | [diff] [blame] | 999 | #if defined(USE_FGETS_IN_GETLINE) |
| 1000 | #ifdef WITH_UNIVERSAL_NEWLINES |
| 1001 | if (n <= 0 && !univ_newline ) |
| 1002 | #else |
Guido van Rossum | 8628206 | 2001-01-08 01:26:47 +0000 | [diff] [blame] | 1003 | if (n <= 0) |
Jack Jansen | 7b8c754 | 2002-04-14 20:12:41 +0000 | [diff] [blame] | 1004 | #endif |
Tim Peters | f29b64d | 2001-01-15 06:33:19 +0000 | [diff] [blame] | 1005 | return getline_via_fgets(fp); |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 1006 | #endif |
Neil Schemenauer | 3a204a7 | 2002-03-23 19:41:34 +0000 | [diff] [blame] | 1007 | total_v_size = n > 0 ? n : 100; |
| 1008 | v = PyString_FromStringAndSize((char *)NULL, total_v_size); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1009 | if (v == NULL) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1010 | return NULL; |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 1011 | buf = BUF(v); |
Neil Schemenauer | 3a204a7 | 2002-03-23 19:41:34 +0000 | [diff] [blame] | 1012 | end = buf + total_v_size; |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1013 | |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 1014 | for (;;) { |
Guido van Rossum | 1187aa4 | 2001-01-05 14:43:05 +0000 | [diff] [blame] | 1015 | Py_BEGIN_ALLOW_THREADS |
| 1016 | FLOCKFILE(fp); |
Jack Jansen | 7b8c754 | 2002-04-14 20:12:41 +0000 | [diff] [blame] | 1017 | #ifdef WITH_UNIVERSAL_NEWLINES |
| 1018 | if (univ_newline) { |
| 1019 | c = 'x'; /* Shut up gcc warning */ |
| 1020 | while ( buf != end && (c = GETC(fp)) != EOF ) { |
| 1021 | if (skipnextlf ) { |
| 1022 | skipnextlf = 0; |
| 1023 | if (c == '\n') { |
| 1024 | /* Seeing a \n here with skipnextlf true |
| 1025 | ** means we saw a \r before. |
| 1026 | */ |
| 1027 | newlinetypes |= NEWLINE_CRLF; |
| 1028 | c = GETC(fp); |
| 1029 | if (c == EOF) break; |
| 1030 | } else { |
| 1031 | newlinetypes |= NEWLINE_CR; |
| 1032 | } |
| 1033 | } |
| 1034 | if (c == '\r') { |
| 1035 | skipnextlf = 1; |
| 1036 | c = '\n'; |
| 1037 | } else if ( c == '\n') |
| 1038 | newlinetypes |= NEWLINE_LF; |
| 1039 | *buf++ = c; |
| 1040 | if (c == '\n') break; |
| 1041 | } |
| 1042 | if ( c == EOF && skipnextlf ) |
| 1043 | newlinetypes |= NEWLINE_CR; |
| 1044 | } else /* If not universal newlines use the normal loop */ |
| 1045 | #endif |
Guido van Rossum | 1187aa4 | 2001-01-05 14:43:05 +0000 | [diff] [blame] | 1046 | while ((c = GETC(fp)) != EOF && |
| 1047 | (*buf++ = c) != '\n' && |
| 1048 | buf != end) |
| 1049 | ; |
| 1050 | FUNLOCKFILE(fp); |
| 1051 | Py_END_ALLOW_THREADS |
Jack Jansen | 7b8c754 | 2002-04-14 20:12:41 +0000 | [diff] [blame] | 1052 | #ifdef WITH_UNIVERSAL_NEWLINES |
| 1053 | f->f_newlinetypes = newlinetypes; |
| 1054 | f->f_skipnextlf = skipnextlf; |
| 1055 | #endif |
Guido van Rossum | 1187aa4 | 2001-01-05 14:43:05 +0000 | [diff] [blame] | 1056 | if (c == '\n') |
| 1057 | break; |
| 1058 | if (c == EOF) { |
Guido van Rossum | 29206bc | 2001-08-09 18:14:59 +0000 | [diff] [blame] | 1059 | if (ferror(fp)) { |
| 1060 | PyErr_SetFromErrno(PyExc_IOError); |
| 1061 | clearerr(fp); |
| 1062 | Py_DECREF(v); |
| 1063 | return NULL; |
| 1064 | } |
Guido van Rossum | 76ad8ed | 1991-06-03 10:54:55 +0000 | [diff] [blame] | 1065 | clearerr(fp); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1066 | if (PyErr_CheckSignals()) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1067 | Py_DECREF(v); |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 1068 | return NULL; |
| 1069 | } |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 1070 | break; |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 1071 | } |
Guido van Rossum | 1187aa4 | 2001-01-05 14:43:05 +0000 | [diff] [blame] | 1072 | /* Must be because buf == end */ |
| 1073 | if (n > 0) |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 1074 | break; |
Neil Schemenauer | 3a204a7 | 2002-03-23 19:41:34 +0000 | [diff] [blame] | 1075 | used_v_size = total_v_size; |
| 1076 | increment = total_v_size >> 2; /* mild exponential growth */ |
| 1077 | total_v_size += increment; |
| 1078 | if (total_v_size > INT_MAX) { |
Guido van Rossum | 1187aa4 | 2001-01-05 14:43:05 +0000 | [diff] [blame] | 1079 | PyErr_SetString(PyExc_OverflowError, |
| 1080 | "line is longer than a Python string can hold"); |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 1081 | Py_DECREF(v); |
Guido van Rossum | 1187aa4 | 2001-01-05 14:43:05 +0000 | [diff] [blame] | 1082 | return NULL; |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 1083 | } |
Neil Schemenauer | 3a204a7 | 2002-03-23 19:41:34 +0000 | [diff] [blame] | 1084 | if (_PyString_Resize(&v, total_v_size) < 0) |
Guido van Rossum | 1187aa4 | 2001-01-05 14:43:05 +0000 | [diff] [blame] | 1085 | return NULL; |
Neil Schemenauer | 3a204a7 | 2002-03-23 19:41:34 +0000 | [diff] [blame] | 1086 | buf = BUF(v) + used_v_size; |
| 1087 | end = BUF(v) + total_v_size; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1088 | } |
Guido van Rossum | 1984f1e | 1992-08-04 12:41:02 +0000 | [diff] [blame] | 1089 | |
Neil Schemenauer | 3a204a7 | 2002-03-23 19:41:34 +0000 | [diff] [blame] | 1090 | used_v_size = buf - BUF(v); |
| 1091 | if (used_v_size != total_v_size) |
| 1092 | _PyString_Resize(&v, used_v_size); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1093 | return v; |
| 1094 | } |
| 1095 | |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 1096 | /* External C interface */ |
| 1097 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1098 | PyObject * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 1099 | PyFile_GetLine(PyObject *f, int n) |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 1100 | { |
Guido van Rossum | 4ddf0a0 | 2001-01-07 20:51:39 +0000 | [diff] [blame] | 1101 | PyObject *result; |
| 1102 | |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1103 | if (f == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1104 | PyErr_BadInternalCall(); |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 1105 | return NULL; |
| 1106 | } |
Guido van Rossum | 4ddf0a0 | 2001-01-07 20:51:39 +0000 | [diff] [blame] | 1107 | |
| 1108 | if (PyFile_Check(f)) { |
| 1109 | if (((PyFileObject*)f)->f_fp == NULL) |
| 1110 | return err_closed(); |
| 1111 | result = get_line((PyFileObject *)f, n); |
| 1112 | } |
| 1113 | else { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1114 | PyObject *reader; |
| 1115 | PyObject *args; |
Guido van Rossum | 4ddf0a0 | 2001-01-07 20:51:39 +0000 | [diff] [blame] | 1116 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1117 | reader = PyObject_GetAttrString(f, "readline"); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1118 | if (reader == NULL) |
| 1119 | return NULL; |
| 1120 | if (n <= 0) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1121 | args = Py_BuildValue("()"); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1122 | else |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1123 | args = Py_BuildValue("(i)", n); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1124 | if (args == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1125 | Py_DECREF(reader); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1126 | return NULL; |
| 1127 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1128 | result = PyEval_CallObject(reader, args); |
| 1129 | Py_DECREF(reader); |
| 1130 | Py_DECREF(args); |
| 1131 | if (result != NULL && !PyString_Check(result)) { |
| 1132 | Py_DECREF(result); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1133 | result = NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1134 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1135 | "object.readline() returned non-string"); |
| 1136 | } |
Guido van Rossum | 4ddf0a0 | 2001-01-07 20:51:39 +0000 | [diff] [blame] | 1137 | } |
| 1138 | |
| 1139 | if (n < 0 && result != NULL && PyString_Check(result)) { |
| 1140 | char *s = PyString_AS_STRING(result); |
| 1141 | int len = PyString_GET_SIZE(result); |
| 1142 | if (len == 0) { |
| 1143 | Py_DECREF(result); |
| 1144 | result = NULL; |
| 1145 | PyErr_SetString(PyExc_EOFError, |
| 1146 | "EOF when reading a line"); |
| 1147 | } |
| 1148 | else if (s[len-1] == '\n') { |
| 1149 | if (result->ob_refcnt == 1) |
| 1150 | _PyString_Resize(&result, len-1); |
| 1151 | else { |
| 1152 | PyObject *v; |
| 1153 | v = PyString_FromStringAndSize(s, len-1); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1154 | Py_DECREF(result); |
Guido van Rossum | 4ddf0a0 | 2001-01-07 20:51:39 +0000 | [diff] [blame] | 1155 | result = v; |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1156 | } |
| 1157 | } |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1158 | } |
Guido van Rossum | 4ddf0a0 | 2001-01-07 20:51:39 +0000 | [diff] [blame] | 1159 | return result; |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 1160 | } |
| 1161 | |
| 1162 | /* Python method */ |
| 1163 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1164 | static PyObject * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 1165 | file_readline(PyFileObject *f, PyObject *args) |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 1166 | { |
Guido van Rossum | 789a161 | 1997-05-10 22:33:55 +0000 | [diff] [blame] | 1167 | int n = -1; |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 1168 | |
Guido van Rossum | d7297e6 | 1992-07-06 14:19:26 +0000 | [diff] [blame] | 1169 | if (f->f_fp == NULL) |
| 1170 | return err_closed(); |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 1171 | if (!PyArg_ParseTuple(args, "|i:readline", &n)) |
Guido van Rossum | 789a161 | 1997-05-10 22:33:55 +0000 | [diff] [blame] | 1172 | return NULL; |
| 1173 | if (n == 0) |
| 1174 | return PyString_FromString(""); |
| 1175 | if (n < 0) |
| 1176 | n = 0; |
Marc-André Lemburg | 1f46860 | 2000-07-05 15:32:40 +0000 | [diff] [blame] | 1177 | return get_line(f, n); |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 1178 | } |
| 1179 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1180 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1181 | file_xreadlines(PyFileObject *f) |
Guido van Rossum | e07d5cf | 2001-01-09 21:50:24 +0000 | [diff] [blame] | 1182 | { |
| 1183 | static PyObject* xreadlines_function = NULL; |
Tim Peters | f29b64d | 2001-01-15 06:33:19 +0000 | [diff] [blame] | 1184 | |
Neal Norwitz | 649b759 | 2002-01-01 19:07:13 +0000 | [diff] [blame] | 1185 | if (f->f_fp == NULL) |
| 1186 | return err_closed(); |
Guido van Rossum | e07d5cf | 2001-01-09 21:50:24 +0000 | [diff] [blame] | 1187 | if (!xreadlines_function) { |
| 1188 | PyObject *xreadlines_module = |
| 1189 | PyImport_ImportModule("xreadlines"); |
| 1190 | if(!xreadlines_module) |
| 1191 | return NULL; |
| 1192 | |
| 1193 | xreadlines_function = PyObject_GetAttrString(xreadlines_module, |
| 1194 | "xreadlines"); |
| 1195 | Py_DECREF(xreadlines_module); |
| 1196 | if(!xreadlines_function) |
| 1197 | return NULL; |
| 1198 | } |
| 1199 | return PyObject_CallFunction(xreadlines_function, "(O)", f); |
| 1200 | } |
| 1201 | |
| 1202 | static PyObject * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 1203 | file_readlines(PyFileObject *f, PyObject *args) |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 1204 | { |
Guido van Rossum | 789a161 | 1997-05-10 22:33:55 +0000 | [diff] [blame] | 1205 | long sizehint = 0; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1206 | PyObject *list; |
| 1207 | PyObject *line; |
Guido van Rossum | 6263d54 | 1997-05-10 22:07:25 +0000 | [diff] [blame] | 1208 | char small_buffer[SMALLCHUNK]; |
| 1209 | char *buffer = small_buffer; |
| 1210 | size_t buffersize = SMALLCHUNK; |
| 1211 | PyObject *big_buffer = NULL; |
| 1212 | size_t nfilled = 0; |
| 1213 | size_t nread; |
Guido van Rossum | 789a161 | 1997-05-10 22:33:55 +0000 | [diff] [blame] | 1214 | size_t totalread = 0; |
Guido van Rossum | 6263d54 | 1997-05-10 22:07:25 +0000 | [diff] [blame] | 1215 | char *p, *q, *end; |
| 1216 | int err; |
Guido van Rossum | 79fd0fc | 2001-10-12 20:01:53 +0000 | [diff] [blame] | 1217 | int shortread = 0; |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 1218 | |
Guido van Rossum | d7297e6 | 1992-07-06 14:19:26 +0000 | [diff] [blame] | 1219 | if (f->f_fp == NULL) |
| 1220 | return err_closed(); |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 1221 | if (!PyArg_ParseTuple(args, "|l:readlines", &sizehint)) |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 1222 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1223 | if ((list = PyList_New(0)) == NULL) |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 1224 | return NULL; |
| 1225 | for (;;) { |
Guido van Rossum | 79fd0fc | 2001-10-12 20:01:53 +0000 | [diff] [blame] | 1226 | if (shortread) |
| 1227 | nread = 0; |
| 1228 | else { |
| 1229 | Py_BEGIN_ALLOW_THREADS |
| 1230 | errno = 0; |
Tim Peters | 058b141 | 2002-04-21 07:29:14 +0000 | [diff] [blame] | 1231 | nread = Py_UniversalNewlineFread(buffer+nfilled, |
Jack Jansen | 7b8c754 | 2002-04-14 20:12:41 +0000 | [diff] [blame] | 1232 | buffersize-nfilled, f->f_fp, (PyObject *)f); |
Guido van Rossum | 79fd0fc | 2001-10-12 20:01:53 +0000 | [diff] [blame] | 1233 | Py_END_ALLOW_THREADS |
| 1234 | shortread = (nread < buffersize-nfilled); |
| 1235 | } |
Guido van Rossum | 6263d54 | 1997-05-10 22:07:25 +0000 | [diff] [blame] | 1236 | if (nread == 0) { |
Guido van Rossum | 789a161 | 1997-05-10 22:33:55 +0000 | [diff] [blame] | 1237 | sizehint = 0; |
Guido van Rossum | 3da3fce | 1998-02-19 20:46:48 +0000 | [diff] [blame] | 1238 | if (!ferror(f->f_fp)) |
Guido van Rossum | 6263d54 | 1997-05-10 22:07:25 +0000 | [diff] [blame] | 1239 | break; |
| 1240 | PyErr_SetFromErrno(PyExc_IOError); |
| 1241 | clearerr(f->f_fp); |
| 1242 | error: |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1243 | Py_DECREF(list); |
Guido van Rossum | 6263d54 | 1997-05-10 22:07:25 +0000 | [diff] [blame] | 1244 | list = NULL; |
| 1245 | goto cleanup; |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 1246 | } |
Guido van Rossum | 789a161 | 1997-05-10 22:33:55 +0000 | [diff] [blame] | 1247 | totalread += nread; |
Guido van Rossum | 6263d54 | 1997-05-10 22:07:25 +0000 | [diff] [blame] | 1248 | p = memchr(buffer+nfilled, '\n', nread); |
| 1249 | if (p == NULL) { |
| 1250 | /* Need a larger buffer to fit this line */ |
| 1251 | nfilled += nread; |
| 1252 | buffersize *= 2; |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 1253 | if (buffersize > INT_MAX) { |
| 1254 | PyErr_SetString(PyExc_OverflowError, |
Guido van Rossum | e07d5cf | 2001-01-09 21:50:24 +0000 | [diff] [blame] | 1255 | "line is longer than a Python string can hold"); |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 1256 | goto error; |
| 1257 | } |
Guido van Rossum | 6263d54 | 1997-05-10 22:07:25 +0000 | [diff] [blame] | 1258 | if (big_buffer == NULL) { |
| 1259 | /* Create the big buffer */ |
| 1260 | big_buffer = PyString_FromStringAndSize( |
| 1261 | NULL, buffersize); |
| 1262 | if (big_buffer == NULL) |
| 1263 | goto error; |
| 1264 | buffer = PyString_AS_STRING(big_buffer); |
| 1265 | memcpy(buffer, small_buffer, nfilled); |
| 1266 | } |
| 1267 | else { |
| 1268 | /* Grow the big buffer */ |
Jack Jansen | 7b8c754 | 2002-04-14 20:12:41 +0000 | [diff] [blame] | 1269 | if ( _PyString_Resize(&big_buffer, buffersize) < 0 ) |
| 1270 | goto error; |
Guido van Rossum | 6263d54 | 1997-05-10 22:07:25 +0000 | [diff] [blame] | 1271 | buffer = PyString_AS_STRING(big_buffer); |
| 1272 | } |
| 1273 | continue; |
| 1274 | } |
| 1275 | end = buffer+nfilled+nread; |
| 1276 | q = buffer; |
| 1277 | do { |
| 1278 | /* Process complete lines */ |
| 1279 | p++; |
| 1280 | line = PyString_FromStringAndSize(q, p-q); |
| 1281 | if (line == NULL) |
| 1282 | goto error; |
| 1283 | err = PyList_Append(list, line); |
| 1284 | Py_DECREF(line); |
| 1285 | if (err != 0) |
| 1286 | goto error; |
| 1287 | q = p; |
| 1288 | p = memchr(q, '\n', end-q); |
| 1289 | } while (p != NULL); |
| 1290 | /* Move the remaining incomplete line to the start */ |
| 1291 | nfilled = end-q; |
| 1292 | memmove(buffer, q, nfilled); |
Guido van Rossum | 789a161 | 1997-05-10 22:33:55 +0000 | [diff] [blame] | 1293 | if (sizehint > 0) |
| 1294 | if (totalread >= (size_t)sizehint) |
| 1295 | break; |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 1296 | } |
Guido van Rossum | 6263d54 | 1997-05-10 22:07:25 +0000 | [diff] [blame] | 1297 | if (nfilled != 0) { |
| 1298 | /* Partial last line */ |
| 1299 | line = PyString_FromStringAndSize(buffer, nfilled); |
| 1300 | if (line == NULL) |
| 1301 | goto error; |
Guido van Rossum | 789a161 | 1997-05-10 22:33:55 +0000 | [diff] [blame] | 1302 | if (sizehint > 0) { |
| 1303 | /* Need to complete the last line */ |
Marc-André Lemburg | 1f46860 | 2000-07-05 15:32:40 +0000 | [diff] [blame] | 1304 | PyObject *rest = get_line(f, 0); |
Guido van Rossum | 789a161 | 1997-05-10 22:33:55 +0000 | [diff] [blame] | 1305 | if (rest == NULL) { |
| 1306 | Py_DECREF(line); |
| 1307 | goto error; |
| 1308 | } |
| 1309 | PyString_Concat(&line, rest); |
| 1310 | Py_DECREF(rest); |
| 1311 | if (line == NULL) |
| 1312 | goto error; |
| 1313 | } |
Guido van Rossum | 6263d54 | 1997-05-10 22:07:25 +0000 | [diff] [blame] | 1314 | err = PyList_Append(list, line); |
| 1315 | Py_DECREF(line); |
| 1316 | if (err != 0) |
| 1317 | goto error; |
| 1318 | } |
| 1319 | cleanup: |
Tim Peters | 5de9842 | 2002-04-27 18:44:32 +0000 | [diff] [blame] | 1320 | Py_XDECREF(big_buffer); |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 1321 | return list; |
| 1322 | } |
| 1323 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1324 | static PyObject * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 1325 | file_write(PyFileObject *f, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1326 | { |
Guido van Rossum | d7297e6 | 1992-07-06 14:19:26 +0000 | [diff] [blame] | 1327 | char *s; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1328 | int n, n2; |
Guido van Rossum | d7297e6 | 1992-07-06 14:19:26 +0000 | [diff] [blame] | 1329 | if (f->f_fp == NULL) |
| 1330 | return err_closed(); |
Michael W. Hudson | e2ec3eb | 2001-10-31 18:51:01 +0000 | [diff] [blame] | 1331 | if (!PyArg_ParseTuple(args, f->f_binary ? "s#" : "t#", &s, &n)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1332 | return NULL; |
Guido van Rossum | eb183da | 1991-04-04 10:44:06 +0000 | [diff] [blame] | 1333 | f->f_softspace = 0; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1334 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1335 | errno = 0; |
Guido van Rossum | d7297e6 | 1992-07-06 14:19:26 +0000 | [diff] [blame] | 1336 | n2 = fwrite(s, 1, n, f->f_fp); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1337 | Py_END_ALLOW_THREADS |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1338 | if (n2 != n) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1339 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | febd551 | 1992-03-04 16:39:24 +0000 | [diff] [blame] | 1340 | clearerr(f->f_fp); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1341 | return NULL; |
| 1342 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1343 | Py_INCREF(Py_None); |
| 1344 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1345 | } |
| 1346 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1347 | static PyObject * |
Tim Peters | 2c9aa5e | 2001-09-23 04:06:05 +0000 | [diff] [blame] | 1348 | file_writelines(PyFileObject *f, PyObject *seq) |
Guido van Rossum | 5a2a683 | 1993-10-25 09:59:04 +0000 | [diff] [blame] | 1349 | { |
Guido van Rossum | ee70ad1 | 2000-03-13 16:27:06 +0000 | [diff] [blame] | 1350 | #define CHUNKSIZE 1000 |
| 1351 | PyObject *list, *line; |
Tim Peters | 2c9aa5e | 2001-09-23 04:06:05 +0000 | [diff] [blame] | 1352 | PyObject *it; /* iter(seq) */ |
Guido van Rossum | ee70ad1 | 2000-03-13 16:27:06 +0000 | [diff] [blame] | 1353 | PyObject *result; |
| 1354 | int i, j, index, len, nwritten, islist; |
| 1355 | |
Tim Peters | 2c9aa5e | 2001-09-23 04:06:05 +0000 | [diff] [blame] | 1356 | assert(seq != NULL); |
Guido van Rossum | 5a2a683 | 1993-10-25 09:59:04 +0000 | [diff] [blame] | 1357 | if (f->f_fp == NULL) |
| 1358 | return err_closed(); |
Tim Peters | 2c9aa5e | 2001-09-23 04:06:05 +0000 | [diff] [blame] | 1359 | |
| 1360 | result = NULL; |
| 1361 | list = NULL; |
| 1362 | islist = PyList_Check(seq); |
| 1363 | if (islist) |
| 1364 | it = NULL; |
| 1365 | else { |
| 1366 | it = PyObject_GetIter(seq); |
| 1367 | if (it == NULL) { |
| 1368 | PyErr_SetString(PyExc_TypeError, |
| 1369 | "writelines() requires an iterable argument"); |
| 1370 | return NULL; |
| 1371 | } |
| 1372 | /* From here on, fail by going to error, to reclaim "it". */ |
| 1373 | list = PyList_New(CHUNKSIZE); |
| 1374 | if (list == NULL) |
| 1375 | goto error; |
Guido van Rossum | 5a2a683 | 1993-10-25 09:59:04 +0000 | [diff] [blame] | 1376 | } |
Guido van Rossum | ee70ad1 | 2000-03-13 16:27:06 +0000 | [diff] [blame] | 1377 | |
| 1378 | /* Strategy: slurp CHUNKSIZE lines into a private list, |
| 1379 | checking that they are all strings, then write that list |
| 1380 | without holding the interpreter lock, then come back for more. */ |
Tim Peters | 2c9aa5e | 2001-09-23 04:06:05 +0000 | [diff] [blame] | 1381 | for (index = 0; ; index += CHUNKSIZE) { |
Guido van Rossum | ee70ad1 | 2000-03-13 16:27:06 +0000 | [diff] [blame] | 1382 | if (islist) { |
| 1383 | Py_XDECREF(list); |
Tim Peters | 2c9aa5e | 2001-09-23 04:06:05 +0000 | [diff] [blame] | 1384 | list = PyList_GetSlice(seq, index, index+CHUNKSIZE); |
Guido van Rossum | ee70ad1 | 2000-03-13 16:27:06 +0000 | [diff] [blame] | 1385 | if (list == NULL) |
Tim Peters | 2c9aa5e | 2001-09-23 04:06:05 +0000 | [diff] [blame] | 1386 | goto error; |
Guido van Rossum | ee70ad1 | 2000-03-13 16:27:06 +0000 | [diff] [blame] | 1387 | j = PyList_GET_SIZE(list); |
| 1388 | } |
| 1389 | else { |
| 1390 | for (j = 0; j < CHUNKSIZE; j++) { |
Tim Peters | 2c9aa5e | 2001-09-23 04:06:05 +0000 | [diff] [blame] | 1391 | line = PyIter_Next(it); |
Guido van Rossum | ee70ad1 | 2000-03-13 16:27:06 +0000 | [diff] [blame] | 1392 | if (line == NULL) { |
Tim Peters | 2c9aa5e | 2001-09-23 04:06:05 +0000 | [diff] [blame] | 1393 | if (PyErr_Occurred()) |
| 1394 | goto error; |
| 1395 | break; |
Guido van Rossum | ee70ad1 | 2000-03-13 16:27:06 +0000 | [diff] [blame] | 1396 | } |
Guido van Rossum | ee70ad1 | 2000-03-13 16:27:06 +0000 | [diff] [blame] | 1397 | PyList_SetItem(list, j, line); |
| 1398 | } |
| 1399 | } |
| 1400 | if (j == 0) |
| 1401 | break; |
| 1402 | |
Marc-André Lemburg | 6ef68b5 | 2000-08-25 22:39:50 +0000 | [diff] [blame] | 1403 | /* Check that all entries are indeed strings. If not, |
| 1404 | apply the same rules as for file.write() and |
| 1405 | convert the results to strings. This is slow, but |
| 1406 | seems to be the only way since all conversion APIs |
| 1407 | could potentially execute Python code. */ |
| 1408 | for (i = 0; i < j; i++) { |
| 1409 | PyObject *v = PyList_GET_ITEM(list, i); |
| 1410 | if (!PyString_Check(v)) { |
| 1411 | const char *buffer; |
| 1412 | int len; |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 1413 | if (((f->f_binary && |
Marc-André Lemburg | 6ef68b5 | 2000-08-25 22:39:50 +0000 | [diff] [blame] | 1414 | PyObject_AsReadBuffer(v, |
| 1415 | (const void**)&buffer, |
| 1416 | &len)) || |
| 1417 | PyObject_AsCharBuffer(v, |
| 1418 | &buffer, |
| 1419 | &len))) { |
| 1420 | PyErr_SetString(PyExc_TypeError, |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 1421 | "writelines() argument must be a sequence of strings"); |
Marc-André Lemburg | 6ef68b5 | 2000-08-25 22:39:50 +0000 | [diff] [blame] | 1422 | goto error; |
| 1423 | } |
| 1424 | line = PyString_FromStringAndSize(buffer, |
| 1425 | len); |
| 1426 | if (line == NULL) |
| 1427 | goto error; |
| 1428 | Py_DECREF(v); |
Marc-André Lemburg | f5e96fa | 2000-08-25 22:49:05 +0000 | [diff] [blame] | 1429 | PyList_SET_ITEM(list, i, line); |
Marc-André Lemburg | 6ef68b5 | 2000-08-25 22:39:50 +0000 | [diff] [blame] | 1430 | } |
| 1431 | } |
| 1432 | |
| 1433 | /* Since we are releasing the global lock, the |
| 1434 | following code may *not* execute Python code. */ |
Guido van Rossum | ee70ad1 | 2000-03-13 16:27:06 +0000 | [diff] [blame] | 1435 | Py_BEGIN_ALLOW_THREADS |
| 1436 | f->f_softspace = 0; |
| 1437 | errno = 0; |
| 1438 | for (i = 0; i < j; i++) { |
Marc-André Lemburg | 6ef68b5 | 2000-08-25 22:39:50 +0000 | [diff] [blame] | 1439 | line = PyList_GET_ITEM(list, i); |
Guido van Rossum | ee70ad1 | 2000-03-13 16:27:06 +0000 | [diff] [blame] | 1440 | len = PyString_GET_SIZE(line); |
| 1441 | nwritten = fwrite(PyString_AS_STRING(line), |
| 1442 | 1, len, f->f_fp); |
| 1443 | if (nwritten != len) { |
| 1444 | Py_BLOCK_THREADS |
| 1445 | PyErr_SetFromErrno(PyExc_IOError); |
| 1446 | clearerr(f->f_fp); |
| 1447 | goto error; |
| 1448 | } |
| 1449 | } |
| 1450 | Py_END_ALLOW_THREADS |
| 1451 | |
| 1452 | if (j < CHUNKSIZE) |
| 1453 | break; |
Guido van Rossum | ee70ad1 | 2000-03-13 16:27:06 +0000 | [diff] [blame] | 1454 | } |
| 1455 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1456 | Py_INCREF(Py_None); |
Guido van Rossum | ee70ad1 | 2000-03-13 16:27:06 +0000 | [diff] [blame] | 1457 | result = Py_None; |
| 1458 | error: |
| 1459 | Py_XDECREF(list); |
Tim Peters | 2c9aa5e | 2001-09-23 04:06:05 +0000 | [diff] [blame] | 1460 | Py_XDECREF(it); |
Guido van Rossum | ee70ad1 | 2000-03-13 16:27:06 +0000 | [diff] [blame] | 1461 | return result; |
Tim Peters | 2c9aa5e | 2001-09-23 04:06:05 +0000 | [diff] [blame] | 1462 | #undef CHUNKSIZE |
Guido van Rossum | 5a2a683 | 1993-10-25 09:59:04 +0000 | [diff] [blame] | 1463 | } |
| 1464 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1465 | PyDoc_STRVAR(readline_doc, |
Tim Peters | efc3a3a | 2001-09-20 07:55:22 +0000 | [diff] [blame] | 1466 | "readline([size]) -> next line from the file, as a string.\n" |
| 1467 | "\n" |
| 1468 | "Retain newline. A non-negative size argument limits the maximum\n" |
| 1469 | "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] | 1470 | "Return an empty string at EOF."); |
Tim Peters | efc3a3a | 2001-09-20 07:55:22 +0000 | [diff] [blame] | 1471 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1472 | PyDoc_STRVAR(read_doc, |
Tim Peters | efc3a3a | 2001-09-20 07:55:22 +0000 | [diff] [blame] | 1473 | "read([size]) -> read at most size bytes, returned as a string.\n" |
| 1474 | "\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1475 | "If the size argument is negative or omitted, read until EOF is reached."); |
Tim Peters | efc3a3a | 2001-09-20 07:55:22 +0000 | [diff] [blame] | 1476 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1477 | PyDoc_STRVAR(write_doc, |
Tim Peters | efc3a3a | 2001-09-20 07:55:22 +0000 | [diff] [blame] | 1478 | "write(str) -> None. Write string str to file.\n" |
| 1479 | "\n" |
| 1480 | "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] | 1481 | "the file on disk reflects the data written."); |
Tim Peters | efc3a3a | 2001-09-20 07:55:22 +0000 | [diff] [blame] | 1482 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1483 | PyDoc_STRVAR(fileno_doc, |
Tim Peters | efc3a3a | 2001-09-20 07:55:22 +0000 | [diff] [blame] | 1484 | "fileno() -> integer \"file descriptor\".\n" |
| 1485 | "\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1486 | "This is needed for lower-level file interfaces, such os.read()."); |
Tim Peters | efc3a3a | 2001-09-20 07:55:22 +0000 | [diff] [blame] | 1487 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1488 | PyDoc_STRVAR(seek_doc, |
Tim Peters | efc3a3a | 2001-09-20 07:55:22 +0000 | [diff] [blame] | 1489 | "seek(offset[, whence]) -> None. Move to new file position.\n" |
| 1490 | "\n" |
| 1491 | "Argument offset is a byte count. Optional argument whence defaults to\n" |
| 1492 | "0 (offset from start of file, offset should be >= 0); other values are 1\n" |
| 1493 | "(move relative to current position, positive or negative), and 2 (move\n" |
| 1494 | "relative to end of file, usually negative, although many platforms allow\n" |
| 1495 | "seeking beyond the end of a file).\n" |
| 1496 | "\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1497 | "Note that not all file objects are seekable."); |
Tim Peters | efc3a3a | 2001-09-20 07:55:22 +0000 | [diff] [blame] | 1498 | |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 1499 | #ifdef HAVE_FTRUNCATE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1500 | PyDoc_STRVAR(truncate_doc, |
Tim Peters | efc3a3a | 2001-09-20 07:55:22 +0000 | [diff] [blame] | 1501 | "truncate([size]) -> None. Truncate the file to at most size bytes.\n" |
| 1502 | "\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1503 | "Size defaults to the current file position, as returned by tell()."); |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 1504 | #endif |
Tim Peters | efc3a3a | 2001-09-20 07:55:22 +0000 | [diff] [blame] | 1505 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1506 | PyDoc_STRVAR(tell_doc, |
| 1507 | "tell() -> current file position, an integer (may be a long integer)."); |
Tim Peters | efc3a3a | 2001-09-20 07:55:22 +0000 | [diff] [blame] | 1508 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1509 | PyDoc_STRVAR(readinto_doc, |
| 1510 | "readinto() -> Undocumented. Don't use this; it may go away."); |
Tim Peters | efc3a3a | 2001-09-20 07:55:22 +0000 | [diff] [blame] | 1511 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1512 | PyDoc_STRVAR(readlines_doc, |
Tim Peters | efc3a3a | 2001-09-20 07:55:22 +0000 | [diff] [blame] | 1513 | "readlines([size]) -> list of strings, each a line from the file.\n" |
| 1514 | "\n" |
| 1515 | "Call readline() repeatedly and return a list of the lines so read.\n" |
| 1516 | "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] | 1517 | "total number of bytes in the lines returned."); |
Tim Peters | efc3a3a | 2001-09-20 07:55:22 +0000 | [diff] [blame] | 1518 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1519 | PyDoc_STRVAR(xreadlines_doc, |
Tim Peters | efc3a3a | 2001-09-20 07:55:22 +0000 | [diff] [blame] | 1520 | "xreadlines() -> next line from the file, as a string.\n" |
| 1521 | "\n" |
| 1522 | "Equivalent to xreadlines.xreadlines(file). This is like readline(), but\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1523 | "often quicker, due to reading ahead internally."); |
Tim Peters | efc3a3a | 2001-09-20 07:55:22 +0000 | [diff] [blame] | 1524 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1525 | PyDoc_STRVAR(writelines_doc, |
Tim Peters | 2c9aa5e | 2001-09-23 04:06:05 +0000 | [diff] [blame] | 1526 | "writelines(sequence_of_strings) -> None. Write the strings to the file.\n" |
Tim Peters | efc3a3a | 2001-09-20 07:55:22 +0000 | [diff] [blame] | 1527 | "\n" |
Tim Peters | 2c9aa5e | 2001-09-23 04:06:05 +0000 | [diff] [blame] | 1528 | "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] | 1529 | "producing strings. This is equivalent to calling write() for each string."); |
Tim Peters | efc3a3a | 2001-09-20 07:55:22 +0000 | [diff] [blame] | 1530 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1531 | PyDoc_STRVAR(flush_doc, |
| 1532 | "flush() -> None. Flush the internal I/O buffer."); |
Tim Peters | efc3a3a | 2001-09-20 07:55:22 +0000 | [diff] [blame] | 1533 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1534 | PyDoc_STRVAR(close_doc, |
Tim Peters | efc3a3a | 2001-09-20 07:55:22 +0000 | [diff] [blame] | 1535 | "close() -> None or (perhaps) an integer. Close the file.\n" |
| 1536 | "\n" |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 1537 | "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] | 1538 | "further I/O operations. close() may be called more than once without\n" |
| 1539 | "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] | 1540 | "may return an exit status upon closing."); |
Tim Peters | efc3a3a | 2001-09-20 07:55:22 +0000 | [diff] [blame] | 1541 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1542 | PyDoc_STRVAR(isatty_doc, |
| 1543 | "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] | 1544 | |
| 1545 | static PyMethodDef file_methods[] = { |
| 1546 | {"readline", (PyCFunction)file_readline, METH_VARARGS, readline_doc}, |
| 1547 | {"read", (PyCFunction)file_read, METH_VARARGS, read_doc}, |
Michael W. Hudson | e2ec3eb | 2001-10-31 18:51:01 +0000 | [diff] [blame] | 1548 | {"write", (PyCFunction)file_write, METH_VARARGS, write_doc}, |
Tim Peters | efc3a3a | 2001-09-20 07:55:22 +0000 | [diff] [blame] | 1549 | {"fileno", (PyCFunction)file_fileno, METH_NOARGS, fileno_doc}, |
| 1550 | {"seek", (PyCFunction)file_seek, METH_VARARGS, seek_doc}, |
| 1551 | #ifdef HAVE_FTRUNCATE |
| 1552 | {"truncate", (PyCFunction)file_truncate, METH_VARARGS, truncate_doc}, |
| 1553 | #endif |
| 1554 | {"tell", (PyCFunction)file_tell, METH_NOARGS, tell_doc}, |
Neal Norwitz | 62f5a9d | 2002-04-01 00:09:00 +0000 | [diff] [blame] | 1555 | {"readinto", (PyCFunction)file_readinto, METH_VARARGS, readinto_doc}, |
Tim Peters | efc3a3a | 2001-09-20 07:55:22 +0000 | [diff] [blame] | 1556 | {"readlines", (PyCFunction)file_readlines, METH_VARARGS, readlines_doc}, |
| 1557 | {"xreadlines", (PyCFunction)file_xreadlines, METH_NOARGS, xreadlines_doc}, |
| 1558 | {"writelines", (PyCFunction)file_writelines, METH_O, writelines_doc}, |
| 1559 | {"flush", (PyCFunction)file_flush, METH_NOARGS, flush_doc}, |
| 1560 | {"close", (PyCFunction)file_close, METH_NOARGS, close_doc}, |
| 1561 | {"isatty", (PyCFunction)file_isatty, METH_NOARGS, isatty_doc}, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1562 | {NULL, NULL} /* sentinel */ |
| 1563 | }; |
| 1564 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1565 | #define OFF(x) offsetof(PyFileObject, x) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1566 | |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 1567 | static PyMemberDef file_memberlist[] = { |
| 1568 | {"softspace", T_INT, OFF(f_softspace), 0, |
| 1569 | "flag indicating that a space needs to be printed; used by print"}, |
| 1570 | {"mode", T_OBJECT, OFF(f_mode), RO, |
| 1571 | "file mode ('r', 'w', 'a', possibly with 'b' or '+' added)"}, |
| 1572 | {"name", T_OBJECT, OFF(f_name), RO, |
| 1573 | "file name"}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1574 | /* getattr(f, "closed") is implemented without this table */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1575 | {NULL} /* Sentinel */ |
| 1576 | }; |
| 1577 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1578 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1579 | get_closed(PyFileObject *f, void *closure) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1580 | { |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 1581 | return PyBool_FromLong((long)(f->f_fp == 0)); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1582 | } |
Jack Jansen | 7b8c754 | 2002-04-14 20:12:41 +0000 | [diff] [blame] | 1583 | #ifdef WITH_UNIVERSAL_NEWLINES |
| 1584 | static PyObject * |
| 1585 | get_newlines(PyFileObject *f, void *closure) |
| 1586 | { |
| 1587 | switch (f->f_newlinetypes) { |
| 1588 | case NEWLINE_UNKNOWN: |
| 1589 | Py_INCREF(Py_None); |
| 1590 | return Py_None; |
| 1591 | case NEWLINE_CR: |
| 1592 | return PyString_FromString("\r"); |
| 1593 | case NEWLINE_LF: |
| 1594 | return PyString_FromString("\n"); |
| 1595 | case NEWLINE_CR|NEWLINE_LF: |
| 1596 | return Py_BuildValue("(ss)", "\r", "\n"); |
| 1597 | case NEWLINE_CRLF: |
| 1598 | return PyString_FromString("\r\n"); |
| 1599 | case NEWLINE_CR|NEWLINE_CRLF: |
| 1600 | return Py_BuildValue("(ss)", "\r", "\r\n"); |
| 1601 | case NEWLINE_LF|NEWLINE_CRLF: |
| 1602 | return Py_BuildValue("(ss)", "\n", "\r\n"); |
| 1603 | case NEWLINE_CR|NEWLINE_LF|NEWLINE_CRLF: |
| 1604 | return Py_BuildValue("(sss)", "\r", "\n", "\r\n"); |
| 1605 | default: |
| 1606 | PyErr_Format(PyExc_SystemError, "Unknown newlines value 0x%x\n", f->f_newlinetypes); |
| 1607 | return NULL; |
| 1608 | } |
| 1609 | } |
| 1610 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1611 | |
Guido van Rossum | 32d34c8 | 2001-09-20 21:45:26 +0000 | [diff] [blame] | 1612 | static PyGetSetDef file_getsetlist[] = { |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 1613 | {"closed", (getter)get_closed, NULL, "True if the file is closed"}, |
Jack Jansen | 7b8c754 | 2002-04-14 20:12:41 +0000 | [diff] [blame] | 1614 | #ifdef WITH_UNIVERSAL_NEWLINES |
| 1615 | {"newlines", (getter)get_newlines, NULL, "end-of-line convention used in this file"}, |
| 1616 | #endif |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1617 | {0}, |
| 1618 | }; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1619 | |
Guido van Rossum | 6596725 | 2001-04-21 13:20:18 +0000 | [diff] [blame] | 1620 | static PyObject * |
Guido van Rossum | 5b02184 | 2001-05-22 16:48:37 +0000 | [diff] [blame] | 1621 | file_getiter(PyObject *f) |
Guido van Rossum | 6596725 | 2001-04-21 13:20:18 +0000 | [diff] [blame] | 1622 | { |
Guido van Rossum | 5b02184 | 2001-05-22 16:48:37 +0000 | [diff] [blame] | 1623 | return PyObject_CallMethod(f, "xreadlines", ""); |
Guido van Rossum | 6596725 | 2001-04-21 13:20:18 +0000 | [diff] [blame] | 1624 | } |
| 1625 | |
Tim Peters | 59c9a64 | 2001-09-13 05:38:56 +0000 | [diff] [blame] | 1626 | static PyObject * |
| 1627 | file_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1628 | { |
Tim Peters | 4441001 | 2001-09-14 03:26:08 +0000 | [diff] [blame] | 1629 | PyObject *self; |
| 1630 | static PyObject *not_yet_string; |
| 1631 | |
| 1632 | assert(type != NULL && type->tp_alloc != NULL); |
| 1633 | |
| 1634 | if (not_yet_string == NULL) { |
| 1635 | not_yet_string = PyString_FromString("<uninitialized file>"); |
| 1636 | if (not_yet_string == NULL) |
| 1637 | return NULL; |
| 1638 | } |
| 1639 | |
| 1640 | self = type->tp_alloc(type, 0); |
| 1641 | if (self != NULL) { |
| 1642 | /* Always fill in the name and mode, so that nobody else |
| 1643 | needs to special-case NULLs there. */ |
| 1644 | Py_INCREF(not_yet_string); |
| 1645 | ((PyFileObject *)self)->f_name = not_yet_string; |
| 1646 | Py_INCREF(not_yet_string); |
| 1647 | ((PyFileObject *)self)->f_mode = not_yet_string; |
| 1648 | } |
| 1649 | return self; |
| 1650 | } |
| 1651 | |
| 1652 | static int |
| 1653 | file_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 1654 | { |
| 1655 | PyFileObject *foself = (PyFileObject *)self; |
| 1656 | int ret = 0; |
Tim Peters | 59c9a64 | 2001-09-13 05:38:56 +0000 | [diff] [blame] | 1657 | static char *kwlist[] = {"name", "mode", "buffering", 0}; |
| 1658 | char *name = NULL; |
| 1659 | char *mode = "r"; |
| 1660 | int bufsize = -1; |
Tim Peters | 4441001 | 2001-09-14 03:26:08 +0000 | [diff] [blame] | 1661 | |
| 1662 | assert(PyFile_Check(self)); |
| 1663 | if (foself->f_fp != NULL) { |
| 1664 | /* Have to close the existing file first. */ |
| 1665 | PyObject *closeresult = file_close(foself); |
| 1666 | if (closeresult == NULL) |
| 1667 | return -1; |
| 1668 | Py_DECREF(closeresult); |
| 1669 | } |
Tim Peters | 59c9a64 | 2001-09-13 05:38:56 +0000 | [diff] [blame] | 1670 | |
| 1671 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "et|si:file", kwlist, |
| 1672 | Py_FileSystemDefaultEncoding, &name, |
| 1673 | &mode, &bufsize)) |
Tim Peters | 4441001 | 2001-09-14 03:26:08 +0000 | [diff] [blame] | 1674 | return -1; |
| 1675 | if (fill_file_fields(foself, NULL, name, mode, fclose) == NULL) |
| 1676 | goto Error; |
| 1677 | if (open_the_file(foself, name, mode) == NULL) |
| 1678 | goto Error; |
| 1679 | PyFile_SetBufSize(self, bufsize); |
| 1680 | goto Done; |
| 1681 | |
| 1682 | Error: |
| 1683 | ret = -1; |
| 1684 | /* fall through */ |
| 1685 | Done: |
Tim Peters | 59c9a64 | 2001-09-13 05:38:56 +0000 | [diff] [blame] | 1686 | PyMem_Free(name); /* free the encoded string */ |
Tim Peters | 4441001 | 2001-09-14 03:26:08 +0000 | [diff] [blame] | 1687 | return ret; |
Tim Peters | 59c9a64 | 2001-09-13 05:38:56 +0000 | [diff] [blame] | 1688 | } |
| 1689 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1690 | PyDoc_VAR(file_doc) = |
| 1691 | PyDoc_STR( |
Tim Peters | 59c9a64 | 2001-09-13 05:38:56 +0000 | [diff] [blame] | 1692 | "file(name[, mode[, buffering]]) -> file object\n" |
| 1693 | "\n" |
| 1694 | "Open a file. The mode can be 'r', 'w' or 'a' for reading (default),\n" |
| 1695 | "writing or appending. The file will be created if it doesn't exist\n" |
| 1696 | "when opened for writing or appending; it will be truncated when\n" |
| 1697 | "opened for writing. Add a 'b' to the mode for binary files.\n" |
| 1698 | "Add a '+' to the mode to allow simultaneous reading and writing.\n" |
| 1699 | "If the buffering argument is given, 0 means unbuffered, 1 means line\n" |
Tim Peters | 742dfd6 | 2001-09-13 21:49:44 +0000 | [diff] [blame] | 1700 | "buffered, and larger numbers specify the buffer size.\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1701 | ) |
Barry Warsaw | 4be55b5 | 2002-05-22 20:37:53 +0000 | [diff] [blame] | 1702 | #ifdef WITH_UNIVERSAL_NEWLINES |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1703 | PyDoc_STR( |
Barry Warsaw | 4be55b5 | 2002-05-22 20:37:53 +0000 | [diff] [blame] | 1704 | "Add a 'U' to mode to open the file for input with universal newline\n" |
| 1705 | "support. Any line ending in the input file will be seen as a '\\n'\n" |
| 1706 | "in Python. Also, a file so opened gains the attribute 'newlines';\n" |
| 1707 | "the value for this attribute is one of None (no newline read yet),\n" |
| 1708 | "'\\r', '\\n', '\\r\\n' or a tuple containing all the newline types seen.\n" |
| 1709 | "\n" |
| 1710 | "'U' cannot be combined with 'w' or '+' mode.\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1711 | ) |
Barry Warsaw | 4be55b5 | 2002-05-22 20:37:53 +0000 | [diff] [blame] | 1712 | #endif /* WITH_UNIVERSAL_NEWLINES */ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1713 | PyDoc_STR( |
Barry Warsaw | 4be55b5 | 2002-05-22 20:37:53 +0000 | [diff] [blame] | 1714 | "\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1715 | "Note: open() is an alias for file()." |
| 1716 | ); |
Tim Peters | 59c9a64 | 2001-09-13 05:38:56 +0000 | [diff] [blame] | 1717 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1718 | PyTypeObject PyFile_Type = { |
| 1719 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1720 | 0, |
| 1721 | "file", |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1722 | sizeof(PyFileObject), |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1723 | 0, |
Guido van Rossum | 6596725 | 2001-04-21 13:20:18 +0000 | [diff] [blame] | 1724 | (destructor)file_dealloc, /* tp_dealloc */ |
| 1725 | 0, /* tp_print */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1726 | 0, /* tp_getattr */ |
| 1727 | 0, /* tp_setattr */ |
Guido van Rossum | 6596725 | 2001-04-21 13:20:18 +0000 | [diff] [blame] | 1728 | 0, /* tp_compare */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1729 | (reprfunc)file_repr, /* tp_repr */ |
Guido van Rossum | 6596725 | 2001-04-21 13:20:18 +0000 | [diff] [blame] | 1730 | 0, /* tp_as_number */ |
| 1731 | 0, /* tp_as_sequence */ |
| 1732 | 0, /* tp_as_mapping */ |
| 1733 | 0, /* tp_hash */ |
| 1734 | 0, /* tp_call */ |
| 1735 | 0, /* tp_str */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1736 | PyObject_GenericGetAttr, /* tp_getattro */ |
Guido van Rossum | 6596725 | 2001-04-21 13:20:18 +0000 | [diff] [blame] | 1737 | 0, /* tp_setattro */ |
| 1738 | 0, /* tp_as_buffer */ |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 1739 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Tim Peters | 59c9a64 | 2001-09-13 05:38:56 +0000 | [diff] [blame] | 1740 | file_doc, /* tp_doc */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1741 | 0, /* tp_traverse */ |
| 1742 | 0, /* tp_clear */ |
Guido van Rossum | 6596725 | 2001-04-21 13:20:18 +0000 | [diff] [blame] | 1743 | 0, /* tp_richcompare */ |
| 1744 | 0, /* tp_weaklistoffset */ |
Guido van Rossum | 5b02184 | 2001-05-22 16:48:37 +0000 | [diff] [blame] | 1745 | file_getiter, /* tp_iter */ |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 1746 | 0, /* tp_iternext */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1747 | file_methods, /* tp_methods */ |
| 1748 | file_memberlist, /* tp_members */ |
| 1749 | file_getsetlist, /* tp_getset */ |
| 1750 | 0, /* tp_base */ |
| 1751 | 0, /* tp_dict */ |
Tim Peters | 59c9a64 | 2001-09-13 05:38:56 +0000 | [diff] [blame] | 1752 | 0, /* tp_descr_get */ |
| 1753 | 0, /* tp_descr_set */ |
| 1754 | 0, /* tp_dictoffset */ |
Tim Peters | 4441001 | 2001-09-14 03:26:08 +0000 | [diff] [blame] | 1755 | (initproc)file_init, /* tp_init */ |
| 1756 | PyType_GenericAlloc, /* tp_alloc */ |
Tim Peters | 59c9a64 | 2001-09-13 05:38:56 +0000 | [diff] [blame] | 1757 | file_new, /* tp_new */ |
Neil Schemenauer | aa769ae | 2002-04-12 02:44:10 +0000 | [diff] [blame] | 1758 | PyObject_Del, /* tp_free */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1759 | }; |
Guido van Rossum | eb183da | 1991-04-04 10:44:06 +0000 | [diff] [blame] | 1760 | |
| 1761 | /* Interface for the 'soft space' between print items. */ |
| 1762 | |
| 1763 | int |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 1764 | PyFile_SoftSpace(PyObject *f, int newflag) |
Guido van Rossum | eb183da | 1991-04-04 10:44:06 +0000 | [diff] [blame] | 1765 | { |
| 1766 | int oldflag = 0; |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1767 | if (f == NULL) { |
| 1768 | /* Do nothing */ |
| 1769 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1770 | else if (PyFile_Check(f)) { |
| 1771 | oldflag = ((PyFileObject *)f)->f_softspace; |
| 1772 | ((PyFileObject *)f)->f_softspace = newflag; |
Guido van Rossum | eb183da | 1991-04-04 10:44:06 +0000 | [diff] [blame] | 1773 | } |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1774 | else { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1775 | PyObject *v; |
| 1776 | v = PyObject_GetAttrString(f, "softspace"); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1777 | if (v == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1778 | PyErr_Clear(); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1779 | else { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1780 | if (PyInt_Check(v)) |
| 1781 | oldflag = PyInt_AsLong(v); |
| 1782 | Py_DECREF(v); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1783 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1784 | v = PyInt_FromLong((long)newflag); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1785 | if (v == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1786 | PyErr_Clear(); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1787 | else { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1788 | if (PyObject_SetAttrString(f, "softspace", v) != 0) |
| 1789 | PyErr_Clear(); |
| 1790 | Py_DECREF(v); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1791 | } |
| 1792 | } |
Guido van Rossum | eb183da | 1991-04-04 10:44:06 +0000 | [diff] [blame] | 1793 | return oldflag; |
| 1794 | } |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1795 | |
| 1796 | /* Interfaces to write objects/strings to file-like objects */ |
| 1797 | |
| 1798 | int |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 1799 | PyFile_WriteObject(PyObject *v, PyObject *f, int flags) |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1800 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1801 | PyObject *writer, *value, *args, *result; |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1802 | if (f == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1803 | PyErr_SetString(PyExc_TypeError, "writeobject with NULL file"); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1804 | return -1; |
| 1805 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1806 | else if (PyFile_Check(f)) { |
| 1807 | FILE *fp = PyFile_AsFile(f); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1808 | if (fp == NULL) { |
| 1809 | err_closed(); |
| 1810 | return -1; |
| 1811 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1812 | return PyObject_Print(v, fp, flags); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1813 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1814 | writer = PyObject_GetAttrString(f, "write"); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1815 | if (writer == NULL) |
| 1816 | return -1; |
Martin v. Löwis | 2777c02 | 2001-09-19 13:47:32 +0000 | [diff] [blame] | 1817 | if (flags & Py_PRINT_RAW) { |
| 1818 | if (PyUnicode_Check(v)) { |
| 1819 | value = v; |
| 1820 | Py_INCREF(value); |
| 1821 | } else |
| 1822 | value = PyObject_Str(v); |
| 1823 | } |
| 1824 | else |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1825 | value = PyObject_Repr(v); |
Guido van Rossum | c600411 | 1993-11-05 10:22:19 +0000 | [diff] [blame] | 1826 | if (value == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1827 | Py_DECREF(writer); |
Guido van Rossum | c600411 | 1993-11-05 10:22:19 +0000 | [diff] [blame] | 1828 | return -1; |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1829 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1830 | args = Py_BuildValue("(O)", value); |
Guido van Rossum | e9eec54 | 1997-05-22 14:02:25 +0000 | [diff] [blame] | 1831 | if (args == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1832 | Py_DECREF(value); |
| 1833 | Py_DECREF(writer); |
Guido van Rossum | d3f9a1a | 1995-07-10 23:32:26 +0000 | [diff] [blame] | 1834 | return -1; |
| 1835 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1836 | result = PyEval_CallObject(writer, args); |
| 1837 | Py_DECREF(args); |
| 1838 | Py_DECREF(value); |
| 1839 | Py_DECREF(writer); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1840 | if (result == NULL) |
| 1841 | return -1; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1842 | Py_DECREF(result); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1843 | return 0; |
| 1844 | } |
| 1845 | |
Guido van Rossum | 27a60b1 | 1997-05-22 22:25:11 +0000 | [diff] [blame] | 1846 | int |
Tim Peters | c1bbcb8 | 2001-11-28 22:13:25 +0000 | [diff] [blame] | 1847 | PyFile_WriteString(const char *s, PyObject *f) |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1848 | { |
| 1849 | if (f == NULL) { |
Guido van Rossum | 27a60b1 | 1997-05-22 22:25:11 +0000 | [diff] [blame] | 1850 | /* Should be caused by a pre-existing error */ |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 1851 | if (!PyErr_Occurred()) |
Guido van Rossum | 27a60b1 | 1997-05-22 22:25:11 +0000 | [diff] [blame] | 1852 | PyErr_SetString(PyExc_SystemError, |
| 1853 | "null file for PyFile_WriteString"); |
| 1854 | return -1; |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1855 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1856 | else if (PyFile_Check(f)) { |
| 1857 | FILE *fp = PyFile_AsFile(f); |
Guido van Rossum | 27a60b1 | 1997-05-22 22:25:11 +0000 | [diff] [blame] | 1858 | if (fp == NULL) { |
| 1859 | err_closed(); |
| 1860 | return -1; |
| 1861 | } |
| 1862 | fputs(s, fp); |
| 1863 | return 0; |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1864 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1865 | else if (!PyErr_Occurred()) { |
| 1866 | PyObject *v = PyString_FromString(s); |
Guido van Rossum | 27a60b1 | 1997-05-22 22:25:11 +0000 | [diff] [blame] | 1867 | int err; |
| 1868 | if (v == NULL) |
| 1869 | return -1; |
| 1870 | err = PyFile_WriteObject(v, f, Py_PRINT_RAW); |
| 1871 | Py_DECREF(v); |
| 1872 | return err; |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1873 | } |
Guido van Rossum | 74ba247 | 1997-07-13 03:56:50 +0000 | [diff] [blame] | 1874 | else |
| 1875 | return -1; |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1876 | } |
Andrew M. Kuchling | 06051ed | 2000-07-13 23:56:54 +0000 | [diff] [blame] | 1877 | |
| 1878 | /* Try to get a file-descriptor from a Python object. If the object |
| 1879 | is an integer or long integer, its value is returned. If not, the |
| 1880 | object's fileno() method is called if it exists; the method must return |
| 1881 | an integer or long integer, which is returned as the file descriptor value. |
| 1882 | -1 is returned on failure. |
| 1883 | */ |
| 1884 | |
| 1885 | int PyObject_AsFileDescriptor(PyObject *o) |
| 1886 | { |
| 1887 | int fd; |
| 1888 | PyObject *meth; |
| 1889 | |
| 1890 | if (PyInt_Check(o)) { |
| 1891 | fd = PyInt_AsLong(o); |
| 1892 | } |
| 1893 | else if (PyLong_Check(o)) { |
| 1894 | fd = PyLong_AsLong(o); |
| 1895 | } |
| 1896 | else if ((meth = PyObject_GetAttrString(o, "fileno")) != NULL) |
| 1897 | { |
| 1898 | PyObject *fno = PyEval_CallObject(meth, NULL); |
| 1899 | Py_DECREF(meth); |
| 1900 | if (fno == NULL) |
| 1901 | return -1; |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 1902 | |
Andrew M. Kuchling | 06051ed | 2000-07-13 23:56:54 +0000 | [diff] [blame] | 1903 | if (PyInt_Check(fno)) { |
| 1904 | fd = PyInt_AsLong(fno); |
| 1905 | Py_DECREF(fno); |
| 1906 | } |
| 1907 | else if (PyLong_Check(fno)) { |
| 1908 | fd = PyLong_AsLong(fno); |
| 1909 | Py_DECREF(fno); |
| 1910 | } |
| 1911 | else { |
| 1912 | PyErr_SetString(PyExc_TypeError, |
| 1913 | "fileno() returned a non-integer"); |
| 1914 | Py_DECREF(fno); |
| 1915 | return -1; |
| 1916 | } |
| 1917 | } |
| 1918 | else { |
| 1919 | PyErr_SetString(PyExc_TypeError, |
| 1920 | "argument must be an int, or have a fileno() method."); |
| 1921 | return -1; |
| 1922 | } |
| 1923 | |
| 1924 | if (fd < 0) { |
| 1925 | PyErr_Format(PyExc_ValueError, |
| 1926 | "file descriptor cannot be a negative integer (%i)", |
| 1927 | fd); |
| 1928 | return -1; |
| 1929 | } |
| 1930 | return fd; |
| 1931 | } |
Jack Jansen | 7b8c754 | 2002-04-14 20:12:41 +0000 | [diff] [blame] | 1932 | |
| 1933 | #ifdef WITH_UNIVERSAL_NEWLINES |
| 1934 | /* From here on we need access to the real fgets and fread */ |
| 1935 | #undef fgets |
| 1936 | #undef fread |
| 1937 | |
| 1938 | /* |
| 1939 | ** Py_UniversalNewlineFgets is an fgets variation that understands |
| 1940 | ** all of \r, \n and \r\n conventions. |
| 1941 | ** The stream should be opened in binary mode. |
| 1942 | ** If fobj is NULL the routine always does newline conversion, and |
| 1943 | ** it may peek one char ahead to gobble the second char in \r\n. |
| 1944 | ** If fobj is non-NULL it must be a PyFileObject. In this case there |
| 1945 | ** is no readahead but in stead a flag is used to skip a following |
| 1946 | ** \n on the next read. Also, if the file is open in binary mode |
| 1947 | ** the whole conversion is skipped. Finally, the routine keeps track of |
| 1948 | ** the different types of newlines seen. |
| 1949 | ** Note that we need no error handling: fgets() treats error and eof |
| 1950 | ** identically. |
| 1951 | */ |
| 1952 | char * |
| 1953 | Py_UniversalNewlineFgets(char *buf, int n, FILE *stream, PyObject *fobj) |
| 1954 | { |
| 1955 | char *p = buf; |
| 1956 | int c; |
| 1957 | int newlinetypes = 0; |
| 1958 | int skipnextlf = 0; |
| 1959 | int univ_newline = 1; |
Tim Peters | 058b141 | 2002-04-21 07:29:14 +0000 | [diff] [blame] | 1960 | |
Jack Jansen | 7b8c754 | 2002-04-14 20:12:41 +0000 | [diff] [blame] | 1961 | if (fobj) { |
| 1962 | if (!PyFile_Check(fobj)) { |
| 1963 | errno = ENXIO; /* What can you do... */ |
| 1964 | return NULL; |
| 1965 | } |
| 1966 | univ_newline = ((PyFileObject *)fobj)->f_univ_newline; |
| 1967 | if ( !univ_newline ) |
| 1968 | return fgets(buf, n, stream); |
| 1969 | newlinetypes = ((PyFileObject *)fobj)->f_newlinetypes; |
| 1970 | skipnextlf = ((PyFileObject *)fobj)->f_skipnextlf; |
| 1971 | } |
| 1972 | FLOCKFILE(stream); |
| 1973 | c = 'x'; /* Shut up gcc warning */ |
| 1974 | while (--n > 0 && (c = GETC(stream)) != EOF ) { |
| 1975 | if (skipnextlf ) { |
| 1976 | skipnextlf = 0; |
| 1977 | if (c == '\n') { |
| 1978 | /* Seeing a \n here with skipnextlf true |
| 1979 | ** means we saw a \r before. |
| 1980 | */ |
| 1981 | newlinetypes |= NEWLINE_CRLF; |
| 1982 | c = GETC(stream); |
| 1983 | if (c == EOF) break; |
| 1984 | } else { |
| 1985 | /* |
| 1986 | ** Note that c == EOF also brings us here, |
| 1987 | ** so we're okay if the last char in the file |
| 1988 | ** is a CR. |
| 1989 | */ |
| 1990 | newlinetypes |= NEWLINE_CR; |
| 1991 | } |
| 1992 | } |
| 1993 | if (c == '\r') { |
| 1994 | /* A \r is translated into a \n, and we skip |
| 1995 | ** an adjacent \n, if any. We don't set the |
| 1996 | ** newlinetypes flag until we've seen the next char. |
| 1997 | */ |
| 1998 | skipnextlf = 1; |
| 1999 | c = '\n'; |
| 2000 | } else if ( c == '\n') { |
| 2001 | newlinetypes |= NEWLINE_LF; |
| 2002 | } |
| 2003 | *p++ = c; |
| 2004 | if (c == '\n') break; |
| 2005 | } |
| 2006 | if ( c == EOF && skipnextlf ) |
| 2007 | newlinetypes |= NEWLINE_CR; |
| 2008 | FUNLOCKFILE(stream); |
| 2009 | *p = '\0'; |
| 2010 | if (fobj) { |
| 2011 | ((PyFileObject *)fobj)->f_newlinetypes = newlinetypes; |
| 2012 | ((PyFileObject *)fobj)->f_skipnextlf = skipnextlf; |
| 2013 | } else if ( skipnextlf ) { |
| 2014 | /* If we have no file object we cannot save the |
| 2015 | ** skipnextlf flag. We have to readahead, which |
| 2016 | ** will cause a pause if we're reading from an |
| 2017 | ** interactive stream, but that is very unlikely |
| 2018 | ** unless we're doing something silly like |
| 2019 | ** execfile("/dev/tty"). |
| 2020 | */ |
| 2021 | c = GETC(stream); |
| 2022 | if ( c != '\n' ) |
| 2023 | ungetc(c, stream); |
| 2024 | } |
| 2025 | if (p == buf) |
| 2026 | return NULL; |
| 2027 | return buf; |
| 2028 | } |
| 2029 | |
| 2030 | /* |
| 2031 | ** Py_UniversalNewlineFread is an fread variation that understands |
| 2032 | ** all of \r, \n and \r\n conventions. |
| 2033 | ** The stream should be opened in binary mode. |
| 2034 | ** fobj must be a PyFileObject. In this case there |
| 2035 | ** is no readahead but in stead a flag is used to skip a following |
| 2036 | ** \n on the next read. Also, if the file is open in binary mode |
| 2037 | ** the whole conversion is skipped. Finally, the routine keeps track of |
| 2038 | ** the different types of newlines seen. |
| 2039 | */ |
| 2040 | size_t |
Tim Peters | 058b141 | 2002-04-21 07:29:14 +0000 | [diff] [blame] | 2041 | Py_UniversalNewlineFread(char *buf, size_t n, |
Jack Jansen | 7b8c754 | 2002-04-14 20:12:41 +0000 | [diff] [blame] | 2042 | FILE *stream, PyObject *fobj) |
| 2043 | { |
Tim Peters | 058b141 | 2002-04-21 07:29:14 +0000 | [diff] [blame] | 2044 | char *dst = buf; |
| 2045 | PyFileObject *f = (PyFileObject *)fobj; |
| 2046 | int newlinetypes, skipnextlf; |
| 2047 | |
| 2048 | assert(buf != NULL); |
| 2049 | assert(stream != NULL); |
| 2050 | |
Jack Jansen | 7b8c754 | 2002-04-14 20:12:41 +0000 | [diff] [blame] | 2051 | if (!fobj || !PyFile_Check(fobj)) { |
| 2052 | errno = ENXIO; /* What can you do... */ |
| 2053 | return -1; |
| 2054 | } |
Tim Peters | 058b141 | 2002-04-21 07:29:14 +0000 | [diff] [blame] | 2055 | if (!f->f_univ_newline) |
Jack Jansen | 7b8c754 | 2002-04-14 20:12:41 +0000 | [diff] [blame] | 2056 | return fread(buf, 1, n, stream); |
Tim Peters | 058b141 | 2002-04-21 07:29:14 +0000 | [diff] [blame] | 2057 | newlinetypes = f->f_newlinetypes; |
| 2058 | skipnextlf = f->f_skipnextlf; |
| 2059 | /* Invariant: n is the number of bytes remaining to be filled |
| 2060 | * in the buffer. |
| 2061 | */ |
| 2062 | while (n) { |
| 2063 | size_t nread; |
| 2064 | int shortread; |
| 2065 | char *src = dst; |
| 2066 | |
| 2067 | nread = fread(dst, 1, n, stream); |
| 2068 | assert(nread <= n); |
Tim Peters | e1682a8 | 2002-04-21 18:15:20 +0000 | [diff] [blame] | 2069 | n -= nread; /* assuming 1 byte out for each in; will adjust */ |
| 2070 | shortread = n != 0; /* true iff EOF or error */ |
Tim Peters | 058b141 | 2002-04-21 07:29:14 +0000 | [diff] [blame] | 2071 | while (nread--) { |
| 2072 | char c = *src++; |
Jack Jansen | 7b8c754 | 2002-04-14 20:12:41 +0000 | [diff] [blame] | 2073 | if (c == '\r') { |
Tim Peters | 058b141 | 2002-04-21 07:29:14 +0000 | [diff] [blame] | 2074 | /* Save as LF and set flag to skip next LF. */ |
Jack Jansen | 7b8c754 | 2002-04-14 20:12:41 +0000 | [diff] [blame] | 2075 | *dst++ = '\n'; |
| 2076 | skipnextlf = 1; |
Tim Peters | 058b141 | 2002-04-21 07:29:14 +0000 | [diff] [blame] | 2077 | } |
| 2078 | else if (skipnextlf && c == '\n') { |
| 2079 | /* Skip LF, and remember we saw CR LF. */ |
Jack Jansen | 7b8c754 | 2002-04-14 20:12:41 +0000 | [diff] [blame] | 2080 | skipnextlf = 0; |
| 2081 | newlinetypes |= NEWLINE_CRLF; |
Tim Peters | e1682a8 | 2002-04-21 18:15:20 +0000 | [diff] [blame] | 2082 | ++n; |
Tim Peters | 058b141 | 2002-04-21 07:29:14 +0000 | [diff] [blame] | 2083 | } |
| 2084 | else { |
| 2085 | /* Normal char to be stored in buffer. Also |
| 2086 | * update the newlinetypes flag if either this |
| 2087 | * is an LF or the previous char was a CR. |
| 2088 | */ |
Jack Jansen | 7b8c754 | 2002-04-14 20:12:41 +0000 | [diff] [blame] | 2089 | if (c == '\n') |
| 2090 | newlinetypes |= NEWLINE_LF; |
| 2091 | else if (skipnextlf) |
| 2092 | newlinetypes |= NEWLINE_CR; |
| 2093 | *dst++ = c; |
| 2094 | skipnextlf = 0; |
| 2095 | } |
| 2096 | } |
Tim Peters | 058b141 | 2002-04-21 07:29:14 +0000 | [diff] [blame] | 2097 | if (shortread) { |
| 2098 | /* If this is EOF, update type flags. */ |
| 2099 | if (skipnextlf && feof(stream)) |
| 2100 | newlinetypes |= NEWLINE_CR; |
| 2101 | break; |
| 2102 | } |
Jack Jansen | 7b8c754 | 2002-04-14 20:12:41 +0000 | [diff] [blame] | 2103 | } |
Tim Peters | 058b141 | 2002-04-21 07:29:14 +0000 | [diff] [blame] | 2104 | f->f_newlinetypes = newlinetypes; |
| 2105 | f->f_skipnextlf = skipnextlf; |
| 2106 | return dst - buf; |
Jack Jansen | 7b8c754 | 2002-04-14 20:12:41 +0000 | [diff] [blame] | 2107 | } |
| 2108 | #endif |