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); |
| 1215 | if (result != NULL && !PyString_Check(result)) { |
| 1216 | Py_DECREF(result); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1217 | result = NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1218 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1219 | "object.readline() returned non-string"); |
| 1220 | } |
Guido van Rossum | 4ddf0a0 | 2001-01-07 20:51:39 +0000 | [diff] [blame] | 1221 | } |
| 1222 | |
| 1223 | if (n < 0 && result != NULL && PyString_Check(result)) { |
| 1224 | char *s = PyString_AS_STRING(result); |
| 1225 | int len = PyString_GET_SIZE(result); |
| 1226 | if (len == 0) { |
| 1227 | Py_DECREF(result); |
| 1228 | result = NULL; |
| 1229 | PyErr_SetString(PyExc_EOFError, |
| 1230 | "EOF when reading a line"); |
| 1231 | } |
| 1232 | else if (s[len-1] == '\n') { |
| 1233 | if (result->ob_refcnt == 1) |
| 1234 | _PyString_Resize(&result, len-1); |
| 1235 | else { |
| 1236 | PyObject *v; |
| 1237 | v = PyString_FromStringAndSize(s, len-1); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1238 | Py_DECREF(result); |
Guido van Rossum | 4ddf0a0 | 2001-01-07 20:51:39 +0000 | [diff] [blame] | 1239 | result = v; |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1240 | } |
| 1241 | } |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1242 | } |
Guido van Rossum | 4ddf0a0 | 2001-01-07 20:51:39 +0000 | [diff] [blame] | 1243 | return result; |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 1244 | } |
| 1245 | |
| 1246 | /* Python method */ |
| 1247 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1248 | static PyObject * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 1249 | file_readline(PyFileObject *f, PyObject *args) |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 1250 | { |
Guido van Rossum | 789a161 | 1997-05-10 22:33:55 +0000 | [diff] [blame] | 1251 | int n = -1; |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 1252 | |
Guido van Rossum | d7297e6 | 1992-07-06 14:19:26 +0000 | [diff] [blame] | 1253 | if (f->f_fp == NULL) |
| 1254 | return err_closed(); |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 1255 | if (!PyArg_ParseTuple(args, "|i:readline", &n)) |
Guido van Rossum | 789a161 | 1997-05-10 22:33:55 +0000 | [diff] [blame] | 1256 | return NULL; |
| 1257 | if (n == 0) |
| 1258 | return PyString_FromString(""); |
| 1259 | if (n < 0) |
| 1260 | n = 0; |
Marc-André Lemburg | 1f46860 | 2000-07-05 15:32:40 +0000 | [diff] [blame] | 1261 | return get_line(f, n); |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 1262 | } |
| 1263 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1264 | static PyObject * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 1265 | file_readlines(PyFileObject *f, PyObject *args) |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 1266 | { |
Guido van Rossum | 789a161 | 1997-05-10 22:33:55 +0000 | [diff] [blame] | 1267 | long sizehint = 0; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1268 | PyObject *list; |
| 1269 | PyObject *line; |
Guido van Rossum | 6263d54 | 1997-05-10 22:07:25 +0000 | [diff] [blame] | 1270 | char small_buffer[SMALLCHUNK]; |
| 1271 | char *buffer = small_buffer; |
| 1272 | size_t buffersize = SMALLCHUNK; |
| 1273 | PyObject *big_buffer = NULL; |
| 1274 | size_t nfilled = 0; |
| 1275 | size_t nread; |
Guido van Rossum | 789a161 | 1997-05-10 22:33:55 +0000 | [diff] [blame] | 1276 | size_t totalread = 0; |
Guido van Rossum | 6263d54 | 1997-05-10 22:07:25 +0000 | [diff] [blame] | 1277 | char *p, *q, *end; |
| 1278 | int err; |
Guido van Rossum | 79fd0fc | 2001-10-12 20:01:53 +0000 | [diff] [blame] | 1279 | int shortread = 0; |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 1280 | |
Guido van Rossum | d7297e6 | 1992-07-06 14:19:26 +0000 | [diff] [blame] | 1281 | if (f->f_fp == NULL) |
| 1282 | return err_closed(); |
Guido van Rossum | 43713e5 | 2000-02-29 13:59:29 +0000 | [diff] [blame] | 1283 | if (!PyArg_ParseTuple(args, "|l:readlines", &sizehint)) |
Guido van Rossum | 0bd2441 | 1991-04-04 15:21:57 +0000 | [diff] [blame] | 1284 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1285 | if ((list = PyList_New(0)) == NULL) |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 1286 | return NULL; |
| 1287 | for (;;) { |
Guido van Rossum | 79fd0fc | 2001-10-12 20:01:53 +0000 | [diff] [blame] | 1288 | if (shortread) |
| 1289 | nread = 0; |
| 1290 | else { |
| 1291 | Py_BEGIN_ALLOW_THREADS |
| 1292 | errno = 0; |
Tim Peters | 058b141 | 2002-04-21 07:29:14 +0000 | [diff] [blame] | 1293 | nread = Py_UniversalNewlineFread(buffer+nfilled, |
Jack Jansen | 7b8c754 | 2002-04-14 20:12:41 +0000 | [diff] [blame] | 1294 | buffersize-nfilled, f->f_fp, (PyObject *)f); |
Guido van Rossum | 79fd0fc | 2001-10-12 20:01:53 +0000 | [diff] [blame] | 1295 | Py_END_ALLOW_THREADS |
| 1296 | shortread = (nread < buffersize-nfilled); |
| 1297 | } |
Guido van Rossum | 6263d54 | 1997-05-10 22:07:25 +0000 | [diff] [blame] | 1298 | if (nread == 0) { |
Guido van Rossum | 789a161 | 1997-05-10 22:33:55 +0000 | [diff] [blame] | 1299 | sizehint = 0; |
Guido van Rossum | 3da3fce | 1998-02-19 20:46:48 +0000 | [diff] [blame] | 1300 | if (!ferror(f->f_fp)) |
Guido van Rossum | 6263d54 | 1997-05-10 22:07:25 +0000 | [diff] [blame] | 1301 | break; |
| 1302 | PyErr_SetFromErrno(PyExc_IOError); |
| 1303 | clearerr(f->f_fp); |
| 1304 | error: |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1305 | Py_DECREF(list); |
Guido van Rossum | 6263d54 | 1997-05-10 22:07:25 +0000 | [diff] [blame] | 1306 | list = NULL; |
| 1307 | goto cleanup; |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 1308 | } |
Guido van Rossum | 789a161 | 1997-05-10 22:33:55 +0000 | [diff] [blame] | 1309 | totalread += nread; |
Guido van Rossum | 6263d54 | 1997-05-10 22:07:25 +0000 | [diff] [blame] | 1310 | p = memchr(buffer+nfilled, '\n', nread); |
| 1311 | if (p == NULL) { |
| 1312 | /* Need a larger buffer to fit this line */ |
| 1313 | nfilled += nread; |
| 1314 | buffersize *= 2; |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 1315 | if (buffersize > INT_MAX) { |
| 1316 | PyErr_SetString(PyExc_OverflowError, |
Guido van Rossum | e07d5cf | 2001-01-09 21:50:24 +0000 | [diff] [blame] | 1317 | "line is longer than a Python string can hold"); |
Trent Mick | f29f47b | 2000-08-11 19:02:59 +0000 | [diff] [blame] | 1318 | goto error; |
| 1319 | } |
Guido van Rossum | 6263d54 | 1997-05-10 22:07:25 +0000 | [diff] [blame] | 1320 | if (big_buffer == NULL) { |
| 1321 | /* Create the big buffer */ |
| 1322 | big_buffer = PyString_FromStringAndSize( |
| 1323 | NULL, buffersize); |
| 1324 | if (big_buffer == NULL) |
| 1325 | goto error; |
| 1326 | buffer = PyString_AS_STRING(big_buffer); |
| 1327 | memcpy(buffer, small_buffer, nfilled); |
| 1328 | } |
| 1329 | else { |
| 1330 | /* Grow the big buffer */ |
Jack Jansen | 7b8c754 | 2002-04-14 20:12:41 +0000 | [diff] [blame] | 1331 | if ( _PyString_Resize(&big_buffer, buffersize) < 0 ) |
| 1332 | goto error; |
Guido van Rossum | 6263d54 | 1997-05-10 22:07:25 +0000 | [diff] [blame] | 1333 | buffer = PyString_AS_STRING(big_buffer); |
| 1334 | } |
| 1335 | continue; |
| 1336 | } |
| 1337 | end = buffer+nfilled+nread; |
| 1338 | q = buffer; |
| 1339 | do { |
| 1340 | /* Process complete lines */ |
| 1341 | p++; |
| 1342 | line = PyString_FromStringAndSize(q, p-q); |
| 1343 | if (line == NULL) |
| 1344 | goto error; |
| 1345 | err = PyList_Append(list, line); |
| 1346 | Py_DECREF(line); |
| 1347 | if (err != 0) |
| 1348 | goto error; |
| 1349 | q = p; |
| 1350 | p = memchr(q, '\n', end-q); |
| 1351 | } while (p != NULL); |
| 1352 | /* Move the remaining incomplete line to the start */ |
| 1353 | nfilled = end-q; |
| 1354 | memmove(buffer, q, nfilled); |
Guido van Rossum | 789a161 | 1997-05-10 22:33:55 +0000 | [diff] [blame] | 1355 | if (sizehint > 0) |
| 1356 | if (totalread >= (size_t)sizehint) |
| 1357 | break; |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 1358 | } |
Guido van Rossum | 6263d54 | 1997-05-10 22:07:25 +0000 | [diff] [blame] | 1359 | if (nfilled != 0) { |
| 1360 | /* Partial last line */ |
| 1361 | line = PyString_FromStringAndSize(buffer, nfilled); |
| 1362 | if (line == NULL) |
| 1363 | goto error; |
Guido van Rossum | 789a161 | 1997-05-10 22:33:55 +0000 | [diff] [blame] | 1364 | if (sizehint > 0) { |
| 1365 | /* Need to complete the last line */ |
Marc-André Lemburg | 1f46860 | 2000-07-05 15:32:40 +0000 | [diff] [blame] | 1366 | PyObject *rest = get_line(f, 0); |
Guido van Rossum | 789a161 | 1997-05-10 22:33:55 +0000 | [diff] [blame] | 1367 | if (rest == NULL) { |
| 1368 | Py_DECREF(line); |
| 1369 | goto error; |
| 1370 | } |
| 1371 | PyString_Concat(&line, rest); |
| 1372 | Py_DECREF(rest); |
| 1373 | if (line == NULL) |
| 1374 | goto error; |
| 1375 | } |
Guido van Rossum | 6263d54 | 1997-05-10 22:07:25 +0000 | [diff] [blame] | 1376 | err = PyList_Append(list, line); |
| 1377 | Py_DECREF(line); |
| 1378 | if (err != 0) |
| 1379 | goto error; |
| 1380 | } |
| 1381 | cleanup: |
Tim Peters | 5de9842 | 2002-04-27 18:44:32 +0000 | [diff] [blame] | 1382 | Py_XDECREF(big_buffer); |
Guido van Rossum | ce5ba84 | 1991-03-06 13:06:18 +0000 | [diff] [blame] | 1383 | return list; |
| 1384 | } |
| 1385 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1386 | static PyObject * |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 1387 | file_write(PyFileObject *f, PyObject *args) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1388 | { |
Guido van Rossum | d7297e6 | 1992-07-06 14:19:26 +0000 | [diff] [blame] | 1389 | char *s; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1390 | int n, n2; |
Guido van Rossum | d7297e6 | 1992-07-06 14:19:26 +0000 | [diff] [blame] | 1391 | if (f->f_fp == NULL) |
| 1392 | return err_closed(); |
Michael W. Hudson | e2ec3eb | 2001-10-31 18:51:01 +0000 | [diff] [blame] | 1393 | if (!PyArg_ParseTuple(args, f->f_binary ? "s#" : "t#", &s, &n)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1394 | return NULL; |
Guido van Rossum | eb183da | 1991-04-04 10:44:06 +0000 | [diff] [blame] | 1395 | f->f_softspace = 0; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1396 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1397 | errno = 0; |
Guido van Rossum | d7297e6 | 1992-07-06 14:19:26 +0000 | [diff] [blame] | 1398 | n2 = fwrite(s, 1, n, f->f_fp); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1399 | Py_END_ALLOW_THREADS |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1400 | if (n2 != n) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1401 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | febd551 | 1992-03-04 16:39:24 +0000 | [diff] [blame] | 1402 | clearerr(f->f_fp); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1403 | return NULL; |
| 1404 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1405 | Py_INCREF(Py_None); |
| 1406 | return Py_None; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1407 | } |
| 1408 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1409 | static PyObject * |
Tim Peters | 2c9aa5e | 2001-09-23 04:06:05 +0000 | [diff] [blame] | 1410 | file_writelines(PyFileObject *f, PyObject *seq) |
Guido van Rossum | 5a2a683 | 1993-10-25 09:59:04 +0000 | [diff] [blame] | 1411 | { |
Guido van Rossum | ee70ad1 | 2000-03-13 16:27:06 +0000 | [diff] [blame] | 1412 | #define CHUNKSIZE 1000 |
| 1413 | PyObject *list, *line; |
Tim Peters | 2c9aa5e | 2001-09-23 04:06:05 +0000 | [diff] [blame] | 1414 | PyObject *it; /* iter(seq) */ |
Guido van Rossum | ee70ad1 | 2000-03-13 16:27:06 +0000 | [diff] [blame] | 1415 | PyObject *result; |
| 1416 | int i, j, index, len, nwritten, islist; |
| 1417 | |
Tim Peters | 2c9aa5e | 2001-09-23 04:06:05 +0000 | [diff] [blame] | 1418 | assert(seq != NULL); |
Guido van Rossum | 5a2a683 | 1993-10-25 09:59:04 +0000 | [diff] [blame] | 1419 | if (f->f_fp == NULL) |
| 1420 | return err_closed(); |
Tim Peters | 2c9aa5e | 2001-09-23 04:06:05 +0000 | [diff] [blame] | 1421 | |
| 1422 | result = NULL; |
| 1423 | list = NULL; |
| 1424 | islist = PyList_Check(seq); |
| 1425 | if (islist) |
| 1426 | it = NULL; |
| 1427 | else { |
| 1428 | it = PyObject_GetIter(seq); |
| 1429 | if (it == NULL) { |
| 1430 | PyErr_SetString(PyExc_TypeError, |
| 1431 | "writelines() requires an iterable argument"); |
| 1432 | return NULL; |
| 1433 | } |
| 1434 | /* From here on, fail by going to error, to reclaim "it". */ |
| 1435 | list = PyList_New(CHUNKSIZE); |
| 1436 | if (list == NULL) |
| 1437 | goto error; |
Guido van Rossum | 5a2a683 | 1993-10-25 09:59:04 +0000 | [diff] [blame] | 1438 | } |
Guido van Rossum | ee70ad1 | 2000-03-13 16:27:06 +0000 | [diff] [blame] | 1439 | |
| 1440 | /* Strategy: slurp CHUNKSIZE lines into a private list, |
| 1441 | checking that they are all strings, then write that list |
| 1442 | without holding the interpreter lock, then come back for more. */ |
Tim Peters | 2c9aa5e | 2001-09-23 04:06:05 +0000 | [diff] [blame] | 1443 | for (index = 0; ; index += CHUNKSIZE) { |
Guido van Rossum | ee70ad1 | 2000-03-13 16:27:06 +0000 | [diff] [blame] | 1444 | if (islist) { |
| 1445 | Py_XDECREF(list); |
Tim Peters | 2c9aa5e | 2001-09-23 04:06:05 +0000 | [diff] [blame] | 1446 | list = PyList_GetSlice(seq, index, index+CHUNKSIZE); |
Guido van Rossum | ee70ad1 | 2000-03-13 16:27:06 +0000 | [diff] [blame] | 1447 | if (list == NULL) |
Tim Peters | 2c9aa5e | 2001-09-23 04:06:05 +0000 | [diff] [blame] | 1448 | goto error; |
Guido van Rossum | ee70ad1 | 2000-03-13 16:27:06 +0000 | [diff] [blame] | 1449 | j = PyList_GET_SIZE(list); |
| 1450 | } |
| 1451 | else { |
| 1452 | for (j = 0; j < CHUNKSIZE; j++) { |
Tim Peters | 2c9aa5e | 2001-09-23 04:06:05 +0000 | [diff] [blame] | 1453 | line = PyIter_Next(it); |
Guido van Rossum | ee70ad1 | 2000-03-13 16:27:06 +0000 | [diff] [blame] | 1454 | if (line == NULL) { |
Tim Peters | 2c9aa5e | 2001-09-23 04:06:05 +0000 | [diff] [blame] | 1455 | if (PyErr_Occurred()) |
| 1456 | goto error; |
| 1457 | break; |
Guido van Rossum | ee70ad1 | 2000-03-13 16:27:06 +0000 | [diff] [blame] | 1458 | } |
Guido van Rossum | ee70ad1 | 2000-03-13 16:27:06 +0000 | [diff] [blame] | 1459 | PyList_SetItem(list, j, line); |
| 1460 | } |
| 1461 | } |
| 1462 | if (j == 0) |
| 1463 | break; |
| 1464 | |
Marc-André Lemburg | 6ef68b5 | 2000-08-25 22:39:50 +0000 | [diff] [blame] | 1465 | /* Check that all entries are indeed strings. If not, |
| 1466 | apply the same rules as for file.write() and |
| 1467 | convert the results to strings. This is slow, but |
| 1468 | seems to be the only way since all conversion APIs |
| 1469 | could potentially execute Python code. */ |
| 1470 | for (i = 0; i < j; i++) { |
| 1471 | PyObject *v = PyList_GET_ITEM(list, i); |
| 1472 | if (!PyString_Check(v)) { |
| 1473 | const char *buffer; |
| 1474 | int len; |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 1475 | if (((f->f_binary && |
Marc-André Lemburg | 6ef68b5 | 2000-08-25 22:39:50 +0000 | [diff] [blame] | 1476 | PyObject_AsReadBuffer(v, |
| 1477 | (const void**)&buffer, |
| 1478 | &len)) || |
| 1479 | PyObject_AsCharBuffer(v, |
| 1480 | &buffer, |
| 1481 | &len))) { |
| 1482 | PyErr_SetString(PyExc_TypeError, |
Jeremy Hylton | 8b73542 | 2002-08-14 21:01:41 +0000 | [diff] [blame] | 1483 | "writelines() argument must be a sequence of strings"); |
Marc-André Lemburg | 6ef68b5 | 2000-08-25 22:39:50 +0000 | [diff] [blame] | 1484 | goto error; |
| 1485 | } |
| 1486 | line = PyString_FromStringAndSize(buffer, |
| 1487 | len); |
| 1488 | if (line == NULL) |
| 1489 | goto error; |
| 1490 | Py_DECREF(v); |
Marc-André Lemburg | f5e96fa | 2000-08-25 22:49:05 +0000 | [diff] [blame] | 1491 | PyList_SET_ITEM(list, i, line); |
Marc-André Lemburg | 6ef68b5 | 2000-08-25 22:39:50 +0000 | [diff] [blame] | 1492 | } |
| 1493 | } |
| 1494 | |
| 1495 | /* Since we are releasing the global lock, the |
| 1496 | following code may *not* execute Python code. */ |
Guido van Rossum | ee70ad1 | 2000-03-13 16:27:06 +0000 | [diff] [blame] | 1497 | Py_BEGIN_ALLOW_THREADS |
| 1498 | f->f_softspace = 0; |
| 1499 | errno = 0; |
| 1500 | for (i = 0; i < j; i++) { |
Marc-André Lemburg | 6ef68b5 | 2000-08-25 22:39:50 +0000 | [diff] [blame] | 1501 | line = PyList_GET_ITEM(list, i); |
Guido van Rossum | ee70ad1 | 2000-03-13 16:27:06 +0000 | [diff] [blame] | 1502 | len = PyString_GET_SIZE(line); |
| 1503 | nwritten = fwrite(PyString_AS_STRING(line), |
| 1504 | 1, len, f->f_fp); |
| 1505 | if (nwritten != len) { |
| 1506 | Py_BLOCK_THREADS |
| 1507 | PyErr_SetFromErrno(PyExc_IOError); |
| 1508 | clearerr(f->f_fp); |
| 1509 | goto error; |
| 1510 | } |
| 1511 | } |
| 1512 | Py_END_ALLOW_THREADS |
| 1513 | |
| 1514 | if (j < CHUNKSIZE) |
| 1515 | break; |
Guido van Rossum | ee70ad1 | 2000-03-13 16:27:06 +0000 | [diff] [blame] | 1516 | } |
| 1517 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1518 | Py_INCREF(Py_None); |
Guido van Rossum | ee70ad1 | 2000-03-13 16:27:06 +0000 | [diff] [blame] | 1519 | result = Py_None; |
| 1520 | error: |
| 1521 | Py_XDECREF(list); |
Tim Peters | 2c9aa5e | 2001-09-23 04:06:05 +0000 | [diff] [blame] | 1522 | Py_XDECREF(it); |
Guido van Rossum | ee70ad1 | 2000-03-13 16:27:06 +0000 | [diff] [blame] | 1523 | return result; |
Tim Peters | 2c9aa5e | 2001-09-23 04:06:05 +0000 | [diff] [blame] | 1524 | #undef CHUNKSIZE |
Guido van Rossum | 5a2a683 | 1993-10-25 09:59:04 +0000 | [diff] [blame] | 1525 | } |
| 1526 | |
Guido van Rossum | 7a6e959 | 2002-08-06 15:55:28 +0000 | [diff] [blame] | 1527 | static PyObject * |
| 1528 | file_getiter(PyFileObject *f) |
| 1529 | { |
| 1530 | if (f->f_fp == NULL) |
| 1531 | return err_closed(); |
| 1532 | Py_INCREF(f); |
| 1533 | return (PyObject *)f; |
| 1534 | } |
| 1535 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1536 | PyDoc_STRVAR(readline_doc, |
Tim Peters | efc3a3a | 2001-09-20 07:55:22 +0000 | [diff] [blame] | 1537 | "readline([size]) -> next line from the file, as a string.\n" |
| 1538 | "\n" |
| 1539 | "Retain newline. A non-negative size argument limits the maximum\n" |
| 1540 | "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] | 1541 | "Return an empty string at EOF."); |
Tim Peters | efc3a3a | 2001-09-20 07:55:22 +0000 | [diff] [blame] | 1542 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1543 | PyDoc_STRVAR(read_doc, |
Tim Peters | efc3a3a | 2001-09-20 07:55:22 +0000 | [diff] [blame] | 1544 | "read([size]) -> read at most size bytes, returned as a string.\n" |
| 1545 | "\n" |
Gustavo Niemeyer | 786ddb2 | 2002-12-16 18:12:53 +0000 | [diff] [blame] | 1546 | "If the size argument is negative or omitted, read until EOF is reached.\n" |
| 1547 | "Notice that when in non-blocking mode, less data than what was requested\n" |
| 1548 | "may be returned, even if no size parameter was given."); |
Tim Peters | efc3a3a | 2001-09-20 07:55:22 +0000 | [diff] [blame] | 1549 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1550 | PyDoc_STRVAR(write_doc, |
Tim Peters | efc3a3a | 2001-09-20 07:55:22 +0000 | [diff] [blame] | 1551 | "write(str) -> None. Write string str to file.\n" |
| 1552 | "\n" |
| 1553 | "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] | 1554 | "the file on disk reflects the data written."); |
Tim Peters | efc3a3a | 2001-09-20 07:55:22 +0000 | [diff] [blame] | 1555 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1556 | PyDoc_STRVAR(fileno_doc, |
Tim Peters | efc3a3a | 2001-09-20 07:55:22 +0000 | [diff] [blame] | 1557 | "fileno() -> integer \"file descriptor\".\n" |
| 1558 | "\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1559 | "This is needed for lower-level file interfaces, such os.read()."); |
Tim Peters | efc3a3a | 2001-09-20 07:55:22 +0000 | [diff] [blame] | 1560 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1561 | PyDoc_STRVAR(seek_doc, |
Tim Peters | efc3a3a | 2001-09-20 07:55:22 +0000 | [diff] [blame] | 1562 | "seek(offset[, whence]) -> None. Move to new file position.\n" |
| 1563 | "\n" |
| 1564 | "Argument offset is a byte count. Optional argument whence defaults to\n" |
| 1565 | "0 (offset from start of file, offset should be >= 0); other values are 1\n" |
| 1566 | "(move relative to current position, positive or negative), and 2 (move\n" |
| 1567 | "relative to end of file, usually negative, although many platforms allow\n" |
| 1568 | "seeking beyond the end of a file).\n" |
| 1569 | "\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1570 | "Note that not all file objects are seekable."); |
Tim Peters | efc3a3a | 2001-09-20 07:55:22 +0000 | [diff] [blame] | 1571 | |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 1572 | #ifdef HAVE_FTRUNCATE |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1573 | PyDoc_STRVAR(truncate_doc, |
Tim Peters | efc3a3a | 2001-09-20 07:55:22 +0000 | [diff] [blame] | 1574 | "truncate([size]) -> None. Truncate the file to at most size bytes.\n" |
| 1575 | "\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1576 | "Size defaults to the current file position, as returned by tell()."); |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 1577 | #endif |
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(tell_doc, |
| 1580 | "tell() -> current file position, an integer (may be a long integer)."); |
Tim Peters | efc3a3a | 2001-09-20 07:55:22 +0000 | [diff] [blame] | 1581 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1582 | PyDoc_STRVAR(readinto_doc, |
| 1583 | "readinto() -> Undocumented. Don't use this; it may go away."); |
Tim Peters | efc3a3a | 2001-09-20 07:55:22 +0000 | [diff] [blame] | 1584 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1585 | PyDoc_STRVAR(readlines_doc, |
Tim Peters | efc3a3a | 2001-09-20 07:55:22 +0000 | [diff] [blame] | 1586 | "readlines([size]) -> list of strings, each a line from the file.\n" |
| 1587 | "\n" |
| 1588 | "Call readline() repeatedly and return a list of the lines so read.\n" |
| 1589 | "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] | 1590 | "total number of bytes in the lines returned."); |
Tim Peters | efc3a3a | 2001-09-20 07:55:22 +0000 | [diff] [blame] | 1591 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1592 | PyDoc_STRVAR(xreadlines_doc, |
Guido van Rossum | 7a6e959 | 2002-08-06 15:55:28 +0000 | [diff] [blame] | 1593 | "xreadlines() -> returns self.\n" |
Tim Peters | efc3a3a | 2001-09-20 07:55:22 +0000 | [diff] [blame] | 1594 | "\n" |
Guido van Rossum | 7a6e959 | 2002-08-06 15:55:28 +0000 | [diff] [blame] | 1595 | "For backward compatibility. File objects now include the performance\n" |
| 1596 | "optimizations previously implemented in the xreadlines module."); |
Tim Peters | efc3a3a | 2001-09-20 07:55:22 +0000 | [diff] [blame] | 1597 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1598 | PyDoc_STRVAR(writelines_doc, |
Tim Peters | 2c9aa5e | 2001-09-23 04:06:05 +0000 | [diff] [blame] | 1599 | "writelines(sequence_of_strings) -> None. Write the strings to the file.\n" |
Tim Peters | efc3a3a | 2001-09-20 07:55:22 +0000 | [diff] [blame] | 1600 | "\n" |
Tim Peters | 2c9aa5e | 2001-09-23 04:06:05 +0000 | [diff] [blame] | 1601 | "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] | 1602 | "producing strings. This is equivalent to calling write() for each string."); |
Tim Peters | efc3a3a | 2001-09-20 07:55:22 +0000 | [diff] [blame] | 1603 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1604 | PyDoc_STRVAR(flush_doc, |
| 1605 | "flush() -> None. Flush the internal I/O buffer."); |
Tim Peters | efc3a3a | 2001-09-20 07:55:22 +0000 | [diff] [blame] | 1606 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1607 | PyDoc_STRVAR(close_doc, |
Tim Peters | efc3a3a | 2001-09-20 07:55:22 +0000 | [diff] [blame] | 1608 | "close() -> None or (perhaps) an integer. Close the file.\n" |
| 1609 | "\n" |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 1610 | "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] | 1611 | "further I/O operations. close() may be called more than once without\n" |
| 1612 | "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] | 1613 | "may return an exit status upon closing."); |
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(isatty_doc, |
| 1616 | "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] | 1617 | |
| 1618 | static PyMethodDef file_methods[] = { |
Jeremy Hylton | 8b73542 | 2002-08-14 21:01:41 +0000 | [diff] [blame] | 1619 | {"readline", (PyCFunction)file_readline, METH_VARARGS, readline_doc}, |
| 1620 | {"read", (PyCFunction)file_read, METH_VARARGS, read_doc}, |
| 1621 | {"write", (PyCFunction)file_write, METH_VARARGS, write_doc}, |
| 1622 | {"fileno", (PyCFunction)file_fileno, METH_NOARGS, fileno_doc}, |
| 1623 | {"seek", (PyCFunction)file_seek, METH_VARARGS, seek_doc}, |
Tim Peters | efc3a3a | 2001-09-20 07:55:22 +0000 | [diff] [blame] | 1624 | #ifdef HAVE_FTRUNCATE |
Jeremy Hylton | 8b73542 | 2002-08-14 21:01:41 +0000 | [diff] [blame] | 1625 | {"truncate", (PyCFunction)file_truncate, METH_VARARGS, truncate_doc}, |
Tim Peters | efc3a3a | 2001-09-20 07:55:22 +0000 | [diff] [blame] | 1626 | #endif |
Jeremy Hylton | 8b73542 | 2002-08-14 21:01:41 +0000 | [diff] [blame] | 1627 | {"tell", (PyCFunction)file_tell, METH_NOARGS, tell_doc}, |
| 1628 | {"readinto", (PyCFunction)file_readinto, METH_VARARGS, readinto_doc}, |
| 1629 | {"readlines", (PyCFunction)file_readlines,METH_VARARGS, readlines_doc}, |
| 1630 | {"xreadlines",(PyCFunction)file_getiter, METH_NOARGS, xreadlines_doc}, |
| 1631 | {"writelines",(PyCFunction)file_writelines, METH_O, writelines_doc}, |
| 1632 | {"flush", (PyCFunction)file_flush, METH_NOARGS, flush_doc}, |
| 1633 | {"close", (PyCFunction)file_close, METH_NOARGS, close_doc}, |
| 1634 | {"isatty", (PyCFunction)file_isatty, METH_NOARGS, isatty_doc}, |
| 1635 | {NULL, NULL} /* sentinel */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1636 | }; |
| 1637 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1638 | #define OFF(x) offsetof(PyFileObject, x) |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1639 | |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 1640 | static PyMemberDef file_memberlist[] = { |
| 1641 | {"softspace", T_INT, OFF(f_softspace), 0, |
| 1642 | "flag indicating that a space needs to be printed; used by print"}, |
| 1643 | {"mode", T_OBJECT, OFF(f_mode), RO, |
Martin v. Löwis | 6233c9b | 2002-12-11 13:06:53 +0000 | [diff] [blame] | 1644 | "file mode ('r', 'U', 'w', 'a', possibly with 'b' or '+' added)"}, |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 1645 | {"name", T_OBJECT, OFF(f_name), RO, |
| 1646 | "file name"}, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1647 | /* getattr(f, "closed") is implemented without this table */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1648 | {NULL} /* Sentinel */ |
| 1649 | }; |
| 1650 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1651 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1652 | get_closed(PyFileObject *f, void *closure) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1653 | { |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 1654 | return PyBool_FromLong((long)(f->f_fp == 0)); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1655 | } |
Jack Jansen | 7b8c754 | 2002-04-14 20:12:41 +0000 | [diff] [blame] | 1656 | #ifdef WITH_UNIVERSAL_NEWLINES |
| 1657 | static PyObject * |
| 1658 | get_newlines(PyFileObject *f, void *closure) |
| 1659 | { |
| 1660 | switch (f->f_newlinetypes) { |
| 1661 | case NEWLINE_UNKNOWN: |
| 1662 | Py_INCREF(Py_None); |
| 1663 | return Py_None; |
| 1664 | case NEWLINE_CR: |
| 1665 | return PyString_FromString("\r"); |
| 1666 | case NEWLINE_LF: |
| 1667 | return PyString_FromString("\n"); |
| 1668 | case NEWLINE_CR|NEWLINE_LF: |
| 1669 | return Py_BuildValue("(ss)", "\r", "\n"); |
| 1670 | case NEWLINE_CRLF: |
| 1671 | return PyString_FromString("\r\n"); |
| 1672 | case NEWLINE_CR|NEWLINE_CRLF: |
| 1673 | return Py_BuildValue("(ss)", "\r", "\r\n"); |
| 1674 | case NEWLINE_LF|NEWLINE_CRLF: |
| 1675 | return Py_BuildValue("(ss)", "\n", "\r\n"); |
| 1676 | case NEWLINE_CR|NEWLINE_LF|NEWLINE_CRLF: |
| 1677 | return Py_BuildValue("(sss)", "\r", "\n", "\r\n"); |
| 1678 | default: |
Jeremy Hylton | 8b73542 | 2002-08-14 21:01:41 +0000 | [diff] [blame] | 1679 | PyErr_Format(PyExc_SystemError, |
| 1680 | "Unknown newlines value 0x%x\n", |
| 1681 | f->f_newlinetypes); |
Jack Jansen | 7b8c754 | 2002-04-14 20:12:41 +0000 | [diff] [blame] | 1682 | return NULL; |
| 1683 | } |
| 1684 | } |
| 1685 | #endif |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1686 | |
Guido van Rossum | 32d34c8 | 2001-09-20 21:45:26 +0000 | [diff] [blame] | 1687 | static PyGetSetDef file_getsetlist[] = { |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 1688 | {"closed", (getter)get_closed, NULL, "True if the file is closed"}, |
Jack Jansen | 7b8c754 | 2002-04-14 20:12:41 +0000 | [diff] [blame] | 1689 | #ifdef WITH_UNIVERSAL_NEWLINES |
Jeremy Hylton | 8b73542 | 2002-08-14 21:01:41 +0000 | [diff] [blame] | 1690 | {"newlines", (getter)get_newlines, NULL, |
| 1691 | "end-of-line convention used in this file"}, |
Jack Jansen | 7b8c754 | 2002-04-14 20:12:41 +0000 | [diff] [blame] | 1692 | #endif |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1693 | {0}, |
| 1694 | }; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1695 | |
Neal Norwitz | d8b995f | 2002-08-06 21:50:54 +0000 | [diff] [blame] | 1696 | static void |
Guido van Rossum | 7a6e959 | 2002-08-06 15:55:28 +0000 | [diff] [blame] | 1697 | drop_readahead(PyFileObject *f) |
Guido van Rossum | 6596725 | 2001-04-21 13:20:18 +0000 | [diff] [blame] | 1698 | { |
Guido van Rossum | 7a6e959 | 2002-08-06 15:55:28 +0000 | [diff] [blame] | 1699 | if (f->f_buf != NULL) { |
| 1700 | PyMem_Free(f->f_buf); |
| 1701 | f->f_buf = NULL; |
| 1702 | } |
Guido van Rossum | 6596725 | 2001-04-21 13:20:18 +0000 | [diff] [blame] | 1703 | } |
| 1704 | |
Guido van Rossum | 7a6e959 | 2002-08-06 15:55:28 +0000 | [diff] [blame] | 1705 | /* Make sure that file has a readahead buffer with at least one byte |
| 1706 | (unless at EOF) and no more than bufsize. Returns negative value on |
| 1707 | error */ |
Neal Norwitz | d8b995f | 2002-08-06 21:50:54 +0000 | [diff] [blame] | 1708 | static int |
| 1709 | readahead(PyFileObject *f, int bufsize) |
| 1710 | { |
Guido van Rossum | 7a6e959 | 2002-08-06 15:55:28 +0000 | [diff] [blame] | 1711 | int chunksize; |
| 1712 | |
| 1713 | if (f->f_buf != NULL) { |
| 1714 | if( (f->f_bufend - f->f_bufptr) >= 1) |
| 1715 | return 0; |
| 1716 | else |
| 1717 | drop_readahead(f); |
| 1718 | } |
| 1719 | if ((f->f_buf = PyMem_Malloc(bufsize)) == NULL) { |
| 1720 | return -1; |
| 1721 | } |
| 1722 | Py_BEGIN_ALLOW_THREADS |
| 1723 | errno = 0; |
| 1724 | chunksize = Py_UniversalNewlineFread( |
| 1725 | f->f_buf, bufsize, f->f_fp, (PyObject *)f); |
| 1726 | Py_END_ALLOW_THREADS |
| 1727 | if (chunksize == 0) { |
| 1728 | if (ferror(f->f_fp)) { |
| 1729 | PyErr_SetFromErrno(PyExc_IOError); |
| 1730 | clearerr(f->f_fp); |
| 1731 | drop_readahead(f); |
| 1732 | return -1; |
| 1733 | } |
| 1734 | } |
| 1735 | f->f_bufptr = f->f_buf; |
| 1736 | f->f_bufend = f->f_buf + chunksize; |
| 1737 | return 0; |
| 1738 | } |
| 1739 | |
| 1740 | /* Used by file_iternext. The returned string will start with 'skip' |
| 1741 | uninitialized bytes followed by the remainder of the line. Don't be |
| 1742 | horrified by the recursive call: maximum recursion depth is limited by |
| 1743 | logarithmic buffer growth to about 50 even when reading a 1gb line. */ |
| 1744 | |
Neal Norwitz | d8b995f | 2002-08-06 21:50:54 +0000 | [diff] [blame] | 1745 | static PyStringObject * |
| 1746 | readahead_get_line_skip(PyFileObject *f, int skip, int bufsize) |
| 1747 | { |
Guido van Rossum | 7a6e959 | 2002-08-06 15:55:28 +0000 | [diff] [blame] | 1748 | PyStringObject* s; |
| 1749 | char *bufptr; |
| 1750 | char *buf; |
| 1751 | int len; |
| 1752 | |
| 1753 | if (f->f_buf == NULL) |
| 1754 | if (readahead(f, bufsize) < 0) |
| 1755 | return NULL; |
| 1756 | |
| 1757 | len = f->f_bufend - f->f_bufptr; |
| 1758 | if (len == 0) |
| 1759 | return (PyStringObject *) |
| 1760 | PyString_FromStringAndSize(NULL, skip); |
| 1761 | bufptr = memchr(f->f_bufptr, '\n', len); |
| 1762 | if (bufptr != NULL) { |
| 1763 | bufptr++; /* Count the '\n' */ |
| 1764 | len = bufptr - f->f_bufptr; |
| 1765 | s = (PyStringObject *) |
| 1766 | PyString_FromStringAndSize(NULL, skip+len); |
| 1767 | if (s == NULL) |
| 1768 | return NULL; |
| 1769 | memcpy(PyString_AS_STRING(s)+skip, f->f_bufptr, len); |
| 1770 | f->f_bufptr = bufptr; |
| 1771 | if (bufptr == f->f_bufend) |
| 1772 | drop_readahead(f); |
| 1773 | } else { |
| 1774 | bufptr = f->f_bufptr; |
| 1775 | buf = f->f_buf; |
| 1776 | f->f_buf = NULL; /* Force new readahead buffer */ |
| 1777 | s = readahead_get_line_skip( |
| 1778 | f, skip+len, bufsize + (bufsize>>2) ); |
| 1779 | if (s == NULL) { |
| 1780 | PyMem_Free(buf); |
| 1781 | return NULL; |
| 1782 | } |
| 1783 | memcpy(PyString_AS_STRING(s)+skip, bufptr, len); |
| 1784 | PyMem_Free(buf); |
| 1785 | } |
| 1786 | return s; |
| 1787 | } |
| 1788 | |
| 1789 | /* A larger buffer size may actually decrease performance. */ |
| 1790 | #define READAHEAD_BUFSIZE 8192 |
| 1791 | |
| 1792 | static PyObject * |
| 1793 | file_iternext(PyFileObject *f) |
| 1794 | { |
| 1795 | PyStringObject* l; |
| 1796 | |
Guido van Rossum | 7a6e959 | 2002-08-06 15:55:28 +0000 | [diff] [blame] | 1797 | if (f->f_fp == NULL) |
| 1798 | return err_closed(); |
| 1799 | |
Guido van Rossum | 7a6e959 | 2002-08-06 15:55:28 +0000 | [diff] [blame] | 1800 | l = readahead_get_line_skip(f, 0, READAHEAD_BUFSIZE); |
| 1801 | if (l == NULL || PyString_GET_SIZE(l) == 0) { |
| 1802 | Py_XDECREF(l); |
| 1803 | return NULL; |
| 1804 | } |
| 1805 | return (PyObject *)l; |
| 1806 | } |
| 1807 | |
| 1808 | |
Tim Peters | 59c9a64 | 2001-09-13 05:38:56 +0000 | [diff] [blame] | 1809 | static PyObject * |
| 1810 | file_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 1811 | { |
Tim Peters | 4441001 | 2001-09-14 03:26:08 +0000 | [diff] [blame] | 1812 | PyObject *self; |
| 1813 | static PyObject *not_yet_string; |
| 1814 | |
| 1815 | assert(type != NULL && type->tp_alloc != NULL); |
| 1816 | |
| 1817 | if (not_yet_string == NULL) { |
| 1818 | not_yet_string = PyString_FromString("<uninitialized file>"); |
| 1819 | if (not_yet_string == NULL) |
| 1820 | return NULL; |
| 1821 | } |
| 1822 | |
| 1823 | self = type->tp_alloc(type, 0); |
| 1824 | if (self != NULL) { |
| 1825 | /* Always fill in the name and mode, so that nobody else |
| 1826 | needs to special-case NULLs there. */ |
| 1827 | Py_INCREF(not_yet_string); |
| 1828 | ((PyFileObject *)self)->f_name = not_yet_string; |
| 1829 | Py_INCREF(not_yet_string); |
| 1830 | ((PyFileObject *)self)->f_mode = not_yet_string; |
| 1831 | } |
| 1832 | return self; |
| 1833 | } |
| 1834 | |
| 1835 | static int |
| 1836 | file_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 1837 | { |
| 1838 | PyFileObject *foself = (PyFileObject *)self; |
| 1839 | int ret = 0; |
Tim Peters | 59c9a64 | 2001-09-13 05:38:56 +0000 | [diff] [blame] | 1840 | static char *kwlist[] = {"name", "mode", "buffering", 0}; |
| 1841 | char *name = NULL; |
| 1842 | char *mode = "r"; |
| 1843 | int bufsize = -1; |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1844 | int wideargument = 0; |
Tim Peters | 4441001 | 2001-09-14 03:26:08 +0000 | [diff] [blame] | 1845 | |
| 1846 | assert(PyFile_Check(self)); |
| 1847 | if (foself->f_fp != NULL) { |
| 1848 | /* Have to close the existing file first. */ |
| 1849 | PyObject *closeresult = file_close(foself); |
| 1850 | if (closeresult == NULL) |
| 1851 | return -1; |
| 1852 | Py_DECREF(closeresult); |
| 1853 | } |
Tim Peters | 59c9a64 | 2001-09-13 05:38:56 +0000 | [diff] [blame] | 1854 | |
Mark Hammond | c2e85bd | 2002-10-03 05:10:39 +0000 | [diff] [blame] | 1855 | #ifdef Py_WIN_WIDE_FILENAMES |
| 1856 | if (GetVersion() < 0x80000000) { /* On NT, so wide API available */ |
| 1857 | PyObject *po; |
| 1858 | if (PyArg_ParseTupleAndKeywords(args, kwds, "U|si:file", |
| 1859 | kwlist, &po, &mode, &bufsize)) { |
| 1860 | wideargument = 1; |
| 1861 | if (fill_file_fields(foself, NULL, name, mode, |
| 1862 | fclose, po) == NULL) |
| 1863 | goto Error; |
| 1864 | } else { |
| 1865 | /* Drop the argument parsing error as narrow |
| 1866 | strings are also valid. */ |
| 1867 | PyErr_Clear(); |
| 1868 | } |
| 1869 | } |
| 1870 | #endif |
| 1871 | |
| 1872 | if (!wideargument) { |
| 1873 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "et|si:file", kwlist, |
| 1874 | Py_FileSystemDefaultEncoding, |
| 1875 | &name, |
| 1876 | &mode, &bufsize)) |
| 1877 | return -1; |
| 1878 | if (fill_file_fields(foself, NULL, name, mode, |
| 1879 | fclose, NULL) == NULL) |
| 1880 | goto Error; |
| 1881 | } |
Tim Peters | 4441001 | 2001-09-14 03:26:08 +0000 | [diff] [blame] | 1882 | if (open_the_file(foself, name, mode) == NULL) |
| 1883 | goto Error; |
| 1884 | PyFile_SetBufSize(self, bufsize); |
| 1885 | goto Done; |
| 1886 | |
| 1887 | Error: |
| 1888 | ret = -1; |
| 1889 | /* fall through */ |
| 1890 | Done: |
Tim Peters | 59c9a64 | 2001-09-13 05:38:56 +0000 | [diff] [blame] | 1891 | PyMem_Free(name); /* free the encoded string */ |
Tim Peters | 4441001 | 2001-09-14 03:26:08 +0000 | [diff] [blame] | 1892 | return ret; |
Tim Peters | 59c9a64 | 2001-09-13 05:38:56 +0000 | [diff] [blame] | 1893 | } |
| 1894 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1895 | PyDoc_VAR(file_doc) = |
| 1896 | PyDoc_STR( |
Tim Peters | 59c9a64 | 2001-09-13 05:38:56 +0000 | [diff] [blame] | 1897 | "file(name[, mode[, buffering]]) -> file object\n" |
| 1898 | "\n" |
| 1899 | "Open a file. The mode can be 'r', 'w' or 'a' for reading (default),\n" |
| 1900 | "writing or appending. The file will be created if it doesn't exist\n" |
| 1901 | "when opened for writing or appending; it will be truncated when\n" |
| 1902 | "opened for writing. Add a 'b' to the mode for binary files.\n" |
| 1903 | "Add a '+' to the mode to allow simultaneous reading and writing.\n" |
| 1904 | "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] | 1905 | "buffered, and larger numbers specify the buffer size.\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1906 | ) |
Barry Warsaw | 4be55b5 | 2002-05-22 20:37:53 +0000 | [diff] [blame] | 1907 | #ifdef WITH_UNIVERSAL_NEWLINES |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1908 | PyDoc_STR( |
Barry Warsaw | 4be55b5 | 2002-05-22 20:37:53 +0000 | [diff] [blame] | 1909 | "Add a 'U' to mode to open the file for input with universal newline\n" |
| 1910 | "support. Any line ending in the input file will be seen as a '\\n'\n" |
| 1911 | "in Python. Also, a file so opened gains the attribute 'newlines';\n" |
| 1912 | "the value for this attribute is one of None (no newline read yet),\n" |
| 1913 | "'\\r', '\\n', '\\r\\n' or a tuple containing all the newline types seen.\n" |
| 1914 | "\n" |
| 1915 | "'U' cannot be combined with 'w' or '+' mode.\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1916 | ) |
Barry Warsaw | 4be55b5 | 2002-05-22 20:37:53 +0000 | [diff] [blame] | 1917 | #endif /* WITH_UNIVERSAL_NEWLINES */ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1918 | PyDoc_STR( |
Barry Warsaw | 4be55b5 | 2002-05-22 20:37:53 +0000 | [diff] [blame] | 1919 | "\n" |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 1920 | "Note: open() is an alias for file()." |
| 1921 | ); |
Tim Peters | 59c9a64 | 2001-09-13 05:38:56 +0000 | [diff] [blame] | 1922 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1923 | PyTypeObject PyFile_Type = { |
| 1924 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1925 | 0, |
| 1926 | "file", |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1927 | sizeof(PyFileObject), |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1928 | 0, |
Guido van Rossum | 6596725 | 2001-04-21 13:20:18 +0000 | [diff] [blame] | 1929 | (destructor)file_dealloc, /* tp_dealloc */ |
| 1930 | 0, /* tp_print */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1931 | 0, /* tp_getattr */ |
| 1932 | 0, /* tp_setattr */ |
Guido van Rossum | 6596725 | 2001-04-21 13:20:18 +0000 | [diff] [blame] | 1933 | 0, /* tp_compare */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1934 | (reprfunc)file_repr, /* tp_repr */ |
Guido van Rossum | 6596725 | 2001-04-21 13:20:18 +0000 | [diff] [blame] | 1935 | 0, /* tp_as_number */ |
| 1936 | 0, /* tp_as_sequence */ |
| 1937 | 0, /* tp_as_mapping */ |
| 1938 | 0, /* tp_hash */ |
| 1939 | 0, /* tp_call */ |
| 1940 | 0, /* tp_str */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1941 | PyObject_GenericGetAttr, /* tp_getattro */ |
Guido van Rossum | 6596725 | 2001-04-21 13:20:18 +0000 | [diff] [blame] | 1942 | 0, /* tp_setattro */ |
| 1943 | 0, /* tp_as_buffer */ |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 1944 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Tim Peters | 59c9a64 | 2001-09-13 05:38:56 +0000 | [diff] [blame] | 1945 | file_doc, /* tp_doc */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1946 | 0, /* tp_traverse */ |
| 1947 | 0, /* tp_clear */ |
Guido van Rossum | 6596725 | 2001-04-21 13:20:18 +0000 | [diff] [blame] | 1948 | 0, /* tp_richcompare */ |
| 1949 | 0, /* tp_weaklistoffset */ |
Guido van Rossum | 7a6e959 | 2002-08-06 15:55:28 +0000 | [diff] [blame] | 1950 | (getiterfunc)file_getiter, /* tp_iter */ |
| 1951 | (iternextfunc)file_iternext, /* tp_iternext */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1952 | file_methods, /* tp_methods */ |
| 1953 | file_memberlist, /* tp_members */ |
| 1954 | file_getsetlist, /* tp_getset */ |
| 1955 | 0, /* tp_base */ |
| 1956 | 0, /* tp_dict */ |
Tim Peters | 59c9a64 | 2001-09-13 05:38:56 +0000 | [diff] [blame] | 1957 | 0, /* tp_descr_get */ |
| 1958 | 0, /* tp_descr_set */ |
| 1959 | 0, /* tp_dictoffset */ |
Tim Peters | 4441001 | 2001-09-14 03:26:08 +0000 | [diff] [blame] | 1960 | (initproc)file_init, /* tp_init */ |
| 1961 | PyType_GenericAlloc, /* tp_alloc */ |
Tim Peters | 59c9a64 | 2001-09-13 05:38:56 +0000 | [diff] [blame] | 1962 | file_new, /* tp_new */ |
Neil Schemenauer | aa769ae | 2002-04-12 02:44:10 +0000 | [diff] [blame] | 1963 | PyObject_Del, /* tp_free */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1964 | }; |
Guido van Rossum | eb183da | 1991-04-04 10:44:06 +0000 | [diff] [blame] | 1965 | |
| 1966 | /* Interface for the 'soft space' between print items. */ |
| 1967 | |
| 1968 | int |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 1969 | PyFile_SoftSpace(PyObject *f, int newflag) |
Guido van Rossum | eb183da | 1991-04-04 10:44:06 +0000 | [diff] [blame] | 1970 | { |
| 1971 | int oldflag = 0; |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1972 | if (f == NULL) { |
| 1973 | /* Do nothing */ |
| 1974 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1975 | else if (PyFile_Check(f)) { |
| 1976 | oldflag = ((PyFileObject *)f)->f_softspace; |
| 1977 | ((PyFileObject *)f)->f_softspace = newflag; |
Guido van Rossum | eb183da | 1991-04-04 10:44:06 +0000 | [diff] [blame] | 1978 | } |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1979 | else { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1980 | PyObject *v; |
| 1981 | v = PyObject_GetAttrString(f, "softspace"); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1982 | if (v == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1983 | PyErr_Clear(); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1984 | else { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1985 | if (PyInt_Check(v)) |
| 1986 | oldflag = PyInt_AsLong(v); |
| 1987 | Py_DECREF(v); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1988 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1989 | v = PyInt_FromLong((long)newflag); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1990 | if (v == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1991 | PyErr_Clear(); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1992 | else { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1993 | if (PyObject_SetAttrString(f, "softspace", v) != 0) |
| 1994 | PyErr_Clear(); |
| 1995 | Py_DECREF(v); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 1996 | } |
| 1997 | } |
Guido van Rossum | eb183da | 1991-04-04 10:44:06 +0000 | [diff] [blame] | 1998 | return oldflag; |
| 1999 | } |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 2000 | |
| 2001 | /* Interfaces to write objects/strings to file-like objects */ |
| 2002 | |
| 2003 | int |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 2004 | PyFile_WriteObject(PyObject *v, PyObject *f, int flags) |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 2005 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2006 | PyObject *writer, *value, *args, *result; |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 2007 | if (f == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2008 | PyErr_SetString(PyExc_TypeError, "writeobject with NULL file"); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 2009 | return -1; |
| 2010 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2011 | else if (PyFile_Check(f)) { |
| 2012 | FILE *fp = PyFile_AsFile(f); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 2013 | if (fp == NULL) { |
| 2014 | err_closed(); |
| 2015 | return -1; |
| 2016 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2017 | return PyObject_Print(v, fp, flags); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 2018 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2019 | writer = PyObject_GetAttrString(f, "write"); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 2020 | if (writer == NULL) |
| 2021 | return -1; |
Martin v. Löwis | 2777c02 | 2001-09-19 13:47:32 +0000 | [diff] [blame] | 2022 | if (flags & Py_PRINT_RAW) { |
| 2023 | if (PyUnicode_Check(v)) { |
| 2024 | value = v; |
| 2025 | Py_INCREF(value); |
| 2026 | } else |
| 2027 | value = PyObject_Str(v); |
| 2028 | } |
| 2029 | else |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2030 | value = PyObject_Repr(v); |
Guido van Rossum | c600411 | 1993-11-05 10:22:19 +0000 | [diff] [blame] | 2031 | if (value == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2032 | Py_DECREF(writer); |
Guido van Rossum | c600411 | 1993-11-05 10:22:19 +0000 | [diff] [blame] | 2033 | return -1; |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 2034 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2035 | args = Py_BuildValue("(O)", value); |
Guido van Rossum | e9eec54 | 1997-05-22 14:02:25 +0000 | [diff] [blame] | 2036 | if (args == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2037 | Py_DECREF(value); |
| 2038 | Py_DECREF(writer); |
Guido van Rossum | d3f9a1a | 1995-07-10 23:32:26 +0000 | [diff] [blame] | 2039 | return -1; |
| 2040 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2041 | result = PyEval_CallObject(writer, args); |
| 2042 | Py_DECREF(args); |
| 2043 | Py_DECREF(value); |
| 2044 | Py_DECREF(writer); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 2045 | if (result == NULL) |
| 2046 | return -1; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2047 | Py_DECREF(result); |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 2048 | return 0; |
| 2049 | } |
| 2050 | |
Guido van Rossum | 27a60b1 | 1997-05-22 22:25:11 +0000 | [diff] [blame] | 2051 | int |
Tim Peters | c1bbcb8 | 2001-11-28 22:13:25 +0000 | [diff] [blame] | 2052 | PyFile_WriteString(const char *s, PyObject *f) |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 2053 | { |
| 2054 | if (f == NULL) { |
Guido van Rossum | 27a60b1 | 1997-05-22 22:25:11 +0000 | [diff] [blame] | 2055 | /* Should be caused by a pre-existing error */ |
Fred Drake | fd99de6 | 2000-07-09 05:02:18 +0000 | [diff] [blame] | 2056 | if (!PyErr_Occurred()) |
Guido van Rossum | 27a60b1 | 1997-05-22 22:25:11 +0000 | [diff] [blame] | 2057 | PyErr_SetString(PyExc_SystemError, |
| 2058 | "null file for PyFile_WriteString"); |
| 2059 | return -1; |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 2060 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2061 | else if (PyFile_Check(f)) { |
| 2062 | FILE *fp = PyFile_AsFile(f); |
Guido van Rossum | 27a60b1 | 1997-05-22 22:25:11 +0000 | [diff] [blame] | 2063 | if (fp == NULL) { |
| 2064 | err_closed(); |
| 2065 | return -1; |
| 2066 | } |
| 2067 | fputs(s, fp); |
| 2068 | return 0; |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 2069 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2070 | else if (!PyErr_Occurred()) { |
| 2071 | PyObject *v = PyString_FromString(s); |
Guido van Rossum | 27a60b1 | 1997-05-22 22:25:11 +0000 | [diff] [blame] | 2072 | int err; |
| 2073 | if (v == NULL) |
| 2074 | return -1; |
| 2075 | err = PyFile_WriteObject(v, f, Py_PRINT_RAW); |
| 2076 | Py_DECREF(v); |
| 2077 | return err; |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 2078 | } |
Guido van Rossum | 74ba247 | 1997-07-13 03:56:50 +0000 | [diff] [blame] | 2079 | else |
| 2080 | return -1; |
Guido van Rossum | 3165fe6 | 1992-09-25 21:59:05 +0000 | [diff] [blame] | 2081 | } |
Andrew M. Kuchling | 06051ed | 2000-07-13 23:56:54 +0000 | [diff] [blame] | 2082 | |
| 2083 | /* Try to get a file-descriptor from a Python object. If the object |
| 2084 | is an integer or long integer, its value is returned. If not, the |
| 2085 | object's fileno() method is called if it exists; the method must return |
| 2086 | an integer or long integer, which is returned as the file descriptor value. |
| 2087 | -1 is returned on failure. |
| 2088 | */ |
| 2089 | |
| 2090 | int PyObject_AsFileDescriptor(PyObject *o) |
| 2091 | { |
| 2092 | int fd; |
| 2093 | PyObject *meth; |
| 2094 | |
| 2095 | if (PyInt_Check(o)) { |
| 2096 | fd = PyInt_AsLong(o); |
| 2097 | } |
| 2098 | else if (PyLong_Check(o)) { |
| 2099 | fd = PyLong_AsLong(o); |
| 2100 | } |
| 2101 | else if ((meth = PyObject_GetAttrString(o, "fileno")) != NULL) |
| 2102 | { |
| 2103 | PyObject *fno = PyEval_CallObject(meth, NULL); |
| 2104 | Py_DECREF(meth); |
| 2105 | if (fno == NULL) |
| 2106 | return -1; |
Tim Peters | 86821b2 | 2001-01-07 21:19:34 +0000 | [diff] [blame] | 2107 | |
Andrew M. Kuchling | 06051ed | 2000-07-13 23:56:54 +0000 | [diff] [blame] | 2108 | if (PyInt_Check(fno)) { |
| 2109 | fd = PyInt_AsLong(fno); |
| 2110 | Py_DECREF(fno); |
| 2111 | } |
| 2112 | else if (PyLong_Check(fno)) { |
| 2113 | fd = PyLong_AsLong(fno); |
| 2114 | Py_DECREF(fno); |
| 2115 | } |
| 2116 | else { |
| 2117 | PyErr_SetString(PyExc_TypeError, |
| 2118 | "fileno() returned a non-integer"); |
| 2119 | Py_DECREF(fno); |
| 2120 | return -1; |
| 2121 | } |
| 2122 | } |
| 2123 | else { |
| 2124 | PyErr_SetString(PyExc_TypeError, |
| 2125 | "argument must be an int, or have a fileno() method."); |
| 2126 | return -1; |
| 2127 | } |
| 2128 | |
| 2129 | if (fd < 0) { |
| 2130 | PyErr_Format(PyExc_ValueError, |
| 2131 | "file descriptor cannot be a negative integer (%i)", |
| 2132 | fd); |
| 2133 | return -1; |
| 2134 | } |
| 2135 | return fd; |
| 2136 | } |
Jack Jansen | 7b8c754 | 2002-04-14 20:12:41 +0000 | [diff] [blame] | 2137 | |
| 2138 | #ifdef WITH_UNIVERSAL_NEWLINES |
| 2139 | /* From here on we need access to the real fgets and fread */ |
| 2140 | #undef fgets |
| 2141 | #undef fread |
| 2142 | |
| 2143 | /* |
| 2144 | ** Py_UniversalNewlineFgets is an fgets variation that understands |
| 2145 | ** all of \r, \n and \r\n conventions. |
| 2146 | ** The stream should be opened in binary mode. |
| 2147 | ** If fobj is NULL the routine always does newline conversion, and |
| 2148 | ** it may peek one char ahead to gobble the second char in \r\n. |
| 2149 | ** If fobj is non-NULL it must be a PyFileObject. In this case there |
| 2150 | ** is no readahead but in stead a flag is used to skip a following |
| 2151 | ** \n on the next read. Also, if the file is open in binary mode |
| 2152 | ** the whole conversion is skipped. Finally, the routine keeps track of |
| 2153 | ** the different types of newlines seen. |
| 2154 | ** Note that we need no error handling: fgets() treats error and eof |
| 2155 | ** identically. |
| 2156 | */ |
| 2157 | char * |
| 2158 | Py_UniversalNewlineFgets(char *buf, int n, FILE *stream, PyObject *fobj) |
| 2159 | { |
| 2160 | char *p = buf; |
| 2161 | int c; |
| 2162 | int newlinetypes = 0; |
| 2163 | int skipnextlf = 0; |
| 2164 | int univ_newline = 1; |
Tim Peters | 058b141 | 2002-04-21 07:29:14 +0000 | [diff] [blame] | 2165 | |
Jack Jansen | 7b8c754 | 2002-04-14 20:12:41 +0000 | [diff] [blame] | 2166 | if (fobj) { |
| 2167 | if (!PyFile_Check(fobj)) { |
| 2168 | errno = ENXIO; /* What can you do... */ |
| 2169 | return NULL; |
| 2170 | } |
| 2171 | univ_newline = ((PyFileObject *)fobj)->f_univ_newline; |
| 2172 | if ( !univ_newline ) |
| 2173 | return fgets(buf, n, stream); |
| 2174 | newlinetypes = ((PyFileObject *)fobj)->f_newlinetypes; |
| 2175 | skipnextlf = ((PyFileObject *)fobj)->f_skipnextlf; |
| 2176 | } |
| 2177 | FLOCKFILE(stream); |
| 2178 | c = 'x'; /* Shut up gcc warning */ |
| 2179 | while (--n > 0 && (c = GETC(stream)) != EOF ) { |
| 2180 | if (skipnextlf ) { |
| 2181 | skipnextlf = 0; |
| 2182 | if (c == '\n') { |
| 2183 | /* Seeing a \n here with skipnextlf true |
| 2184 | ** means we saw a \r before. |
| 2185 | */ |
| 2186 | newlinetypes |= NEWLINE_CRLF; |
| 2187 | c = GETC(stream); |
| 2188 | if (c == EOF) break; |
| 2189 | } else { |
| 2190 | /* |
| 2191 | ** Note that c == EOF also brings us here, |
| 2192 | ** so we're okay if the last char in the file |
| 2193 | ** is a CR. |
| 2194 | */ |
| 2195 | newlinetypes |= NEWLINE_CR; |
| 2196 | } |
| 2197 | } |
| 2198 | if (c == '\r') { |
| 2199 | /* A \r is translated into a \n, and we skip |
| 2200 | ** an adjacent \n, if any. We don't set the |
| 2201 | ** newlinetypes flag until we've seen the next char. |
| 2202 | */ |
| 2203 | skipnextlf = 1; |
| 2204 | c = '\n'; |
| 2205 | } else if ( c == '\n') { |
| 2206 | newlinetypes |= NEWLINE_LF; |
| 2207 | } |
| 2208 | *p++ = c; |
| 2209 | if (c == '\n') break; |
| 2210 | } |
| 2211 | if ( c == EOF && skipnextlf ) |
| 2212 | newlinetypes |= NEWLINE_CR; |
| 2213 | FUNLOCKFILE(stream); |
| 2214 | *p = '\0'; |
| 2215 | if (fobj) { |
| 2216 | ((PyFileObject *)fobj)->f_newlinetypes = newlinetypes; |
| 2217 | ((PyFileObject *)fobj)->f_skipnextlf = skipnextlf; |
| 2218 | } else if ( skipnextlf ) { |
| 2219 | /* If we have no file object we cannot save the |
| 2220 | ** skipnextlf flag. We have to readahead, which |
| 2221 | ** will cause a pause if we're reading from an |
| 2222 | ** interactive stream, but that is very unlikely |
| 2223 | ** unless we're doing something silly like |
| 2224 | ** execfile("/dev/tty"). |
| 2225 | */ |
| 2226 | c = GETC(stream); |
| 2227 | if ( c != '\n' ) |
| 2228 | ungetc(c, stream); |
| 2229 | } |
| 2230 | if (p == buf) |
| 2231 | return NULL; |
| 2232 | return buf; |
| 2233 | } |
| 2234 | |
| 2235 | /* |
| 2236 | ** Py_UniversalNewlineFread is an fread variation that understands |
| 2237 | ** all of \r, \n and \r\n conventions. |
| 2238 | ** The stream should be opened in binary mode. |
| 2239 | ** fobj must be a PyFileObject. In this case there |
| 2240 | ** is no readahead but in stead a flag is used to skip a following |
| 2241 | ** \n on the next read. Also, if the file is open in binary mode |
| 2242 | ** the whole conversion is skipped. Finally, the routine keeps track of |
| 2243 | ** the different types of newlines seen. |
| 2244 | */ |
| 2245 | size_t |
Tim Peters | 058b141 | 2002-04-21 07:29:14 +0000 | [diff] [blame] | 2246 | Py_UniversalNewlineFread(char *buf, size_t n, |
Jack Jansen | 7b8c754 | 2002-04-14 20:12:41 +0000 | [diff] [blame] | 2247 | FILE *stream, PyObject *fobj) |
| 2248 | { |
Tim Peters | 058b141 | 2002-04-21 07:29:14 +0000 | [diff] [blame] | 2249 | char *dst = buf; |
| 2250 | PyFileObject *f = (PyFileObject *)fobj; |
| 2251 | int newlinetypes, skipnextlf; |
| 2252 | |
| 2253 | assert(buf != NULL); |
| 2254 | assert(stream != NULL); |
| 2255 | |
Jack Jansen | 7b8c754 | 2002-04-14 20:12:41 +0000 | [diff] [blame] | 2256 | if (!fobj || !PyFile_Check(fobj)) { |
| 2257 | errno = ENXIO; /* What can you do... */ |
| 2258 | return -1; |
| 2259 | } |
Tim Peters | 058b141 | 2002-04-21 07:29:14 +0000 | [diff] [blame] | 2260 | if (!f->f_univ_newline) |
Jack Jansen | 7b8c754 | 2002-04-14 20:12:41 +0000 | [diff] [blame] | 2261 | return fread(buf, 1, n, stream); |
Tim Peters | 058b141 | 2002-04-21 07:29:14 +0000 | [diff] [blame] | 2262 | newlinetypes = f->f_newlinetypes; |
| 2263 | skipnextlf = f->f_skipnextlf; |
| 2264 | /* Invariant: n is the number of bytes remaining to be filled |
| 2265 | * in the buffer. |
| 2266 | */ |
| 2267 | while (n) { |
| 2268 | size_t nread; |
| 2269 | int shortread; |
| 2270 | char *src = dst; |
| 2271 | |
| 2272 | nread = fread(dst, 1, n, stream); |
| 2273 | assert(nread <= n); |
Tim Peters | e1682a8 | 2002-04-21 18:15:20 +0000 | [diff] [blame] | 2274 | n -= nread; /* assuming 1 byte out for each in; will adjust */ |
| 2275 | shortread = n != 0; /* true iff EOF or error */ |
Tim Peters | 058b141 | 2002-04-21 07:29:14 +0000 | [diff] [blame] | 2276 | while (nread--) { |
| 2277 | char c = *src++; |
Jack Jansen | 7b8c754 | 2002-04-14 20:12:41 +0000 | [diff] [blame] | 2278 | if (c == '\r') { |
Tim Peters | 058b141 | 2002-04-21 07:29:14 +0000 | [diff] [blame] | 2279 | /* Save as LF and set flag to skip next LF. */ |
Jack Jansen | 7b8c754 | 2002-04-14 20:12:41 +0000 | [diff] [blame] | 2280 | *dst++ = '\n'; |
| 2281 | skipnextlf = 1; |
Tim Peters | 058b141 | 2002-04-21 07:29:14 +0000 | [diff] [blame] | 2282 | } |
| 2283 | else if (skipnextlf && c == '\n') { |
| 2284 | /* Skip LF, and remember we saw CR LF. */ |
Jack Jansen | 7b8c754 | 2002-04-14 20:12:41 +0000 | [diff] [blame] | 2285 | skipnextlf = 0; |
| 2286 | newlinetypes |= NEWLINE_CRLF; |
Tim Peters | e1682a8 | 2002-04-21 18:15:20 +0000 | [diff] [blame] | 2287 | ++n; |
Tim Peters | 058b141 | 2002-04-21 07:29:14 +0000 | [diff] [blame] | 2288 | } |
| 2289 | else { |
| 2290 | /* Normal char to be stored in buffer. Also |
| 2291 | * update the newlinetypes flag if either this |
| 2292 | * is an LF or the previous char was a CR. |
| 2293 | */ |
Jack Jansen | 7b8c754 | 2002-04-14 20:12:41 +0000 | [diff] [blame] | 2294 | if (c == '\n') |
| 2295 | newlinetypes |= NEWLINE_LF; |
| 2296 | else if (skipnextlf) |
| 2297 | newlinetypes |= NEWLINE_CR; |
| 2298 | *dst++ = c; |
| 2299 | skipnextlf = 0; |
| 2300 | } |
| 2301 | } |
Tim Peters | 058b141 | 2002-04-21 07:29:14 +0000 | [diff] [blame] | 2302 | if (shortread) { |
| 2303 | /* If this is EOF, update type flags. */ |
| 2304 | if (skipnextlf && feof(stream)) |
| 2305 | newlinetypes |= NEWLINE_CR; |
| 2306 | break; |
| 2307 | } |
Jack Jansen | 7b8c754 | 2002-04-14 20:12:41 +0000 | [diff] [blame] | 2308 | } |
Tim Peters | 058b141 | 2002-04-21 07:29:14 +0000 | [diff] [blame] | 2309 | f->f_newlinetypes = newlinetypes; |
| 2310 | f->f_skipnextlf = skipnextlf; |
| 2311 | return dst - buf; |
Jack Jansen | 7b8c754 | 2002-04-14 20:12:41 +0000 | [diff] [blame] | 2312 | } |
| 2313 | #endif |