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