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