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