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