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