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