blob: 1e3e57c606f411b8a118dad2f42a6c69f475c6e3 [file] [log] [blame]
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001/* File object implementation */
2
Martin v. Löwis18e16552006-02-15 17:27:45 +00003#define PY_SSIZE_T_CLEAN
Guido van Rossumc0b618a1997-05-02 03:12:38 +00004#include "Python.h"
Guido van Rossumb6775db1994-08-01 11:34:53 +00005#include "structmember.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006
Thomas Wouters0e3f5912006-08-11 14:57:12 +00007#ifdef HAVE_SYS_TYPES_H
Guido van Rossum41498431999-01-07 22:09:51 +00008#include <sys/types.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +00009#endif /* HAVE_SYS_TYPES_H */
Guido van Rossum41498431999-01-07 22:09:51 +000010
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000011#ifdef MS_WINDOWS
Guido van Rossumb8199141997-05-06 15:23:24 +000012#define fileno _fileno
Tim Petersfb05db22002-03-11 00:24:00 +000013/* can simulate truncate with Win32 API functions; see file_truncate */
Guido van Rossumb8199141997-05-06 15:23:24 +000014#define HAVE_FTRUNCATE
Tim Peters7a1f9172002-07-14 22:14:19 +000015#define WIN32_LEAN_AND_MEAN
Tim Petersfb05db22002-03-11 00:24:00 +000016#include <windows.h>
Guido van Rossumb8199141997-05-06 15:23:24 +000017#endif
18
Mark Hammondc2e85bd2002-10-03 05:10:39 +000019#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 MacIntyrec4874392002-02-26 11:36:35 +000025#if defined(PYOS_OS2) && defined(PYCC_GCC)
26#include <io.h>
27#endif
28
Guido van Rossumc0b618a1997-05-02 03:12:38 +000029#define BUF(v) PyString_AS_STRING((PyStringObject *)v)
Guido van Rossumce5ba841991-03-06 13:06:18 +000030
Guido van Rossumff7e83d1999-08-27 20:39:37 +000031#ifndef DONT_HAVE_ERRNO_H
Guido van Rossumf1dc5661993-07-05 10:31:29 +000032#include <errno.h>
Guido van Rossumff7e83d1999-08-27 20:39:37 +000033#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000034
Jack Jansen7b8c7542002-04-14 20:12:41 +000035#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 Jansen7b8c7542002-04-14 20:12:41 +000045/* 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 Mickf29f47b2000-08-11 19:02:59 +000050
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000051#ifdef __cplusplus
52extern "C" {
53#endif
54
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000055FILE *
Fred Drakefd99de62000-07-09 05:02:18 +000056PyFile_AsFile(PyObject *f)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000057{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000058 if (f == NULL || !PyFile_Check(f))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000059 return NULL;
Guido van Rossum3165fe61992-09-25 21:59:05 +000060 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +000061 return ((PyFileObject *)f)->f_fp;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000062}
63
Guido van Rossumc0b618a1997-05-02 03:12:38 +000064PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +000065PyFile_Name(PyObject *f)
Guido van Rossumdb3165e1993-10-18 17:06:59 +000066{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000067 if (f == NULL || !PyFile_Check(f))
Guido van Rossumdb3165e1993-10-18 17:06:59 +000068 return NULL;
69 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +000070 return ((PyFileObject *)f)->f_name;
Guido van Rossumdb3165e1993-10-18 17:06:59 +000071}
72
Neil Schemenauered19b882002-03-23 02:06:50 +000073/* 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
77static PyFileObject*
78dircheck(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 Petersf1827cf2003-09-07 03:30:18 +000091 PyObject *exc = PyObject_CallFunction(PyExc_IOError, "(is)",
Jeremy Hylton8b735422002-08-14 21:01:41 +000092 EISDIR, msg);
Neil Schemenauered19b882002-03-23 02:06:50 +000093 PyErr_SetObject(PyExc_IOError, exc);
Neal Norwitz98cad482003-08-15 20:05:45 +000094 Py_XDECREF(exc);
Neil Schemenauered19b882002-03-23 02:06:50 +000095 return NULL;
96 }
97#endif
98 return f;
99}
100
Tim Peters59c9a642001-09-13 05:38:56 +0000101
102static PyObject *
Nicholas Bastinabce8a62004-03-21 20:24:07 +0000103fill_file_fields(PyFileObject *f, FILE *fp, PyObject *name, char *mode,
104 int (*close)(FILE *))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000105{
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000106 assert(name != NULL);
Tim Peters59c9a642001-09-13 05:38:56 +0000107 assert(f != NULL);
108 assert(PyFile_Check(f));
Tim Peters44410012001-09-14 03:26:08 +0000109 assert(f->f_fp == NULL);
110
111 Py_DECREF(f->f_name);
112 Py_DECREF(f->f_mode);
Martin v. Löwis5467d4c2003-05-10 07:10:12 +0000113 Py_DECREF(f->f_encoding);
Nicholas Bastinabce8a62004-03-21 20:24:07 +0000114
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000115 Py_INCREF(name);
Nicholas Bastinabce8a62004-03-21 20:24:07 +0000116 f->f_name = name;
117
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000118 f->f_mode = PyString_FromString(mode);
Tim Peters44410012001-09-14 03:26:08 +0000119
Guido van Rossuma1ab7fa1991-06-04 19:37:39 +0000120 f->f_close = close;
Guido van Rossumeb183da1991-04-04 10:44:06 +0000121 f->f_softspace = 0;
Tim Peters59c9a642001-09-13 05:38:56 +0000122 f->f_binary = strchr(mode,'b') != NULL;
Guido van Rossum7a6e9592002-08-06 15:55:28 +0000123 f->f_buf = NULL;
Jack Jansen7b8c7542002-04-14 20:12:41 +0000124 f->f_univ_newline = (strchr(mode, 'U') != NULL);
125 f->f_newlinetypes = NEWLINE_UNKNOWN;
126 f->f_skipnextlf = 0;
Martin v. Löwis5467d4c2003-05-10 07:10:12 +0000127 Py_INCREF(Py_None);
128 f->f_encoding = Py_None;
Tim Petersf1827cf2003-09-07 03:30:18 +0000129
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000130 if (f->f_mode == NULL)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000131 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000132 f->f_fp = fp;
Neil Schemenauered19b882002-03-23 02:06:50 +0000133 f = dircheck(f);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000134 return (PyObject *) f;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000135}
136
Skip Montanarobbf12ba2005-05-20 03:07:06 +0000137/* check for known incorrect mode strings - problem is, platforms are
138 free to accept any mode characters they like and are supposed to
139 ignore stuff they don't understand... write or append mode with
Thomas Wouters477c8d52006-05-27 19:21:47 +0000140 universal newline support is expressly forbidden by PEP 278.
141 Additionally, remove the 'U' from the mode string as platforms
142 won't know what it is. */
Skip Montanarobbf12ba2005-05-20 03:07:06 +0000143/* zero return is kewl - one is un-kewl */
144static int
Thomas Wouters477c8d52006-05-27 19:21:47 +0000145sanitize_the_mode(char *mode)
Skip Montanarobbf12ba2005-05-20 03:07:06 +0000146{
Thomas Wouters477c8d52006-05-27 19:21:47 +0000147 char *upos;
Neal Norwitz76dc0812006-01-08 06:13:13 +0000148 size_t len = strlen(mode);
Skip Montanarobbf12ba2005-05-20 03:07:06 +0000149
Thomas Wouters477c8d52006-05-27 19:21:47 +0000150 if (!len) {
Skip Montanarobbf12ba2005-05-20 03:07:06 +0000151 PyErr_SetString(PyExc_ValueError, "empty mode string");
152 return 1;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000153 }
Skip Montanarobbf12ba2005-05-20 03:07:06 +0000154
Thomas Wouters477c8d52006-05-27 19:21:47 +0000155 upos = strchr(mode, 'U');
156 if (upos) {
157 memmove(upos, upos+1, len-(upos-mode)); /* incl null char */
Skip Montanarobbf12ba2005-05-20 03:07:06 +0000158
Thomas Wouters477c8d52006-05-27 19:21:47 +0000159 if (mode[0] == 'w' || mode[0] == 'a') {
160 PyErr_Format(PyExc_ValueError, "universal newline "
161 "mode can only be used with modes "
162 "starting with 'r'");
163 return 1;
Skip Montanarobbf12ba2005-05-20 03:07:06 +0000164 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000165
166 if (mode[0] != 'r') {
167 memmove(mode+1, mode, strlen(mode)+1);
168 mode[0] = 'r';
169 }
170
171 if (!strchr(mode, 'b')) {
172 memmove(mode+2, mode+1, strlen(mode));
173 mode[1] = 'b';
174 }
175 } else if (mode[0] != 'r' && mode[0] != 'w' && mode[0] != 'a') {
176 PyErr_Format(PyExc_ValueError, "mode string must begin with "
177 "one of 'r', 'w', 'a' or 'U', not '%.200s'", mode);
178 return 1;
Skip Montanarobbf12ba2005-05-20 03:07:06 +0000179 }
180
181 return 0;
182}
183
Tim Peters59c9a642001-09-13 05:38:56 +0000184static PyObject *
185open_the_file(PyFileObject *f, char *name, char *mode)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000186{
Thomas Wouters477c8d52006-05-27 19:21:47 +0000187 char *newmode;
Tim Peters59c9a642001-09-13 05:38:56 +0000188 assert(f != NULL);
189 assert(PyFile_Check(f));
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000190#ifdef MS_WINDOWS
191 /* windows ignores the passed name in order to support Unicode */
192 assert(f->f_name != NULL);
193#else
Tim Peters59c9a642001-09-13 05:38:56 +0000194 assert(name != NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000195#endif
Tim Peters59c9a642001-09-13 05:38:56 +0000196 assert(mode != NULL);
Tim Peters44410012001-09-14 03:26:08 +0000197 assert(f->f_fp == NULL);
Tim Peters59c9a642001-09-13 05:38:56 +0000198
Thomas Wouters477c8d52006-05-27 19:21:47 +0000199 /* probably need to replace 'U' by 'rb' */
200 newmode = PyMem_MALLOC(strlen(mode) + 3);
201 if (!newmode) {
202 PyErr_NoMemory();
Skip Montanarobbf12ba2005-05-20 03:07:06 +0000203 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000204 }
205 strcpy(newmode, mode);
206
207 if (sanitize_the_mode(newmode)) {
208 f = NULL;
209 goto cleanup;
210 }
Skip Montanarobbf12ba2005-05-20 03:07:06 +0000211
Tim Peters8fa45672001-09-13 21:01:29 +0000212 /* rexec.py can't stop a user from getting the file() constructor --
213 all they have to do is get *any* file object f, and then do
214 type(f). Here we prevent them from doing damage with it. */
215 if (PyEval_GetRestricted()) {
216 PyErr_SetString(PyExc_IOError,
Jeremy Hylton8b735422002-08-14 21:01:41 +0000217 "file() constructor not accessible in restricted mode");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000218 f = NULL;
219 goto cleanup;
Tim Peters8fa45672001-09-13 21:01:29 +0000220 }
Tim Petersa27a1502001-11-09 20:59:14 +0000221 errno = 0;
Skip Montanaro51ffac62004-06-11 04:49:03 +0000222
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000223#ifdef MS_WINDOWS
Skip Montanaro51ffac62004-06-11 04:49:03 +0000224 if (PyUnicode_Check(f->f_name)) {
225 PyObject *wmode;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000226 wmode = PyUnicode_DecodeASCII(newmode, strlen(newmode), NULL);
Skip Montanaro51ffac62004-06-11 04:49:03 +0000227 if (f->f_name && wmode) {
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000228 Py_BEGIN_ALLOW_THREADS
Skip Montanaro51ffac62004-06-11 04:49:03 +0000229 /* PyUnicode_AS_UNICODE OK without thread
230 lock as it is a simple dereference. */
231 f->f_fp = _wfopen(PyUnicode_AS_UNICODE(f->f_name),
232 PyUnicode_AS_UNICODE(wmode));
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000233 Py_END_ALLOW_THREADS
234 }
Skip Montanaro51ffac62004-06-11 04:49:03 +0000235 Py_XDECREF(wmode);
Guido van Rossumff4949e1992-08-05 19:58:53 +0000236 }
Skip Montanaro51ffac62004-06-11 04:49:03 +0000237#endif
238 if (NULL == f->f_fp && NULL != name) {
239 Py_BEGIN_ALLOW_THREADS
Thomas Wouters477c8d52006-05-27 19:21:47 +0000240 f->f_fp = fopen(name, newmode);
Skip Montanaro51ffac62004-06-11 04:49:03 +0000241 Py_END_ALLOW_THREADS
242 }
243
Guido van Rossuma08095a1991-02-13 23:25:27 +0000244 if (f->f_fp == NULL) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000245#if defined _MSC_VER && (_MSC_VER < 1400 || !defined(__STDC_SECURE_LIB__))
Tim Peters2ea91112002-04-08 04:13:12 +0000246 /* MSVC 6 (Microsoft) leaves errno at 0 for bad mode strings,
247 * across all Windows flavors. When it sets EINVAL varies
248 * across Windows flavors, the exact conditions aren't
249 * documented, and the answer lies in the OS's implementation
250 * of Win32's CreateFile function (whose source is secret).
251 * Seems the best we can do is map EINVAL to ENOENT.
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000252 * Starting with Visual Studio .NET 2005, EINVAL is correctly
253 * set by our CRT error handler (set in exceptions.c.)
Tim Peters2ea91112002-04-08 04:13:12 +0000254 */
255 if (errno == 0) /* bad mode string */
256 errno = EINVAL;
257 else if (errno == EINVAL) /* unknown, but not a mode string */
258 errno = ENOENT;
259#endif
Jeremy Hylton41c83212001-11-09 16:17:24 +0000260 if (errno == EINVAL)
Tim Peters2ea91112002-04-08 04:13:12 +0000261 PyErr_Format(PyExc_IOError, "invalid mode: %s",
Jeremy Hylton41c83212001-11-09 16:17:24 +0000262 mode);
263 else
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000264 PyErr_SetFromErrnoWithFilenameObject(PyExc_IOError, f->f_name);
Tim Peters59c9a642001-09-13 05:38:56 +0000265 f = NULL;
266 }
Tim Peters2ea91112002-04-08 04:13:12 +0000267 if (f != NULL)
Neil Schemenauered19b882002-03-23 02:06:50 +0000268 f = dircheck(f);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000269
270cleanup:
271 PyMem_FREE(newmode);
272
Tim Peters59c9a642001-09-13 05:38:56 +0000273 return (PyObject *)f;
274}
275
276PyObject *
277PyFile_FromFile(FILE *fp, char *name, char *mode, int (*close)(FILE *))
278{
Tim Peters44410012001-09-14 03:26:08 +0000279 PyFileObject *f = (PyFileObject *)PyFile_Type.tp_new(&PyFile_Type,
280 NULL, NULL);
Tim Peters59c9a642001-09-13 05:38:56 +0000281 if (f != NULL) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000282 PyObject *o_name = PyString_FromString(name);
283 if (o_name == NULL)
284 return NULL;
Nicholas Bastinabce8a62004-03-21 20:24:07 +0000285 if (fill_file_fields(f, fp, o_name, mode, close) == NULL) {
Tim Peters59c9a642001-09-13 05:38:56 +0000286 Py_DECREF(f);
287 f = NULL;
288 }
Nicholas Bastinabce8a62004-03-21 20:24:07 +0000289 Py_DECREF(o_name);
Tim Peters59c9a642001-09-13 05:38:56 +0000290 }
291 return (PyObject *) f;
292}
293
294PyObject *
295PyFile_FromString(char *name, char *mode)
296{
297 extern int fclose(FILE *);
298 PyFileObject *f;
299
300 f = (PyFileObject *)PyFile_FromFile((FILE *)NULL, name, mode, fclose);
301 if (f != NULL) {
302 if (open_the_file(f, name, mode) == NULL) {
303 Py_DECREF(f);
304 f = NULL;
305 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000306 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000307 return (PyObject *)f;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000308}
309
Guido van Rossumb6775db1994-08-01 11:34:53 +0000310void
Fred Drakefd99de62000-07-09 05:02:18 +0000311PyFile_SetBufSize(PyObject *f, int bufsize)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000312{
Martin v. Löwis1e3bdf62003-09-04 19:01:46 +0000313 PyFileObject *file = (PyFileObject *)f;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000314 if (bufsize >= 0) {
Guido van Rossumb6775db1994-08-01 11:34:53 +0000315 int type;
316 switch (bufsize) {
317 case 0:
318 type = _IONBF;
319 break;
Martin v. Löwis1e3bdf62003-09-04 19:01:46 +0000320#ifdef HAVE_SETVBUF
Guido van Rossumb6775db1994-08-01 11:34:53 +0000321 case 1:
322 type = _IOLBF;
323 bufsize = BUFSIZ;
324 break;
Martin v. Löwis1e3bdf62003-09-04 19:01:46 +0000325#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000326 default:
327 type = _IOFBF;
Martin v. Löwis1e3bdf62003-09-04 19:01:46 +0000328#ifndef HAVE_SETVBUF
329 bufsize = BUFSIZ;
330#endif
331 break;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000332 }
Martin v. Löwis1e3bdf62003-09-04 19:01:46 +0000333 fflush(file->f_fp);
334 if (type == _IONBF) {
335 PyMem_Free(file->f_setbuf);
336 file->f_setbuf = NULL;
337 } else {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000338 file->f_setbuf = (char *)PyMem_Realloc(file->f_setbuf,
339 bufsize);
Martin v. Löwis1e3bdf62003-09-04 19:01:46 +0000340 }
341#ifdef HAVE_SETVBUF
342 setvbuf(file->f_fp, file->f_setbuf, type, bufsize);
Guido van Rossumf8b4de01998-03-06 15:32:40 +0000343#else /* !HAVE_SETVBUF */
Martin v. Löwis1e3bdf62003-09-04 19:01:46 +0000344 setbuf(file->f_fp, file->f_setbuf);
Guido van Rossumf8b4de01998-03-06 15:32:40 +0000345#endif /* !HAVE_SETVBUF */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000346 }
347}
348
Martin v. Löwis5467d4c2003-05-10 07:10:12 +0000349/* Set the encoding used to output Unicode strings.
350 Returh 1 on success, 0 on failure. */
351
352int
353PyFile_SetEncoding(PyObject *f, const char *enc)
354{
355 PyFileObject *file = (PyFileObject*)f;
356 PyObject *str = PyString_FromString(enc);
Thomas Woutersb2137042007-02-01 18:02:27 +0000357
358 assert(PyFile_Check(f));
Martin v. Löwis5467d4c2003-05-10 07:10:12 +0000359 if (!str)
360 return 0;
361 Py_DECREF(file->f_encoding);
362 file->f_encoding = str;
363 return 1;
364}
365
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000366static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000367err_closed(void)
Guido van Rossumd7297e61992-07-06 14:19:26 +0000368{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000369 PyErr_SetString(PyExc_ValueError, "I/O operation on closed file");
Guido van Rossumd7297e61992-07-06 14:19:26 +0000370 return NULL;
371}
372
Thomas Woutersc45251a2006-02-12 11:53:32 +0000373/* Refuse regular file I/O if there's data in the iteration-buffer.
374 * Mixing them would cause data to arrive out of order, as the read*
375 * methods don't use the iteration buffer. */
376static PyObject *
377err_iterbuffered(void)
378{
379 PyErr_SetString(PyExc_ValueError,
380 "Mixing iteration and read methods would lose data");
381 return NULL;
382}
383
Neal Norwitzd8b995f2002-08-06 21:50:54 +0000384static void drop_readahead(PyFileObject *);
Guido van Rossum7a6e9592002-08-06 15:55:28 +0000385
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000386/* Methods */
387
388static void
Fred Drakefd99de62000-07-09 05:02:18 +0000389file_dealloc(PyFileObject *f)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000390{
Peter Astrandf8e74b12004-11-07 14:15:28 +0000391 int sts = 0;
Raymond Hettingercb87bc82004-05-31 00:35:52 +0000392 if (f->weakreflist != NULL)
393 PyObject_ClearWeakRefs((PyObject *) f);
Guido van Rossumff4949e1992-08-05 19:58:53 +0000394 if (f->f_fp != NULL && f->f_close != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000395 Py_BEGIN_ALLOW_THREADS
Peter Astrandf8e74b12004-11-07 14:15:28 +0000396 sts = (*f->f_close)(f->f_fp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000397 Py_END_ALLOW_THREADS
Peter Astrandf8e74b12004-11-07 14:15:28 +0000398 if (sts == EOF)
399#ifdef HAVE_STRERROR
400 PySys_WriteStderr("close failed: [Errno %d] %s\n", errno, strerror(errno));
401#else
402 PySys_WriteStderr("close failed: [Errno %d]\n", errno);
403#endif
Guido van Rossumff4949e1992-08-05 19:58:53 +0000404 }
Andrew MacIntyre4e10ed32004-04-04 07:01:35 +0000405 PyMem_Free(f->f_setbuf);
Tim Peters44410012001-09-14 03:26:08 +0000406 Py_XDECREF(f->f_name);
407 Py_XDECREF(f->f_mode);
Martin v. Löwis5467d4c2003-05-10 07:10:12 +0000408 Py_XDECREF(f->f_encoding);
Guido van Rossum7a6e9592002-08-06 15:55:28 +0000409 drop_readahead(f);
Guido van Rossum9475a232001-10-05 20:51:39 +0000410 f->ob_type->tp_free((PyObject *)f);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000411}
412
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000413static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000414file_repr(PyFileObject *f)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000415{
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000416 if (PyUnicode_Check(f->f_name)) {
Martin v. Löwis0073f2e2002-11-21 23:52:35 +0000417#ifdef Py_USING_UNICODE
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000418 PyObject *ret = NULL;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000419 PyObject *name = PyUnicode_AsUnicodeEscapeString(f->f_name);
420 const char *name_str = name ? PyString_AsString(name) : "?";
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000421 ret = PyString_FromFormat("<%s file u'%s', mode '%s' at %p>",
422 f->f_fp == NULL ? "closed" : "open",
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000423 name_str,
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000424 PyString_AsString(f->f_mode),
425 f);
426 Py_XDECREF(name);
427 return ret;
Martin v. Löwis0073f2e2002-11-21 23:52:35 +0000428#endif
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000429 } else {
430 return PyString_FromFormat("<%s file '%s', mode '%s' at %p>",
Barry Warsaw7ce36942001-08-24 18:34:26 +0000431 f->f_fp == NULL ? "closed" : "open",
432 PyString_AsString(f->f_name),
433 PyString_AsString(f->f_mode),
434 f);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000435 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000436}
437
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000438static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000439file_close(PyFileObject *f)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000440{
Guido van Rossuma1ab7fa1991-06-04 19:37:39 +0000441 int sts = 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000442 if (f->f_fp != NULL) {
Guido van Rossumff4949e1992-08-05 19:58:53 +0000443 if (f->f_close != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000444 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000445 errno = 0;
Guido van Rossuma1ab7fa1991-06-04 19:37:39 +0000446 sts = (*f->f_close)(f->f_fp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000447 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000448 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000449 f->f_fp = NULL;
450 }
Martin v. Löwis7bbcde72003-09-07 20:42:29 +0000451 PyMem_Free(f->f_setbuf);
Andrew MacIntyre4e10ed32004-04-04 07:01:35 +0000452 f->f_setbuf = NULL;
Guido van Rossumfebd5511992-03-04 16:39:24 +0000453 if (sts == EOF)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000454 return PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossuma1ab7fa1991-06-04 19:37:39 +0000455 if (sts != 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000456 return PyInt_FromLong((long)sts);
457 Py_INCREF(Py_None);
458 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000459}
460
Trent Mickf29f47b2000-08-11 19:02:59 +0000461
Guido van Rossumb8552162001-09-05 14:58:11 +0000462/* Our very own off_t-like type, 64-bit if possible */
463#if !defined(HAVE_LARGEFILE_SUPPORT)
464typedef off_t Py_off_t;
465#elif SIZEOF_OFF_T >= 8
466typedef off_t Py_off_t;
467#elif SIZEOF_FPOS_T >= 8
Guido van Rossum4f53da02001-03-01 18:26:53 +0000468typedef fpos_t Py_off_t;
469#else
Guido van Rossumb8552162001-09-05 14:58:11 +0000470#error "Large file support, but neither off_t nor fpos_t is large enough."
Guido van Rossum4f53da02001-03-01 18:26:53 +0000471#endif
472
473
Trent Mickf29f47b2000-08-11 19:02:59 +0000474/* a portable fseek() function
475 return 0 on success, non-zero on failure (with errno set) */
Guido van Rossumf68d8e52001-04-14 17:55:09 +0000476static int
Guido van Rossum4f53da02001-03-01 18:26:53 +0000477_portable_fseek(FILE *fp, Py_off_t offset, int whence)
Trent Mickf29f47b2000-08-11 19:02:59 +0000478{
Guido van Rossumb8552162001-09-05 14:58:11 +0000479#if !defined(HAVE_LARGEFILE_SUPPORT)
480 return fseek(fp, offset, whence);
481#elif defined(HAVE_FSEEKO) && SIZEOF_OFF_T >= 8
Trent Mickf29f47b2000-08-11 19:02:59 +0000482 return fseeko(fp, offset, whence);
483#elif defined(HAVE_FSEEK64)
484 return fseek64(fp, offset, whence);
Fred Drakedb810ac2000-10-06 20:42:33 +0000485#elif defined(__BEOS__)
486 return _fseek(fp, offset, whence);
Guido van Rossumb8552162001-09-05 14:58:11 +0000487#elif SIZEOF_FPOS_T >= 8
Guido van Rossume54e0be2001-01-16 20:53:31 +0000488 /* lacking a 64-bit capable fseek(), use a 64-bit capable fsetpos()
489 and fgetpos() to implement fseek()*/
Trent Mickf29f47b2000-08-11 19:02:59 +0000490 fpos_t pos;
491 switch (whence) {
Guido van Rossume54e0be2001-01-16 20:53:31 +0000492 case SEEK_END:
Guido van Rossum8b4e43e2001-09-10 20:43:35 +0000493#ifdef MS_WINDOWS
494 fflush(fp);
495 if (_lseeki64(fileno(fp), 0, 2) == -1)
496 return -1;
497#else
Guido van Rossume54e0be2001-01-16 20:53:31 +0000498 if (fseek(fp, 0, SEEK_END) != 0)
499 return -1;
Guido van Rossum8b4e43e2001-09-10 20:43:35 +0000500#endif
Guido van Rossume54e0be2001-01-16 20:53:31 +0000501 /* fall through */
502 case SEEK_CUR:
503 if (fgetpos(fp, &pos) != 0)
504 return -1;
505 offset += pos;
506 break;
507 /* case SEEK_SET: break; */
Trent Mickf29f47b2000-08-11 19:02:59 +0000508 }
509 return fsetpos(fp, &offset);
510#else
Guido van Rossumb8552162001-09-05 14:58:11 +0000511#error "Large file support, but no way to fseek."
Trent Mickf29f47b2000-08-11 19:02:59 +0000512#endif
513}
514
515
516/* a portable ftell() function
517 Return -1 on failure with errno set appropriately, current file
518 position on success */
Guido van Rossumf68d8e52001-04-14 17:55:09 +0000519static Py_off_t
Fred Drake8ce159a2000-08-31 05:18:54 +0000520_portable_ftell(FILE* fp)
Trent Mickf29f47b2000-08-11 19:02:59 +0000521{
Guido van Rossumb8552162001-09-05 14:58:11 +0000522#if !defined(HAVE_LARGEFILE_SUPPORT)
523 return ftell(fp);
524#elif defined(HAVE_FTELLO) && SIZEOF_OFF_T >= 8
525 return ftello(fp);
526#elif defined(HAVE_FTELL64)
527 return ftell64(fp);
528#elif SIZEOF_FPOS_T >= 8
Trent Mickf29f47b2000-08-11 19:02:59 +0000529 fpos_t pos;
530 if (fgetpos(fp, &pos) != 0)
531 return -1;
532 return pos;
533#else
Guido van Rossumb8552162001-09-05 14:58:11 +0000534#error "Large file support, but no way to ftell."
Trent Mickf29f47b2000-08-11 19:02:59 +0000535#endif
536}
537
538
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000539static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000540file_seek(PyFileObject *f, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000541{
Guido van Rossumd7297e61992-07-06 14:19:26 +0000542 int whence;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000543 int ret;
Guido van Rossum4f53da02001-03-01 18:26:53 +0000544 Py_off_t offset;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000545 PyObject *offobj, *off_index;
Tim Peters86821b22001-01-07 21:19:34 +0000546
Guido van Rossumd7297e61992-07-06 14:19:26 +0000547 if (f->f_fp == NULL)
548 return err_closed();
Guido van Rossum7a6e9592002-08-06 15:55:28 +0000549 drop_readahead(f);
Guido van Rossumd7297e61992-07-06 14:19:26 +0000550 whence = 0;
Guido van Rossum43713e52000-02-29 13:59:29 +0000551 if (!PyArg_ParseTuple(args, "O|i:seek", &offobj, &whence))
Guido van Rossum3c9fe0c1999-01-06 18:51:17 +0000552 return NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +0000553 off_index = PyNumber_Index(offobj);
554 if (!off_index) {
555 if (!PyFloat_Check(offobj))
556 return NULL;
557 /* Deprecated in 2.6 */
558 PyErr_Clear();
559 if (PyErr_Warn(PyExc_DeprecationWarning,
560 "integer argument expected, got float"))
561 return NULL;
562 off_index = offobj;
563 Py_INCREF(offobj);
564 }
Guido van Rossum3c9fe0c1999-01-06 18:51:17 +0000565#if !defined(HAVE_LARGEFILE_SUPPORT)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000566 offset = PyInt_AsLong(off_index);
Guido van Rossum3c9fe0c1999-01-06 18:51:17 +0000567#else
Thomas Wouters89f507f2006-12-13 04:49:30 +0000568 offset = PyLong_Check(off_index) ?
569 PyLong_AsLongLong(off_index) : PyInt_AsLong(off_index);
Guido van Rossum3c9fe0c1999-01-06 18:51:17 +0000570#endif
Thomas Wouters89f507f2006-12-13 04:49:30 +0000571 Py_DECREF(off_index);
Guido van Rossum3c9fe0c1999-01-06 18:51:17 +0000572 if (PyErr_Occurred())
Guido van Rossum88303191999-01-04 17:22:18 +0000573 return NULL;
Tim Peters86821b22001-01-07 21:19:34 +0000574
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000575 Py_BEGIN_ALLOW_THREADS
Guido van Rossumce5ba841991-03-06 13:06:18 +0000576 errno = 0;
Trent Mickf29f47b2000-08-11 19:02:59 +0000577 ret = _portable_fseek(f->f_fp, offset, whence);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000578 Py_END_ALLOW_THREADS
Trent Mickf29f47b2000-08-11 19:02:59 +0000579
Guido van Rossumff4949e1992-08-05 19:58:53 +0000580 if (ret != 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000581 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossumfebd5511992-03-04 16:39:24 +0000582 clearerr(f->f_fp);
583 return NULL;
Guido van Rossumce5ba841991-03-06 13:06:18 +0000584 }
Jack Jansen7b8c7542002-04-14 20:12:41 +0000585 f->f_skipnextlf = 0;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000586 Py_INCREF(Py_None);
587 return Py_None;
Guido van Rossumce5ba841991-03-06 13:06:18 +0000588}
589
Trent Mickf29f47b2000-08-11 19:02:59 +0000590
Guido van Rossumd7047b31995-01-02 19:07:15 +0000591#ifdef HAVE_FTRUNCATE
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000592static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000593file_truncate(PyFileObject *f, PyObject *args)
Guido van Rossumd7047b31995-01-02 19:07:15 +0000594{
Guido van Rossum4f53da02001-03-01 18:26:53 +0000595 Py_off_t newsize;
Tim Petersf1827cf2003-09-07 03:30:18 +0000596 PyObject *newsizeobj = NULL;
597 Py_off_t initialpos;
598 int ret;
Tim Peters86821b22001-01-07 21:19:34 +0000599
Guido van Rossumd7047b31995-01-02 19:07:15 +0000600 if (f->f_fp == NULL)
601 return err_closed();
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000602 if (!PyArg_UnpackTuple(args, "truncate", 0, 1, &newsizeobj))
Guido van Rossum88303191999-01-04 17:22:18 +0000603 return NULL;
Tim Petersfb05db22002-03-11 00:24:00 +0000604
Tim Petersf1827cf2003-09-07 03:30:18 +0000605 /* Get current file position. If the file happens to be open for
606 * update and the last operation was an input operation, C doesn't
607 * define what the later fflush() will do, but we promise truncate()
608 * won't change the current position (and fflush() *does* change it
609 * then at least on Windows). The easiest thing is to capture
610 * current pos now and seek back to it at the end.
611 */
612 Py_BEGIN_ALLOW_THREADS
613 errno = 0;
614 initialpos = _portable_ftell(f->f_fp);
615 Py_END_ALLOW_THREADS
616 if (initialpos == -1)
617 goto onioerror;
618
Tim Petersfb05db22002-03-11 00:24:00 +0000619 /* Set newsize to current postion if newsizeobj NULL, else to the
Tim Petersf1827cf2003-09-07 03:30:18 +0000620 * specified value.
621 */
Guido van Rossum3c9fe0c1999-01-06 18:51:17 +0000622 if (newsizeobj != NULL) {
623#if !defined(HAVE_LARGEFILE_SUPPORT)
624 newsize = PyInt_AsLong(newsizeobj);
625#else
626 newsize = PyLong_Check(newsizeobj) ?
627 PyLong_AsLongLong(newsizeobj) :
628 PyInt_AsLong(newsizeobj);
629#endif
630 if (PyErr_Occurred())
631 return NULL;
Tim Petersfb05db22002-03-11 00:24:00 +0000632 }
Tim Petersf1827cf2003-09-07 03:30:18 +0000633 else /* default to current position */
634 newsize = initialpos;
Tim Petersfb05db22002-03-11 00:24:00 +0000635
Tim Petersf1827cf2003-09-07 03:30:18 +0000636 /* Flush the stream. We're mixing stream-level I/O with lower-level
637 * I/O, and a flush may be necessary to synch both platform views
638 * of the current file state.
639 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000640 Py_BEGIN_ALLOW_THREADS
Guido van Rossumd7047b31995-01-02 19:07:15 +0000641 errno = 0;
642 ret = fflush(f->f_fp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000643 Py_END_ALLOW_THREADS
Tim Petersfb05db22002-03-11 00:24:00 +0000644 if (ret != 0)
645 goto onioerror;
Trent Mickf29f47b2000-08-11 19:02:59 +0000646
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000647#ifdef MS_WINDOWS
Tim Petersfb05db22002-03-11 00:24:00 +0000648 /* MS _chsize doesn't work if newsize doesn't fit in 32 bits,
Tim Peters8f01b682002-03-12 03:04:44 +0000649 so don't even try using it. */
Tim Petersfb05db22002-03-11 00:24:00 +0000650 {
Tim Petersfb05db22002-03-11 00:24:00 +0000651 HANDLE hFile;
Tim Petersfb05db22002-03-11 00:24:00 +0000652
Tim Petersf1827cf2003-09-07 03:30:18 +0000653 /* Have to move current pos to desired endpoint on Windows. */
654 Py_BEGIN_ALLOW_THREADS
655 errno = 0;
656 ret = _portable_fseek(f->f_fp, newsize, SEEK_SET) != 0;
657 Py_END_ALLOW_THREADS
658 if (ret)
659 goto onioerror;
Tim Petersfb05db22002-03-11 00:24:00 +0000660
Tim Peters8f01b682002-03-12 03:04:44 +0000661 /* Truncate. Note that this may grow the file! */
662 Py_BEGIN_ALLOW_THREADS
663 errno = 0;
664 hFile = (HANDLE)_get_osfhandle(fileno(f->f_fp));
Tim Petersf1827cf2003-09-07 03:30:18 +0000665 ret = hFile == (HANDLE)-1;
666 if (ret == 0) {
667 ret = SetEndOfFile(hFile) == 0;
668 if (ret)
Tim Peters8f01b682002-03-12 03:04:44 +0000669 errno = EACCES;
670 }
671 Py_END_ALLOW_THREADS
Tim Petersf1827cf2003-09-07 03:30:18 +0000672 if (ret)
Tim Peters8f01b682002-03-12 03:04:44 +0000673 goto onioerror;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000674 }
Trent Mickf29f47b2000-08-11 19:02:59 +0000675#else
676 Py_BEGIN_ALLOW_THREADS
677 errno = 0;
678 ret = ftruncate(fileno(f->f_fp), newsize);
679 Py_END_ALLOW_THREADS
Tim Petersf1827cf2003-09-07 03:30:18 +0000680 if (ret != 0)
681 goto onioerror;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000682#endif /* !MS_WINDOWS */
Tim Peters86821b22001-01-07 21:19:34 +0000683
Tim Petersf1827cf2003-09-07 03:30:18 +0000684 /* Restore original file position. */
685 Py_BEGIN_ALLOW_THREADS
686 errno = 0;
687 ret = _portable_fseek(f->f_fp, initialpos, SEEK_SET) != 0;
688 Py_END_ALLOW_THREADS
689 if (ret)
690 goto onioerror;
691
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000692 Py_INCREF(Py_None);
693 return Py_None;
Trent Mickf29f47b2000-08-11 19:02:59 +0000694
695onioerror:
696 PyErr_SetFromErrno(PyExc_IOError);
697 clearerr(f->f_fp);
698 return NULL;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000699}
700#endif /* HAVE_FTRUNCATE */
701
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000702static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000703file_tell(PyFileObject *f)
Guido van Rossumce5ba841991-03-06 13:06:18 +0000704{
Guido van Rossum4f53da02001-03-01 18:26:53 +0000705 Py_off_t pos;
Trent Mickf29f47b2000-08-11 19:02:59 +0000706
Guido van Rossumd7297e61992-07-06 14:19:26 +0000707 if (f->f_fp == NULL)
708 return err_closed();
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000709 Py_BEGIN_ALLOW_THREADS
Guido van Rossumce5ba841991-03-06 13:06:18 +0000710 errno = 0;
Trent Mickf29f47b2000-08-11 19:02:59 +0000711 pos = _portable_ftell(f->f_fp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000712 Py_END_ALLOW_THREADS
Trent Mickf29f47b2000-08-11 19:02:59 +0000713 if (pos == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000714 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossumfebd5511992-03-04 16:39:24 +0000715 clearerr(f->f_fp);
716 return NULL;
Guido van Rossumce5ba841991-03-06 13:06:18 +0000717 }
Jack Jansen7b8c7542002-04-14 20:12:41 +0000718 if (f->f_skipnextlf) {
719 int c;
720 c = GETC(f->f_fp);
721 if (c == '\n') {
722 pos++;
723 f->f_skipnextlf = 0;
724 } else if (c != EOF) ungetc(c, f->f_fp);
725 }
Guido van Rossum3c9fe0c1999-01-06 18:51:17 +0000726#if !defined(HAVE_LARGEFILE_SUPPORT)
Trent Mickf29f47b2000-08-11 19:02:59 +0000727 return PyInt_FromLong(pos);
Guido van Rossum3c9fe0c1999-01-06 18:51:17 +0000728#else
Trent Mickf29f47b2000-08-11 19:02:59 +0000729 return PyLong_FromLongLong(pos);
Guido van Rossum3c9fe0c1999-01-06 18:51:17 +0000730#endif
Guido van Rossumce5ba841991-03-06 13:06:18 +0000731}
732
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000733static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000734file_fileno(PyFileObject *f)
Guido van Rossumed233a51992-06-23 09:07:03 +0000735{
Guido van Rossumd7297e61992-07-06 14:19:26 +0000736 if (f->f_fp == NULL)
737 return err_closed();
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000738 return PyInt_FromLong((long) fileno(f->f_fp));
Guido van Rossumed233a51992-06-23 09:07:03 +0000739}
740
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000741static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000742file_flush(PyFileObject *f)
Guido van Rossumce5ba841991-03-06 13:06:18 +0000743{
Guido van Rossumff4949e1992-08-05 19:58:53 +0000744 int res;
Tim Peters86821b22001-01-07 21:19:34 +0000745
Guido van Rossumd7297e61992-07-06 14:19:26 +0000746 if (f->f_fp == NULL)
747 return err_closed();
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000748 Py_BEGIN_ALLOW_THREADS
Guido van Rossumce5ba841991-03-06 13:06:18 +0000749 errno = 0;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000750 res = fflush(f->f_fp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000751 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000752 if (res != 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000753 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossumfebd5511992-03-04 16:39:24 +0000754 clearerr(f->f_fp);
755 return NULL;
Guido van Rossumce5ba841991-03-06 13:06:18 +0000756 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000757 Py_INCREF(Py_None);
758 return Py_None;
Guido van Rossumce5ba841991-03-06 13:06:18 +0000759}
760
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000761static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000762file_isatty(PyFileObject *f)
Guido van Rossuma1ab7fa1991-06-04 19:37:39 +0000763{
Guido van Rossumff4949e1992-08-05 19:58:53 +0000764 long res;
Guido van Rossumd7297e61992-07-06 14:19:26 +0000765 if (f->f_fp == NULL)
766 return err_closed();
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000767 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000768 res = isatty((int)fileno(f->f_fp));
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000769 Py_END_ALLOW_THREADS
Guido van Rossum7f7666f2002-04-07 06:28:00 +0000770 return PyBool_FromLong(res);
Guido van Rossuma1ab7fa1991-06-04 19:37:39 +0000771}
772
Guido van Rossumff7e83d1999-08-27 20:39:37 +0000773
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000774#if BUFSIZ < 8192
775#define SMALLCHUNK 8192
776#else
777#define SMALLCHUNK BUFSIZ
778#endif
779
Guido van Rossum3c259041999-01-14 19:00:14 +0000780#if SIZEOF_INT < 4
781#define BIGCHUNK (512 * 32)
782#else
783#define BIGCHUNK (512 * 1024)
784#endif
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000785
786static size_t
Fred Drakefd99de62000-07-09 05:02:18 +0000787new_buffersize(PyFileObject *f, size_t currentsize)
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000788{
789#ifdef HAVE_FSTAT
Fred Drake1bc8fab2001-07-19 21:49:38 +0000790 off_t pos, end;
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000791 struct stat st;
792 if (fstat(fileno(f->f_fp), &st) == 0) {
793 end = st.st_size;
Guido van Rossumcada2931998-12-11 20:44:56 +0000794 /* The following is not a bug: we really need to call lseek()
795 *and* ftell(). The reason is that some stdio libraries
796 mistakenly flush their buffer when ftell() is called and
797 the lseek() call it makes fails, thereby throwing away
798 data that cannot be recovered in any way. To avoid this,
799 we first test lseek(), and only call ftell() if lseek()
800 works. We can't use the lseek() value either, because we
801 need to take the amount of buffered data into account.
802 (Yet another reason why stdio stinks. :-) */
Guido van Rossum91aaa921998-05-05 22:21:35 +0000803 pos = lseek(fileno(f->f_fp), 0L, SEEK_CUR);
Jack Jansen2771b5b2001-10-10 22:03:27 +0000804 if (pos >= 0) {
Guido van Rossum91aaa921998-05-05 22:21:35 +0000805 pos = ftell(f->f_fp);
Jack Jansen2771b5b2001-10-10 22:03:27 +0000806 }
Guido van Rossumd30dc0a1998-04-27 19:01:08 +0000807 if (pos < 0)
808 clearerr(f->f_fp);
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000809 if (end > pos && pos >= 0)
Guido van Rossumcada2931998-12-11 20:44:56 +0000810 return currentsize + end - pos + 1;
Guido van Rossumdcb5e7f1998-03-03 22:36:10 +0000811 /* Add 1 so if the file were to grow we'd notice. */
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000812 }
813#endif
814 if (currentsize > SMALLCHUNK) {
815 /* Keep doubling until we reach BIGCHUNK;
816 then keep adding BIGCHUNK. */
817 if (currentsize <= BIGCHUNK)
818 return currentsize + currentsize;
819 else
820 return currentsize + BIGCHUNK;
821 }
822 return currentsize + SMALLCHUNK;
823}
824
Gustavo Niemeyer786ddb22002-12-16 18:12:53 +0000825#if defined(EWOULDBLOCK) && defined(EAGAIN) && EWOULDBLOCK != EAGAIN
826#define BLOCKED_ERRNO(x) ((x) == EWOULDBLOCK || (x) == EAGAIN)
827#else
828#ifdef EWOULDBLOCK
829#define BLOCKED_ERRNO(x) ((x) == EWOULDBLOCK)
830#else
831#ifdef EAGAIN
832#define BLOCKED_ERRNO(x) ((x) == EAGAIN)
833#else
834#define BLOCKED_ERRNO(x) 0
835#endif
836#endif
837#endif
838
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000839static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000840file_read(PyFileObject *f, PyObject *args)
Guido van Rossumce5ba841991-03-06 13:06:18 +0000841{
Guido van Rossum789a1611997-05-10 22:33:55 +0000842 long bytesrequested = -1;
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000843 size_t bytesread, buffersize, chunksize;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000844 PyObject *v;
Tim Peters86821b22001-01-07 21:19:34 +0000845
Guido van Rossumd7297e61992-07-06 14:19:26 +0000846 if (f->f_fp == NULL)
847 return err_closed();
Thomas Woutersc45251a2006-02-12 11:53:32 +0000848 /* refuse to mix with f.next() */
849 if (f->f_buf != NULL &&
850 (f->f_bufend - f->f_bufptr) > 0 &&
851 f->f_buf[0] != '\0')
852 return err_iterbuffered();
Guido van Rossum43713e52000-02-29 13:59:29 +0000853 if (!PyArg_ParseTuple(args, "|l:read", &bytesrequested))
Guido van Rossum789a1611997-05-10 22:33:55 +0000854 return NULL;
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000855 if (bytesrequested < 0)
Guido van Rossumff1ccbf1999-04-10 15:48:23 +0000856 buffersize = new_buffersize(f, (size_t)0);
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000857 else
858 buffersize = bytesrequested;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000859 if (buffersize > PY_SSIZE_T_MAX) {
Trent Mickf29f47b2000-08-11 19:02:59 +0000860 PyErr_SetString(PyExc_OverflowError,
Jeremy Hylton8b735422002-08-14 21:01:41 +0000861 "requested number of bytes is more than a Python string can hold");
Trent Mickf29f47b2000-08-11 19:02:59 +0000862 return NULL;
863 }
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000864 v = PyString_FromStringAndSize((char *)NULL, buffersize);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000865 if (v == NULL)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000866 return NULL;
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000867 bytesread = 0;
Guido van Rossumce5ba841991-03-06 13:06:18 +0000868 for (;;) {
Guido van Rossum6263d541997-05-10 22:07:25 +0000869 Py_BEGIN_ALLOW_THREADS
870 errno = 0;
Jack Jansen7b8c7542002-04-14 20:12:41 +0000871 chunksize = Py_UniversalNewlineFread(BUF(v) + bytesread,
Jeremy Hylton8b735422002-08-14 21:01:41 +0000872 buffersize - bytesread, f->f_fp, (PyObject *)f);
Guido van Rossum6263d541997-05-10 22:07:25 +0000873 Py_END_ALLOW_THREADS
874 if (chunksize == 0) {
875 if (!ferror(f->f_fp))
876 break;
Guido van Rossum6263d541997-05-10 22:07:25 +0000877 clearerr(f->f_fp);
Gustavo Niemeyer786ddb22002-12-16 18:12:53 +0000878 /* When in non-blocking mode, data shouldn't
879 * be discarded if a blocking signal was
880 * received. That will also happen if
881 * chunksize != 0, but bytesread < buffersize. */
882 if (bytesread > 0 && BLOCKED_ERRNO(errno))
883 break;
884 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum6263d541997-05-10 22:07:25 +0000885 Py_DECREF(v);
886 return NULL;
887 }
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000888 bytesread += chunksize;
Gustavo Niemeyer786ddb22002-12-16 18:12:53 +0000889 if (bytesread < buffersize) {
890 clearerr(f->f_fp);
Guido van Rossumce5ba841991-03-06 13:06:18 +0000891 break;
Gustavo Niemeyer786ddb22002-12-16 18:12:53 +0000892 }
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000893 if (bytesrequested < 0) {
Guido van Rossumcada2931998-12-11 20:44:56 +0000894 buffersize = new_buffersize(f, buffersize);
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000895 if (_PyString_Resize(&v, buffersize) < 0)
Guido van Rossumce5ba841991-03-06 13:06:18 +0000896 return NULL;
Gustavo Niemeyer786ddb22002-12-16 18:12:53 +0000897 } else {
Gustavo Niemeyera080be82002-12-17 17:48:00 +0000898 /* Got what was requested. */
Gustavo Niemeyer786ddb22002-12-16 18:12:53 +0000899 break;
Guido van Rossumce5ba841991-03-06 13:06:18 +0000900 }
901 }
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000902 if (bytesread != buffersize)
903 _PyString_Resize(&v, bytesread);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000904 return v;
905}
906
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000907static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000908file_readinto(PyFileObject *f, PyObject *args)
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000909{
910 char *ptr;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000911 Py_ssize_t ntodo;
912 Py_ssize_t ndone, nnow;
Tim Peters86821b22001-01-07 21:19:34 +0000913
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000914 if (f->f_fp == NULL)
915 return err_closed();
Guido van Rossumd624f182006-04-24 13:47:05 +0000916 if (!f->f_binary) {
917 PyErr_SetString(PyExc_TypeError,
918 "readinto() requires binary mode");
919 return NULL;
920 }
Thomas Woutersc45251a2006-02-12 11:53:32 +0000921 /* refuse to mix with f.next() */
922 if (f->f_buf != NULL &&
923 (f->f_bufend - f->f_bufptr) > 0 &&
924 f->f_buf[0] != '\0')
925 return err_iterbuffered();
Neal Norwitz62f5a9d2002-04-01 00:09:00 +0000926 if (!PyArg_ParseTuple(args, "w#", &ptr, &ntodo))
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000927 return NULL;
928 ndone = 0;
Guido van Rossum6263d541997-05-10 22:07:25 +0000929 while (ntodo > 0) {
930 Py_BEGIN_ALLOW_THREADS
931 errno = 0;
Tim Petersf1827cf2003-09-07 03:30:18 +0000932 nnow = Py_UniversalNewlineFread(ptr+ndone, ntodo, f->f_fp,
Jeremy Hylton8b735422002-08-14 21:01:41 +0000933 (PyObject *)f);
Guido van Rossum6263d541997-05-10 22:07:25 +0000934 Py_END_ALLOW_THREADS
935 if (nnow == 0) {
936 if (!ferror(f->f_fp))
937 break;
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000938 PyErr_SetFromErrno(PyExc_IOError);
939 clearerr(f->f_fp);
940 return NULL;
941 }
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000942 ndone += nnow;
943 ntodo -= nnow;
944 }
Thomas Wouters89f507f2006-12-13 04:49:30 +0000945 return PyInt_FromSsize_t(ndone);
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000946}
947
Tim Peters86821b22001-01-07 21:19:34 +0000948/**************************************************************************
Tim Petersf29b64d2001-01-15 06:33:19 +0000949Routine to get next line using platform fgets().
Tim Peters86821b22001-01-07 21:19:34 +0000950
951Under MSVC 6:
952
Tim Peters1c733232001-01-08 04:02:07 +0000953+ MS threadsafe getc is very slow (multiple layers of function calls before+
954 after each character, to lock+unlock the stream).
955+ The stream-locking functions are MS-internal -- can't access them from user
956 code.
957+ There's nothing Tim could find in the MS C or platform SDK libraries that
958 can worm around this.
Tim Peters86821b22001-01-07 21:19:34 +0000959+ MS fgets locks/unlocks only once per line; it's the only hook we have.
960
961So we use fgets for speed(!), despite that it's painful.
962
963MS realloc is also slow.
964
Tim Petersf29b64d2001-01-15 06:33:19 +0000965Reports from other platforms on this method vs getc_unlocked (which MS doesn't
966have):
967 Linux a wash
968 Solaris a wash
969 Tru64 Unix getline_via_fgets significantly faster
Tim Peters86821b22001-01-07 21:19:34 +0000970
Tim Petersf29b64d2001-01-15 06:33:19 +0000971CAUTION: The C std isn't clear about this: in those cases where fgets
972writes something into the buffer, can it write into any position beyond the
973required trailing null byte? MSVC 6 fgets does not, and no platform is (yet)
974known on which it does; and it would be a strange way to code fgets. Still,
975getline_via_fgets may not work correctly if it does. The std test
976test_bufio.py should fail if platform fgets() routinely writes beyond the
977trailing null byte. #define DONT_USE_FGETS_IN_GETLINE to disable this code.
Tim Peters86821b22001-01-07 21:19:34 +0000978**************************************************************************/
979
Tim Petersf29b64d2001-01-15 06:33:19 +0000980/* Use this routine if told to, or by default on non-get_unlocked()
981 * platforms unless told not to. Yikes! Let's spell that out:
982 * On a platform with getc_unlocked():
983 * By default, use getc_unlocked().
984 * If you want to use fgets() instead, #define USE_FGETS_IN_GETLINE.
985 * On a platform without getc_unlocked():
986 * By default, use fgets().
987 * If you don't want to use fgets(), #define DONT_USE_FGETS_IN_GETLINE.
988 */
989#if !defined(USE_FGETS_IN_GETLINE) && !defined(HAVE_GETC_UNLOCKED)
990#define USE_FGETS_IN_GETLINE
Tim Peters86821b22001-01-07 21:19:34 +0000991#endif
992
Tim Petersf29b64d2001-01-15 06:33:19 +0000993#if defined(DONT_USE_FGETS_IN_GETLINE) && defined(USE_FGETS_IN_GETLINE)
994#undef USE_FGETS_IN_GETLINE
995#endif
996
997#ifdef USE_FGETS_IN_GETLINE
Tim Peters86821b22001-01-07 21:19:34 +0000998static PyObject*
Tim Petersf29b64d2001-01-15 06:33:19 +0000999getline_via_fgets(FILE *fp)
Tim Peters86821b22001-01-07 21:19:34 +00001000{
Tim Peters15b83852001-01-08 00:53:12 +00001001/* INITBUFSIZE is the maximum line length that lets us get away with the fast
Tim Peters142297a2001-01-15 10:36:56 +00001002 * no-realloc, one-fgets()-call path. Boosting it isn't free, because we have
1003 * to fill this much of the buffer with a known value in order to figure out
1004 * how much of the buffer fgets() overwrites. So if INITBUFSIZE is larger
1005 * than "most" lines, we waste time filling unused buffer slots. 100 is
1006 * surely adequate for most peoples' email archives, chewing over source code,
1007 * etc -- "regular old text files".
1008 * MAXBUFSIZE is the maximum line length that lets us get away with the less
1009 * fast (but still zippy) no-realloc, two-fgets()-call path. See above for
1010 * cautions about boosting that. 300 was chosen because the worst real-life
1011 * text-crunching job reported on Python-Dev was a mail-log crawler where over
1012 * half the lines were 254 chars.
Tim Peters15b83852001-01-08 00:53:12 +00001013 */
Tim Peters142297a2001-01-15 10:36:56 +00001014#define INITBUFSIZE 100
1015#define MAXBUFSIZE 300
Tim Peters142297a2001-01-15 10:36:56 +00001016 char* p; /* temp */
1017 char buf[MAXBUFSIZE];
Tim Peters86821b22001-01-07 21:19:34 +00001018 PyObject* v; /* the string object result */
Tim Peters86821b22001-01-07 21:19:34 +00001019 char* pvfree; /* address of next free slot */
1020 char* pvend; /* address one beyond last free slot */
Tim Peters142297a2001-01-15 10:36:56 +00001021 size_t nfree; /* # of free buffer slots; pvend-pvfree */
1022 size_t total_v_size; /* total # of slots in buffer */
Tim Petersddea2082002-03-23 10:03:50 +00001023 size_t increment; /* amount to increment the buffer */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001024 size_t prev_v_size;
Tim Peters86821b22001-01-07 21:19:34 +00001025
Tim Peters15b83852001-01-08 00:53:12 +00001026 /* Optimize for normal case: avoid _PyString_Resize if at all
Tim Peters142297a2001-01-15 10:36:56 +00001027 * possible via first reading into stack buffer "buf".
Tim Peters15b83852001-01-08 00:53:12 +00001028 */
Tim Peters142297a2001-01-15 10:36:56 +00001029 total_v_size = INITBUFSIZE; /* start small and pray */
1030 pvfree = buf;
1031 for (;;) {
1032 Py_BEGIN_ALLOW_THREADS
1033 pvend = buf + total_v_size;
1034 nfree = pvend - pvfree;
1035 memset(pvfree, '\n', nfree);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001036 assert(nfree < INT_MAX); /* Should be atmost MAXBUFSIZE */
1037 p = fgets(pvfree, (int)nfree, fp);
Tim Peters142297a2001-01-15 10:36:56 +00001038 Py_END_ALLOW_THREADS
Tim Peters15b83852001-01-08 00:53:12 +00001039
Tim Peters142297a2001-01-15 10:36:56 +00001040 if (p == NULL) {
1041 clearerr(fp);
1042 if (PyErr_CheckSignals())
1043 return NULL;
1044 v = PyString_FromStringAndSize(buf, pvfree - buf);
Tim Peters86821b22001-01-07 21:19:34 +00001045 return v;
1046 }
Tim Peters142297a2001-01-15 10:36:56 +00001047 /* fgets read *something* */
1048 p = memchr(pvfree, '\n', nfree);
1049 if (p != NULL) {
1050 /* Did the \n come from fgets or from us?
1051 * Since fgets stops at the first \n, and then writes
1052 * \0, if it's from fgets a \0 must be next. But if
1053 * that's so, it could not have come from us, since
1054 * the \n's we filled the buffer with have only more
1055 * \n's to the right.
1056 */
1057 if (p+1 < pvend && *(p+1) == '\0') {
1058 /* It's from fgets: we win! In particular,
1059 * we haven't done any mallocs yet, and can
1060 * build the final result on the first try.
1061 */
1062 ++p; /* include \n from fgets */
1063 }
1064 else {
1065 /* Must be from us: fgets didn't fill the
1066 * buffer and didn't find a newline, so it
1067 * must be the last and newline-free line of
1068 * the file.
1069 */
1070 assert(p > pvfree && *(p-1) == '\0');
1071 --p; /* don't include \0 from fgets */
1072 }
1073 v = PyString_FromStringAndSize(buf, p - buf);
1074 return v;
1075 }
1076 /* yuck: fgets overwrote all the newlines, i.e. the entire
1077 * buffer. So this line isn't over yet, or maybe it is but
1078 * we're exactly at EOF. If we haven't already, try using the
1079 * rest of the stack buffer.
Tim Peters86821b22001-01-07 21:19:34 +00001080 */
Tim Peters142297a2001-01-15 10:36:56 +00001081 assert(*(pvend-1) == '\0');
1082 if (pvfree == buf) {
1083 pvfree = pvend - 1; /* overwrite trailing null */
1084 total_v_size = MAXBUFSIZE;
1085 }
1086 else
1087 break;
Tim Peters86821b22001-01-07 21:19:34 +00001088 }
Tim Peters142297a2001-01-15 10:36:56 +00001089
1090 /* The stack buffer isn't big enough; malloc a string object and read
1091 * into its buffer.
Tim Peters15b83852001-01-08 00:53:12 +00001092 */
Tim Petersddea2082002-03-23 10:03:50 +00001093 total_v_size = MAXBUFSIZE << 1;
Tim Peters1c733232001-01-08 04:02:07 +00001094 v = PyString_FromStringAndSize((char*)NULL, (int)total_v_size);
Tim Peters15b83852001-01-08 00:53:12 +00001095 if (v == NULL)
1096 return v;
1097 /* copy over everything except the last null byte */
Tim Peters142297a2001-01-15 10:36:56 +00001098 memcpy(BUF(v), buf, MAXBUFSIZE-1);
1099 pvfree = BUF(v) + MAXBUFSIZE - 1;
Tim Peters86821b22001-01-07 21:19:34 +00001100
1101 /* Keep reading stuff into v; if it ever ends successfully, break
Tim Peters15b83852001-01-08 00:53:12 +00001102 * after setting p one beyond the end of the line. The code here is
1103 * very much like the code above, except reads into v's buffer; see
1104 * the code above for detailed comments about the logic.
Tim Peters86821b22001-01-07 21:19:34 +00001105 */
1106 for (;;) {
Tim Peters86821b22001-01-07 21:19:34 +00001107 Py_BEGIN_ALLOW_THREADS
1108 pvend = BUF(v) + total_v_size;
1109 nfree = pvend - pvfree;
1110 memset(pvfree, '\n', nfree);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001111 assert(nfree < INT_MAX);
1112 p = fgets(pvfree, (int)nfree, fp);
Tim Peters86821b22001-01-07 21:19:34 +00001113 Py_END_ALLOW_THREADS
1114
1115 if (p == NULL) {
1116 clearerr(fp);
1117 if (PyErr_CheckSignals()) {
1118 Py_DECREF(v);
1119 return NULL;
1120 }
1121 p = pvfree;
1122 break;
1123 }
Tim Peters86821b22001-01-07 21:19:34 +00001124 p = memchr(pvfree, '\n', nfree);
1125 if (p != NULL) {
1126 if (p+1 < pvend && *(p+1) == '\0') {
1127 /* \n came from fgets */
1128 ++p;
1129 break;
1130 }
1131 /* \n came from us; last line of file, no newline */
1132 assert(p > pvfree && *(p-1) == '\0');
1133 --p;
1134 break;
1135 }
1136 /* expand buffer and try again */
1137 assert(*(pvend-1) == '\0');
Tim Petersddea2082002-03-23 10:03:50 +00001138 increment = total_v_size >> 2; /* mild exponential growth */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001139 prev_v_size = total_v_size;
Tim Petersddea2082002-03-23 10:03:50 +00001140 total_v_size += increment;
Thomas Wouters89f507f2006-12-13 04:49:30 +00001141 /* check for overflow */
1142 if (total_v_size <= prev_v_size ||
1143 total_v_size > PY_SSIZE_T_MAX) {
Tim Peters86821b22001-01-07 21:19:34 +00001144 PyErr_SetString(PyExc_OverflowError,
1145 "line is longer than a Python string can hold");
1146 Py_DECREF(v);
1147 return NULL;
1148 }
1149 if (_PyString_Resize(&v, (int)total_v_size) < 0)
1150 return NULL;
1151 /* overwrite the trailing null byte */
Thomas Wouters89f507f2006-12-13 04:49:30 +00001152 pvfree = BUF(v) + (prev_v_size - 1);
Tim Peters86821b22001-01-07 21:19:34 +00001153 }
1154 if (BUF(v) + total_v_size != p)
1155 _PyString_Resize(&v, p - BUF(v));
1156 return v;
1157#undef INITBUFSIZE
Tim Peters142297a2001-01-15 10:36:56 +00001158#undef MAXBUFSIZE
Tim Peters86821b22001-01-07 21:19:34 +00001159}
Tim Petersf29b64d2001-01-15 06:33:19 +00001160#endif /* ifdef USE_FGETS_IN_GETLINE */
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001161
Guido van Rossum0bd24411991-04-04 15:21:57 +00001162/* Internal routine to get a line.
1163 Size argument interpretation:
1164 > 0: max length;
Guido van Rossum86282062001-01-08 01:26:47 +00001165 <= 0: read arbitrary line
Guido van Rossumce5ba841991-03-06 13:06:18 +00001166*/
1167
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001168static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +00001169get_line(PyFileObject *f, int n)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001170{
Guido van Rossum1187aa42001-01-05 14:43:05 +00001171 FILE *fp = f->f_fp;
1172 int c;
Andrew M. Kuchling4b2b4452000-11-29 02:53:22 +00001173 char *buf, *end;
Neil Schemenauer3a204a72002-03-23 19:41:34 +00001174 size_t total_v_size; /* total # of slots in buffer */
1175 size_t used_v_size; /* # used slots in buffer */
1176 size_t increment; /* amount to increment the buffer */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001177 PyObject *v;
Jack Jansen7b8c7542002-04-14 20:12:41 +00001178 int newlinetypes = f->f_newlinetypes;
1179 int skipnextlf = f->f_skipnextlf;
1180 int univ_newline = f->f_univ_newline;
Guido van Rossum0bd24411991-04-04 15:21:57 +00001181
Jack Jansen7b8c7542002-04-14 20:12:41 +00001182#if defined(USE_FGETS_IN_GETLINE)
Jack Jansen7b8c7542002-04-14 20:12:41 +00001183 if (n <= 0 && !univ_newline )
Tim Petersf29b64d2001-01-15 06:33:19 +00001184 return getline_via_fgets(fp);
Tim Peters86821b22001-01-07 21:19:34 +00001185#endif
Neil Schemenauer3a204a72002-03-23 19:41:34 +00001186 total_v_size = n > 0 ? n : 100;
1187 v = PyString_FromStringAndSize((char *)NULL, total_v_size);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001188 if (v == NULL)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001189 return NULL;
Guido van Rossumce5ba841991-03-06 13:06:18 +00001190 buf = BUF(v);
Neil Schemenauer3a204a72002-03-23 19:41:34 +00001191 end = buf + total_v_size;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001192
Guido van Rossumce5ba841991-03-06 13:06:18 +00001193 for (;;) {
Guido van Rossum1187aa42001-01-05 14:43:05 +00001194 Py_BEGIN_ALLOW_THREADS
1195 FLOCKFILE(fp);
Jack Jansen7b8c7542002-04-14 20:12:41 +00001196 if (univ_newline) {
1197 c = 'x'; /* Shut up gcc warning */
1198 while ( buf != end && (c = GETC(fp)) != EOF ) {
1199 if (skipnextlf ) {
1200 skipnextlf = 0;
1201 if (c == '\n') {
Tim Petersf1827cf2003-09-07 03:30:18 +00001202 /* Seeing a \n here with
1203 * skipnextlf true means we
Jeremy Hylton8b735422002-08-14 21:01:41 +00001204 * saw a \r before.
1205 */
Jack Jansen7b8c7542002-04-14 20:12:41 +00001206 newlinetypes |= NEWLINE_CRLF;
1207 c = GETC(fp);
1208 if (c == EOF) break;
1209 } else {
1210 newlinetypes |= NEWLINE_CR;
1211 }
1212 }
1213 if (c == '\r') {
1214 skipnextlf = 1;
1215 c = '\n';
1216 } else if ( c == '\n')
1217 newlinetypes |= NEWLINE_LF;
1218 *buf++ = c;
1219 if (c == '\n') break;
1220 }
1221 if ( c == EOF && skipnextlf )
1222 newlinetypes |= NEWLINE_CR;
1223 } else /* If not universal newlines use the normal loop */
Guido van Rossum1187aa42001-01-05 14:43:05 +00001224 while ((c = GETC(fp)) != EOF &&
1225 (*buf++ = c) != '\n' &&
1226 buf != end)
1227 ;
1228 FUNLOCKFILE(fp);
1229 Py_END_ALLOW_THREADS
Jack Jansen7b8c7542002-04-14 20:12:41 +00001230 f->f_newlinetypes = newlinetypes;
1231 f->f_skipnextlf = skipnextlf;
Guido van Rossum1187aa42001-01-05 14:43:05 +00001232 if (c == '\n')
1233 break;
1234 if (c == EOF) {
Guido van Rossum29206bc2001-08-09 18:14:59 +00001235 if (ferror(fp)) {
1236 PyErr_SetFromErrno(PyExc_IOError);
1237 clearerr(fp);
1238 Py_DECREF(v);
1239 return NULL;
1240 }
Guido van Rossum76ad8ed1991-06-03 10:54:55 +00001241 clearerr(fp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001242 if (PyErr_CheckSignals()) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001243 Py_DECREF(v);
Guido van Rossum0bd24411991-04-04 15:21:57 +00001244 return NULL;
1245 }
Guido van Rossumce5ba841991-03-06 13:06:18 +00001246 break;
Guido van Rossum0bd24411991-04-04 15:21:57 +00001247 }
Guido van Rossum1187aa42001-01-05 14:43:05 +00001248 /* Must be because buf == end */
1249 if (n > 0)
Guido van Rossum0bd24411991-04-04 15:21:57 +00001250 break;
Neil Schemenauer3a204a72002-03-23 19:41:34 +00001251 used_v_size = total_v_size;
1252 increment = total_v_size >> 2; /* mild exponential growth */
1253 total_v_size += increment;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001254 if (total_v_size > PY_SSIZE_T_MAX) {
Guido van Rossum1187aa42001-01-05 14:43:05 +00001255 PyErr_SetString(PyExc_OverflowError,
1256 "line is longer than a Python string can hold");
Tim Peters86821b22001-01-07 21:19:34 +00001257 Py_DECREF(v);
Guido van Rossum1187aa42001-01-05 14:43:05 +00001258 return NULL;
Guido van Rossum0bd24411991-04-04 15:21:57 +00001259 }
Neil Schemenauer3a204a72002-03-23 19:41:34 +00001260 if (_PyString_Resize(&v, total_v_size) < 0)
Guido van Rossum1187aa42001-01-05 14:43:05 +00001261 return NULL;
Neil Schemenauer3a204a72002-03-23 19:41:34 +00001262 buf = BUF(v) + used_v_size;
1263 end = BUF(v) + total_v_size;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001264 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001265
Neil Schemenauer3a204a72002-03-23 19:41:34 +00001266 used_v_size = buf - BUF(v);
1267 if (used_v_size != total_v_size)
1268 _PyString_Resize(&v, used_v_size);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001269 return v;
1270}
1271
Guido van Rossum0bd24411991-04-04 15:21:57 +00001272/* External C interface */
1273
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001274PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +00001275PyFile_GetLine(PyObject *f, int n)
Guido van Rossum0bd24411991-04-04 15:21:57 +00001276{
Guido van Rossum4ddf0a02001-01-07 20:51:39 +00001277 PyObject *result;
1278
Guido van Rossum3165fe61992-09-25 21:59:05 +00001279 if (f == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001280 PyErr_BadInternalCall();
Guido van Rossum0bd24411991-04-04 15:21:57 +00001281 return NULL;
1282 }
Guido van Rossum4ddf0a02001-01-07 20:51:39 +00001283
1284 if (PyFile_Check(f)) {
Thomas Woutersc45251a2006-02-12 11:53:32 +00001285 PyFileObject *fo = (PyFileObject *)f;
1286 if (fo->f_fp == NULL)
Guido van Rossum4ddf0a02001-01-07 20:51:39 +00001287 return err_closed();
Thomas Woutersc45251a2006-02-12 11:53:32 +00001288 /* refuse to mix with f.next() */
1289 if (fo->f_buf != NULL &&
1290 (fo->f_bufend - fo->f_bufptr) > 0 &&
1291 fo->f_buf[0] != '\0')
1292 return err_iterbuffered();
1293 result = get_line(fo, n);
Guido van Rossum4ddf0a02001-01-07 20:51:39 +00001294 }
1295 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001296 PyObject *reader;
1297 PyObject *args;
Guido van Rossum4ddf0a02001-01-07 20:51:39 +00001298
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001299 reader = PyObject_GetAttrString(f, "readline");
Guido van Rossum3165fe61992-09-25 21:59:05 +00001300 if (reader == NULL)
1301 return NULL;
1302 if (n <= 0)
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001303 args = PyTuple_New(0);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001304 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001305 args = Py_BuildValue("(i)", n);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001306 if (args == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001307 Py_DECREF(reader);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001308 return NULL;
1309 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001310 result = PyEval_CallObject(reader, args);
1311 Py_DECREF(reader);
1312 Py_DECREF(args);
Martin v. Löwisaf6a27a2003-01-03 19:16:14 +00001313 if (result != NULL && !PyString_Check(result) &&
1314 !PyUnicode_Check(result)) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001315 Py_DECREF(result);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001316 result = NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001317 PyErr_SetString(PyExc_TypeError,
Guido van Rossum3165fe61992-09-25 21:59:05 +00001318 "object.readline() returned non-string");
1319 }
Guido van Rossum4ddf0a02001-01-07 20:51:39 +00001320 }
1321
1322 if (n < 0 && result != NULL && PyString_Check(result)) {
1323 char *s = PyString_AS_STRING(result);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001324 Py_ssize_t len = PyString_GET_SIZE(result);
Guido van Rossum4ddf0a02001-01-07 20:51:39 +00001325 if (len == 0) {
1326 Py_DECREF(result);
1327 result = NULL;
1328 PyErr_SetString(PyExc_EOFError,
1329 "EOF when reading a line");
1330 }
1331 else if (s[len-1] == '\n') {
1332 if (result->ob_refcnt == 1)
1333 _PyString_Resize(&result, len-1);
1334 else {
1335 PyObject *v;
1336 v = PyString_FromStringAndSize(s, len-1);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001337 Py_DECREF(result);
Guido van Rossum4ddf0a02001-01-07 20:51:39 +00001338 result = v;
Guido van Rossum3165fe61992-09-25 21:59:05 +00001339 }
1340 }
Guido van Rossum3165fe61992-09-25 21:59:05 +00001341 }
Martin v. Löwisaf6a27a2003-01-03 19:16:14 +00001342#ifdef Py_USING_UNICODE
1343 if (n < 0 && result != NULL && PyUnicode_Check(result)) {
1344 Py_UNICODE *s = PyUnicode_AS_UNICODE(result);
Martin v. Löwis18e16552006-02-15 17:27:45 +00001345 Py_ssize_t len = PyUnicode_GET_SIZE(result);
Martin v. Löwisaf6a27a2003-01-03 19:16:14 +00001346 if (len == 0) {
1347 Py_DECREF(result);
1348 result = NULL;
1349 PyErr_SetString(PyExc_EOFError,
1350 "EOF when reading a line");
1351 }
1352 else if (s[len-1] == '\n') {
1353 if (result->ob_refcnt == 1)
1354 PyUnicode_Resize(&result, len-1);
1355 else {
1356 PyObject *v;
1357 v = PyUnicode_FromUnicode(s, len-1);
1358 Py_DECREF(result);
1359 result = v;
1360 }
1361 }
1362 }
1363#endif
Guido van Rossum4ddf0a02001-01-07 20:51:39 +00001364 return result;
Guido van Rossum0bd24411991-04-04 15:21:57 +00001365}
1366
1367/* Python method */
1368
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001369static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +00001370file_readline(PyFileObject *f, PyObject *args)
Guido van Rossum0bd24411991-04-04 15:21:57 +00001371{
Guido van Rossum789a1611997-05-10 22:33:55 +00001372 int n = -1;
Guido van Rossum0bd24411991-04-04 15:21:57 +00001373
Guido van Rossumd7297e61992-07-06 14:19:26 +00001374 if (f->f_fp == NULL)
1375 return err_closed();
Thomas Woutersc45251a2006-02-12 11:53:32 +00001376 /* refuse to mix with f.next() */
1377 if (f->f_buf != NULL &&
1378 (f->f_bufend - f->f_bufptr) > 0 &&
1379 f->f_buf[0] != '\0')
1380 return err_iterbuffered();
Guido van Rossum43713e52000-02-29 13:59:29 +00001381 if (!PyArg_ParseTuple(args, "|i:readline", &n))
Guido van Rossum789a1611997-05-10 22:33:55 +00001382 return NULL;
1383 if (n == 0)
1384 return PyString_FromString("");
1385 if (n < 0)
1386 n = 0;
Marc-André Lemburg1f468602000-07-05 15:32:40 +00001387 return get_line(f, n);
Guido van Rossum0bd24411991-04-04 15:21:57 +00001388}
1389
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001390static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +00001391file_readlines(PyFileObject *f, PyObject *args)
Guido van Rossumce5ba841991-03-06 13:06:18 +00001392{
Guido van Rossum789a1611997-05-10 22:33:55 +00001393 long sizehint = 0;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001394 PyObject *list;
1395 PyObject *line;
Guido van Rossum6263d541997-05-10 22:07:25 +00001396 char small_buffer[SMALLCHUNK];
1397 char *buffer = small_buffer;
1398 size_t buffersize = SMALLCHUNK;
1399 PyObject *big_buffer = NULL;
1400 size_t nfilled = 0;
1401 size_t nread;
Guido van Rossum789a1611997-05-10 22:33:55 +00001402 size_t totalread = 0;
Guido van Rossum6263d541997-05-10 22:07:25 +00001403 char *p, *q, *end;
1404 int err;
Guido van Rossum79fd0fc2001-10-12 20:01:53 +00001405 int shortread = 0;
Guido van Rossum0bd24411991-04-04 15:21:57 +00001406
Guido van Rossumd7297e61992-07-06 14:19:26 +00001407 if (f->f_fp == NULL)
1408 return err_closed();
Thomas Woutersc45251a2006-02-12 11:53:32 +00001409 /* refuse to mix with f.next() */
1410 if (f->f_buf != NULL &&
1411 (f->f_bufend - f->f_bufptr) > 0 &&
1412 f->f_buf[0] != '\0')
1413 return err_iterbuffered();
Guido van Rossum43713e52000-02-29 13:59:29 +00001414 if (!PyArg_ParseTuple(args, "|l:readlines", &sizehint))
Guido van Rossum0bd24411991-04-04 15:21:57 +00001415 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001416 if ((list = PyList_New(0)) == NULL)
Guido van Rossumce5ba841991-03-06 13:06:18 +00001417 return NULL;
1418 for (;;) {
Guido van Rossum79fd0fc2001-10-12 20:01:53 +00001419 if (shortread)
1420 nread = 0;
1421 else {
1422 Py_BEGIN_ALLOW_THREADS
1423 errno = 0;
Tim Peters058b1412002-04-21 07:29:14 +00001424 nread = Py_UniversalNewlineFread(buffer+nfilled,
Jack Jansen7b8c7542002-04-14 20:12:41 +00001425 buffersize-nfilled, f->f_fp, (PyObject *)f);
Guido van Rossum79fd0fc2001-10-12 20:01:53 +00001426 Py_END_ALLOW_THREADS
1427 shortread = (nread < buffersize-nfilled);
1428 }
Guido van Rossum6263d541997-05-10 22:07:25 +00001429 if (nread == 0) {
Guido van Rossum789a1611997-05-10 22:33:55 +00001430 sizehint = 0;
Guido van Rossum3da3fce1998-02-19 20:46:48 +00001431 if (!ferror(f->f_fp))
Guido van Rossum6263d541997-05-10 22:07:25 +00001432 break;
1433 PyErr_SetFromErrno(PyExc_IOError);
1434 clearerr(f->f_fp);
1435 error:
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001436 Py_DECREF(list);
Guido van Rossum6263d541997-05-10 22:07:25 +00001437 list = NULL;
1438 goto cleanup;
Guido van Rossumce5ba841991-03-06 13:06:18 +00001439 }
Guido van Rossum789a1611997-05-10 22:33:55 +00001440 totalread += nread;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001441 p = (char *)memchr(buffer+nfilled, '\n', nread);
Guido van Rossum6263d541997-05-10 22:07:25 +00001442 if (p == NULL) {
1443 /* Need a larger buffer to fit this line */
1444 nfilled += nread;
1445 buffersize *= 2;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001446 if (buffersize > PY_SSIZE_T_MAX) {
Trent Mickf29f47b2000-08-11 19:02:59 +00001447 PyErr_SetString(PyExc_OverflowError,
Guido van Rossume07d5cf2001-01-09 21:50:24 +00001448 "line is longer than a Python string can hold");
Trent Mickf29f47b2000-08-11 19:02:59 +00001449 goto error;
1450 }
Guido van Rossum6263d541997-05-10 22:07:25 +00001451 if (big_buffer == NULL) {
1452 /* Create the big buffer */
1453 big_buffer = PyString_FromStringAndSize(
1454 NULL, buffersize);
1455 if (big_buffer == NULL)
1456 goto error;
1457 buffer = PyString_AS_STRING(big_buffer);
1458 memcpy(buffer, small_buffer, nfilled);
1459 }
1460 else {
1461 /* Grow the big buffer */
Jack Jansen7b8c7542002-04-14 20:12:41 +00001462 if ( _PyString_Resize(&big_buffer, buffersize) < 0 )
1463 goto error;
Guido van Rossum6263d541997-05-10 22:07:25 +00001464 buffer = PyString_AS_STRING(big_buffer);
1465 }
1466 continue;
1467 }
1468 end = buffer+nfilled+nread;
1469 q = buffer;
1470 do {
1471 /* Process complete lines */
1472 p++;
1473 line = PyString_FromStringAndSize(q, p-q);
1474 if (line == NULL)
1475 goto error;
1476 err = PyList_Append(list, line);
1477 Py_DECREF(line);
1478 if (err != 0)
1479 goto error;
1480 q = p;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001481 p = (char *)memchr(q, '\n', end-q);
Guido van Rossum6263d541997-05-10 22:07:25 +00001482 } while (p != NULL);
1483 /* Move the remaining incomplete line to the start */
1484 nfilled = end-q;
1485 memmove(buffer, q, nfilled);
Guido van Rossum789a1611997-05-10 22:33:55 +00001486 if (sizehint > 0)
1487 if (totalread >= (size_t)sizehint)
1488 break;
Guido van Rossumce5ba841991-03-06 13:06:18 +00001489 }
Guido van Rossum6263d541997-05-10 22:07:25 +00001490 if (nfilled != 0) {
1491 /* Partial last line */
1492 line = PyString_FromStringAndSize(buffer, nfilled);
1493 if (line == NULL)
1494 goto error;
Guido van Rossum789a1611997-05-10 22:33:55 +00001495 if (sizehint > 0) {
1496 /* Need to complete the last line */
Marc-André Lemburg1f468602000-07-05 15:32:40 +00001497 PyObject *rest = get_line(f, 0);
Guido van Rossum789a1611997-05-10 22:33:55 +00001498 if (rest == NULL) {
1499 Py_DECREF(line);
1500 goto error;
1501 }
1502 PyString_Concat(&line, rest);
1503 Py_DECREF(rest);
1504 if (line == NULL)
1505 goto error;
1506 }
Guido van Rossum6263d541997-05-10 22:07:25 +00001507 err = PyList_Append(list, line);
1508 Py_DECREF(line);
1509 if (err != 0)
1510 goto error;
1511 }
1512 cleanup:
Tim Peters5de98422002-04-27 18:44:32 +00001513 Py_XDECREF(big_buffer);
Guido van Rossumce5ba841991-03-06 13:06:18 +00001514 return list;
1515}
1516
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001517static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +00001518file_write(PyFileObject *f, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001519{
Guido van Rossumd7297e61992-07-06 14:19:26 +00001520 char *s;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001521 Py_ssize_t n, n2;
Guido van Rossumd7297e61992-07-06 14:19:26 +00001522 if (f->f_fp == NULL)
1523 return err_closed();
Michael W. Hudsone2ec3eb2001-10-31 18:51:01 +00001524 if (!PyArg_ParseTuple(args, f->f_binary ? "s#" : "t#", &s, &n))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001525 return NULL;
Guido van Rossumeb183da1991-04-04 10:44:06 +00001526 f->f_softspace = 0;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001527 Py_BEGIN_ALLOW_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001528 errno = 0;
Guido van Rossumd7297e61992-07-06 14:19:26 +00001529 n2 = fwrite(s, 1, n, f->f_fp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001530 Py_END_ALLOW_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001531 if (n2 != n) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001532 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossumfebd5511992-03-04 16:39:24 +00001533 clearerr(f->f_fp);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001534 return NULL;
1535 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001536 Py_INCREF(Py_None);
1537 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001538}
1539
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001540static PyObject *
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001541file_writelines(PyFileObject *f, PyObject *seq)
Guido van Rossum5a2a6831993-10-25 09:59:04 +00001542{
Guido van Rossumee70ad12000-03-13 16:27:06 +00001543#define CHUNKSIZE 1000
1544 PyObject *list, *line;
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001545 PyObject *it; /* iter(seq) */
Guido van Rossumee70ad12000-03-13 16:27:06 +00001546 PyObject *result;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001547 int index, islist;
1548 Py_ssize_t i, j, nwritten, len;
Guido van Rossumee70ad12000-03-13 16:27:06 +00001549
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001550 assert(seq != NULL);
Guido van Rossum5a2a6831993-10-25 09:59:04 +00001551 if (f->f_fp == NULL)
1552 return err_closed();
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001553
1554 result = NULL;
1555 list = NULL;
1556 islist = PyList_Check(seq);
1557 if (islist)
1558 it = NULL;
1559 else {
1560 it = PyObject_GetIter(seq);
1561 if (it == NULL) {
1562 PyErr_SetString(PyExc_TypeError,
1563 "writelines() requires an iterable argument");
1564 return NULL;
1565 }
1566 /* From here on, fail by going to error, to reclaim "it". */
1567 list = PyList_New(CHUNKSIZE);
1568 if (list == NULL)
1569 goto error;
Guido van Rossum5a2a6831993-10-25 09:59:04 +00001570 }
Guido van Rossumee70ad12000-03-13 16:27:06 +00001571
1572 /* Strategy: slurp CHUNKSIZE lines into a private list,
1573 checking that they are all strings, then write that list
1574 without holding the interpreter lock, then come back for more. */
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001575 for (index = 0; ; index += CHUNKSIZE) {
Guido van Rossumee70ad12000-03-13 16:27:06 +00001576 if (islist) {
1577 Py_XDECREF(list);
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001578 list = PyList_GetSlice(seq, index, index+CHUNKSIZE);
Guido van Rossumee70ad12000-03-13 16:27:06 +00001579 if (list == NULL)
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001580 goto error;
Guido van Rossumee70ad12000-03-13 16:27:06 +00001581 j = PyList_GET_SIZE(list);
1582 }
1583 else {
1584 for (j = 0; j < CHUNKSIZE; j++) {
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001585 line = PyIter_Next(it);
Guido van Rossumee70ad12000-03-13 16:27:06 +00001586 if (line == NULL) {
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001587 if (PyErr_Occurred())
1588 goto error;
1589 break;
Guido van Rossumee70ad12000-03-13 16:27:06 +00001590 }
Guido van Rossumee70ad12000-03-13 16:27:06 +00001591 PyList_SetItem(list, j, line);
1592 }
1593 }
1594 if (j == 0)
1595 break;
1596
Marc-André Lemburg6ef68b52000-08-25 22:39:50 +00001597 /* Check that all entries are indeed strings. If not,
1598 apply the same rules as for file.write() and
1599 convert the results to strings. This is slow, but
1600 seems to be the only way since all conversion APIs
1601 could potentially execute Python code. */
1602 for (i = 0; i < j; i++) {
1603 PyObject *v = PyList_GET_ITEM(list, i);
1604 if (!PyString_Check(v)) {
1605 const char *buffer;
Tim Peters86821b22001-01-07 21:19:34 +00001606 if (((f->f_binary &&
Marc-André Lemburg6ef68b52000-08-25 22:39:50 +00001607 PyObject_AsReadBuffer(v,
1608 (const void**)&buffer,
1609 &len)) ||
1610 PyObject_AsCharBuffer(v,
1611 &buffer,
1612 &len))) {
1613 PyErr_SetString(PyExc_TypeError,
Jeremy Hylton8b735422002-08-14 21:01:41 +00001614 "writelines() argument must be a sequence of strings");
Marc-André Lemburg6ef68b52000-08-25 22:39:50 +00001615 goto error;
1616 }
1617 line = PyString_FromStringAndSize(buffer,
1618 len);
1619 if (line == NULL)
1620 goto error;
1621 Py_DECREF(v);
Marc-André Lemburgf5e96fa2000-08-25 22:49:05 +00001622 PyList_SET_ITEM(list, i, line);
Marc-André Lemburg6ef68b52000-08-25 22:39:50 +00001623 }
1624 }
1625
1626 /* Since we are releasing the global lock, the
1627 following code may *not* execute Python code. */
Guido van Rossumee70ad12000-03-13 16:27:06 +00001628 Py_BEGIN_ALLOW_THREADS
1629 f->f_softspace = 0;
1630 errno = 0;
1631 for (i = 0; i < j; i++) {
Marc-André Lemburg6ef68b52000-08-25 22:39:50 +00001632 line = PyList_GET_ITEM(list, i);
Guido van Rossumee70ad12000-03-13 16:27:06 +00001633 len = PyString_GET_SIZE(line);
1634 nwritten = fwrite(PyString_AS_STRING(line),
1635 1, len, f->f_fp);
1636 if (nwritten != len) {
1637 Py_BLOCK_THREADS
1638 PyErr_SetFromErrno(PyExc_IOError);
1639 clearerr(f->f_fp);
1640 goto error;
1641 }
1642 }
1643 Py_END_ALLOW_THREADS
1644
1645 if (j < CHUNKSIZE)
1646 break;
Guido van Rossumee70ad12000-03-13 16:27:06 +00001647 }
1648
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001649 Py_INCREF(Py_None);
Guido van Rossumee70ad12000-03-13 16:27:06 +00001650 result = Py_None;
1651 error:
1652 Py_XDECREF(list);
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001653 Py_XDECREF(it);
Guido van Rossumee70ad12000-03-13 16:27:06 +00001654 return result;
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001655#undef CHUNKSIZE
Guido van Rossum5a2a6831993-10-25 09:59:04 +00001656}
1657
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001658static PyObject *
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00001659file_self(PyFileObject *f)
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001660{
1661 if (f->f_fp == NULL)
1662 return err_closed();
1663 Py_INCREF(f);
1664 return (PyObject *)f;
1665}
1666
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001667static PyObject *
1668file_exit(PyFileObject *f, PyObject *args)
1669{
1670 PyObject *ret = file_close(f);
1671 if (!ret)
1672 /* If error occurred, pass through */
1673 return NULL;
1674 Py_DECREF(ret);
1675 /* We cannot return the result of close since a true
1676 * value will be interpreted as "yes, swallow the
1677 * exception if one was raised inside the with block". */
1678 Py_RETURN_NONE;
1679}
1680
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001681PyDoc_STRVAR(readline_doc,
Tim Petersefc3a3a2001-09-20 07:55:22 +00001682"readline([size]) -> next line from the file, as a string.\n"
1683"\n"
1684"Retain newline. A non-negative size argument limits the maximum\n"
1685"number of bytes to return (an incomplete line may be returned then).\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001686"Return an empty string at EOF.");
Tim Petersefc3a3a2001-09-20 07:55:22 +00001687
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001688PyDoc_STRVAR(read_doc,
Tim Petersefc3a3a2001-09-20 07:55:22 +00001689"read([size]) -> read at most size bytes, returned as a string.\n"
1690"\n"
Gustavo Niemeyer786ddb22002-12-16 18:12:53 +00001691"If the size argument is negative or omitted, read until EOF is reached.\n"
1692"Notice that when in non-blocking mode, less data than what was requested\n"
1693"may be returned, even if no size parameter was given.");
Tim Petersefc3a3a2001-09-20 07:55:22 +00001694
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001695PyDoc_STRVAR(write_doc,
Tim Petersefc3a3a2001-09-20 07:55:22 +00001696"write(str) -> None. Write string str to file.\n"
1697"\n"
1698"Note that due to buffering, flush() or close() may be needed before\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001699"the file on disk reflects the data written.");
Tim Petersefc3a3a2001-09-20 07:55:22 +00001700
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001701PyDoc_STRVAR(fileno_doc,
Tim Petersefc3a3a2001-09-20 07:55:22 +00001702"fileno() -> integer \"file descriptor\".\n"
1703"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001704"This is needed for lower-level file interfaces, such os.read().");
Tim Petersefc3a3a2001-09-20 07:55:22 +00001705
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001706PyDoc_STRVAR(seek_doc,
Tim Petersefc3a3a2001-09-20 07:55:22 +00001707"seek(offset[, whence]) -> None. Move to new file position.\n"
1708"\n"
1709"Argument offset is a byte count. Optional argument whence defaults to\n"
1710"0 (offset from start of file, offset should be >= 0); other values are 1\n"
1711"(move relative to current position, positive or negative), and 2 (move\n"
1712"relative to end of file, usually negative, although many platforms allow\n"
Martin v. Löwis849a9722003-10-18 09:38:01 +00001713"seeking beyond the end of a file). If the file is opened in text mode,\n"
1714"only offsets returned by tell() are legal. Use of other offsets causes\n"
1715"undefined behavior."
Tim Petersefc3a3a2001-09-20 07:55:22 +00001716"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001717"Note that not all file objects are seekable.");
Tim Petersefc3a3a2001-09-20 07:55:22 +00001718
Guido van Rossumd7047b31995-01-02 19:07:15 +00001719#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001720PyDoc_STRVAR(truncate_doc,
Tim Petersefc3a3a2001-09-20 07:55:22 +00001721"truncate([size]) -> None. Truncate the file to at most size bytes.\n"
1722"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001723"Size defaults to the current file position, as returned by tell().");
Guido van Rossumd7047b31995-01-02 19:07:15 +00001724#endif
Tim Petersefc3a3a2001-09-20 07:55:22 +00001725
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001726PyDoc_STRVAR(tell_doc,
1727"tell() -> current file position, an integer (may be a long integer).");
Tim Petersefc3a3a2001-09-20 07:55:22 +00001728
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001729PyDoc_STRVAR(readinto_doc,
1730"readinto() -> Undocumented. Don't use this; it may go away.");
Tim Petersefc3a3a2001-09-20 07:55:22 +00001731
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001732PyDoc_STRVAR(readlines_doc,
Tim Petersefc3a3a2001-09-20 07:55:22 +00001733"readlines([size]) -> list of strings, each a line from the file.\n"
1734"\n"
1735"Call readline() repeatedly and return a list of the lines so read.\n"
1736"The optional size argument, if given, is an approximate bound on the\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001737"total number of bytes in the lines returned.");
Tim Petersefc3a3a2001-09-20 07:55:22 +00001738
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001739PyDoc_STRVAR(writelines_doc,
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001740"writelines(sequence_of_strings) -> None. Write the strings to the file.\n"
Tim Petersefc3a3a2001-09-20 07:55:22 +00001741"\n"
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001742"Note that newlines are not added. The sequence can be any iterable object\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001743"producing strings. This is equivalent to calling write() for each string.");
Tim Petersefc3a3a2001-09-20 07:55:22 +00001744
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001745PyDoc_STRVAR(flush_doc,
1746"flush() -> None. Flush the internal I/O buffer.");
Tim Petersefc3a3a2001-09-20 07:55:22 +00001747
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001748PyDoc_STRVAR(close_doc,
Tim Petersefc3a3a2001-09-20 07:55:22 +00001749"close() -> None or (perhaps) an integer. Close the file.\n"
1750"\n"
Guido van Rossum77f6a652002-04-03 22:41:51 +00001751"Sets data attribute .closed to True. A closed file cannot be used for\n"
Tim Petersefc3a3a2001-09-20 07:55:22 +00001752"further I/O operations. close() may be called more than once without\n"
1753"error. Some kinds of file objects (for example, opened by popen())\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001754"may return an exit status upon closing.");
Tim Petersefc3a3a2001-09-20 07:55:22 +00001755
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001756PyDoc_STRVAR(isatty_doc,
1757"isatty() -> true or false. True if the file is connected to a tty device.");
Tim Petersefc3a3a2001-09-20 07:55:22 +00001758
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00001759PyDoc_STRVAR(enter_doc,
1760 "__enter__() -> self.");
1761
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001762PyDoc_STRVAR(exit_doc,
1763 "__exit__(*excinfo) -> None. Closes the file.");
1764
Tim Petersefc3a3a2001-09-20 07:55:22 +00001765static PyMethodDef file_methods[] = {
Jeremy Hylton8b735422002-08-14 21:01:41 +00001766 {"readline", (PyCFunction)file_readline, METH_VARARGS, readline_doc},
1767 {"read", (PyCFunction)file_read, METH_VARARGS, read_doc},
1768 {"write", (PyCFunction)file_write, METH_VARARGS, write_doc},
1769 {"fileno", (PyCFunction)file_fileno, METH_NOARGS, fileno_doc},
1770 {"seek", (PyCFunction)file_seek, METH_VARARGS, seek_doc},
Tim Petersefc3a3a2001-09-20 07:55:22 +00001771#ifdef HAVE_FTRUNCATE
Jeremy Hylton8b735422002-08-14 21:01:41 +00001772 {"truncate", (PyCFunction)file_truncate, METH_VARARGS, truncate_doc},
Tim Petersefc3a3a2001-09-20 07:55:22 +00001773#endif
Jeremy Hylton8b735422002-08-14 21:01:41 +00001774 {"tell", (PyCFunction)file_tell, METH_NOARGS, tell_doc},
1775 {"readinto", (PyCFunction)file_readinto, METH_VARARGS, readinto_doc},
1776 {"readlines", (PyCFunction)file_readlines,METH_VARARGS, readlines_doc},
Jeremy Hylton8b735422002-08-14 21:01:41 +00001777 {"writelines",(PyCFunction)file_writelines, METH_O, writelines_doc},
1778 {"flush", (PyCFunction)file_flush, METH_NOARGS, flush_doc},
1779 {"close", (PyCFunction)file_close, METH_NOARGS, close_doc},
1780 {"isatty", (PyCFunction)file_isatty, METH_NOARGS, isatty_doc},
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00001781 {"__enter__", (PyCFunction)file_self, METH_NOARGS, enter_doc},
Thomas Wouters73e5a5b2006-06-08 15:35:45 +00001782 {"__exit__", (PyCFunction)file_exit, METH_VARARGS, exit_doc},
Jeremy Hylton8b735422002-08-14 21:01:41 +00001783 {NULL, NULL} /* sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001784};
1785
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001786#define OFF(x) offsetof(PyFileObject, x)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001787
Guido van Rossum6f799372001-09-20 20:46:19 +00001788static PyMemberDef file_memberlist[] = {
1789 {"softspace", T_INT, OFF(f_softspace), 0,
1790 "flag indicating that a space needs to be printed; used by print"},
1791 {"mode", T_OBJECT, OFF(f_mode), RO,
Martin v. Löwis6233c9b2002-12-11 13:06:53 +00001792 "file mode ('r', 'U', 'w', 'a', possibly with 'b' or '+' added)"},
Guido van Rossum6f799372001-09-20 20:46:19 +00001793 {"name", T_OBJECT, OFF(f_name), RO,
1794 "file name"},
Martin v. Löwis5467d4c2003-05-10 07:10:12 +00001795 {"encoding", T_OBJECT, OFF(f_encoding), RO,
1796 "file encoding"},
Guido van Rossumb6775db1994-08-01 11:34:53 +00001797 /* getattr(f, "closed") is implemented without this table */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001798 {NULL} /* Sentinel */
1799};
1800
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001801static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00001802get_closed(PyFileObject *f, void *closure)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001803{
Guido van Rossum77f6a652002-04-03 22:41:51 +00001804 return PyBool_FromLong((long)(f->f_fp == 0));
Guido van Rossumb6775db1994-08-01 11:34:53 +00001805}
Jack Jansen7b8c7542002-04-14 20:12:41 +00001806static PyObject *
1807get_newlines(PyFileObject *f, void *closure)
1808{
1809 switch (f->f_newlinetypes) {
1810 case NEWLINE_UNKNOWN:
1811 Py_INCREF(Py_None);
1812 return Py_None;
1813 case NEWLINE_CR:
1814 return PyString_FromString("\r");
1815 case NEWLINE_LF:
1816 return PyString_FromString("\n");
1817 case NEWLINE_CR|NEWLINE_LF:
1818 return Py_BuildValue("(ss)", "\r", "\n");
1819 case NEWLINE_CRLF:
1820 return PyString_FromString("\r\n");
1821 case NEWLINE_CR|NEWLINE_CRLF:
1822 return Py_BuildValue("(ss)", "\r", "\r\n");
1823 case NEWLINE_LF|NEWLINE_CRLF:
1824 return Py_BuildValue("(ss)", "\n", "\r\n");
1825 case NEWLINE_CR|NEWLINE_LF|NEWLINE_CRLF:
1826 return Py_BuildValue("(sss)", "\r", "\n", "\r\n");
1827 default:
Tim Petersf1827cf2003-09-07 03:30:18 +00001828 PyErr_Format(PyExc_SystemError,
1829 "Unknown newlines value 0x%x\n",
Jeremy Hylton8b735422002-08-14 21:01:41 +00001830 f->f_newlinetypes);
Jack Jansen7b8c7542002-04-14 20:12:41 +00001831 return NULL;
1832 }
1833}
Guido van Rossumb6775db1994-08-01 11:34:53 +00001834
Guido van Rossum32d34c82001-09-20 21:45:26 +00001835static PyGetSetDef file_getsetlist[] = {
Guido van Rossum77f6a652002-04-03 22:41:51 +00001836 {"closed", (getter)get_closed, NULL, "True if the file is closed"},
Tim Petersf1827cf2003-09-07 03:30:18 +00001837 {"newlines", (getter)get_newlines, NULL,
Jeremy Hylton8b735422002-08-14 21:01:41 +00001838 "end-of-line convention used in this file"},
Tim Peters6d6c1a32001-08-02 04:15:00 +00001839 {0},
1840};
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001841
Neal Norwitzd8b995f2002-08-06 21:50:54 +00001842static void
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001843drop_readahead(PyFileObject *f)
Guido van Rossum65967252001-04-21 13:20:18 +00001844{
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001845 if (f->f_buf != NULL) {
1846 PyMem_Free(f->f_buf);
1847 f->f_buf = NULL;
1848 }
Guido van Rossum65967252001-04-21 13:20:18 +00001849}
1850
Tim Petersf1827cf2003-09-07 03:30:18 +00001851/* Make sure that file has a readahead buffer with at least one byte
1852 (unless at EOF) and no more than bufsize. Returns negative value on
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001853 error, will set MemoryError if bufsize bytes cannot be allocated. */
Neal Norwitzd8b995f2002-08-06 21:50:54 +00001854static int
1855readahead(PyFileObject *f, int bufsize)
1856{
Martin v. Löwis18e16552006-02-15 17:27:45 +00001857 Py_ssize_t chunksize;
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001858
1859 if (f->f_buf != NULL) {
Tim Petersf1827cf2003-09-07 03:30:18 +00001860 if( (f->f_bufend - f->f_bufptr) >= 1)
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001861 return 0;
1862 else
1863 drop_readahead(f);
1864 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001865 if ((f->f_buf = (char *)PyMem_Malloc(bufsize)) == NULL) {
1866 PyErr_NoMemory();
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001867 return -1;
1868 }
1869 Py_BEGIN_ALLOW_THREADS
1870 errno = 0;
1871 chunksize = Py_UniversalNewlineFread(
1872 f->f_buf, bufsize, f->f_fp, (PyObject *)f);
1873 Py_END_ALLOW_THREADS
1874 if (chunksize == 0) {
1875 if (ferror(f->f_fp)) {
1876 PyErr_SetFromErrno(PyExc_IOError);
1877 clearerr(f->f_fp);
1878 drop_readahead(f);
1879 return -1;
1880 }
1881 }
1882 f->f_bufptr = f->f_buf;
1883 f->f_bufend = f->f_buf + chunksize;
1884 return 0;
1885}
1886
1887/* Used by file_iternext. The returned string will start with 'skip'
Tim Petersf1827cf2003-09-07 03:30:18 +00001888 uninitialized bytes followed by the remainder of the line. Don't be
1889 horrified by the recursive call: maximum recursion depth is limited by
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001890 logarithmic buffer growth to about 50 even when reading a 1gb line. */
1891
Neal Norwitzd8b995f2002-08-06 21:50:54 +00001892static PyStringObject *
1893readahead_get_line_skip(PyFileObject *f, int skip, int bufsize)
1894{
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001895 PyStringObject* s;
1896 char *bufptr;
1897 char *buf;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001898 Py_ssize_t len;
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001899
1900 if (f->f_buf == NULL)
Tim Petersf1827cf2003-09-07 03:30:18 +00001901 if (readahead(f, bufsize) < 0)
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001902 return NULL;
1903
1904 len = f->f_bufend - f->f_bufptr;
Tim Petersf1827cf2003-09-07 03:30:18 +00001905 if (len == 0)
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001906 return (PyStringObject *)
1907 PyString_FromStringAndSize(NULL, skip);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001908 bufptr = (char *)memchr(f->f_bufptr, '\n', len);
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001909 if (bufptr != NULL) {
1910 bufptr++; /* Count the '\n' */
1911 len = bufptr - f->f_bufptr;
1912 s = (PyStringObject *)
1913 PyString_FromStringAndSize(NULL, skip+len);
Tim Petersf1827cf2003-09-07 03:30:18 +00001914 if (s == NULL)
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001915 return NULL;
1916 memcpy(PyString_AS_STRING(s)+skip, f->f_bufptr, len);
1917 f->f_bufptr = bufptr;
1918 if (bufptr == f->f_bufend)
1919 drop_readahead(f);
1920 } else {
1921 bufptr = f->f_bufptr;
1922 buf = f->f_buf;
1923 f->f_buf = NULL; /* Force new readahead buffer */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001924 assert(skip+len < INT_MAX);
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001925 s = readahead_get_line_skip(
Martin v. Löwis18e16552006-02-15 17:27:45 +00001926 f, (int)(skip+len), bufsize + (bufsize>>2) );
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001927 if (s == NULL) {
1928 PyMem_Free(buf);
1929 return NULL;
1930 }
1931 memcpy(PyString_AS_STRING(s)+skip, bufptr, len);
1932 PyMem_Free(buf);
1933 }
1934 return s;
1935}
1936
1937/* A larger buffer size may actually decrease performance. */
1938#define READAHEAD_BUFSIZE 8192
1939
1940static PyObject *
1941file_iternext(PyFileObject *f)
1942{
1943 PyStringObject* l;
1944
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001945 if (f->f_fp == NULL)
1946 return err_closed();
1947
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001948 l = readahead_get_line_skip(f, 0, READAHEAD_BUFSIZE);
1949 if (l == NULL || PyString_GET_SIZE(l) == 0) {
1950 Py_XDECREF(l);
1951 return NULL;
1952 }
1953 return (PyObject *)l;
1954}
1955
1956
Tim Peters59c9a642001-09-13 05:38:56 +00001957static PyObject *
1958file_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1959{
Tim Peters44410012001-09-14 03:26:08 +00001960 PyObject *self;
1961 static PyObject *not_yet_string;
1962
1963 assert(type != NULL && type->tp_alloc != NULL);
1964
1965 if (not_yet_string == NULL) {
1966 not_yet_string = PyString_FromString("<uninitialized file>");
1967 if (not_yet_string == NULL)
1968 return NULL;
1969 }
1970
1971 self = type->tp_alloc(type, 0);
1972 if (self != NULL) {
1973 /* Always fill in the name and mode, so that nobody else
1974 needs to special-case NULLs there. */
1975 Py_INCREF(not_yet_string);
1976 ((PyFileObject *)self)->f_name = not_yet_string;
1977 Py_INCREF(not_yet_string);
1978 ((PyFileObject *)self)->f_mode = not_yet_string;
Martin v. Löwis5467d4c2003-05-10 07:10:12 +00001979 Py_INCREF(Py_None);
1980 ((PyFileObject *)self)->f_encoding = Py_None;
Raymond Hettingercb87bc82004-05-31 00:35:52 +00001981 ((PyFileObject *)self)->weakreflist = NULL;
Tim Peters44410012001-09-14 03:26:08 +00001982 }
1983 return self;
1984}
1985
1986static int
1987file_init(PyObject *self, PyObject *args, PyObject *kwds)
1988{
1989 PyFileObject *foself = (PyFileObject *)self;
1990 int ret = 0;
Martin v. Löwis15e62742006-02-27 16:46:16 +00001991 static char *kwlist[] = {"name", "mode", "buffering", 0};
Tim Peters59c9a642001-09-13 05:38:56 +00001992 char *name = NULL;
1993 char *mode = "r";
1994 int bufsize = -1;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001995 int wideargument = 0;
Tim Peters44410012001-09-14 03:26:08 +00001996
1997 assert(PyFile_Check(self));
1998 if (foself->f_fp != NULL) {
1999 /* Have to close the existing file first. */
2000 PyObject *closeresult = file_close(foself);
2001 if (closeresult == NULL)
2002 return -1;
2003 Py_DECREF(closeresult);
2004 }
Tim Peters59c9a642001-09-13 05:38:56 +00002005
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002006#ifdef Py_WIN_WIDE_FILENAMES
2007 if (GetVersion() < 0x80000000) { /* On NT, so wide API available */
2008 PyObject *po;
2009 if (PyArg_ParseTupleAndKeywords(args, kwds, "U|si:file",
2010 kwlist, &po, &mode, &bufsize)) {
2011 wideargument = 1;
Nicholas Bastinabce8a62004-03-21 20:24:07 +00002012 if (fill_file_fields(foself, NULL, po, mode,
2013 fclose) == NULL)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002014 goto Error;
2015 } else {
2016 /* Drop the argument parsing error as narrow
2017 strings are also valid. */
2018 PyErr_Clear();
2019 }
2020 }
2021#endif
2022
2023 if (!wideargument) {
Nicholas Bastinabce8a62004-03-21 20:24:07 +00002024 PyObject *o_name;
2025
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002026 if (!PyArg_ParseTupleAndKeywords(args, kwds, "et|si:file", kwlist,
2027 Py_FileSystemDefaultEncoding,
2028 &name,
2029 &mode, &bufsize))
2030 return -1;
Nicholas Bastinabce8a62004-03-21 20:24:07 +00002031
2032 /* We parse again to get the name as a PyObject */
Jeremy Hyltonaf68c872005-12-10 18:50:16 +00002033 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|si:file",
2034 kwlist, &o_name, &mode,
2035 &bufsize))
Thomas Wouters89f507f2006-12-13 04:49:30 +00002036 goto Error;
Nicholas Bastinabce8a62004-03-21 20:24:07 +00002037
2038 if (fill_file_fields(foself, NULL, o_name, mode,
2039 fclose) == NULL)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00002040 goto Error;
2041 }
Tim Peters44410012001-09-14 03:26:08 +00002042 if (open_the_file(foself, name, mode) == NULL)
2043 goto Error;
Martin v. Löwis1e3bdf62003-09-04 19:01:46 +00002044 foself->f_setbuf = NULL;
Tim Peters44410012001-09-14 03:26:08 +00002045 PyFile_SetBufSize(self, bufsize);
2046 goto Done;
2047
2048Error:
2049 ret = -1;
2050 /* fall through */
2051Done:
Tim Peters59c9a642001-09-13 05:38:56 +00002052 PyMem_Free(name); /* free the encoded string */
Tim Peters44410012001-09-14 03:26:08 +00002053 return ret;
Tim Peters59c9a642001-09-13 05:38:56 +00002054}
2055
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002056PyDoc_VAR(file_doc) =
2057PyDoc_STR(
Tim Peters59c9a642001-09-13 05:38:56 +00002058"file(name[, mode[, buffering]]) -> file object\n"
2059"\n"
2060"Open a file. The mode can be 'r', 'w' or 'a' for reading (default),\n"
2061"writing or appending. The file will be created if it doesn't exist\n"
2062"when opened for writing or appending; it will be truncated when\n"
2063"opened for writing. Add a 'b' to the mode for binary files.\n"
2064"Add a '+' to the mode to allow simultaneous reading and writing.\n"
2065"If the buffering argument is given, 0 means unbuffered, 1 means line\n"
Tim Peters742dfd62001-09-13 21:49:44 +00002066"buffered, and larger numbers specify the buffer size.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002067)
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002068PyDoc_STR(
Barry Warsaw4be55b52002-05-22 20:37:53 +00002069"Add a 'U' to mode to open the file for input with universal newline\n"
2070"support. Any line ending in the input file will be seen as a '\\n'\n"
2071"in Python. Also, a file so opened gains the attribute 'newlines';\n"
2072"the value for this attribute is one of None (no newline read yet),\n"
2073"'\\r', '\\n', '\\r\\n' or a tuple containing all the newline types seen.\n"
2074"\n"
2075"'U' cannot be combined with 'w' or '+' mode.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002076);
Tim Peters59c9a642001-09-13 05:38:56 +00002077
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002078PyTypeObject PyFile_Type = {
2079 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002080 0,
2081 "file",
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002082 sizeof(PyFileObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002083 0,
Guido van Rossum65967252001-04-21 13:20:18 +00002084 (destructor)file_dealloc, /* tp_dealloc */
2085 0, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002086 0, /* tp_getattr */
2087 0, /* tp_setattr */
Guido van Rossum65967252001-04-21 13:20:18 +00002088 0, /* tp_compare */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002089 (reprfunc)file_repr, /* tp_repr */
Guido van Rossum65967252001-04-21 13:20:18 +00002090 0, /* tp_as_number */
2091 0, /* tp_as_sequence */
2092 0, /* tp_as_mapping */
2093 0, /* tp_hash */
2094 0, /* tp_call */
2095 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002096 PyObject_GenericGetAttr, /* tp_getattro */
Tim Peters015dd822003-05-04 04:16:52 +00002097 /* softspace is writable: we must supply tp_setattro */
2098 PyObject_GenericSetAttr, /* tp_setattro */
Guido van Rossum65967252001-04-21 13:20:18 +00002099 0, /* tp_as_buffer */
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +00002100 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
Tim Peters59c9a642001-09-13 05:38:56 +00002101 file_doc, /* tp_doc */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002102 0, /* tp_traverse */
2103 0, /* tp_clear */
Guido van Rossum65967252001-04-21 13:20:18 +00002104 0, /* tp_richcompare */
Raymond Hettingercb87bc82004-05-31 00:35:52 +00002105 offsetof(PyFileObject, weakreflist), /* tp_weaklistoffset */
Guido van Rossum1a5e21e2006-02-28 21:57:43 +00002106 (getiterfunc)file_self, /* tp_iter */
Guido van Rossum7a6e9592002-08-06 15:55:28 +00002107 (iternextfunc)file_iternext, /* tp_iternext */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002108 file_methods, /* tp_methods */
2109 file_memberlist, /* tp_members */
2110 file_getsetlist, /* tp_getset */
2111 0, /* tp_base */
2112 0, /* tp_dict */
Tim Peters59c9a642001-09-13 05:38:56 +00002113 0, /* tp_descr_get */
2114 0, /* tp_descr_set */
2115 0, /* tp_dictoffset */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002116 file_init, /* tp_init */
Tim Peters44410012001-09-14 03:26:08 +00002117 PyType_GenericAlloc, /* tp_alloc */
Tim Peters59c9a642001-09-13 05:38:56 +00002118 file_new, /* tp_new */
Neil Schemenaueraa769ae2002-04-12 02:44:10 +00002119 PyObject_Del, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002120};
Guido van Rossumeb183da1991-04-04 10:44:06 +00002121
2122/* Interface for the 'soft space' between print items. */
2123
2124int
Fred Drakefd99de62000-07-09 05:02:18 +00002125PyFile_SoftSpace(PyObject *f, int newflag)
Guido van Rossumeb183da1991-04-04 10:44:06 +00002126{
Martin v. Löwis18e16552006-02-15 17:27:45 +00002127 long oldflag = 0;
Guido van Rossum3165fe61992-09-25 21:59:05 +00002128 if (f == NULL) {
2129 /* Do nothing */
2130 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002131 else if (PyFile_Check(f)) {
2132 oldflag = ((PyFileObject *)f)->f_softspace;
2133 ((PyFileObject *)f)->f_softspace = newflag;
Guido van Rossumeb183da1991-04-04 10:44:06 +00002134 }
Guido van Rossum3165fe61992-09-25 21:59:05 +00002135 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002136 PyObject *v;
2137 v = PyObject_GetAttrString(f, "softspace");
Guido van Rossum3165fe61992-09-25 21:59:05 +00002138 if (v == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002139 PyErr_Clear();
Guido van Rossum3165fe61992-09-25 21:59:05 +00002140 else {
Guido van Rossumddefaf32007-01-14 03:31:43 +00002141 if (PyInt_CheckExact(v))
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002142 oldflag = PyInt_AsLong(v);
Martin v. Löwis18e16552006-02-15 17:27:45 +00002143 assert(oldflag < INT_MAX);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002144 Py_DECREF(v);
Guido van Rossum3165fe61992-09-25 21:59:05 +00002145 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002146 v = PyInt_FromLong((long)newflag);
Guido van Rossum3165fe61992-09-25 21:59:05 +00002147 if (v == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002148 PyErr_Clear();
Guido van Rossum3165fe61992-09-25 21:59:05 +00002149 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002150 if (PyObject_SetAttrString(f, "softspace", v) != 0)
2151 PyErr_Clear();
2152 Py_DECREF(v);
Guido van Rossum3165fe61992-09-25 21:59:05 +00002153 }
2154 }
Martin v. Löwis18e16552006-02-15 17:27:45 +00002155 return (int)oldflag;
Guido van Rossumeb183da1991-04-04 10:44:06 +00002156}
Guido van Rossum3165fe61992-09-25 21:59:05 +00002157
2158/* Interfaces to write objects/strings to file-like objects */
2159
2160int
Fred Drakefd99de62000-07-09 05:02:18 +00002161PyFile_WriteObject(PyObject *v, PyObject *f, int flags)
Guido van Rossum3165fe61992-09-25 21:59:05 +00002162{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002163 PyObject *writer, *value, *args, *result;
Guido van Rossum3165fe61992-09-25 21:59:05 +00002164 if (f == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002165 PyErr_SetString(PyExc_TypeError, "writeobject with NULL file");
Guido van Rossum3165fe61992-09-25 21:59:05 +00002166 return -1;
2167 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002168 else if (PyFile_Check(f)) {
2169 FILE *fp = PyFile_AsFile(f);
Fred Drake086a0f72004-03-19 15:22:36 +00002170#ifdef Py_USING_UNICODE
Martin v. Löwis5467d4c2003-05-10 07:10:12 +00002171 PyObject *enc = ((PyFileObject*)f)->f_encoding;
2172 int result;
Fred Drake086a0f72004-03-19 15:22:36 +00002173#endif
Guido van Rossum3165fe61992-09-25 21:59:05 +00002174 if (fp == NULL) {
2175 err_closed();
2176 return -1;
2177 }
Martin v. Löwis5467d4c2003-05-10 07:10:12 +00002178#ifdef Py_USING_UNICODE
Tim Petersf1827cf2003-09-07 03:30:18 +00002179 if ((flags & Py_PRINT_RAW) &&
Martin v. Löwis415da6e2003-05-18 12:56:25 +00002180 PyUnicode_Check(v) && enc != Py_None) {
Martin v. Löwis5467d4c2003-05-10 07:10:12 +00002181 char *cenc = PyString_AS_STRING(enc);
2182 value = PyUnicode_AsEncodedString(v, cenc, "strict");
2183 if (value == NULL)
2184 return -1;
2185 } else {
2186 value = v;
2187 Py_INCREF(value);
2188 }
2189 result = PyObject_Print(value, fp, flags);
2190 Py_DECREF(value);
2191 return result;
2192#else
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002193 return PyObject_Print(v, fp, flags);
Martin v. Löwis5467d4c2003-05-10 07:10:12 +00002194#endif
Guido van Rossum3165fe61992-09-25 21:59:05 +00002195 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002196 writer = PyObject_GetAttrString(f, "write");
Guido van Rossum3165fe61992-09-25 21:59:05 +00002197 if (writer == NULL)
2198 return -1;
Martin v. Löwis2777c022001-09-19 13:47:32 +00002199 if (flags & Py_PRINT_RAW) {
2200 if (PyUnicode_Check(v)) {
2201 value = v;
2202 Py_INCREF(value);
2203 } else
2204 value = PyObject_Str(v);
2205 }
2206 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002207 value = PyObject_Repr(v);
Guido van Rossumc6004111993-11-05 10:22:19 +00002208 if (value == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002209 Py_DECREF(writer);
Guido van Rossumc6004111993-11-05 10:22:19 +00002210 return -1;
Guido van Rossum3165fe61992-09-25 21:59:05 +00002211 }
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002212 args = PyTuple_Pack(1, value);
Guido van Rossume9eec541997-05-22 14:02:25 +00002213 if (args == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002214 Py_DECREF(value);
2215 Py_DECREF(writer);
Guido van Rossumd3f9a1a1995-07-10 23:32:26 +00002216 return -1;
2217 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002218 result = PyEval_CallObject(writer, args);
2219 Py_DECREF(args);
2220 Py_DECREF(value);
2221 Py_DECREF(writer);
Guido van Rossum3165fe61992-09-25 21:59:05 +00002222 if (result == NULL)
2223 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002224 Py_DECREF(result);
Guido van Rossum3165fe61992-09-25 21:59:05 +00002225 return 0;
2226}
2227
Guido van Rossum27a60b11997-05-22 22:25:11 +00002228int
Tim Petersc1bbcb82001-11-28 22:13:25 +00002229PyFile_WriteString(const char *s, PyObject *f)
Guido van Rossum3165fe61992-09-25 21:59:05 +00002230{
2231 if (f == NULL) {
Guido van Rossum27a60b11997-05-22 22:25:11 +00002232 /* Should be caused by a pre-existing error */
Fred Drakefd99de62000-07-09 05:02:18 +00002233 if (!PyErr_Occurred())
Guido van Rossum27a60b11997-05-22 22:25:11 +00002234 PyErr_SetString(PyExc_SystemError,
2235 "null file for PyFile_WriteString");
2236 return -1;
Guido van Rossum3165fe61992-09-25 21:59:05 +00002237 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002238 else if (PyFile_Check(f)) {
2239 FILE *fp = PyFile_AsFile(f);
Guido van Rossum27a60b11997-05-22 22:25:11 +00002240 if (fp == NULL) {
2241 err_closed();
2242 return -1;
2243 }
2244 fputs(s, fp);
2245 return 0;
Guido van Rossum3165fe61992-09-25 21:59:05 +00002246 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002247 else if (!PyErr_Occurred()) {
2248 PyObject *v = PyString_FromString(s);
Guido van Rossum27a60b11997-05-22 22:25:11 +00002249 int err;
2250 if (v == NULL)
2251 return -1;
2252 err = PyFile_WriteObject(v, f, Py_PRINT_RAW);
2253 Py_DECREF(v);
2254 return err;
Guido van Rossum3165fe61992-09-25 21:59:05 +00002255 }
Guido van Rossum74ba2471997-07-13 03:56:50 +00002256 else
2257 return -1;
Guido van Rossum3165fe61992-09-25 21:59:05 +00002258}
Andrew M. Kuchling06051ed2000-07-13 23:56:54 +00002259
2260/* Try to get a file-descriptor from a Python object. If the object
2261 is an integer or long integer, its value is returned. If not, the
2262 object's fileno() method is called if it exists; the method must return
2263 an integer or long integer, which is returned as the file descriptor value.
2264 -1 is returned on failure.
2265*/
2266
2267int PyObject_AsFileDescriptor(PyObject *o)
2268{
2269 int fd;
2270 PyObject *meth;
2271
2272 if (PyInt_Check(o)) {
2273 fd = PyInt_AsLong(o);
2274 }
2275 else if (PyLong_Check(o)) {
2276 fd = PyLong_AsLong(o);
2277 }
2278 else if ((meth = PyObject_GetAttrString(o, "fileno")) != NULL)
2279 {
2280 PyObject *fno = PyEval_CallObject(meth, NULL);
2281 Py_DECREF(meth);
2282 if (fno == NULL)
2283 return -1;
Tim Peters86821b22001-01-07 21:19:34 +00002284
Andrew M. Kuchling06051ed2000-07-13 23:56:54 +00002285 if (PyInt_Check(fno)) {
2286 fd = PyInt_AsLong(fno);
2287 Py_DECREF(fno);
2288 }
2289 else if (PyLong_Check(fno)) {
2290 fd = PyLong_AsLong(fno);
2291 Py_DECREF(fno);
2292 }
2293 else {
2294 PyErr_SetString(PyExc_TypeError,
2295 "fileno() returned a non-integer");
2296 Py_DECREF(fno);
2297 return -1;
2298 }
2299 }
2300 else {
2301 PyErr_SetString(PyExc_TypeError,
2302 "argument must be an int, or have a fileno() method.");
2303 return -1;
2304 }
2305
Guido van Rossumddefaf32007-01-14 03:31:43 +00002306 if (fd == -1 && PyErr_Occurred())
2307 return -1;
Andrew M. Kuchling06051ed2000-07-13 23:56:54 +00002308 if (fd < 0) {
2309 PyErr_Format(PyExc_ValueError,
2310 "file descriptor cannot be a negative integer (%i)",
2311 fd);
2312 return -1;
2313 }
2314 return fd;
2315}
Jack Jansen7b8c7542002-04-14 20:12:41 +00002316
Jack Jansen7b8c7542002-04-14 20:12:41 +00002317/* From here on we need access to the real fgets and fread */
2318#undef fgets
2319#undef fread
2320
2321/*
2322** Py_UniversalNewlineFgets is an fgets variation that understands
2323** all of \r, \n and \r\n conventions.
2324** The stream should be opened in binary mode.
2325** If fobj is NULL the routine always does newline conversion, and
2326** it may peek one char ahead to gobble the second char in \r\n.
2327** If fobj is non-NULL it must be a PyFileObject. In this case there
2328** is no readahead but in stead a flag is used to skip a following
2329** \n on the next read. Also, if the file is open in binary mode
2330** the whole conversion is skipped. Finally, the routine keeps track of
2331** the different types of newlines seen.
2332** Note that we need no error handling: fgets() treats error and eof
2333** identically.
2334*/
2335char *
2336Py_UniversalNewlineFgets(char *buf, int n, FILE *stream, PyObject *fobj)
2337{
2338 char *p = buf;
2339 int c;
2340 int newlinetypes = 0;
2341 int skipnextlf = 0;
2342 int univ_newline = 1;
Tim Peters058b1412002-04-21 07:29:14 +00002343
Jack Jansen7b8c7542002-04-14 20:12:41 +00002344 if (fobj) {
2345 if (!PyFile_Check(fobj)) {
2346 errno = ENXIO; /* What can you do... */
2347 return NULL;
2348 }
2349 univ_newline = ((PyFileObject *)fobj)->f_univ_newline;
2350 if ( !univ_newline )
2351 return fgets(buf, n, stream);
2352 newlinetypes = ((PyFileObject *)fobj)->f_newlinetypes;
2353 skipnextlf = ((PyFileObject *)fobj)->f_skipnextlf;
2354 }
2355 FLOCKFILE(stream);
2356 c = 'x'; /* Shut up gcc warning */
2357 while (--n > 0 && (c = GETC(stream)) != EOF ) {
2358 if (skipnextlf ) {
2359 skipnextlf = 0;
2360 if (c == '\n') {
2361 /* Seeing a \n here with skipnextlf true
2362 ** means we saw a \r before.
2363 */
2364 newlinetypes |= NEWLINE_CRLF;
2365 c = GETC(stream);
2366 if (c == EOF) break;
2367 } else {
2368 /*
2369 ** Note that c == EOF also brings us here,
2370 ** so we're okay if the last char in the file
2371 ** is a CR.
2372 */
2373 newlinetypes |= NEWLINE_CR;
2374 }
2375 }
2376 if (c == '\r') {
2377 /* A \r is translated into a \n, and we skip
2378 ** an adjacent \n, if any. We don't set the
2379 ** newlinetypes flag until we've seen the next char.
2380 */
2381 skipnextlf = 1;
2382 c = '\n';
2383 } else if ( c == '\n') {
2384 newlinetypes |= NEWLINE_LF;
2385 }
2386 *p++ = c;
2387 if (c == '\n') break;
2388 }
2389 if ( c == EOF && skipnextlf )
2390 newlinetypes |= NEWLINE_CR;
2391 FUNLOCKFILE(stream);
2392 *p = '\0';
2393 if (fobj) {
2394 ((PyFileObject *)fobj)->f_newlinetypes = newlinetypes;
2395 ((PyFileObject *)fobj)->f_skipnextlf = skipnextlf;
2396 } else if ( skipnextlf ) {
2397 /* If we have no file object we cannot save the
2398 ** skipnextlf flag. We have to readahead, which
2399 ** will cause a pause if we're reading from an
2400 ** interactive stream, but that is very unlikely
2401 ** unless we're doing something silly like
2402 ** execfile("/dev/tty").
2403 */
2404 c = GETC(stream);
2405 if ( c != '\n' )
2406 ungetc(c, stream);
2407 }
2408 if (p == buf)
2409 return NULL;
2410 return buf;
2411}
2412
2413/*
2414** Py_UniversalNewlineFread is an fread variation that understands
2415** all of \r, \n and \r\n conventions.
2416** The stream should be opened in binary mode.
2417** fobj must be a PyFileObject. In this case there
2418** is no readahead but in stead a flag is used to skip a following
2419** \n on the next read. Also, if the file is open in binary mode
2420** the whole conversion is skipped. Finally, the routine keeps track of
2421** the different types of newlines seen.
2422*/
2423size_t
Tim Peters058b1412002-04-21 07:29:14 +00002424Py_UniversalNewlineFread(char *buf, size_t n,
Jack Jansen7b8c7542002-04-14 20:12:41 +00002425 FILE *stream, PyObject *fobj)
2426{
Tim Peters058b1412002-04-21 07:29:14 +00002427 char *dst = buf;
2428 PyFileObject *f = (PyFileObject *)fobj;
2429 int newlinetypes, skipnextlf;
2430
2431 assert(buf != NULL);
2432 assert(stream != NULL);
2433
Jack Jansen7b8c7542002-04-14 20:12:41 +00002434 if (!fobj || !PyFile_Check(fobj)) {
2435 errno = ENXIO; /* What can you do... */
Neal Norwitzcb3319f2003-02-09 01:10:02 +00002436 return 0;
Jack Jansen7b8c7542002-04-14 20:12:41 +00002437 }
Tim Peters058b1412002-04-21 07:29:14 +00002438 if (!f->f_univ_newline)
Jack Jansen7b8c7542002-04-14 20:12:41 +00002439 return fread(buf, 1, n, stream);
Tim Peters058b1412002-04-21 07:29:14 +00002440 newlinetypes = f->f_newlinetypes;
2441 skipnextlf = f->f_skipnextlf;
2442 /* Invariant: n is the number of bytes remaining to be filled
2443 * in the buffer.
2444 */
2445 while (n) {
2446 size_t nread;
2447 int shortread;
2448 char *src = dst;
2449
2450 nread = fread(dst, 1, n, stream);
2451 assert(nread <= n);
Neal Norwitzcb3319f2003-02-09 01:10:02 +00002452 if (nread == 0)
2453 break;
2454
Tim Peterse1682a82002-04-21 18:15:20 +00002455 n -= nread; /* assuming 1 byte out for each in; will adjust */
2456 shortread = n != 0; /* true iff EOF or error */
Tim Peters058b1412002-04-21 07:29:14 +00002457 while (nread--) {
2458 char c = *src++;
Jack Jansen7b8c7542002-04-14 20:12:41 +00002459 if (c == '\r') {
Tim Peters058b1412002-04-21 07:29:14 +00002460 /* Save as LF and set flag to skip next LF. */
Jack Jansen7b8c7542002-04-14 20:12:41 +00002461 *dst++ = '\n';
2462 skipnextlf = 1;
Tim Peters058b1412002-04-21 07:29:14 +00002463 }
2464 else if (skipnextlf && c == '\n') {
2465 /* Skip LF, and remember we saw CR LF. */
Jack Jansen7b8c7542002-04-14 20:12:41 +00002466 skipnextlf = 0;
2467 newlinetypes |= NEWLINE_CRLF;
Tim Peterse1682a82002-04-21 18:15:20 +00002468 ++n;
Tim Peters058b1412002-04-21 07:29:14 +00002469 }
2470 else {
2471 /* Normal char to be stored in buffer. Also
2472 * update the newlinetypes flag if either this
2473 * is an LF or the previous char was a CR.
2474 */
Jack Jansen7b8c7542002-04-14 20:12:41 +00002475 if (c == '\n')
2476 newlinetypes |= NEWLINE_LF;
2477 else if (skipnextlf)
2478 newlinetypes |= NEWLINE_CR;
2479 *dst++ = c;
2480 skipnextlf = 0;
2481 }
2482 }
Tim Peters058b1412002-04-21 07:29:14 +00002483 if (shortread) {
2484 /* If this is EOF, update type flags. */
2485 if (skipnextlf && feof(stream))
2486 newlinetypes |= NEWLINE_CR;
2487 break;
2488 }
Jack Jansen7b8c7542002-04-14 20:12:41 +00002489 }
Tim Peters058b1412002-04-21 07:29:14 +00002490 f->f_newlinetypes = newlinetypes;
2491 f->f_skipnextlf = skipnextlf;
2492 return dst - buf;
Jack Jansen7b8c7542002-04-14 20:12:41 +00002493}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00002494
2495#ifdef __cplusplus
2496}
2497#endif