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