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