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