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