blob: 6f90fb9e84a14d97203c94065000263df30af1d4 [file] [log] [blame]
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001/* File object implementation */
2
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003#include "Python.h"
Guido van Rossumb6775db1994-08-01 11:34:53 +00004#include "structmember.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005
Guido van Rossumff7e83d1999-08-27 20:39:37 +00006#ifndef DONT_HAVE_SYS_TYPES_H
Guido van Rossum41498431999-01-07 22:09:51 +00007#include <sys/types.h>
Guido van Rossumff7e83d1999-08-27 20:39:37 +00008#endif /* DONT_HAVE_SYS_TYPES_H */
Guido van Rossum41498431999-01-07 22:09:51 +00009
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000010#ifdef MS_WINDOWS
Guido van Rossumb8199141997-05-06 15:23:24 +000011#define fileno _fileno
Tim Petersfb05db22002-03-11 00:24:00 +000012/* can simulate truncate with Win32 API functions; see file_truncate */
Guido van Rossumb8199141997-05-06 15:23:24 +000013#define HAVE_FTRUNCATE
Tim Peters7a1f9172002-07-14 22:14:19 +000014#define WIN32_LEAN_AND_MEAN
Tim Petersfb05db22002-03-11 00:24:00 +000015#include <windows.h>
Guido van Rossumb8199141997-05-06 15:23:24 +000016#endif
17
Mark Hammondc2e85bd2002-10-03 05:10:39 +000018#ifdef _MSC_VER
19/* Need GetVersion to see if on NT so safe to use _wfopen */
20#define WIN32_LEAN_AND_MEAN
21#include <windows.h>
22#endif /* _MSC_VER */
23
Guido van Rossumf2044e11998-04-28 16:05:59 +000024#ifdef macintosh
25#ifdef USE_GUSI
26#define HAVE_FTRUNCATE
27#endif
28#endif
29
Jack Jansene08dea191995-04-23 22:12:47 +000030#ifdef __MWERKS__
31/* Mwerks fopen() doesn't always set errno */
32#define NO_FOPEN_ERRNO
33#endif
Guido van Rossum295d1711995-02-19 15:55:19 +000034
Andrew MacIntyrec4874392002-02-26 11:36:35 +000035#if defined(PYOS_OS2) && defined(PYCC_GCC)
36#include <io.h>
37#endif
38
Guido van Rossumc0b618a1997-05-02 03:12:38 +000039#define BUF(v) PyString_AS_STRING((PyStringObject *)v)
Guido van Rossumce5ba841991-03-06 13:06:18 +000040
Guido van Rossumff7e83d1999-08-27 20:39:37 +000041#ifndef DONT_HAVE_ERRNO_H
Guido van Rossumf1dc5661993-07-05 10:31:29 +000042#include <errno.h>
Guido van Rossumff7e83d1999-08-27 20:39:37 +000043#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000044
Jack Jansen7b8c7542002-04-14 20:12:41 +000045#ifdef HAVE_GETC_UNLOCKED
46#define GETC(f) getc_unlocked(f)
47#define FLOCKFILE(f) flockfile(f)
48#define FUNLOCKFILE(f) funlockfile(f)
49#else
50#define GETC(f) getc(f)
51#define FLOCKFILE(f)
52#define FUNLOCKFILE(f)
53#endif
54
55#ifdef WITH_UNIVERSAL_NEWLINES
56/* Bits in f_newlinetypes */
57#define NEWLINE_UNKNOWN 0 /* No newline seen, yet */
58#define NEWLINE_CR 1 /* \r newline seen */
59#define NEWLINE_LF 2 /* \n newline seen */
60#define NEWLINE_CRLF 4 /* \r\n newline seen */
61#endif
Trent Mickf29f47b2000-08-11 19:02:59 +000062
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000063FILE *
Fred Drakefd99de62000-07-09 05:02:18 +000064PyFile_AsFile(PyObject *f)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000065{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000066 if (f == NULL || !PyFile_Check(f))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000067 return NULL;
Guido van Rossum3165fe61992-09-25 21:59:05 +000068 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +000069 return ((PyFileObject *)f)->f_fp;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000070}
71
Guido van Rossumc0b618a1997-05-02 03:12:38 +000072PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +000073PyFile_Name(PyObject *f)
Guido van Rossumdb3165e1993-10-18 17:06:59 +000074{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000075 if (f == NULL || !PyFile_Check(f))
Guido van Rossumdb3165e1993-10-18 17:06:59 +000076 return NULL;
77 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +000078 return ((PyFileObject *)f)->f_name;
Guido van Rossumdb3165e1993-10-18 17:06:59 +000079}
80
Neil Schemenauered19b882002-03-23 02:06:50 +000081/* On Unix, fopen will succeed for directories.
82 In Python, there should be no file objects referring to
83 directories, so we need a check. */
84
85static PyFileObject*
86dircheck(PyFileObject* f)
87{
88#if defined(HAVE_FSTAT) && defined(S_IFDIR) && defined(EISDIR)
89 struct stat buf;
90 if (f->f_fp == NULL)
91 return f;
92 if (fstat(fileno(f->f_fp), &buf) == 0 &&
93 S_ISDIR(buf.st_mode)) {
94#ifdef HAVE_STRERROR
95 char *msg = strerror(EISDIR);
96#else
97 char *msg = "Is a directory";
98#endif
Tim Petersf1827cf2003-09-07 03:30:18 +000099 PyObject *exc = PyObject_CallFunction(PyExc_IOError, "(is)",
Jeremy Hylton8b735422002-08-14 21:01:41 +0000100 EISDIR, msg);
Neil Schemenauered19b882002-03-23 02:06:50 +0000101 PyErr_SetObject(PyExc_IOError, exc);
Neal Norwitz98cad482003-08-15 20:05:45 +0000102 Py_XDECREF(exc);
Neil Schemenauered19b882002-03-23 02:06:50 +0000103 return NULL;
104 }
105#endif
106 return f;
107}
108
Tim Peters59c9a642001-09-13 05:38:56 +0000109
110static PyObject *
111fill_file_fields(PyFileObject *f, FILE *fp, char *name, char *mode,
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000112 int (*close)(FILE *), PyObject *wname)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000113{
Tim Peters59c9a642001-09-13 05:38:56 +0000114 assert(f != NULL);
115 assert(PyFile_Check(f));
Tim Peters44410012001-09-14 03:26:08 +0000116 assert(f->f_fp == NULL);
117
118 Py_DECREF(f->f_name);
119 Py_DECREF(f->f_mode);
Martin v. Löwis5467d4c2003-05-10 07:10:12 +0000120 Py_DECREF(f->f_encoding);
Martin v. Löwis0073f2e2002-11-21 23:52:35 +0000121#ifdef Py_USING_UNICODE
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000122 if (wname)
123 f->f_name = PyUnicode_FromObject(wname);
124 else
Martin v. Löwis0073f2e2002-11-21 23:52:35 +0000125#endif
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000126 f->f_name = PyString_FromString(name);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000127 f->f_mode = PyString_FromString(mode);
Tim Peters44410012001-09-14 03:26:08 +0000128
Guido van Rossuma1ab7fa1991-06-04 19:37:39 +0000129 f->f_close = close;
Guido van Rossumeb183da1991-04-04 10:44:06 +0000130 f->f_softspace = 0;
Tim Peters59c9a642001-09-13 05:38:56 +0000131 f->f_binary = strchr(mode,'b') != NULL;
Guido van Rossum7a6e9592002-08-06 15:55:28 +0000132 f->f_buf = NULL;
Jack Jansen7b8c7542002-04-14 20:12:41 +0000133#ifdef WITH_UNIVERSAL_NEWLINES
134 f->f_univ_newline = (strchr(mode, 'U') != NULL);
135 f->f_newlinetypes = NEWLINE_UNKNOWN;
136 f->f_skipnextlf = 0;
137#endif
Martin v. Löwis5467d4c2003-05-10 07:10:12 +0000138 Py_INCREF(Py_None);
139 f->f_encoding = Py_None;
Tim Petersf1827cf2003-09-07 03:30:18 +0000140
Tim Peters59c9a642001-09-13 05:38:56 +0000141 if (f->f_name == NULL || f->f_mode == NULL)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000142 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000143 f->f_fp = fp;
Neil Schemenauered19b882002-03-23 02:06:50 +0000144 f = dircheck(f);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000145 return (PyObject *) f;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000146}
147
Tim Peters59c9a642001-09-13 05:38:56 +0000148static PyObject *
149open_the_file(PyFileObject *f, char *name, char *mode)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000150{
Tim Peters59c9a642001-09-13 05:38:56 +0000151 assert(f != NULL);
152 assert(PyFile_Check(f));
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000153#ifdef MS_WINDOWS
154 /* windows ignores the passed name in order to support Unicode */
155 assert(f->f_name != NULL);
156#else
Tim Peters59c9a642001-09-13 05:38:56 +0000157 assert(name != NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000158#endif
Tim Peters59c9a642001-09-13 05:38:56 +0000159 assert(mode != NULL);
Tim Peters44410012001-09-14 03:26:08 +0000160 assert(f->f_fp == NULL);
Tim Peters59c9a642001-09-13 05:38:56 +0000161
Tim Peters8fa45672001-09-13 21:01:29 +0000162 /* rexec.py can't stop a user from getting the file() constructor --
163 all they have to do is get *any* file object f, and then do
164 type(f). Here we prevent them from doing damage with it. */
165 if (PyEval_GetRestricted()) {
166 PyErr_SetString(PyExc_IOError,
Jeremy Hylton8b735422002-08-14 21:01:41 +0000167 "file() constructor not accessible in restricted mode");
Tim Peters8fa45672001-09-13 21:01:29 +0000168 return NULL;
169 }
Tim Petersa27a1502001-11-09 20:59:14 +0000170 errno = 0;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000171#ifdef HAVE_FOPENRF
Guido van Rossuma08095a1991-02-13 23:25:27 +0000172 if (*mode == '*') {
173 FILE *fopenRF();
174 f->f_fp = fopenRF(name, mode+1);
175 }
176 else
177#endif
Guido van Rossumff4949e1992-08-05 19:58:53 +0000178 {
Jack Jansen7b8c7542002-04-14 20:12:41 +0000179#ifdef WITH_UNIVERSAL_NEWLINES
180 if (strcmp(mode, "U") == 0 || strcmp(mode, "rU") == 0)
181 mode = "rb";
182#else
183 /* Compatibility: specifying U in a Python without universal
184 ** newlines is allowed, and the file is opened as a normal text
185 ** file.
186 */
187 if (strcmp(mode, "U") == 0 || strcmp(mode, "rU") == 0)
188 mode = "r";
189#endif
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000190#ifdef MS_WINDOWS
191 if (PyUnicode_Check(f->f_name)) {
Tim Petersf1827cf2003-09-07 03:30:18 +0000192 PyObject *wmode;
193 wmode = PyUnicode_DecodeASCII(mode, strlen(mode), NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000194 if (f->f_name && wmode) {
195 Py_BEGIN_ALLOW_THREADS
196 /* PyUnicode_AS_UNICODE OK without thread
197 lock as it is a simple dereference. */
198 f->f_fp = _wfopen(PyUnicode_AS_UNICODE(f->f_name),
199 PyUnicode_AS_UNICODE(wmode));
200 Py_END_ALLOW_THREADS
201 }
202 Py_XDECREF(wmode);
203 }
204#endif
205 if (NULL == f->f_fp && NULL != name) {
206 Py_BEGIN_ALLOW_THREADS
207 f->f_fp = fopen(name, mode);
208 Py_END_ALLOW_THREADS
209 }
Guido van Rossumff4949e1992-08-05 19:58:53 +0000210 }
Guido van Rossuma08095a1991-02-13 23:25:27 +0000211 if (f->f_fp == NULL) {
Jack Jansene08dea191995-04-23 22:12:47 +0000212#ifdef NO_FOPEN_ERRNO
Jack Jansenb3be2162001-11-30 14:16:36 +0000213 /* Metroworks only, wich does not always sets errno */
Jeremy Hylton41c83212001-11-09 16:17:24 +0000214 if (errno == 0) {
Jack Jansenb3be2162001-11-30 14:16:36 +0000215 PyObject *v;
216 v = Py_BuildValue("(is)", 0, "Cannot open file");
217 if (v != NULL) {
218 PyErr_SetObject(PyExc_IOError, v);
219 Py_DECREF(v);
220 }
Jack Jansene08dea191995-04-23 22:12:47 +0000221 return NULL;
222 }
223#endif
Tim Peters2ea91112002-04-08 04:13:12 +0000224#ifdef _MSC_VER
225 /* MSVC 6 (Microsoft) leaves errno at 0 for bad mode strings,
226 * across all Windows flavors. When it sets EINVAL varies
227 * across Windows flavors, the exact conditions aren't
228 * documented, and the answer lies in the OS's implementation
229 * of Win32's CreateFile function (whose source is secret).
230 * Seems the best we can do is map EINVAL to ENOENT.
231 */
232 if (errno == 0) /* bad mode string */
233 errno = EINVAL;
234 else if (errno == EINVAL) /* unknown, but not a mode string */
235 errno = ENOENT;
236#endif
Jeremy Hylton41c83212001-11-09 16:17:24 +0000237 if (errno == EINVAL)
Tim Peters2ea91112002-04-08 04:13:12 +0000238 PyErr_Format(PyExc_IOError, "invalid mode: %s",
Jeremy Hylton41c83212001-11-09 16:17:24 +0000239 mode);
240 else
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000241#ifdef MS_WINDOWS
242 PyErr_SetFromErrnoWithFilenameObject(PyExc_IOError, f->f_name);
243#else
Jeremy Hylton41c83212001-11-09 16:17:24 +0000244 PyErr_SetFromErrnoWithFilename(PyExc_IOError, name);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000245#endif /* MS_WINDOWS */
Tim Peters59c9a642001-09-13 05:38:56 +0000246 f = NULL;
247 }
Tim Peters2ea91112002-04-08 04:13:12 +0000248 if (f != NULL)
Neil Schemenauered19b882002-03-23 02:06:50 +0000249 f = dircheck(f);
Tim Peters59c9a642001-09-13 05:38:56 +0000250 return (PyObject *)f;
251}
252
253PyObject *
254PyFile_FromFile(FILE *fp, char *name, char *mode, int (*close)(FILE *))
255{
Tim Peters44410012001-09-14 03:26:08 +0000256 PyFileObject *f = (PyFileObject *)PyFile_Type.tp_new(&PyFile_Type,
257 NULL, NULL);
Tim Peters59c9a642001-09-13 05:38:56 +0000258 if (f != NULL) {
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000259 if (fill_file_fields(f, fp, name, mode, close, NULL) == NULL) {
Tim Peters59c9a642001-09-13 05:38:56 +0000260 Py_DECREF(f);
261 f = NULL;
262 }
263 }
264 return (PyObject *) f;
265}
266
267PyObject *
268PyFile_FromString(char *name, char *mode)
269{
270 extern int fclose(FILE *);
271 PyFileObject *f;
272
273 f = (PyFileObject *)PyFile_FromFile((FILE *)NULL, name, mode, fclose);
274 if (f != NULL) {
275 if (open_the_file(f, name, mode) == NULL) {
276 Py_DECREF(f);
277 f = NULL;
278 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000279 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000280 return (PyObject *)f;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000281}
282
Guido van Rossumb6775db1994-08-01 11:34:53 +0000283void
Fred Drakefd99de62000-07-09 05:02:18 +0000284PyFile_SetBufSize(PyObject *f, int bufsize)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000285{
Martin v. Löwis1e3bdf62003-09-04 19:01:46 +0000286 PyFileObject *file = (PyFileObject *)f;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000287 if (bufsize >= 0) {
Guido van Rossumb6775db1994-08-01 11:34:53 +0000288 int type;
289 switch (bufsize) {
290 case 0:
291 type = _IONBF;
292 break;
Martin v. Löwis1e3bdf62003-09-04 19:01:46 +0000293#ifdef HAVE_SETVBUF
Guido van Rossumb6775db1994-08-01 11:34:53 +0000294 case 1:
295 type = _IOLBF;
296 bufsize = BUFSIZ;
297 break;
Martin v. Löwis1e3bdf62003-09-04 19:01:46 +0000298#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000299 default:
300 type = _IOFBF;
Martin v. Löwis1e3bdf62003-09-04 19:01:46 +0000301#ifndef HAVE_SETVBUF
302 bufsize = BUFSIZ;
303#endif
304 break;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000305 }
Martin v. Löwis1e3bdf62003-09-04 19:01:46 +0000306 fflush(file->f_fp);
307 if (type == _IONBF) {
308 PyMem_Free(file->f_setbuf);
309 file->f_setbuf = NULL;
310 } else {
311 file->f_setbuf = PyMem_Realloc(file->f_setbuf, bufsize);
312 }
313#ifdef HAVE_SETVBUF
314 setvbuf(file->f_fp, file->f_setbuf, type, bufsize);
Guido van Rossumf8b4de01998-03-06 15:32:40 +0000315#else /* !HAVE_SETVBUF */
Martin v. Löwis1e3bdf62003-09-04 19:01:46 +0000316 setbuf(file->f_fp, file->f_setbuf);
Guido van Rossumf8b4de01998-03-06 15:32:40 +0000317#endif /* !HAVE_SETVBUF */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000318 }
319}
320
Martin v. Löwis5467d4c2003-05-10 07:10:12 +0000321/* Set the encoding used to output Unicode strings.
322 Returh 1 on success, 0 on failure. */
323
324int
325PyFile_SetEncoding(PyObject *f, const char *enc)
326{
327 PyFileObject *file = (PyFileObject*)f;
328 PyObject *str = PyString_FromString(enc);
329 if (!str)
330 return 0;
331 Py_DECREF(file->f_encoding);
332 file->f_encoding = str;
333 return 1;
334}
335
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000336static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000337err_closed(void)
Guido van Rossumd7297e61992-07-06 14:19:26 +0000338{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000339 PyErr_SetString(PyExc_ValueError, "I/O operation on closed file");
Guido van Rossumd7297e61992-07-06 14:19:26 +0000340 return NULL;
341}
342
Neal Norwitzd8b995f2002-08-06 21:50:54 +0000343static void drop_readahead(PyFileObject *);
Guido van Rossum7a6e9592002-08-06 15:55:28 +0000344
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000345/* Methods */
346
347static void
Fred Drakefd99de62000-07-09 05:02:18 +0000348file_dealloc(PyFileObject *f)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000349{
Guido van Rossumff4949e1992-08-05 19:58:53 +0000350 if (f->f_fp != NULL && f->f_close != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000351 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma1ab7fa1991-06-04 19:37:39 +0000352 (*f->f_close)(f->f_fp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000353 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000354 }
Tim Peters44410012001-09-14 03:26:08 +0000355 Py_XDECREF(f->f_name);
356 Py_XDECREF(f->f_mode);
Martin v. Löwis5467d4c2003-05-10 07:10:12 +0000357 Py_XDECREF(f->f_encoding);
Guido van Rossum7a6e9592002-08-06 15:55:28 +0000358 drop_readahead(f);
Guido van Rossum9475a232001-10-05 20:51:39 +0000359 f->ob_type->tp_free((PyObject *)f);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000360}
361
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000362static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000363file_repr(PyFileObject *f)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000364{
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000365 if (PyUnicode_Check(f->f_name)) {
Martin v. Löwis0073f2e2002-11-21 23:52:35 +0000366#ifdef Py_USING_UNICODE
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000367 PyObject *ret = NULL;
368 PyObject *name;
369 name = PyUnicode_AsUnicodeEscapeString(f->f_name);
370 ret = PyString_FromFormat("<%s file u'%s', mode '%s' at %p>",
371 f->f_fp == NULL ? "closed" : "open",
372 PyString_AsString(name),
373 PyString_AsString(f->f_mode),
374 f);
375 Py_XDECREF(name);
376 return ret;
Martin v. Löwis0073f2e2002-11-21 23:52:35 +0000377#endif
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000378 } else {
379 return PyString_FromFormat("<%s file '%s', mode '%s' at %p>",
Barry Warsaw7ce36942001-08-24 18:34:26 +0000380 f->f_fp == NULL ? "closed" : "open",
381 PyString_AsString(f->f_name),
382 PyString_AsString(f->f_mode),
383 f);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000384 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000385}
386
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000387static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000388file_close(PyFileObject *f)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000389{
Guido van Rossuma1ab7fa1991-06-04 19:37:39 +0000390 int sts = 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000391 if (f->f_fp != NULL) {
Guido van Rossumff4949e1992-08-05 19:58:53 +0000392 if (f->f_close != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000393 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000394 errno = 0;
Guido van Rossuma1ab7fa1991-06-04 19:37:39 +0000395 sts = (*f->f_close)(f->f_fp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000396 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000397 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000398 f->f_fp = NULL;
399 }
Martin v. Löwis7bbcde72003-09-07 20:42:29 +0000400 PyMem_Free(f->f_setbuf);
Guido van Rossumfebd5511992-03-04 16:39:24 +0000401 if (sts == EOF)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000402 return PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossuma1ab7fa1991-06-04 19:37:39 +0000403 if (sts != 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000404 return PyInt_FromLong((long)sts);
405 Py_INCREF(Py_None);
406 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000407}
408
Trent Mickf29f47b2000-08-11 19:02:59 +0000409
Guido van Rossumb8552162001-09-05 14:58:11 +0000410/* Our very own off_t-like type, 64-bit if possible */
411#if !defined(HAVE_LARGEFILE_SUPPORT)
412typedef off_t Py_off_t;
413#elif SIZEOF_OFF_T >= 8
414typedef off_t Py_off_t;
415#elif SIZEOF_FPOS_T >= 8
Guido van Rossum4f53da02001-03-01 18:26:53 +0000416typedef fpos_t Py_off_t;
417#else
Guido van Rossumb8552162001-09-05 14:58:11 +0000418#error "Large file support, but neither off_t nor fpos_t is large enough."
Guido van Rossum4f53da02001-03-01 18:26:53 +0000419#endif
420
421
Trent Mickf29f47b2000-08-11 19:02:59 +0000422/* a portable fseek() function
423 return 0 on success, non-zero on failure (with errno set) */
Guido van Rossumf68d8e52001-04-14 17:55:09 +0000424static int
Guido van Rossum4f53da02001-03-01 18:26:53 +0000425_portable_fseek(FILE *fp, Py_off_t offset, int whence)
Trent Mickf29f47b2000-08-11 19:02:59 +0000426{
Guido van Rossumb8552162001-09-05 14:58:11 +0000427#if !defined(HAVE_LARGEFILE_SUPPORT)
428 return fseek(fp, offset, whence);
429#elif defined(HAVE_FSEEKO) && SIZEOF_OFF_T >= 8
Trent Mickf29f47b2000-08-11 19:02:59 +0000430 return fseeko(fp, offset, whence);
431#elif defined(HAVE_FSEEK64)
432 return fseek64(fp, offset, whence);
Fred Drakedb810ac2000-10-06 20:42:33 +0000433#elif defined(__BEOS__)
434 return _fseek(fp, offset, whence);
Guido van Rossumb8552162001-09-05 14:58:11 +0000435#elif SIZEOF_FPOS_T >= 8
Guido van Rossume54e0be2001-01-16 20:53:31 +0000436 /* lacking a 64-bit capable fseek(), use a 64-bit capable fsetpos()
437 and fgetpos() to implement fseek()*/
Trent Mickf29f47b2000-08-11 19:02:59 +0000438 fpos_t pos;
439 switch (whence) {
Guido van Rossume54e0be2001-01-16 20:53:31 +0000440 case SEEK_END:
Guido van Rossum8b4e43e2001-09-10 20:43:35 +0000441#ifdef MS_WINDOWS
442 fflush(fp);
443 if (_lseeki64(fileno(fp), 0, 2) == -1)
444 return -1;
445#else
Guido van Rossume54e0be2001-01-16 20:53:31 +0000446 if (fseek(fp, 0, SEEK_END) != 0)
447 return -1;
Guido van Rossum8b4e43e2001-09-10 20:43:35 +0000448#endif
Guido van Rossume54e0be2001-01-16 20:53:31 +0000449 /* fall through */
450 case SEEK_CUR:
451 if (fgetpos(fp, &pos) != 0)
452 return -1;
453 offset += pos;
454 break;
455 /* case SEEK_SET: break; */
Trent Mickf29f47b2000-08-11 19:02:59 +0000456 }
457 return fsetpos(fp, &offset);
458#else
Guido van Rossumb8552162001-09-05 14:58:11 +0000459#error "Large file support, but no way to fseek."
Trent Mickf29f47b2000-08-11 19:02:59 +0000460#endif
461}
462
463
464/* a portable ftell() function
465 Return -1 on failure with errno set appropriately, current file
466 position on success */
Guido van Rossumf68d8e52001-04-14 17:55:09 +0000467static Py_off_t
Fred Drake8ce159a2000-08-31 05:18:54 +0000468_portable_ftell(FILE* fp)
Trent Mickf29f47b2000-08-11 19:02:59 +0000469{
Guido van Rossumb8552162001-09-05 14:58:11 +0000470#if !defined(HAVE_LARGEFILE_SUPPORT)
471 return ftell(fp);
472#elif defined(HAVE_FTELLO) && SIZEOF_OFF_T >= 8
473 return ftello(fp);
474#elif defined(HAVE_FTELL64)
475 return ftell64(fp);
476#elif SIZEOF_FPOS_T >= 8
Trent Mickf29f47b2000-08-11 19:02:59 +0000477 fpos_t pos;
478 if (fgetpos(fp, &pos) != 0)
479 return -1;
480 return pos;
481#else
Guido van Rossumb8552162001-09-05 14:58:11 +0000482#error "Large file support, but no way to ftell."
Trent Mickf29f47b2000-08-11 19:02:59 +0000483#endif
484}
485
486
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000487static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000488file_seek(PyFileObject *f, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000489{
Guido van Rossumd7297e61992-07-06 14:19:26 +0000490 int whence;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000491 int ret;
Guido van Rossum4f53da02001-03-01 18:26:53 +0000492 Py_off_t offset;
Guido van Rossum3c9fe0c1999-01-06 18:51:17 +0000493 PyObject *offobj;
Tim Peters86821b22001-01-07 21:19:34 +0000494
Guido van Rossumd7297e61992-07-06 14:19:26 +0000495 if (f->f_fp == NULL)
496 return err_closed();
Guido van Rossum7a6e9592002-08-06 15:55:28 +0000497 drop_readahead(f);
Guido van Rossumd7297e61992-07-06 14:19:26 +0000498 whence = 0;
Guido van Rossum43713e52000-02-29 13:59:29 +0000499 if (!PyArg_ParseTuple(args, "O|i:seek", &offobj, &whence))
Guido van Rossum3c9fe0c1999-01-06 18:51:17 +0000500 return NULL;
501#if !defined(HAVE_LARGEFILE_SUPPORT)
502 offset = PyInt_AsLong(offobj);
503#else
504 offset = PyLong_Check(offobj) ?
505 PyLong_AsLongLong(offobj) : PyInt_AsLong(offobj);
506#endif
507 if (PyErr_Occurred())
Guido van Rossum88303191999-01-04 17:22:18 +0000508 return NULL;
Tim Peters86821b22001-01-07 21:19:34 +0000509
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000510 Py_BEGIN_ALLOW_THREADS
Guido van Rossumce5ba841991-03-06 13:06:18 +0000511 errno = 0;
Trent Mickf29f47b2000-08-11 19:02:59 +0000512 ret = _portable_fseek(f->f_fp, offset, whence);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000513 Py_END_ALLOW_THREADS
Trent Mickf29f47b2000-08-11 19:02:59 +0000514
Guido van Rossumff4949e1992-08-05 19:58:53 +0000515 if (ret != 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000516 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossumfebd5511992-03-04 16:39:24 +0000517 clearerr(f->f_fp);
518 return NULL;
Guido van Rossumce5ba841991-03-06 13:06:18 +0000519 }
Jack Jansen7b8c7542002-04-14 20:12:41 +0000520#ifdef WITH_UNIVERSAL_NEWLINES
521 f->f_skipnextlf = 0;
522#endif
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000523 Py_INCREF(Py_None);
524 return Py_None;
Guido van Rossumce5ba841991-03-06 13:06:18 +0000525}
526
Trent Mickf29f47b2000-08-11 19:02:59 +0000527
Guido van Rossumd7047b31995-01-02 19:07:15 +0000528#ifdef HAVE_FTRUNCATE
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000529static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000530file_truncate(PyFileObject *f, PyObject *args)
Guido van Rossumd7047b31995-01-02 19:07:15 +0000531{
Guido van Rossum4f53da02001-03-01 18:26:53 +0000532 Py_off_t newsize;
Tim Petersf1827cf2003-09-07 03:30:18 +0000533 PyObject *newsizeobj = NULL;
534 Py_off_t initialpos;
535 int ret;
Tim Peters86821b22001-01-07 21:19:34 +0000536
Guido van Rossumd7047b31995-01-02 19:07:15 +0000537 if (f->f_fp == NULL)
538 return err_closed();
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000539 if (!PyArg_UnpackTuple(args, "truncate", 0, 1, &newsizeobj))
Guido van Rossum88303191999-01-04 17:22:18 +0000540 return NULL;
Tim Petersfb05db22002-03-11 00:24:00 +0000541
Tim Petersf1827cf2003-09-07 03:30:18 +0000542 /* Get current file position. If the file happens to be open for
543 * update and the last operation was an input operation, C doesn't
544 * define what the later fflush() will do, but we promise truncate()
545 * won't change the current position (and fflush() *does* change it
546 * then at least on Windows). The easiest thing is to capture
547 * current pos now and seek back to it at the end.
548 */
549 Py_BEGIN_ALLOW_THREADS
550 errno = 0;
551 initialpos = _portable_ftell(f->f_fp);
552 Py_END_ALLOW_THREADS
553 if (initialpos == -1)
554 goto onioerror;
555
Tim Petersfb05db22002-03-11 00:24:00 +0000556 /* Set newsize to current postion if newsizeobj NULL, else to the
Tim Petersf1827cf2003-09-07 03:30:18 +0000557 * specified value.
558 */
Guido van Rossum3c9fe0c1999-01-06 18:51:17 +0000559 if (newsizeobj != NULL) {
560#if !defined(HAVE_LARGEFILE_SUPPORT)
561 newsize = PyInt_AsLong(newsizeobj);
562#else
563 newsize = PyLong_Check(newsizeobj) ?
564 PyLong_AsLongLong(newsizeobj) :
565 PyInt_AsLong(newsizeobj);
566#endif
567 if (PyErr_Occurred())
568 return NULL;
Tim Petersfb05db22002-03-11 00:24:00 +0000569 }
Tim Petersf1827cf2003-09-07 03:30:18 +0000570 else /* default to current position */
571 newsize = initialpos;
Tim Petersfb05db22002-03-11 00:24:00 +0000572
Tim Petersf1827cf2003-09-07 03:30:18 +0000573 /* Flush the stream. We're mixing stream-level I/O with lower-level
574 * I/O, and a flush may be necessary to synch both platform views
575 * of the current file state.
576 */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000577 Py_BEGIN_ALLOW_THREADS
Guido van Rossumd7047b31995-01-02 19:07:15 +0000578 errno = 0;
579 ret = fflush(f->f_fp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000580 Py_END_ALLOW_THREADS
Tim Petersfb05db22002-03-11 00:24:00 +0000581 if (ret != 0)
582 goto onioerror;
Trent Mickf29f47b2000-08-11 19:02:59 +0000583
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000584#ifdef MS_WINDOWS
Tim Petersfb05db22002-03-11 00:24:00 +0000585 /* MS _chsize doesn't work if newsize doesn't fit in 32 bits,
Tim Peters8f01b682002-03-12 03:04:44 +0000586 so don't even try using it. */
Tim Petersfb05db22002-03-11 00:24:00 +0000587 {
Tim Petersfb05db22002-03-11 00:24:00 +0000588 HANDLE hFile;
Tim Petersfb05db22002-03-11 00:24:00 +0000589
Tim Petersf1827cf2003-09-07 03:30:18 +0000590 /* Have to move current pos to desired endpoint on Windows. */
591 Py_BEGIN_ALLOW_THREADS
592 errno = 0;
593 ret = _portable_fseek(f->f_fp, newsize, SEEK_SET) != 0;
594 Py_END_ALLOW_THREADS
595 if (ret)
596 goto onioerror;
Tim Petersfb05db22002-03-11 00:24:00 +0000597
Tim Peters8f01b682002-03-12 03:04:44 +0000598 /* Truncate. Note that this may grow the file! */
599 Py_BEGIN_ALLOW_THREADS
600 errno = 0;
601 hFile = (HANDLE)_get_osfhandle(fileno(f->f_fp));
Tim Petersf1827cf2003-09-07 03:30:18 +0000602 ret = hFile == (HANDLE)-1;
603 if (ret == 0) {
604 ret = SetEndOfFile(hFile) == 0;
605 if (ret)
Tim Peters8f01b682002-03-12 03:04:44 +0000606 errno = EACCES;
607 }
608 Py_END_ALLOW_THREADS
Tim Petersf1827cf2003-09-07 03:30:18 +0000609 if (ret)
Tim Peters8f01b682002-03-12 03:04:44 +0000610 goto onioerror;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000611 }
Trent Mickf29f47b2000-08-11 19:02:59 +0000612#else
613 Py_BEGIN_ALLOW_THREADS
614 errno = 0;
615 ret = ftruncate(fileno(f->f_fp), newsize);
616 Py_END_ALLOW_THREADS
Tim Petersf1827cf2003-09-07 03:30:18 +0000617 if (ret != 0)
618 goto onioerror;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000619#endif /* !MS_WINDOWS */
Tim Peters86821b22001-01-07 21:19:34 +0000620
Tim Petersf1827cf2003-09-07 03:30:18 +0000621 /* Restore original file position. */
622 Py_BEGIN_ALLOW_THREADS
623 errno = 0;
624 ret = _portable_fseek(f->f_fp, initialpos, SEEK_SET) != 0;
625 Py_END_ALLOW_THREADS
626 if (ret)
627 goto onioerror;
628
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000629 Py_INCREF(Py_None);
630 return Py_None;
Trent Mickf29f47b2000-08-11 19:02:59 +0000631
632onioerror:
633 PyErr_SetFromErrno(PyExc_IOError);
634 clearerr(f->f_fp);
635 return NULL;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000636}
637#endif /* HAVE_FTRUNCATE */
638
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000639static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000640file_tell(PyFileObject *f)
Guido van Rossumce5ba841991-03-06 13:06:18 +0000641{
Guido van Rossum4f53da02001-03-01 18:26:53 +0000642 Py_off_t pos;
Trent Mickf29f47b2000-08-11 19:02:59 +0000643
Guido van Rossumd7297e61992-07-06 14:19:26 +0000644 if (f->f_fp == NULL)
645 return err_closed();
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000646 Py_BEGIN_ALLOW_THREADS
Guido van Rossumce5ba841991-03-06 13:06:18 +0000647 errno = 0;
Trent Mickf29f47b2000-08-11 19:02:59 +0000648 pos = _portable_ftell(f->f_fp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000649 Py_END_ALLOW_THREADS
Trent Mickf29f47b2000-08-11 19:02:59 +0000650 if (pos == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000651 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossumfebd5511992-03-04 16:39:24 +0000652 clearerr(f->f_fp);
653 return NULL;
Guido van Rossumce5ba841991-03-06 13:06:18 +0000654 }
Jack Jansen7b8c7542002-04-14 20:12:41 +0000655#ifdef WITH_UNIVERSAL_NEWLINES
656 if (f->f_skipnextlf) {
657 int c;
658 c = GETC(f->f_fp);
659 if (c == '\n') {
660 pos++;
661 f->f_skipnextlf = 0;
662 } else if (c != EOF) ungetc(c, f->f_fp);
663 }
664#endif
Guido van Rossum3c9fe0c1999-01-06 18:51:17 +0000665#if !defined(HAVE_LARGEFILE_SUPPORT)
Trent Mickf29f47b2000-08-11 19:02:59 +0000666 return PyInt_FromLong(pos);
Guido van Rossum3c9fe0c1999-01-06 18:51:17 +0000667#else
Trent Mickf29f47b2000-08-11 19:02:59 +0000668 return PyLong_FromLongLong(pos);
Guido van Rossum3c9fe0c1999-01-06 18:51:17 +0000669#endif
Guido van Rossumce5ba841991-03-06 13:06:18 +0000670}
671
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000672static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000673file_fileno(PyFileObject *f)
Guido van Rossumed233a51992-06-23 09:07:03 +0000674{
Guido van Rossumd7297e61992-07-06 14:19:26 +0000675 if (f->f_fp == NULL)
676 return err_closed();
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000677 return PyInt_FromLong((long) fileno(f->f_fp));
Guido van Rossumed233a51992-06-23 09:07:03 +0000678}
679
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000680static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000681file_flush(PyFileObject *f)
Guido van Rossumce5ba841991-03-06 13:06:18 +0000682{
Guido van Rossumff4949e1992-08-05 19:58:53 +0000683 int res;
Tim Peters86821b22001-01-07 21:19:34 +0000684
Guido van Rossumd7297e61992-07-06 14:19:26 +0000685 if (f->f_fp == NULL)
686 return err_closed();
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000687 Py_BEGIN_ALLOW_THREADS
Guido van Rossumce5ba841991-03-06 13:06:18 +0000688 errno = 0;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000689 res = fflush(f->f_fp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000690 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000691 if (res != 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000692 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossumfebd5511992-03-04 16:39:24 +0000693 clearerr(f->f_fp);
694 return NULL;
Guido van Rossumce5ba841991-03-06 13:06:18 +0000695 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000696 Py_INCREF(Py_None);
697 return Py_None;
Guido van Rossumce5ba841991-03-06 13:06:18 +0000698}
699
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000700static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000701file_isatty(PyFileObject *f)
Guido van Rossuma1ab7fa1991-06-04 19:37:39 +0000702{
Guido van Rossumff4949e1992-08-05 19:58:53 +0000703 long res;
Guido van Rossumd7297e61992-07-06 14:19:26 +0000704 if (f->f_fp == NULL)
705 return err_closed();
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000706 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000707 res = isatty((int)fileno(f->f_fp));
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000708 Py_END_ALLOW_THREADS
Guido van Rossum7f7666f2002-04-07 06:28:00 +0000709 return PyBool_FromLong(res);
Guido van Rossuma1ab7fa1991-06-04 19:37:39 +0000710}
711
Guido van Rossumff7e83d1999-08-27 20:39:37 +0000712
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000713#if BUFSIZ < 8192
714#define SMALLCHUNK 8192
715#else
716#define SMALLCHUNK BUFSIZ
717#endif
718
Guido van Rossum3c259041999-01-14 19:00:14 +0000719#if SIZEOF_INT < 4
720#define BIGCHUNK (512 * 32)
721#else
722#define BIGCHUNK (512 * 1024)
723#endif
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000724
725static size_t
Fred Drakefd99de62000-07-09 05:02:18 +0000726new_buffersize(PyFileObject *f, size_t currentsize)
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000727{
728#ifdef HAVE_FSTAT
Fred Drake1bc8fab2001-07-19 21:49:38 +0000729 off_t pos, end;
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000730 struct stat st;
731 if (fstat(fileno(f->f_fp), &st) == 0) {
732 end = st.st_size;
Guido van Rossumcada2931998-12-11 20:44:56 +0000733 /* The following is not a bug: we really need to call lseek()
734 *and* ftell(). The reason is that some stdio libraries
735 mistakenly flush their buffer when ftell() is called and
736 the lseek() call it makes fails, thereby throwing away
737 data that cannot be recovered in any way. To avoid this,
738 we first test lseek(), and only call ftell() if lseek()
739 works. We can't use the lseek() value either, because we
740 need to take the amount of buffered data into account.
741 (Yet another reason why stdio stinks. :-) */
Jack Jansen2771b5b2001-10-10 22:03:27 +0000742#ifdef USE_GUSI2
743 pos = lseek(fileno(f->f_fp), 1L, SEEK_CUR);
744 pos = lseek(fileno(f->f_fp), -1L, SEEK_CUR);
745#else
Guido van Rossum91aaa921998-05-05 22:21:35 +0000746 pos = lseek(fileno(f->f_fp), 0L, SEEK_CUR);
Jack Jansen2771b5b2001-10-10 22:03:27 +0000747#endif
748 if (pos >= 0) {
Guido van Rossum91aaa921998-05-05 22:21:35 +0000749 pos = ftell(f->f_fp);
Jack Jansen2771b5b2001-10-10 22:03:27 +0000750 }
Guido van Rossumd30dc0a1998-04-27 19:01:08 +0000751 if (pos < 0)
752 clearerr(f->f_fp);
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000753 if (end > pos && pos >= 0)
Guido van Rossumcada2931998-12-11 20:44:56 +0000754 return currentsize + end - pos + 1;
Guido van Rossumdcb5e7f1998-03-03 22:36:10 +0000755 /* Add 1 so if the file were to grow we'd notice. */
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000756 }
757#endif
758 if (currentsize > SMALLCHUNK) {
759 /* Keep doubling until we reach BIGCHUNK;
760 then keep adding BIGCHUNK. */
761 if (currentsize <= BIGCHUNK)
762 return currentsize + currentsize;
763 else
764 return currentsize + BIGCHUNK;
765 }
766 return currentsize + SMALLCHUNK;
767}
768
Gustavo Niemeyer786ddb22002-12-16 18:12:53 +0000769#if defined(EWOULDBLOCK) && defined(EAGAIN) && EWOULDBLOCK != EAGAIN
770#define BLOCKED_ERRNO(x) ((x) == EWOULDBLOCK || (x) == EAGAIN)
771#else
772#ifdef EWOULDBLOCK
773#define BLOCKED_ERRNO(x) ((x) == EWOULDBLOCK)
774#else
775#ifdef EAGAIN
776#define BLOCKED_ERRNO(x) ((x) == EAGAIN)
777#else
778#define BLOCKED_ERRNO(x) 0
779#endif
780#endif
781#endif
782
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000783static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000784file_read(PyFileObject *f, PyObject *args)
Guido van Rossumce5ba841991-03-06 13:06:18 +0000785{
Guido van Rossum789a1611997-05-10 22:33:55 +0000786 long bytesrequested = -1;
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000787 size_t bytesread, buffersize, chunksize;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000788 PyObject *v;
Tim Peters86821b22001-01-07 21:19:34 +0000789
Guido van Rossumd7297e61992-07-06 14:19:26 +0000790 if (f->f_fp == NULL)
791 return err_closed();
Guido van Rossum43713e52000-02-29 13:59:29 +0000792 if (!PyArg_ParseTuple(args, "|l:read", &bytesrequested))
Guido van Rossum789a1611997-05-10 22:33:55 +0000793 return NULL;
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000794 if (bytesrequested < 0)
Guido van Rossumff1ccbf1999-04-10 15:48:23 +0000795 buffersize = new_buffersize(f, (size_t)0);
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000796 else
797 buffersize = bytesrequested;
Trent Mickf29f47b2000-08-11 19:02:59 +0000798 if (buffersize > INT_MAX) {
799 PyErr_SetString(PyExc_OverflowError,
Jeremy Hylton8b735422002-08-14 21:01:41 +0000800 "requested number of bytes is more than a Python string can hold");
Trent Mickf29f47b2000-08-11 19:02:59 +0000801 return NULL;
802 }
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000803 v = PyString_FromStringAndSize((char *)NULL, buffersize);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000804 if (v == NULL)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000805 return NULL;
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000806 bytesread = 0;
Guido van Rossumce5ba841991-03-06 13:06:18 +0000807 for (;;) {
Guido van Rossum6263d541997-05-10 22:07:25 +0000808 Py_BEGIN_ALLOW_THREADS
809 errno = 0;
Jack Jansen7b8c7542002-04-14 20:12:41 +0000810 chunksize = Py_UniversalNewlineFread(BUF(v) + bytesread,
Jeremy Hylton8b735422002-08-14 21:01:41 +0000811 buffersize - bytesread, f->f_fp, (PyObject *)f);
Guido van Rossum6263d541997-05-10 22:07:25 +0000812 Py_END_ALLOW_THREADS
813 if (chunksize == 0) {
814 if (!ferror(f->f_fp))
815 break;
Guido van Rossum6263d541997-05-10 22:07:25 +0000816 clearerr(f->f_fp);
Gustavo Niemeyer786ddb22002-12-16 18:12:53 +0000817 /* When in non-blocking mode, data shouldn't
818 * be discarded if a blocking signal was
819 * received. That will also happen if
820 * chunksize != 0, but bytesread < buffersize. */
821 if (bytesread > 0 && BLOCKED_ERRNO(errno))
822 break;
823 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum6263d541997-05-10 22:07:25 +0000824 Py_DECREF(v);
825 return NULL;
826 }
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000827 bytesread += chunksize;
Gustavo Niemeyer786ddb22002-12-16 18:12:53 +0000828 if (bytesread < buffersize) {
829 clearerr(f->f_fp);
Guido van Rossumce5ba841991-03-06 13:06:18 +0000830 break;
Gustavo Niemeyer786ddb22002-12-16 18:12:53 +0000831 }
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000832 if (bytesrequested < 0) {
Guido van Rossumcada2931998-12-11 20:44:56 +0000833 buffersize = new_buffersize(f, buffersize);
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000834 if (_PyString_Resize(&v, buffersize) < 0)
Guido van Rossumce5ba841991-03-06 13:06:18 +0000835 return NULL;
Gustavo Niemeyer786ddb22002-12-16 18:12:53 +0000836 } else {
Gustavo Niemeyera080be82002-12-17 17:48:00 +0000837 /* Got what was requested. */
Gustavo Niemeyer786ddb22002-12-16 18:12:53 +0000838 break;
Guido van Rossumce5ba841991-03-06 13:06:18 +0000839 }
840 }
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000841 if (bytesread != buffersize)
842 _PyString_Resize(&v, bytesread);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000843 return v;
844}
845
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000846static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000847file_readinto(PyFileObject *f, PyObject *args)
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000848{
849 char *ptr;
Guido van Rossum00ebd462001-10-23 21:25:24 +0000850 int ntodo;
851 size_t ndone, nnow;
Tim Peters86821b22001-01-07 21:19:34 +0000852
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000853 if (f->f_fp == NULL)
854 return err_closed();
Neal Norwitz62f5a9d2002-04-01 00:09:00 +0000855 if (!PyArg_ParseTuple(args, "w#", &ptr, &ntodo))
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000856 return NULL;
857 ndone = 0;
Guido van Rossum6263d541997-05-10 22:07:25 +0000858 while (ntodo > 0) {
859 Py_BEGIN_ALLOW_THREADS
860 errno = 0;
Tim Petersf1827cf2003-09-07 03:30:18 +0000861 nnow = Py_UniversalNewlineFread(ptr+ndone, ntodo, f->f_fp,
Jeremy Hylton8b735422002-08-14 21:01:41 +0000862 (PyObject *)f);
Guido van Rossum6263d541997-05-10 22:07:25 +0000863 Py_END_ALLOW_THREADS
864 if (nnow == 0) {
865 if (!ferror(f->f_fp))
866 break;
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000867 PyErr_SetFromErrno(PyExc_IOError);
868 clearerr(f->f_fp);
869 return NULL;
870 }
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000871 ndone += nnow;
872 ntodo -= nnow;
873 }
Trent Mickf29f47b2000-08-11 19:02:59 +0000874 return PyInt_FromLong((long)ndone);
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000875}
876
Tim Peters86821b22001-01-07 21:19:34 +0000877/**************************************************************************
Tim Petersf29b64d2001-01-15 06:33:19 +0000878Routine to get next line using platform fgets().
Tim Peters86821b22001-01-07 21:19:34 +0000879
880Under MSVC 6:
881
Tim Peters1c733232001-01-08 04:02:07 +0000882+ MS threadsafe getc is very slow (multiple layers of function calls before+
883 after each character, to lock+unlock the stream).
884+ The stream-locking functions are MS-internal -- can't access them from user
885 code.
886+ There's nothing Tim could find in the MS C or platform SDK libraries that
887 can worm around this.
Tim Peters86821b22001-01-07 21:19:34 +0000888+ MS fgets locks/unlocks only once per line; it's the only hook we have.
889
890So we use fgets for speed(!), despite that it's painful.
891
892MS realloc is also slow.
893
Tim Petersf29b64d2001-01-15 06:33:19 +0000894Reports from other platforms on this method vs getc_unlocked (which MS doesn't
895have):
896 Linux a wash
897 Solaris a wash
898 Tru64 Unix getline_via_fgets significantly faster
Tim Peters86821b22001-01-07 21:19:34 +0000899
Tim Petersf29b64d2001-01-15 06:33:19 +0000900CAUTION: The C std isn't clear about this: in those cases where fgets
901writes something into the buffer, can it write into any position beyond the
902required trailing null byte? MSVC 6 fgets does not, and no platform is (yet)
903known on which it does; and it would be a strange way to code fgets. Still,
904getline_via_fgets may not work correctly if it does. The std test
905test_bufio.py should fail if platform fgets() routinely writes beyond the
906trailing null byte. #define DONT_USE_FGETS_IN_GETLINE to disable this code.
Tim Peters86821b22001-01-07 21:19:34 +0000907**************************************************************************/
908
Tim Petersf29b64d2001-01-15 06:33:19 +0000909/* Use this routine if told to, or by default on non-get_unlocked()
910 * platforms unless told not to. Yikes! Let's spell that out:
911 * On a platform with getc_unlocked():
912 * By default, use getc_unlocked().
913 * If you want to use fgets() instead, #define USE_FGETS_IN_GETLINE.
914 * On a platform without getc_unlocked():
915 * By default, use fgets().
916 * If you don't want to use fgets(), #define DONT_USE_FGETS_IN_GETLINE.
917 */
918#if !defined(USE_FGETS_IN_GETLINE) && !defined(HAVE_GETC_UNLOCKED)
919#define USE_FGETS_IN_GETLINE
Tim Peters86821b22001-01-07 21:19:34 +0000920#endif
921
Tim Petersf29b64d2001-01-15 06:33:19 +0000922#if defined(DONT_USE_FGETS_IN_GETLINE) && defined(USE_FGETS_IN_GETLINE)
923#undef USE_FGETS_IN_GETLINE
924#endif
925
926#ifdef USE_FGETS_IN_GETLINE
Tim Peters86821b22001-01-07 21:19:34 +0000927static PyObject*
Tim Petersf29b64d2001-01-15 06:33:19 +0000928getline_via_fgets(FILE *fp)
Tim Peters86821b22001-01-07 21:19:34 +0000929{
Tim Peters15b83852001-01-08 00:53:12 +0000930/* INITBUFSIZE is the maximum line length that lets us get away with the fast
Tim Peters142297a2001-01-15 10:36:56 +0000931 * no-realloc, one-fgets()-call path. Boosting it isn't free, because we have
932 * to fill this much of the buffer with a known value in order to figure out
933 * how much of the buffer fgets() overwrites. So if INITBUFSIZE is larger
934 * than "most" lines, we waste time filling unused buffer slots. 100 is
935 * surely adequate for most peoples' email archives, chewing over source code,
936 * etc -- "regular old text files".
937 * MAXBUFSIZE is the maximum line length that lets us get away with the less
938 * fast (but still zippy) no-realloc, two-fgets()-call path. See above for
939 * cautions about boosting that. 300 was chosen because the worst real-life
940 * text-crunching job reported on Python-Dev was a mail-log crawler where over
941 * half the lines were 254 chars.
Tim Peters15b83852001-01-08 00:53:12 +0000942 */
Tim Peters142297a2001-01-15 10:36:56 +0000943#define INITBUFSIZE 100
944#define MAXBUFSIZE 300
Tim Peters142297a2001-01-15 10:36:56 +0000945 char* p; /* temp */
946 char buf[MAXBUFSIZE];
Tim Peters86821b22001-01-07 21:19:34 +0000947 PyObject* v; /* the string object result */
Tim Peters86821b22001-01-07 21:19:34 +0000948 char* pvfree; /* address of next free slot */
949 char* pvend; /* address one beyond last free slot */
Tim Peters142297a2001-01-15 10:36:56 +0000950 size_t nfree; /* # of free buffer slots; pvend-pvfree */
951 size_t total_v_size; /* total # of slots in buffer */
Tim Petersddea2082002-03-23 10:03:50 +0000952 size_t increment; /* amount to increment the buffer */
Tim Peters86821b22001-01-07 21:19:34 +0000953
Tim Peters15b83852001-01-08 00:53:12 +0000954 /* Optimize for normal case: avoid _PyString_Resize if at all
Tim Peters142297a2001-01-15 10:36:56 +0000955 * possible via first reading into stack buffer "buf".
Tim Peters15b83852001-01-08 00:53:12 +0000956 */
Tim Peters142297a2001-01-15 10:36:56 +0000957 total_v_size = INITBUFSIZE; /* start small and pray */
958 pvfree = buf;
959 for (;;) {
960 Py_BEGIN_ALLOW_THREADS
961 pvend = buf + total_v_size;
962 nfree = pvend - pvfree;
963 memset(pvfree, '\n', nfree);
964 p = fgets(pvfree, nfree, fp);
965 Py_END_ALLOW_THREADS
Tim Peters15b83852001-01-08 00:53:12 +0000966
Tim Peters142297a2001-01-15 10:36:56 +0000967 if (p == NULL) {
968 clearerr(fp);
969 if (PyErr_CheckSignals())
970 return NULL;
971 v = PyString_FromStringAndSize(buf, pvfree - buf);
Tim Peters86821b22001-01-07 21:19:34 +0000972 return v;
973 }
Tim Peters142297a2001-01-15 10:36:56 +0000974 /* fgets read *something* */
975 p = memchr(pvfree, '\n', nfree);
976 if (p != NULL) {
977 /* Did the \n come from fgets or from us?
978 * Since fgets stops at the first \n, and then writes
979 * \0, if it's from fgets a \0 must be next. But if
980 * that's so, it could not have come from us, since
981 * the \n's we filled the buffer with have only more
982 * \n's to the right.
983 */
984 if (p+1 < pvend && *(p+1) == '\0') {
985 /* It's from fgets: we win! In particular,
986 * we haven't done any mallocs yet, and can
987 * build the final result on the first try.
988 */
989 ++p; /* include \n from fgets */
990 }
991 else {
992 /* Must be from us: fgets didn't fill the
993 * buffer and didn't find a newline, so it
994 * must be the last and newline-free line of
995 * the file.
996 */
997 assert(p > pvfree && *(p-1) == '\0');
998 --p; /* don't include \0 from fgets */
999 }
1000 v = PyString_FromStringAndSize(buf, p - buf);
1001 return v;
1002 }
1003 /* yuck: fgets overwrote all the newlines, i.e. the entire
1004 * buffer. So this line isn't over yet, or maybe it is but
1005 * we're exactly at EOF. If we haven't already, try using the
1006 * rest of the stack buffer.
Tim Peters86821b22001-01-07 21:19:34 +00001007 */
Tim Peters142297a2001-01-15 10:36:56 +00001008 assert(*(pvend-1) == '\0');
1009 if (pvfree == buf) {
1010 pvfree = pvend - 1; /* overwrite trailing null */
1011 total_v_size = MAXBUFSIZE;
1012 }
1013 else
1014 break;
Tim Peters86821b22001-01-07 21:19:34 +00001015 }
Tim Peters142297a2001-01-15 10:36:56 +00001016
1017 /* The stack buffer isn't big enough; malloc a string object and read
1018 * into its buffer.
Tim Peters15b83852001-01-08 00:53:12 +00001019 */
Tim Petersddea2082002-03-23 10:03:50 +00001020 total_v_size = MAXBUFSIZE << 1;
Tim Peters1c733232001-01-08 04:02:07 +00001021 v = PyString_FromStringAndSize((char*)NULL, (int)total_v_size);
Tim Peters15b83852001-01-08 00:53:12 +00001022 if (v == NULL)
1023 return v;
1024 /* copy over everything except the last null byte */
Tim Peters142297a2001-01-15 10:36:56 +00001025 memcpy(BUF(v), buf, MAXBUFSIZE-1);
1026 pvfree = BUF(v) + MAXBUFSIZE - 1;
Tim Peters86821b22001-01-07 21:19:34 +00001027
1028 /* Keep reading stuff into v; if it ever ends successfully, break
Tim Peters15b83852001-01-08 00:53:12 +00001029 * after setting p one beyond the end of the line. The code here is
1030 * very much like the code above, except reads into v's buffer; see
1031 * the code above for detailed comments about the logic.
Tim Peters86821b22001-01-07 21:19:34 +00001032 */
1033 for (;;) {
Tim Peters86821b22001-01-07 21:19:34 +00001034 Py_BEGIN_ALLOW_THREADS
1035 pvend = BUF(v) + total_v_size;
1036 nfree = pvend - pvfree;
1037 memset(pvfree, '\n', nfree);
1038 p = fgets(pvfree, nfree, fp);
1039 Py_END_ALLOW_THREADS
1040
1041 if (p == NULL) {
1042 clearerr(fp);
1043 if (PyErr_CheckSignals()) {
1044 Py_DECREF(v);
1045 return NULL;
1046 }
1047 p = pvfree;
1048 break;
1049 }
Tim Peters86821b22001-01-07 21:19:34 +00001050 p = memchr(pvfree, '\n', nfree);
1051 if (p != NULL) {
1052 if (p+1 < pvend && *(p+1) == '\0') {
1053 /* \n came from fgets */
1054 ++p;
1055 break;
1056 }
1057 /* \n came from us; last line of file, no newline */
1058 assert(p > pvfree && *(p-1) == '\0');
1059 --p;
1060 break;
1061 }
1062 /* expand buffer and try again */
1063 assert(*(pvend-1) == '\0');
Tim Petersddea2082002-03-23 10:03:50 +00001064 increment = total_v_size >> 2; /* mild exponential growth */
1065 total_v_size += increment;
Tim Peters86821b22001-01-07 21:19:34 +00001066 if (total_v_size > INT_MAX) {
1067 PyErr_SetString(PyExc_OverflowError,
1068 "line is longer than a Python string can hold");
1069 Py_DECREF(v);
1070 return NULL;
1071 }
1072 if (_PyString_Resize(&v, (int)total_v_size) < 0)
1073 return NULL;
1074 /* overwrite the trailing null byte */
Tim Petersddea2082002-03-23 10:03:50 +00001075 pvfree = BUF(v) + (total_v_size - increment - 1);
Tim Peters86821b22001-01-07 21:19:34 +00001076 }
1077 if (BUF(v) + total_v_size != p)
1078 _PyString_Resize(&v, p - BUF(v));
1079 return v;
1080#undef INITBUFSIZE
Tim Peters142297a2001-01-15 10:36:56 +00001081#undef MAXBUFSIZE
Tim Peters86821b22001-01-07 21:19:34 +00001082}
Tim Petersf29b64d2001-01-15 06:33:19 +00001083#endif /* ifdef USE_FGETS_IN_GETLINE */
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001084
Guido van Rossum0bd24411991-04-04 15:21:57 +00001085/* Internal routine to get a line.
1086 Size argument interpretation:
1087 > 0: max length;
Guido van Rossum86282062001-01-08 01:26:47 +00001088 <= 0: read arbitrary line
Guido van Rossumce5ba841991-03-06 13:06:18 +00001089*/
1090
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001091static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +00001092get_line(PyFileObject *f, int n)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001093{
Guido van Rossum1187aa42001-01-05 14:43:05 +00001094 FILE *fp = f->f_fp;
1095 int c;
Andrew M. Kuchling4b2b4452000-11-29 02:53:22 +00001096 char *buf, *end;
Neil Schemenauer3a204a72002-03-23 19:41:34 +00001097 size_t total_v_size; /* total # of slots in buffer */
1098 size_t used_v_size; /* # used slots in buffer */
1099 size_t increment; /* amount to increment the buffer */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001100 PyObject *v;
Jack Jansen7b8c7542002-04-14 20:12:41 +00001101#ifdef WITH_UNIVERSAL_NEWLINES
1102 int newlinetypes = f->f_newlinetypes;
1103 int skipnextlf = f->f_skipnextlf;
1104 int univ_newline = f->f_univ_newline;
1105#endif
Guido van Rossum0bd24411991-04-04 15:21:57 +00001106
Jack Jansen7b8c7542002-04-14 20:12:41 +00001107#if defined(USE_FGETS_IN_GETLINE)
1108#ifdef WITH_UNIVERSAL_NEWLINES
1109 if (n <= 0 && !univ_newline )
1110#else
Guido van Rossum86282062001-01-08 01:26:47 +00001111 if (n <= 0)
Jack Jansen7b8c7542002-04-14 20:12:41 +00001112#endif
Tim Petersf29b64d2001-01-15 06:33:19 +00001113 return getline_via_fgets(fp);
Tim Peters86821b22001-01-07 21:19:34 +00001114#endif
Neil Schemenauer3a204a72002-03-23 19:41:34 +00001115 total_v_size = n > 0 ? n : 100;
1116 v = PyString_FromStringAndSize((char *)NULL, total_v_size);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001117 if (v == NULL)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001118 return NULL;
Guido van Rossumce5ba841991-03-06 13:06:18 +00001119 buf = BUF(v);
Neil Schemenauer3a204a72002-03-23 19:41:34 +00001120 end = buf + total_v_size;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001121
Guido van Rossumce5ba841991-03-06 13:06:18 +00001122 for (;;) {
Guido van Rossum1187aa42001-01-05 14:43:05 +00001123 Py_BEGIN_ALLOW_THREADS
1124 FLOCKFILE(fp);
Jack Jansen7b8c7542002-04-14 20:12:41 +00001125#ifdef WITH_UNIVERSAL_NEWLINES
1126 if (univ_newline) {
1127 c = 'x'; /* Shut up gcc warning */
1128 while ( buf != end && (c = GETC(fp)) != EOF ) {
1129 if (skipnextlf ) {
1130 skipnextlf = 0;
1131 if (c == '\n') {
Tim Petersf1827cf2003-09-07 03:30:18 +00001132 /* Seeing a \n here with
1133 * skipnextlf true means we
Jeremy Hylton8b735422002-08-14 21:01:41 +00001134 * saw a \r before.
1135 */
Jack Jansen7b8c7542002-04-14 20:12:41 +00001136 newlinetypes |= NEWLINE_CRLF;
1137 c = GETC(fp);
1138 if (c == EOF) break;
1139 } else {
1140 newlinetypes |= NEWLINE_CR;
1141 }
1142 }
1143 if (c == '\r') {
1144 skipnextlf = 1;
1145 c = '\n';
1146 } else if ( c == '\n')
1147 newlinetypes |= NEWLINE_LF;
1148 *buf++ = c;
1149 if (c == '\n') break;
1150 }
1151 if ( c == EOF && skipnextlf )
1152 newlinetypes |= NEWLINE_CR;
1153 } else /* If not universal newlines use the normal loop */
1154#endif
Guido van Rossum1187aa42001-01-05 14:43:05 +00001155 while ((c = GETC(fp)) != EOF &&
1156 (*buf++ = c) != '\n' &&
1157 buf != end)
1158 ;
1159 FUNLOCKFILE(fp);
1160 Py_END_ALLOW_THREADS
Jack Jansen7b8c7542002-04-14 20:12:41 +00001161#ifdef WITH_UNIVERSAL_NEWLINES
1162 f->f_newlinetypes = newlinetypes;
1163 f->f_skipnextlf = skipnextlf;
1164#endif
Guido van Rossum1187aa42001-01-05 14:43:05 +00001165 if (c == '\n')
1166 break;
1167 if (c == EOF) {
Guido van Rossum29206bc2001-08-09 18:14:59 +00001168 if (ferror(fp)) {
1169 PyErr_SetFromErrno(PyExc_IOError);
1170 clearerr(fp);
1171 Py_DECREF(v);
1172 return NULL;
1173 }
Guido van Rossum76ad8ed1991-06-03 10:54:55 +00001174 clearerr(fp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001175 if (PyErr_CheckSignals()) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001176 Py_DECREF(v);
Guido van Rossum0bd24411991-04-04 15:21:57 +00001177 return NULL;
1178 }
Guido van Rossumce5ba841991-03-06 13:06:18 +00001179 break;
Guido van Rossum0bd24411991-04-04 15:21:57 +00001180 }
Guido van Rossum1187aa42001-01-05 14:43:05 +00001181 /* Must be because buf == end */
1182 if (n > 0)
Guido van Rossum0bd24411991-04-04 15:21:57 +00001183 break;
Neil Schemenauer3a204a72002-03-23 19:41:34 +00001184 used_v_size = total_v_size;
1185 increment = total_v_size >> 2; /* mild exponential growth */
1186 total_v_size += increment;
1187 if (total_v_size > INT_MAX) {
Guido van Rossum1187aa42001-01-05 14:43:05 +00001188 PyErr_SetString(PyExc_OverflowError,
1189 "line is longer than a Python string can hold");
Tim Peters86821b22001-01-07 21:19:34 +00001190 Py_DECREF(v);
Guido van Rossum1187aa42001-01-05 14:43:05 +00001191 return NULL;
Guido van Rossum0bd24411991-04-04 15:21:57 +00001192 }
Neil Schemenauer3a204a72002-03-23 19:41:34 +00001193 if (_PyString_Resize(&v, total_v_size) < 0)
Guido van Rossum1187aa42001-01-05 14:43:05 +00001194 return NULL;
Neil Schemenauer3a204a72002-03-23 19:41:34 +00001195 buf = BUF(v) + used_v_size;
1196 end = BUF(v) + total_v_size;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001197 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001198
Neil Schemenauer3a204a72002-03-23 19:41:34 +00001199 used_v_size = buf - BUF(v);
1200 if (used_v_size != total_v_size)
1201 _PyString_Resize(&v, used_v_size);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001202 return v;
1203}
1204
Guido van Rossum0bd24411991-04-04 15:21:57 +00001205/* External C interface */
1206
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001207PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +00001208PyFile_GetLine(PyObject *f, int n)
Guido van Rossum0bd24411991-04-04 15:21:57 +00001209{
Guido van Rossum4ddf0a02001-01-07 20:51:39 +00001210 PyObject *result;
1211
Guido van Rossum3165fe61992-09-25 21:59:05 +00001212 if (f == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001213 PyErr_BadInternalCall();
Guido van Rossum0bd24411991-04-04 15:21:57 +00001214 return NULL;
1215 }
Guido van Rossum4ddf0a02001-01-07 20:51:39 +00001216
1217 if (PyFile_Check(f)) {
1218 if (((PyFileObject*)f)->f_fp == NULL)
1219 return err_closed();
1220 result = get_line((PyFileObject *)f, n);
1221 }
1222 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001223 PyObject *reader;
1224 PyObject *args;
Guido van Rossum4ddf0a02001-01-07 20:51:39 +00001225
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001226 reader = PyObject_GetAttrString(f, "readline");
Guido van Rossum3165fe61992-09-25 21:59:05 +00001227 if (reader == NULL)
1228 return NULL;
1229 if (n <= 0)
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001230 args = PyTuple_New(0);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001231 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001232 args = Py_BuildValue("(i)", n);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001233 if (args == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001234 Py_DECREF(reader);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001235 return NULL;
1236 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001237 result = PyEval_CallObject(reader, args);
1238 Py_DECREF(reader);
1239 Py_DECREF(args);
Martin v. Löwisaf6a27a2003-01-03 19:16:14 +00001240 if (result != NULL && !PyString_Check(result) &&
1241 !PyUnicode_Check(result)) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001242 Py_DECREF(result);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001243 result = NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001244 PyErr_SetString(PyExc_TypeError,
Guido van Rossum3165fe61992-09-25 21:59:05 +00001245 "object.readline() returned non-string");
1246 }
Guido van Rossum4ddf0a02001-01-07 20:51:39 +00001247 }
1248
1249 if (n < 0 && result != NULL && PyString_Check(result)) {
1250 char *s = PyString_AS_STRING(result);
1251 int len = PyString_GET_SIZE(result);
1252 if (len == 0) {
1253 Py_DECREF(result);
1254 result = NULL;
1255 PyErr_SetString(PyExc_EOFError,
1256 "EOF when reading a line");
1257 }
1258 else if (s[len-1] == '\n') {
1259 if (result->ob_refcnt == 1)
1260 _PyString_Resize(&result, len-1);
1261 else {
1262 PyObject *v;
1263 v = PyString_FromStringAndSize(s, len-1);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001264 Py_DECREF(result);
Guido van Rossum4ddf0a02001-01-07 20:51:39 +00001265 result = v;
Guido van Rossum3165fe61992-09-25 21:59:05 +00001266 }
1267 }
Guido van Rossum3165fe61992-09-25 21:59:05 +00001268 }
Martin v. Löwisaf6a27a2003-01-03 19:16:14 +00001269#ifdef Py_USING_UNICODE
1270 if (n < 0 && result != NULL && PyUnicode_Check(result)) {
1271 Py_UNICODE *s = PyUnicode_AS_UNICODE(result);
1272 int len = PyUnicode_GET_SIZE(result);
1273 if (len == 0) {
1274 Py_DECREF(result);
1275 result = NULL;
1276 PyErr_SetString(PyExc_EOFError,
1277 "EOF when reading a line");
1278 }
1279 else if (s[len-1] == '\n') {
1280 if (result->ob_refcnt == 1)
1281 PyUnicode_Resize(&result, len-1);
1282 else {
1283 PyObject *v;
1284 v = PyUnicode_FromUnicode(s, len-1);
1285 Py_DECREF(result);
1286 result = v;
1287 }
1288 }
1289 }
1290#endif
Guido van Rossum4ddf0a02001-01-07 20:51:39 +00001291 return result;
Guido van Rossum0bd24411991-04-04 15:21:57 +00001292}
1293
1294/* Python method */
1295
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001296static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +00001297file_readline(PyFileObject *f, PyObject *args)
Guido van Rossum0bd24411991-04-04 15:21:57 +00001298{
Guido van Rossum789a1611997-05-10 22:33:55 +00001299 int n = -1;
Guido van Rossum0bd24411991-04-04 15:21:57 +00001300
Guido van Rossumd7297e61992-07-06 14:19:26 +00001301 if (f->f_fp == NULL)
1302 return err_closed();
Guido van Rossum43713e52000-02-29 13:59:29 +00001303 if (!PyArg_ParseTuple(args, "|i:readline", &n))
Guido van Rossum789a1611997-05-10 22:33:55 +00001304 return NULL;
1305 if (n == 0)
1306 return PyString_FromString("");
1307 if (n < 0)
1308 n = 0;
Marc-André Lemburg1f468602000-07-05 15:32:40 +00001309 return get_line(f, n);
Guido van Rossum0bd24411991-04-04 15:21:57 +00001310}
1311
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001312static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +00001313file_readlines(PyFileObject *f, PyObject *args)
Guido van Rossumce5ba841991-03-06 13:06:18 +00001314{
Guido van Rossum789a1611997-05-10 22:33:55 +00001315 long sizehint = 0;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001316 PyObject *list;
1317 PyObject *line;
Guido van Rossum6263d541997-05-10 22:07:25 +00001318 char small_buffer[SMALLCHUNK];
1319 char *buffer = small_buffer;
1320 size_t buffersize = SMALLCHUNK;
1321 PyObject *big_buffer = NULL;
1322 size_t nfilled = 0;
1323 size_t nread;
Guido van Rossum789a1611997-05-10 22:33:55 +00001324 size_t totalread = 0;
Guido van Rossum6263d541997-05-10 22:07:25 +00001325 char *p, *q, *end;
1326 int err;
Guido van Rossum79fd0fc2001-10-12 20:01:53 +00001327 int shortread = 0;
Guido van Rossum0bd24411991-04-04 15:21:57 +00001328
Guido van Rossumd7297e61992-07-06 14:19:26 +00001329 if (f->f_fp == NULL)
1330 return err_closed();
Guido van Rossum43713e52000-02-29 13:59:29 +00001331 if (!PyArg_ParseTuple(args, "|l:readlines", &sizehint))
Guido van Rossum0bd24411991-04-04 15:21:57 +00001332 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001333 if ((list = PyList_New(0)) == NULL)
Guido van Rossumce5ba841991-03-06 13:06:18 +00001334 return NULL;
1335 for (;;) {
Guido van Rossum79fd0fc2001-10-12 20:01:53 +00001336 if (shortread)
1337 nread = 0;
1338 else {
1339 Py_BEGIN_ALLOW_THREADS
1340 errno = 0;
Tim Peters058b1412002-04-21 07:29:14 +00001341 nread = Py_UniversalNewlineFread(buffer+nfilled,
Jack Jansen7b8c7542002-04-14 20:12:41 +00001342 buffersize-nfilled, f->f_fp, (PyObject *)f);
Guido van Rossum79fd0fc2001-10-12 20:01:53 +00001343 Py_END_ALLOW_THREADS
1344 shortread = (nread < buffersize-nfilled);
1345 }
Guido van Rossum6263d541997-05-10 22:07:25 +00001346 if (nread == 0) {
Guido van Rossum789a1611997-05-10 22:33:55 +00001347 sizehint = 0;
Guido van Rossum3da3fce1998-02-19 20:46:48 +00001348 if (!ferror(f->f_fp))
Guido van Rossum6263d541997-05-10 22:07:25 +00001349 break;
1350 PyErr_SetFromErrno(PyExc_IOError);
1351 clearerr(f->f_fp);
1352 error:
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001353 Py_DECREF(list);
Guido van Rossum6263d541997-05-10 22:07:25 +00001354 list = NULL;
1355 goto cleanup;
Guido van Rossumce5ba841991-03-06 13:06:18 +00001356 }
Guido van Rossum789a1611997-05-10 22:33:55 +00001357 totalread += nread;
Guido van Rossum6263d541997-05-10 22:07:25 +00001358 p = memchr(buffer+nfilled, '\n', nread);
1359 if (p == NULL) {
1360 /* Need a larger buffer to fit this line */
1361 nfilled += nread;
1362 buffersize *= 2;
Trent Mickf29f47b2000-08-11 19:02:59 +00001363 if (buffersize > INT_MAX) {
1364 PyErr_SetString(PyExc_OverflowError,
Guido van Rossume07d5cf2001-01-09 21:50:24 +00001365 "line is longer than a Python string can hold");
Trent Mickf29f47b2000-08-11 19:02:59 +00001366 goto error;
1367 }
Guido van Rossum6263d541997-05-10 22:07:25 +00001368 if (big_buffer == NULL) {
1369 /* Create the big buffer */
1370 big_buffer = PyString_FromStringAndSize(
1371 NULL, buffersize);
1372 if (big_buffer == NULL)
1373 goto error;
1374 buffer = PyString_AS_STRING(big_buffer);
1375 memcpy(buffer, small_buffer, nfilled);
1376 }
1377 else {
1378 /* Grow the big buffer */
Jack Jansen7b8c7542002-04-14 20:12:41 +00001379 if ( _PyString_Resize(&big_buffer, buffersize) < 0 )
1380 goto error;
Guido van Rossum6263d541997-05-10 22:07:25 +00001381 buffer = PyString_AS_STRING(big_buffer);
1382 }
1383 continue;
1384 }
1385 end = buffer+nfilled+nread;
1386 q = buffer;
1387 do {
1388 /* Process complete lines */
1389 p++;
1390 line = PyString_FromStringAndSize(q, p-q);
1391 if (line == NULL)
1392 goto error;
1393 err = PyList_Append(list, line);
1394 Py_DECREF(line);
1395 if (err != 0)
1396 goto error;
1397 q = p;
1398 p = memchr(q, '\n', end-q);
1399 } while (p != NULL);
1400 /* Move the remaining incomplete line to the start */
1401 nfilled = end-q;
1402 memmove(buffer, q, nfilled);
Guido van Rossum789a1611997-05-10 22:33:55 +00001403 if (sizehint > 0)
1404 if (totalread >= (size_t)sizehint)
1405 break;
Guido van Rossumce5ba841991-03-06 13:06:18 +00001406 }
Guido van Rossum6263d541997-05-10 22:07:25 +00001407 if (nfilled != 0) {
1408 /* Partial last line */
1409 line = PyString_FromStringAndSize(buffer, nfilled);
1410 if (line == NULL)
1411 goto error;
Guido van Rossum789a1611997-05-10 22:33:55 +00001412 if (sizehint > 0) {
1413 /* Need to complete the last line */
Marc-André Lemburg1f468602000-07-05 15:32:40 +00001414 PyObject *rest = get_line(f, 0);
Guido van Rossum789a1611997-05-10 22:33:55 +00001415 if (rest == NULL) {
1416 Py_DECREF(line);
1417 goto error;
1418 }
1419 PyString_Concat(&line, rest);
1420 Py_DECREF(rest);
1421 if (line == NULL)
1422 goto error;
1423 }
Guido van Rossum6263d541997-05-10 22:07:25 +00001424 err = PyList_Append(list, line);
1425 Py_DECREF(line);
1426 if (err != 0)
1427 goto error;
1428 }
1429 cleanup:
Tim Peters5de98422002-04-27 18:44:32 +00001430 Py_XDECREF(big_buffer);
Guido van Rossumce5ba841991-03-06 13:06:18 +00001431 return list;
1432}
1433
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001434static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +00001435file_write(PyFileObject *f, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001436{
Guido van Rossumd7297e61992-07-06 14:19:26 +00001437 char *s;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001438 int n, n2;
Guido van Rossumd7297e61992-07-06 14:19:26 +00001439 if (f->f_fp == NULL)
1440 return err_closed();
Michael W. Hudsone2ec3eb2001-10-31 18:51:01 +00001441 if (!PyArg_ParseTuple(args, f->f_binary ? "s#" : "t#", &s, &n))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001442 return NULL;
Guido van Rossumeb183da1991-04-04 10:44:06 +00001443 f->f_softspace = 0;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001444 Py_BEGIN_ALLOW_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001445 errno = 0;
Guido van Rossumd7297e61992-07-06 14:19:26 +00001446 n2 = fwrite(s, 1, n, f->f_fp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001447 Py_END_ALLOW_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001448 if (n2 != n) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001449 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossumfebd5511992-03-04 16:39:24 +00001450 clearerr(f->f_fp);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001451 return NULL;
1452 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001453 Py_INCREF(Py_None);
1454 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001455}
1456
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001457static PyObject *
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001458file_writelines(PyFileObject *f, PyObject *seq)
Guido van Rossum5a2a6831993-10-25 09:59:04 +00001459{
Guido van Rossumee70ad12000-03-13 16:27:06 +00001460#define CHUNKSIZE 1000
1461 PyObject *list, *line;
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001462 PyObject *it; /* iter(seq) */
Guido van Rossumee70ad12000-03-13 16:27:06 +00001463 PyObject *result;
1464 int i, j, index, len, nwritten, islist;
1465
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001466 assert(seq != NULL);
Guido van Rossum5a2a6831993-10-25 09:59:04 +00001467 if (f->f_fp == NULL)
1468 return err_closed();
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001469
1470 result = NULL;
1471 list = NULL;
1472 islist = PyList_Check(seq);
1473 if (islist)
1474 it = NULL;
1475 else {
1476 it = PyObject_GetIter(seq);
1477 if (it == NULL) {
1478 PyErr_SetString(PyExc_TypeError,
1479 "writelines() requires an iterable argument");
1480 return NULL;
1481 }
1482 /* From here on, fail by going to error, to reclaim "it". */
1483 list = PyList_New(CHUNKSIZE);
1484 if (list == NULL)
1485 goto error;
Guido van Rossum5a2a6831993-10-25 09:59:04 +00001486 }
Guido van Rossumee70ad12000-03-13 16:27:06 +00001487
1488 /* Strategy: slurp CHUNKSIZE lines into a private list,
1489 checking that they are all strings, then write that list
1490 without holding the interpreter lock, then come back for more. */
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001491 for (index = 0; ; index += CHUNKSIZE) {
Guido van Rossumee70ad12000-03-13 16:27:06 +00001492 if (islist) {
1493 Py_XDECREF(list);
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001494 list = PyList_GetSlice(seq, index, index+CHUNKSIZE);
Guido van Rossumee70ad12000-03-13 16:27:06 +00001495 if (list == NULL)
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001496 goto error;
Guido van Rossumee70ad12000-03-13 16:27:06 +00001497 j = PyList_GET_SIZE(list);
1498 }
1499 else {
1500 for (j = 0; j < CHUNKSIZE; j++) {
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001501 line = PyIter_Next(it);
Guido van Rossumee70ad12000-03-13 16:27:06 +00001502 if (line == NULL) {
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001503 if (PyErr_Occurred())
1504 goto error;
1505 break;
Guido van Rossumee70ad12000-03-13 16:27:06 +00001506 }
Guido van Rossumee70ad12000-03-13 16:27:06 +00001507 PyList_SetItem(list, j, line);
1508 }
1509 }
1510 if (j == 0)
1511 break;
1512
Marc-André Lemburg6ef68b52000-08-25 22:39:50 +00001513 /* Check that all entries are indeed strings. If not,
1514 apply the same rules as for file.write() and
1515 convert the results to strings. This is slow, but
1516 seems to be the only way since all conversion APIs
1517 could potentially execute Python code. */
1518 for (i = 0; i < j; i++) {
1519 PyObject *v = PyList_GET_ITEM(list, i);
1520 if (!PyString_Check(v)) {
1521 const char *buffer;
1522 int len;
Tim Peters86821b22001-01-07 21:19:34 +00001523 if (((f->f_binary &&
Marc-André Lemburg6ef68b52000-08-25 22:39:50 +00001524 PyObject_AsReadBuffer(v,
1525 (const void**)&buffer,
1526 &len)) ||
1527 PyObject_AsCharBuffer(v,
1528 &buffer,
1529 &len))) {
1530 PyErr_SetString(PyExc_TypeError,
Jeremy Hylton8b735422002-08-14 21:01:41 +00001531 "writelines() argument must be a sequence of strings");
Marc-André Lemburg6ef68b52000-08-25 22:39:50 +00001532 goto error;
1533 }
1534 line = PyString_FromStringAndSize(buffer,
1535 len);
1536 if (line == NULL)
1537 goto error;
1538 Py_DECREF(v);
Marc-André Lemburgf5e96fa2000-08-25 22:49:05 +00001539 PyList_SET_ITEM(list, i, line);
Marc-André Lemburg6ef68b52000-08-25 22:39:50 +00001540 }
1541 }
1542
1543 /* Since we are releasing the global lock, the
1544 following code may *not* execute Python code. */
Guido van Rossumee70ad12000-03-13 16:27:06 +00001545 Py_BEGIN_ALLOW_THREADS
1546 f->f_softspace = 0;
1547 errno = 0;
1548 for (i = 0; i < j; i++) {
Marc-André Lemburg6ef68b52000-08-25 22:39:50 +00001549 line = PyList_GET_ITEM(list, i);
Guido van Rossumee70ad12000-03-13 16:27:06 +00001550 len = PyString_GET_SIZE(line);
1551 nwritten = fwrite(PyString_AS_STRING(line),
1552 1, len, f->f_fp);
1553 if (nwritten != len) {
1554 Py_BLOCK_THREADS
1555 PyErr_SetFromErrno(PyExc_IOError);
1556 clearerr(f->f_fp);
1557 goto error;
1558 }
1559 }
1560 Py_END_ALLOW_THREADS
1561
1562 if (j < CHUNKSIZE)
1563 break;
Guido van Rossumee70ad12000-03-13 16:27:06 +00001564 }
1565
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001566 Py_INCREF(Py_None);
Guido van Rossumee70ad12000-03-13 16:27:06 +00001567 result = Py_None;
1568 error:
1569 Py_XDECREF(list);
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001570 Py_XDECREF(it);
Guido van Rossumee70ad12000-03-13 16:27:06 +00001571 return result;
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001572#undef CHUNKSIZE
Guido van Rossum5a2a6831993-10-25 09:59:04 +00001573}
1574
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001575static PyObject *
1576file_getiter(PyFileObject *f)
1577{
1578 if (f->f_fp == NULL)
1579 return err_closed();
1580 Py_INCREF(f);
1581 return (PyObject *)f;
1582}
1583
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001584PyDoc_STRVAR(readline_doc,
Tim Petersefc3a3a2001-09-20 07:55:22 +00001585"readline([size]) -> next line from the file, as a string.\n"
1586"\n"
1587"Retain newline. A non-negative size argument limits the maximum\n"
1588"number of bytes to return (an incomplete line may be returned then).\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001589"Return an empty string at EOF.");
Tim Petersefc3a3a2001-09-20 07:55:22 +00001590
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001591PyDoc_STRVAR(read_doc,
Tim Petersefc3a3a2001-09-20 07:55:22 +00001592"read([size]) -> read at most size bytes, returned as a string.\n"
1593"\n"
Gustavo Niemeyer786ddb22002-12-16 18:12:53 +00001594"If the size argument is negative or omitted, read until EOF is reached.\n"
1595"Notice that when in non-blocking mode, less data than what was requested\n"
1596"may be returned, even if no size parameter was given.");
Tim Petersefc3a3a2001-09-20 07:55:22 +00001597
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001598PyDoc_STRVAR(write_doc,
Tim Petersefc3a3a2001-09-20 07:55:22 +00001599"write(str) -> None. Write string str to file.\n"
1600"\n"
1601"Note that due to buffering, flush() or close() may be needed before\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001602"the file on disk reflects the data written.");
Tim Petersefc3a3a2001-09-20 07:55:22 +00001603
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001604PyDoc_STRVAR(fileno_doc,
Tim Petersefc3a3a2001-09-20 07:55:22 +00001605"fileno() -> integer \"file descriptor\".\n"
1606"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001607"This is needed for lower-level file interfaces, such os.read().");
Tim Petersefc3a3a2001-09-20 07:55:22 +00001608
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001609PyDoc_STRVAR(seek_doc,
Tim Petersefc3a3a2001-09-20 07:55:22 +00001610"seek(offset[, whence]) -> None. Move to new file position.\n"
1611"\n"
1612"Argument offset is a byte count. Optional argument whence defaults to\n"
1613"0 (offset from start of file, offset should be >= 0); other values are 1\n"
1614"(move relative to current position, positive or negative), and 2 (move\n"
1615"relative to end of file, usually negative, although many platforms allow\n"
1616"seeking beyond the end of a file).\n"
1617"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001618"Note that not all file objects are seekable.");
Tim Petersefc3a3a2001-09-20 07:55:22 +00001619
Guido van Rossumd7047b31995-01-02 19:07:15 +00001620#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001621PyDoc_STRVAR(truncate_doc,
Tim Petersefc3a3a2001-09-20 07:55:22 +00001622"truncate([size]) -> None. Truncate the file to at most size bytes.\n"
1623"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001624"Size defaults to the current file position, as returned by tell().");
Guido van Rossumd7047b31995-01-02 19:07:15 +00001625#endif
Tim Petersefc3a3a2001-09-20 07:55:22 +00001626
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001627PyDoc_STRVAR(tell_doc,
1628"tell() -> current file position, an integer (may be a long integer).");
Tim Petersefc3a3a2001-09-20 07:55:22 +00001629
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001630PyDoc_STRVAR(readinto_doc,
1631"readinto() -> Undocumented. Don't use this; it may go away.");
Tim Petersefc3a3a2001-09-20 07:55:22 +00001632
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001633PyDoc_STRVAR(readlines_doc,
Tim Petersefc3a3a2001-09-20 07:55:22 +00001634"readlines([size]) -> list of strings, each a line from the file.\n"
1635"\n"
1636"Call readline() repeatedly and return a list of the lines so read.\n"
1637"The optional size argument, if given, is an approximate bound on the\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001638"total number of bytes in the lines returned.");
Tim Petersefc3a3a2001-09-20 07:55:22 +00001639
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001640PyDoc_STRVAR(xreadlines_doc,
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001641"xreadlines() -> returns self.\n"
Tim Petersefc3a3a2001-09-20 07:55:22 +00001642"\n"
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001643"For backward compatibility. File objects now include the performance\n"
1644"optimizations previously implemented in the xreadlines module.");
Tim Petersefc3a3a2001-09-20 07:55:22 +00001645
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001646PyDoc_STRVAR(writelines_doc,
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001647"writelines(sequence_of_strings) -> None. Write the strings to the file.\n"
Tim Petersefc3a3a2001-09-20 07:55:22 +00001648"\n"
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001649"Note that newlines are not added. The sequence can be any iterable object\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001650"producing strings. This is equivalent to calling write() for each string.");
Tim Petersefc3a3a2001-09-20 07:55:22 +00001651
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001652PyDoc_STRVAR(flush_doc,
1653"flush() -> None. Flush the internal I/O buffer.");
Tim Petersefc3a3a2001-09-20 07:55:22 +00001654
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001655PyDoc_STRVAR(close_doc,
Tim Petersefc3a3a2001-09-20 07:55:22 +00001656"close() -> None or (perhaps) an integer. Close the file.\n"
1657"\n"
Guido van Rossum77f6a652002-04-03 22:41:51 +00001658"Sets data attribute .closed to True. A closed file cannot be used for\n"
Tim Petersefc3a3a2001-09-20 07:55:22 +00001659"further I/O operations. close() may be called more than once without\n"
1660"error. Some kinds of file objects (for example, opened by popen())\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001661"may return an exit status upon closing.");
Tim Petersefc3a3a2001-09-20 07:55:22 +00001662
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001663PyDoc_STRVAR(isatty_doc,
1664"isatty() -> true or false. True if the file is connected to a tty device.");
Tim Petersefc3a3a2001-09-20 07:55:22 +00001665
1666static PyMethodDef file_methods[] = {
Jeremy Hylton8b735422002-08-14 21:01:41 +00001667 {"readline", (PyCFunction)file_readline, METH_VARARGS, readline_doc},
1668 {"read", (PyCFunction)file_read, METH_VARARGS, read_doc},
1669 {"write", (PyCFunction)file_write, METH_VARARGS, write_doc},
1670 {"fileno", (PyCFunction)file_fileno, METH_NOARGS, fileno_doc},
1671 {"seek", (PyCFunction)file_seek, METH_VARARGS, seek_doc},
Tim Petersefc3a3a2001-09-20 07:55:22 +00001672#ifdef HAVE_FTRUNCATE
Jeremy Hylton8b735422002-08-14 21:01:41 +00001673 {"truncate", (PyCFunction)file_truncate, METH_VARARGS, truncate_doc},
Tim Petersefc3a3a2001-09-20 07:55:22 +00001674#endif
Jeremy Hylton8b735422002-08-14 21:01:41 +00001675 {"tell", (PyCFunction)file_tell, METH_NOARGS, tell_doc},
1676 {"readinto", (PyCFunction)file_readinto, METH_VARARGS, readinto_doc},
1677 {"readlines", (PyCFunction)file_readlines,METH_VARARGS, readlines_doc},
1678 {"xreadlines",(PyCFunction)file_getiter, METH_NOARGS, xreadlines_doc},
1679 {"writelines",(PyCFunction)file_writelines, METH_O, writelines_doc},
1680 {"flush", (PyCFunction)file_flush, METH_NOARGS, flush_doc},
1681 {"close", (PyCFunction)file_close, METH_NOARGS, close_doc},
1682 {"isatty", (PyCFunction)file_isatty, METH_NOARGS, isatty_doc},
1683 {NULL, NULL} /* sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001684};
1685
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001686#define OFF(x) offsetof(PyFileObject, x)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001687
Guido van Rossum6f799372001-09-20 20:46:19 +00001688static PyMemberDef file_memberlist[] = {
1689 {"softspace", T_INT, OFF(f_softspace), 0,
1690 "flag indicating that a space needs to be printed; used by print"},
1691 {"mode", T_OBJECT, OFF(f_mode), RO,
Martin v. Löwis6233c9b2002-12-11 13:06:53 +00001692 "file mode ('r', 'U', 'w', 'a', possibly with 'b' or '+' added)"},
Guido van Rossum6f799372001-09-20 20:46:19 +00001693 {"name", T_OBJECT, OFF(f_name), RO,
1694 "file name"},
Martin v. Löwis5467d4c2003-05-10 07:10:12 +00001695 {"encoding", T_OBJECT, OFF(f_encoding), RO,
1696 "file encoding"},
Guido van Rossumb6775db1994-08-01 11:34:53 +00001697 /* getattr(f, "closed") is implemented without this table */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001698 {NULL} /* Sentinel */
1699};
1700
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001701static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00001702get_closed(PyFileObject *f, void *closure)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001703{
Guido van Rossum77f6a652002-04-03 22:41:51 +00001704 return PyBool_FromLong((long)(f->f_fp == 0));
Guido van Rossumb6775db1994-08-01 11:34:53 +00001705}
Jack Jansen7b8c7542002-04-14 20:12:41 +00001706#ifdef WITH_UNIVERSAL_NEWLINES
1707static PyObject *
1708get_newlines(PyFileObject *f, void *closure)
1709{
1710 switch (f->f_newlinetypes) {
1711 case NEWLINE_UNKNOWN:
1712 Py_INCREF(Py_None);
1713 return Py_None;
1714 case NEWLINE_CR:
1715 return PyString_FromString("\r");
1716 case NEWLINE_LF:
1717 return PyString_FromString("\n");
1718 case NEWLINE_CR|NEWLINE_LF:
1719 return Py_BuildValue("(ss)", "\r", "\n");
1720 case NEWLINE_CRLF:
1721 return PyString_FromString("\r\n");
1722 case NEWLINE_CR|NEWLINE_CRLF:
1723 return Py_BuildValue("(ss)", "\r", "\r\n");
1724 case NEWLINE_LF|NEWLINE_CRLF:
1725 return Py_BuildValue("(ss)", "\n", "\r\n");
1726 case NEWLINE_CR|NEWLINE_LF|NEWLINE_CRLF:
1727 return Py_BuildValue("(sss)", "\r", "\n", "\r\n");
1728 default:
Tim Petersf1827cf2003-09-07 03:30:18 +00001729 PyErr_Format(PyExc_SystemError,
1730 "Unknown newlines value 0x%x\n",
Jeremy Hylton8b735422002-08-14 21:01:41 +00001731 f->f_newlinetypes);
Jack Jansen7b8c7542002-04-14 20:12:41 +00001732 return NULL;
1733 }
1734}
1735#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00001736
Guido van Rossum32d34c82001-09-20 21:45:26 +00001737static PyGetSetDef file_getsetlist[] = {
Guido van Rossum77f6a652002-04-03 22:41:51 +00001738 {"closed", (getter)get_closed, NULL, "True if the file is closed"},
Jack Jansen7b8c7542002-04-14 20:12:41 +00001739#ifdef WITH_UNIVERSAL_NEWLINES
Tim Petersf1827cf2003-09-07 03:30:18 +00001740 {"newlines", (getter)get_newlines, NULL,
Jeremy Hylton8b735422002-08-14 21:01:41 +00001741 "end-of-line convention used in this file"},
Jack Jansen7b8c7542002-04-14 20:12:41 +00001742#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00001743 {0},
1744};
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001745
Neal Norwitzd8b995f2002-08-06 21:50:54 +00001746static void
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001747drop_readahead(PyFileObject *f)
Guido van Rossum65967252001-04-21 13:20:18 +00001748{
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001749 if (f->f_buf != NULL) {
1750 PyMem_Free(f->f_buf);
1751 f->f_buf = NULL;
1752 }
Guido van Rossum65967252001-04-21 13:20:18 +00001753}
1754
Tim Petersf1827cf2003-09-07 03:30:18 +00001755/* Make sure that file has a readahead buffer with at least one byte
1756 (unless at EOF) and no more than bufsize. Returns negative value on
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001757 error */
Neal Norwitzd8b995f2002-08-06 21:50:54 +00001758static int
1759readahead(PyFileObject *f, int bufsize)
1760{
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001761 int chunksize;
1762
1763 if (f->f_buf != NULL) {
Tim Petersf1827cf2003-09-07 03:30:18 +00001764 if( (f->f_bufend - f->f_bufptr) >= 1)
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001765 return 0;
1766 else
1767 drop_readahead(f);
1768 }
1769 if ((f->f_buf = PyMem_Malloc(bufsize)) == NULL) {
1770 return -1;
1771 }
1772 Py_BEGIN_ALLOW_THREADS
1773 errno = 0;
1774 chunksize = Py_UniversalNewlineFread(
1775 f->f_buf, bufsize, f->f_fp, (PyObject *)f);
1776 Py_END_ALLOW_THREADS
1777 if (chunksize == 0) {
1778 if (ferror(f->f_fp)) {
1779 PyErr_SetFromErrno(PyExc_IOError);
1780 clearerr(f->f_fp);
1781 drop_readahead(f);
1782 return -1;
1783 }
1784 }
1785 f->f_bufptr = f->f_buf;
1786 f->f_bufend = f->f_buf + chunksize;
1787 return 0;
1788}
1789
1790/* Used by file_iternext. The returned string will start with 'skip'
Tim Petersf1827cf2003-09-07 03:30:18 +00001791 uninitialized bytes followed by the remainder of the line. Don't be
1792 horrified by the recursive call: maximum recursion depth is limited by
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001793 logarithmic buffer growth to about 50 even when reading a 1gb line. */
1794
Neal Norwitzd8b995f2002-08-06 21:50:54 +00001795static PyStringObject *
1796readahead_get_line_skip(PyFileObject *f, int skip, int bufsize)
1797{
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001798 PyStringObject* s;
1799 char *bufptr;
1800 char *buf;
1801 int len;
1802
1803 if (f->f_buf == NULL)
Tim Petersf1827cf2003-09-07 03:30:18 +00001804 if (readahead(f, bufsize) < 0)
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001805 return NULL;
1806
1807 len = f->f_bufend - f->f_bufptr;
Tim Petersf1827cf2003-09-07 03:30:18 +00001808 if (len == 0)
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001809 return (PyStringObject *)
1810 PyString_FromStringAndSize(NULL, skip);
1811 bufptr = memchr(f->f_bufptr, '\n', len);
1812 if (bufptr != NULL) {
1813 bufptr++; /* Count the '\n' */
1814 len = bufptr - f->f_bufptr;
1815 s = (PyStringObject *)
1816 PyString_FromStringAndSize(NULL, skip+len);
Tim Petersf1827cf2003-09-07 03:30:18 +00001817 if (s == NULL)
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001818 return NULL;
1819 memcpy(PyString_AS_STRING(s)+skip, f->f_bufptr, len);
1820 f->f_bufptr = bufptr;
1821 if (bufptr == f->f_bufend)
1822 drop_readahead(f);
1823 } else {
1824 bufptr = f->f_bufptr;
1825 buf = f->f_buf;
1826 f->f_buf = NULL; /* Force new readahead buffer */
1827 s = readahead_get_line_skip(
1828 f, skip+len, bufsize + (bufsize>>2) );
1829 if (s == NULL) {
1830 PyMem_Free(buf);
1831 return NULL;
1832 }
1833 memcpy(PyString_AS_STRING(s)+skip, bufptr, len);
1834 PyMem_Free(buf);
1835 }
1836 return s;
1837}
1838
1839/* A larger buffer size may actually decrease performance. */
1840#define READAHEAD_BUFSIZE 8192
1841
1842static PyObject *
1843file_iternext(PyFileObject *f)
1844{
1845 PyStringObject* l;
1846
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001847 if (f->f_fp == NULL)
1848 return err_closed();
1849
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001850 l = readahead_get_line_skip(f, 0, READAHEAD_BUFSIZE);
1851 if (l == NULL || PyString_GET_SIZE(l) == 0) {
1852 Py_XDECREF(l);
1853 return NULL;
1854 }
1855 return (PyObject *)l;
1856}
1857
1858
Tim Peters59c9a642001-09-13 05:38:56 +00001859static PyObject *
1860file_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1861{
Tim Peters44410012001-09-14 03:26:08 +00001862 PyObject *self;
1863 static PyObject *not_yet_string;
1864
1865 assert(type != NULL && type->tp_alloc != NULL);
1866
1867 if (not_yet_string == NULL) {
1868 not_yet_string = PyString_FromString("<uninitialized file>");
1869 if (not_yet_string == NULL)
1870 return NULL;
1871 }
1872
1873 self = type->tp_alloc(type, 0);
1874 if (self != NULL) {
1875 /* Always fill in the name and mode, so that nobody else
1876 needs to special-case NULLs there. */
1877 Py_INCREF(not_yet_string);
1878 ((PyFileObject *)self)->f_name = not_yet_string;
1879 Py_INCREF(not_yet_string);
1880 ((PyFileObject *)self)->f_mode = not_yet_string;
Martin v. Löwis5467d4c2003-05-10 07:10:12 +00001881 Py_INCREF(Py_None);
1882 ((PyFileObject *)self)->f_encoding = Py_None;
Tim Peters44410012001-09-14 03:26:08 +00001883 }
1884 return self;
1885}
1886
1887static int
1888file_init(PyObject *self, PyObject *args, PyObject *kwds)
1889{
1890 PyFileObject *foself = (PyFileObject *)self;
1891 int ret = 0;
Tim Peters59c9a642001-09-13 05:38:56 +00001892 static char *kwlist[] = {"name", "mode", "buffering", 0};
1893 char *name = NULL;
1894 char *mode = "r";
1895 int bufsize = -1;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001896 int wideargument = 0;
Tim Peters44410012001-09-14 03:26:08 +00001897
1898 assert(PyFile_Check(self));
1899 if (foself->f_fp != NULL) {
1900 /* Have to close the existing file first. */
1901 PyObject *closeresult = file_close(foself);
1902 if (closeresult == NULL)
1903 return -1;
1904 Py_DECREF(closeresult);
1905 }
Tim Peters59c9a642001-09-13 05:38:56 +00001906
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001907#ifdef Py_WIN_WIDE_FILENAMES
1908 if (GetVersion() < 0x80000000) { /* On NT, so wide API available */
1909 PyObject *po;
1910 if (PyArg_ParseTupleAndKeywords(args, kwds, "U|si:file",
1911 kwlist, &po, &mode, &bufsize)) {
1912 wideargument = 1;
1913 if (fill_file_fields(foself, NULL, name, mode,
1914 fclose, po) == NULL)
1915 goto Error;
1916 } else {
1917 /* Drop the argument parsing error as narrow
1918 strings are also valid. */
1919 PyErr_Clear();
1920 }
1921 }
1922#endif
1923
1924 if (!wideargument) {
1925 if (!PyArg_ParseTupleAndKeywords(args, kwds, "et|si:file", kwlist,
1926 Py_FileSystemDefaultEncoding,
1927 &name,
1928 &mode, &bufsize))
1929 return -1;
1930 if (fill_file_fields(foself, NULL, name, mode,
1931 fclose, NULL) == NULL)
1932 goto Error;
1933 }
Tim Peters44410012001-09-14 03:26:08 +00001934 if (open_the_file(foself, name, mode) == NULL)
1935 goto Error;
Martin v. Löwis1e3bdf62003-09-04 19:01:46 +00001936 foself->f_setbuf = NULL;
Tim Peters44410012001-09-14 03:26:08 +00001937 PyFile_SetBufSize(self, bufsize);
1938 goto Done;
1939
1940Error:
1941 ret = -1;
1942 /* fall through */
1943Done:
Tim Peters59c9a642001-09-13 05:38:56 +00001944 PyMem_Free(name); /* free the encoded string */
Tim Peters44410012001-09-14 03:26:08 +00001945 return ret;
Tim Peters59c9a642001-09-13 05:38:56 +00001946}
1947
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001948PyDoc_VAR(file_doc) =
1949PyDoc_STR(
Tim Peters59c9a642001-09-13 05:38:56 +00001950"file(name[, mode[, buffering]]) -> file object\n"
1951"\n"
1952"Open a file. The mode can be 'r', 'w' or 'a' for reading (default),\n"
1953"writing or appending. The file will be created if it doesn't exist\n"
1954"when opened for writing or appending; it will be truncated when\n"
1955"opened for writing. Add a 'b' to the mode for binary files.\n"
1956"Add a '+' to the mode to allow simultaneous reading and writing.\n"
1957"If the buffering argument is given, 0 means unbuffered, 1 means line\n"
Tim Peters742dfd62001-09-13 21:49:44 +00001958"buffered, and larger numbers specify the buffer size.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001959)
Barry Warsaw4be55b52002-05-22 20:37:53 +00001960#ifdef WITH_UNIVERSAL_NEWLINES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001961PyDoc_STR(
Barry Warsaw4be55b52002-05-22 20:37:53 +00001962"Add a 'U' to mode to open the file for input with universal newline\n"
1963"support. Any line ending in the input file will be seen as a '\\n'\n"
1964"in Python. Also, a file so opened gains the attribute 'newlines';\n"
1965"the value for this attribute is one of None (no newline read yet),\n"
1966"'\\r', '\\n', '\\r\\n' or a tuple containing all the newline types seen.\n"
1967"\n"
1968"'U' cannot be combined with 'w' or '+' mode.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001969)
Barry Warsaw4be55b52002-05-22 20:37:53 +00001970#endif /* WITH_UNIVERSAL_NEWLINES */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001971PyDoc_STR(
Barry Warsaw4be55b52002-05-22 20:37:53 +00001972"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001973"Note: open() is an alias for file()."
1974);
Tim Peters59c9a642001-09-13 05:38:56 +00001975
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001976PyTypeObject PyFile_Type = {
1977 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001978 0,
1979 "file",
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001980 sizeof(PyFileObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001981 0,
Guido van Rossum65967252001-04-21 13:20:18 +00001982 (destructor)file_dealloc, /* tp_dealloc */
1983 0, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001984 0, /* tp_getattr */
1985 0, /* tp_setattr */
Guido van Rossum65967252001-04-21 13:20:18 +00001986 0, /* tp_compare */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001987 (reprfunc)file_repr, /* tp_repr */
Guido van Rossum65967252001-04-21 13:20:18 +00001988 0, /* tp_as_number */
1989 0, /* tp_as_sequence */
1990 0, /* tp_as_mapping */
1991 0, /* tp_hash */
1992 0, /* tp_call */
1993 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001994 PyObject_GenericGetAttr, /* tp_getattro */
Tim Peters015dd822003-05-04 04:16:52 +00001995 /* softspace is writable: we must supply tp_setattro */
1996 PyObject_GenericSetAttr, /* tp_setattro */
Guido van Rossum65967252001-04-21 13:20:18 +00001997 0, /* tp_as_buffer */
Guido van Rossum9475a232001-10-05 20:51:39 +00001998 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
Tim Peters59c9a642001-09-13 05:38:56 +00001999 file_doc, /* tp_doc */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002000 0, /* tp_traverse */
2001 0, /* tp_clear */
Guido van Rossum65967252001-04-21 13:20:18 +00002002 0, /* tp_richcompare */
2003 0, /* tp_weaklistoffset */
Guido van Rossum7a6e9592002-08-06 15:55:28 +00002004 (getiterfunc)file_getiter, /* tp_iter */
2005 (iternextfunc)file_iternext, /* tp_iternext */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002006 file_methods, /* tp_methods */
2007 file_memberlist, /* tp_members */
2008 file_getsetlist, /* tp_getset */
2009 0, /* tp_base */
2010 0, /* tp_dict */
Tim Peters59c9a642001-09-13 05:38:56 +00002011 0, /* tp_descr_get */
2012 0, /* tp_descr_set */
2013 0, /* tp_dictoffset */
Tim Peters44410012001-09-14 03:26:08 +00002014 (initproc)file_init, /* tp_init */
2015 PyType_GenericAlloc, /* tp_alloc */
Tim Peters59c9a642001-09-13 05:38:56 +00002016 file_new, /* tp_new */
Neil Schemenaueraa769ae2002-04-12 02:44:10 +00002017 PyObject_Del, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002018};
Guido van Rossumeb183da1991-04-04 10:44:06 +00002019
2020/* Interface for the 'soft space' between print items. */
2021
2022int
Fred Drakefd99de62000-07-09 05:02:18 +00002023PyFile_SoftSpace(PyObject *f, int newflag)
Guido van Rossumeb183da1991-04-04 10:44:06 +00002024{
2025 int oldflag = 0;
Guido van Rossum3165fe61992-09-25 21:59:05 +00002026 if (f == NULL) {
2027 /* Do nothing */
2028 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002029 else if (PyFile_Check(f)) {
2030 oldflag = ((PyFileObject *)f)->f_softspace;
2031 ((PyFileObject *)f)->f_softspace = newflag;
Guido van Rossumeb183da1991-04-04 10:44:06 +00002032 }
Guido van Rossum3165fe61992-09-25 21:59:05 +00002033 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002034 PyObject *v;
2035 v = PyObject_GetAttrString(f, "softspace");
Guido van Rossum3165fe61992-09-25 21:59:05 +00002036 if (v == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002037 PyErr_Clear();
Guido van Rossum3165fe61992-09-25 21:59:05 +00002038 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002039 if (PyInt_Check(v))
2040 oldflag = PyInt_AsLong(v);
2041 Py_DECREF(v);
Guido van Rossum3165fe61992-09-25 21:59:05 +00002042 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002043 v = PyInt_FromLong((long)newflag);
Guido van Rossum3165fe61992-09-25 21:59:05 +00002044 if (v == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002045 PyErr_Clear();
Guido van Rossum3165fe61992-09-25 21:59:05 +00002046 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002047 if (PyObject_SetAttrString(f, "softspace", v) != 0)
2048 PyErr_Clear();
2049 Py_DECREF(v);
Guido van Rossum3165fe61992-09-25 21:59:05 +00002050 }
2051 }
Guido van Rossumeb183da1991-04-04 10:44:06 +00002052 return oldflag;
2053}
Guido van Rossum3165fe61992-09-25 21:59:05 +00002054
2055/* Interfaces to write objects/strings to file-like objects */
2056
2057int
Fred Drakefd99de62000-07-09 05:02:18 +00002058PyFile_WriteObject(PyObject *v, PyObject *f, int flags)
Guido van Rossum3165fe61992-09-25 21:59:05 +00002059{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002060 PyObject *writer, *value, *args, *result;
Guido van Rossum3165fe61992-09-25 21:59:05 +00002061 if (f == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002062 PyErr_SetString(PyExc_TypeError, "writeobject with NULL file");
Guido van Rossum3165fe61992-09-25 21:59:05 +00002063 return -1;
2064 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002065 else if (PyFile_Check(f)) {
2066 FILE *fp = PyFile_AsFile(f);
Martin v. Löwis5467d4c2003-05-10 07:10:12 +00002067 PyObject *enc = ((PyFileObject*)f)->f_encoding;
2068 int result;
Guido van Rossum3165fe61992-09-25 21:59:05 +00002069 if (fp == NULL) {
2070 err_closed();
2071 return -1;
2072 }
Martin v. Löwis5467d4c2003-05-10 07:10:12 +00002073#ifdef Py_USING_UNICODE
Tim Petersf1827cf2003-09-07 03:30:18 +00002074 if ((flags & Py_PRINT_RAW) &&
Martin v. Löwis415da6e2003-05-18 12:56:25 +00002075 PyUnicode_Check(v) && enc != Py_None) {
Martin v. Löwis5467d4c2003-05-10 07:10:12 +00002076 char *cenc = PyString_AS_STRING(enc);
2077 value = PyUnicode_AsEncodedString(v, cenc, "strict");
2078 if (value == NULL)
2079 return -1;
2080 } else {
2081 value = v;
2082 Py_INCREF(value);
2083 }
2084 result = PyObject_Print(value, fp, flags);
2085 Py_DECREF(value);
2086 return result;
2087#else
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002088 return PyObject_Print(v, fp, flags);
Martin v. Löwis5467d4c2003-05-10 07:10:12 +00002089#endif
Guido van Rossum3165fe61992-09-25 21:59:05 +00002090 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002091 writer = PyObject_GetAttrString(f, "write");
Guido van Rossum3165fe61992-09-25 21:59:05 +00002092 if (writer == NULL)
2093 return -1;
Martin v. Löwis2777c022001-09-19 13:47:32 +00002094 if (flags & Py_PRINT_RAW) {
2095 if (PyUnicode_Check(v)) {
2096 value = v;
2097 Py_INCREF(value);
2098 } else
2099 value = PyObject_Str(v);
2100 }
2101 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002102 value = PyObject_Repr(v);
Guido van Rossumc6004111993-11-05 10:22:19 +00002103 if (value == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002104 Py_DECREF(writer);
Guido van Rossumc6004111993-11-05 10:22:19 +00002105 return -1;
Guido van Rossum3165fe61992-09-25 21:59:05 +00002106 }
Raymond Hettinger8ae46892003-10-12 19:09:37 +00002107 args = PyTuple_Pack(1, value);
Guido van Rossume9eec541997-05-22 14:02:25 +00002108 if (args == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002109 Py_DECREF(value);
2110 Py_DECREF(writer);
Guido van Rossumd3f9a1a1995-07-10 23:32:26 +00002111 return -1;
2112 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002113 result = PyEval_CallObject(writer, args);
2114 Py_DECREF(args);
2115 Py_DECREF(value);
2116 Py_DECREF(writer);
Guido van Rossum3165fe61992-09-25 21:59:05 +00002117 if (result == NULL)
2118 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002119 Py_DECREF(result);
Guido van Rossum3165fe61992-09-25 21:59:05 +00002120 return 0;
2121}
2122
Guido van Rossum27a60b11997-05-22 22:25:11 +00002123int
Tim Petersc1bbcb82001-11-28 22:13:25 +00002124PyFile_WriteString(const char *s, PyObject *f)
Guido van Rossum3165fe61992-09-25 21:59:05 +00002125{
2126 if (f == NULL) {
Guido van Rossum27a60b11997-05-22 22:25:11 +00002127 /* Should be caused by a pre-existing error */
Fred Drakefd99de62000-07-09 05:02:18 +00002128 if (!PyErr_Occurred())
Guido van Rossum27a60b11997-05-22 22:25:11 +00002129 PyErr_SetString(PyExc_SystemError,
2130 "null file for PyFile_WriteString");
2131 return -1;
Guido van Rossum3165fe61992-09-25 21:59:05 +00002132 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002133 else if (PyFile_Check(f)) {
2134 FILE *fp = PyFile_AsFile(f);
Guido van Rossum27a60b11997-05-22 22:25:11 +00002135 if (fp == NULL) {
2136 err_closed();
2137 return -1;
2138 }
2139 fputs(s, fp);
2140 return 0;
Guido van Rossum3165fe61992-09-25 21:59:05 +00002141 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002142 else if (!PyErr_Occurred()) {
2143 PyObject *v = PyString_FromString(s);
Guido van Rossum27a60b11997-05-22 22:25:11 +00002144 int err;
2145 if (v == NULL)
2146 return -1;
2147 err = PyFile_WriteObject(v, f, Py_PRINT_RAW);
2148 Py_DECREF(v);
2149 return err;
Guido van Rossum3165fe61992-09-25 21:59:05 +00002150 }
Guido van Rossum74ba2471997-07-13 03:56:50 +00002151 else
2152 return -1;
Guido van Rossum3165fe61992-09-25 21:59:05 +00002153}
Andrew M. Kuchling06051ed2000-07-13 23:56:54 +00002154
2155/* Try to get a file-descriptor from a Python object. If the object
2156 is an integer or long integer, its value is returned. If not, the
2157 object's fileno() method is called if it exists; the method must return
2158 an integer or long integer, which is returned as the file descriptor value.
2159 -1 is returned on failure.
2160*/
2161
2162int PyObject_AsFileDescriptor(PyObject *o)
2163{
2164 int fd;
2165 PyObject *meth;
2166
2167 if (PyInt_Check(o)) {
2168 fd = PyInt_AsLong(o);
2169 }
2170 else if (PyLong_Check(o)) {
2171 fd = PyLong_AsLong(o);
2172 }
2173 else if ((meth = PyObject_GetAttrString(o, "fileno")) != NULL)
2174 {
2175 PyObject *fno = PyEval_CallObject(meth, NULL);
2176 Py_DECREF(meth);
2177 if (fno == NULL)
2178 return -1;
Tim Peters86821b22001-01-07 21:19:34 +00002179
Andrew M. Kuchling06051ed2000-07-13 23:56:54 +00002180 if (PyInt_Check(fno)) {
2181 fd = PyInt_AsLong(fno);
2182 Py_DECREF(fno);
2183 }
2184 else if (PyLong_Check(fno)) {
2185 fd = PyLong_AsLong(fno);
2186 Py_DECREF(fno);
2187 }
2188 else {
2189 PyErr_SetString(PyExc_TypeError,
2190 "fileno() returned a non-integer");
2191 Py_DECREF(fno);
2192 return -1;
2193 }
2194 }
2195 else {
2196 PyErr_SetString(PyExc_TypeError,
2197 "argument must be an int, or have a fileno() method.");
2198 return -1;
2199 }
2200
2201 if (fd < 0) {
2202 PyErr_Format(PyExc_ValueError,
2203 "file descriptor cannot be a negative integer (%i)",
2204 fd);
2205 return -1;
2206 }
2207 return fd;
2208}
Jack Jansen7b8c7542002-04-14 20:12:41 +00002209
2210#ifdef WITH_UNIVERSAL_NEWLINES
2211/* From here on we need access to the real fgets and fread */
2212#undef fgets
2213#undef fread
2214
2215/*
2216** Py_UniversalNewlineFgets is an fgets variation that understands
2217** all of \r, \n and \r\n conventions.
2218** The stream should be opened in binary mode.
2219** If fobj is NULL the routine always does newline conversion, and
2220** it may peek one char ahead to gobble the second char in \r\n.
2221** If fobj is non-NULL it must be a PyFileObject. In this case there
2222** is no readahead but in stead a flag is used to skip a following
2223** \n on the next read. Also, if the file is open in binary mode
2224** the whole conversion is skipped. Finally, the routine keeps track of
2225** the different types of newlines seen.
2226** Note that we need no error handling: fgets() treats error and eof
2227** identically.
2228*/
2229char *
2230Py_UniversalNewlineFgets(char *buf, int n, FILE *stream, PyObject *fobj)
2231{
2232 char *p = buf;
2233 int c;
2234 int newlinetypes = 0;
2235 int skipnextlf = 0;
2236 int univ_newline = 1;
Tim Peters058b1412002-04-21 07:29:14 +00002237
Jack Jansen7b8c7542002-04-14 20:12:41 +00002238 if (fobj) {
2239 if (!PyFile_Check(fobj)) {
2240 errno = ENXIO; /* What can you do... */
2241 return NULL;
2242 }
2243 univ_newline = ((PyFileObject *)fobj)->f_univ_newline;
2244 if ( !univ_newline )
2245 return fgets(buf, n, stream);
2246 newlinetypes = ((PyFileObject *)fobj)->f_newlinetypes;
2247 skipnextlf = ((PyFileObject *)fobj)->f_skipnextlf;
2248 }
2249 FLOCKFILE(stream);
2250 c = 'x'; /* Shut up gcc warning */
2251 while (--n > 0 && (c = GETC(stream)) != EOF ) {
2252 if (skipnextlf ) {
2253 skipnextlf = 0;
2254 if (c == '\n') {
2255 /* Seeing a \n here with skipnextlf true
2256 ** means we saw a \r before.
2257 */
2258 newlinetypes |= NEWLINE_CRLF;
2259 c = GETC(stream);
2260 if (c == EOF) break;
2261 } else {
2262 /*
2263 ** Note that c == EOF also brings us here,
2264 ** so we're okay if the last char in the file
2265 ** is a CR.
2266 */
2267 newlinetypes |= NEWLINE_CR;
2268 }
2269 }
2270 if (c == '\r') {
2271 /* A \r is translated into a \n, and we skip
2272 ** an adjacent \n, if any. We don't set the
2273 ** newlinetypes flag until we've seen the next char.
2274 */
2275 skipnextlf = 1;
2276 c = '\n';
2277 } else if ( c == '\n') {
2278 newlinetypes |= NEWLINE_LF;
2279 }
2280 *p++ = c;
2281 if (c == '\n') break;
2282 }
2283 if ( c == EOF && skipnextlf )
2284 newlinetypes |= NEWLINE_CR;
2285 FUNLOCKFILE(stream);
2286 *p = '\0';
2287 if (fobj) {
2288 ((PyFileObject *)fobj)->f_newlinetypes = newlinetypes;
2289 ((PyFileObject *)fobj)->f_skipnextlf = skipnextlf;
2290 } else if ( skipnextlf ) {
2291 /* If we have no file object we cannot save the
2292 ** skipnextlf flag. We have to readahead, which
2293 ** will cause a pause if we're reading from an
2294 ** interactive stream, but that is very unlikely
2295 ** unless we're doing something silly like
2296 ** execfile("/dev/tty").
2297 */
2298 c = GETC(stream);
2299 if ( c != '\n' )
2300 ungetc(c, stream);
2301 }
2302 if (p == buf)
2303 return NULL;
2304 return buf;
2305}
2306
2307/*
2308** Py_UniversalNewlineFread is an fread variation that understands
2309** all of \r, \n and \r\n conventions.
2310** The stream should be opened in binary mode.
2311** fobj must be a PyFileObject. In this case there
2312** is no readahead but in stead a flag is used to skip a following
2313** \n on the next read. Also, if the file is open in binary mode
2314** the whole conversion is skipped. Finally, the routine keeps track of
2315** the different types of newlines seen.
2316*/
2317size_t
Tim Peters058b1412002-04-21 07:29:14 +00002318Py_UniversalNewlineFread(char *buf, size_t n,
Jack Jansen7b8c7542002-04-14 20:12:41 +00002319 FILE *stream, PyObject *fobj)
2320{
Tim Peters058b1412002-04-21 07:29:14 +00002321 char *dst = buf;
2322 PyFileObject *f = (PyFileObject *)fobj;
2323 int newlinetypes, skipnextlf;
2324
2325 assert(buf != NULL);
2326 assert(stream != NULL);
2327
Jack Jansen7b8c7542002-04-14 20:12:41 +00002328 if (!fobj || !PyFile_Check(fobj)) {
2329 errno = ENXIO; /* What can you do... */
Neal Norwitzcb3319f2003-02-09 01:10:02 +00002330 return 0;
Jack Jansen7b8c7542002-04-14 20:12:41 +00002331 }
Tim Peters058b1412002-04-21 07:29:14 +00002332 if (!f->f_univ_newline)
Jack Jansen7b8c7542002-04-14 20:12:41 +00002333 return fread(buf, 1, n, stream);
Tim Peters058b1412002-04-21 07:29:14 +00002334 newlinetypes = f->f_newlinetypes;
2335 skipnextlf = f->f_skipnextlf;
2336 /* Invariant: n is the number of bytes remaining to be filled
2337 * in the buffer.
2338 */
2339 while (n) {
2340 size_t nread;
2341 int shortread;
2342 char *src = dst;
2343
2344 nread = fread(dst, 1, n, stream);
2345 assert(nread <= n);
Neal Norwitzcb3319f2003-02-09 01:10:02 +00002346 if (nread == 0)
2347 break;
2348
Tim Peterse1682a82002-04-21 18:15:20 +00002349 n -= nread; /* assuming 1 byte out for each in; will adjust */
2350 shortread = n != 0; /* true iff EOF or error */
Tim Peters058b1412002-04-21 07:29:14 +00002351 while (nread--) {
2352 char c = *src++;
Jack Jansen7b8c7542002-04-14 20:12:41 +00002353 if (c == '\r') {
Tim Peters058b1412002-04-21 07:29:14 +00002354 /* Save as LF and set flag to skip next LF. */
Jack Jansen7b8c7542002-04-14 20:12:41 +00002355 *dst++ = '\n';
2356 skipnextlf = 1;
Tim Peters058b1412002-04-21 07:29:14 +00002357 }
2358 else if (skipnextlf && c == '\n') {
2359 /* Skip LF, and remember we saw CR LF. */
Jack Jansen7b8c7542002-04-14 20:12:41 +00002360 skipnextlf = 0;
2361 newlinetypes |= NEWLINE_CRLF;
Tim Peterse1682a82002-04-21 18:15:20 +00002362 ++n;
Tim Peters058b1412002-04-21 07:29:14 +00002363 }
2364 else {
2365 /* Normal char to be stored in buffer. Also
2366 * update the newlinetypes flag if either this
2367 * is an LF or the previous char was a CR.
2368 */
Jack Jansen7b8c7542002-04-14 20:12:41 +00002369 if (c == '\n')
2370 newlinetypes |= NEWLINE_LF;
2371 else if (skipnextlf)
2372 newlinetypes |= NEWLINE_CR;
2373 *dst++ = c;
2374 skipnextlf = 0;
2375 }
2376 }
Tim Peters058b1412002-04-21 07:29:14 +00002377 if (shortread) {
2378 /* If this is EOF, update type flags. */
2379 if (skipnextlf && feof(stream))
2380 newlinetypes |= NEWLINE_CR;
2381 break;
2382 }
Jack Jansen7b8c7542002-04-14 20:12:41 +00002383 }
Tim Peters058b1412002-04-21 07:29:14 +00002384 f->f_newlinetypes = newlinetypes;
2385 f->f_skipnextlf = skipnextlf;
2386 return dst - buf;
Jack Jansen7b8c7542002-04-14 20:12:41 +00002387}
2388#endif