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