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