blob: fb73385217f21c54db20e5cd9ddbaabca6119505 [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
Jeremy Hylton8b735422002-08-14 21:01:41 +000099 PyObject *exc = PyObject_CallFunction(PyExc_IOError, "(is)",
100 EISDIR, msg);
Neil Schemenauered19b882002-03-23 02:06:50 +0000101 PyErr_SetObject(PyExc_IOError, exc);
102 return NULL;
103 }
104#endif
105 return f;
106}
107
Tim Peters59c9a642001-09-13 05:38:56 +0000108
109static PyObject *
110fill_file_fields(PyFileObject *f, FILE *fp, char *name, char *mode,
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000111 int (*close)(FILE *), PyObject *wname)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000112{
Tim Peters59c9a642001-09-13 05:38:56 +0000113 assert(f != NULL);
114 assert(PyFile_Check(f));
Tim Peters44410012001-09-14 03:26:08 +0000115 assert(f->f_fp == NULL);
116
117 Py_DECREF(f->f_name);
118 Py_DECREF(f->f_mode);
Martin v. Löwis0073f2e2002-11-21 23:52:35 +0000119#ifdef Py_USING_UNICODE
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000120 if (wname)
121 f->f_name = PyUnicode_FromObject(wname);
122 else
Martin v. Löwis0073f2e2002-11-21 23:52:35 +0000123#endif
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000124 f->f_name = PyString_FromString(name);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000125 f->f_mode = PyString_FromString(mode);
Tim Peters44410012001-09-14 03:26:08 +0000126
Guido van Rossuma1ab7fa1991-06-04 19:37:39 +0000127 f->f_close = close;
Guido van Rossumeb183da1991-04-04 10:44:06 +0000128 f->f_softspace = 0;
Tim Peters59c9a642001-09-13 05:38:56 +0000129 f->f_binary = strchr(mode,'b') != NULL;
Guido van Rossum7a6e9592002-08-06 15:55:28 +0000130 f->f_buf = NULL;
Jack Jansen7b8c7542002-04-14 20:12:41 +0000131#ifdef WITH_UNIVERSAL_NEWLINES
132 f->f_univ_newline = (strchr(mode, 'U') != NULL);
133 f->f_newlinetypes = NEWLINE_UNKNOWN;
134 f->f_skipnextlf = 0;
135#endif
Tim Peters44410012001-09-14 03:26:08 +0000136
Tim Peters59c9a642001-09-13 05:38:56 +0000137 if (f->f_name == NULL || f->f_mode == NULL)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000138 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000139 f->f_fp = fp;
Neil Schemenauered19b882002-03-23 02:06:50 +0000140 f = dircheck(f);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000141 return (PyObject *) f;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000142}
143
Tim Peters59c9a642001-09-13 05:38:56 +0000144static PyObject *
145open_the_file(PyFileObject *f, char *name, char *mode)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000146{
Tim Peters59c9a642001-09-13 05:38:56 +0000147 assert(f != NULL);
148 assert(PyFile_Check(f));
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000149#ifdef MS_WINDOWS
150 /* windows ignores the passed name in order to support Unicode */
151 assert(f->f_name != NULL);
152#else
Tim Peters59c9a642001-09-13 05:38:56 +0000153 assert(name != NULL);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000154#endif
Tim Peters59c9a642001-09-13 05:38:56 +0000155 assert(mode != NULL);
Tim Peters44410012001-09-14 03:26:08 +0000156 assert(f->f_fp == NULL);
Tim Peters59c9a642001-09-13 05:38:56 +0000157
Tim Peters8fa45672001-09-13 21:01:29 +0000158 /* rexec.py can't stop a user from getting the file() constructor --
159 all they have to do is get *any* file object f, and then do
160 type(f). Here we prevent them from doing damage with it. */
161 if (PyEval_GetRestricted()) {
162 PyErr_SetString(PyExc_IOError,
Jeremy Hylton8b735422002-08-14 21:01:41 +0000163 "file() constructor not accessible in restricted mode");
Tim Peters8fa45672001-09-13 21:01:29 +0000164 return NULL;
165 }
Tim Petersa27a1502001-11-09 20:59:14 +0000166 errno = 0;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000167#ifdef HAVE_FOPENRF
Guido van Rossuma08095a1991-02-13 23:25:27 +0000168 if (*mode == '*') {
169 FILE *fopenRF();
170 f->f_fp = fopenRF(name, mode+1);
171 }
172 else
173#endif
Guido van Rossumff4949e1992-08-05 19:58:53 +0000174 {
Jack Jansen7b8c7542002-04-14 20:12:41 +0000175#ifdef WITH_UNIVERSAL_NEWLINES
176 if (strcmp(mode, "U") == 0 || strcmp(mode, "rU") == 0)
177 mode = "rb";
178#else
179 /* Compatibility: specifying U in a Python without universal
180 ** newlines is allowed, and the file is opened as a normal text
181 ** file.
182 */
183 if (strcmp(mode, "U") == 0 || strcmp(mode, "rU") == 0)
184 mode = "r";
185#endif
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000186#ifdef MS_WINDOWS
187 if (PyUnicode_Check(f->f_name)) {
188 PyObject *wmode;
189 wmode = PyUnicode_DecodeASCII(mode, strlen(mode), NULL);
190 if (f->f_name && wmode) {
191 Py_BEGIN_ALLOW_THREADS
192 /* PyUnicode_AS_UNICODE OK without thread
193 lock as it is a simple dereference. */
194 f->f_fp = _wfopen(PyUnicode_AS_UNICODE(f->f_name),
195 PyUnicode_AS_UNICODE(wmode));
196 Py_END_ALLOW_THREADS
197 }
198 Py_XDECREF(wmode);
199 }
200#endif
201 if (NULL == f->f_fp && NULL != name) {
202 Py_BEGIN_ALLOW_THREADS
203 f->f_fp = fopen(name, mode);
204 Py_END_ALLOW_THREADS
205 }
Guido van Rossumff4949e1992-08-05 19:58:53 +0000206 }
Guido van Rossuma08095a1991-02-13 23:25:27 +0000207 if (f->f_fp == NULL) {
Jack Jansene08dea191995-04-23 22:12:47 +0000208#ifdef NO_FOPEN_ERRNO
Jack Jansenb3be2162001-11-30 14:16:36 +0000209 /* Metroworks only, wich does not always sets errno */
Jeremy Hylton41c83212001-11-09 16:17:24 +0000210 if (errno == 0) {
Jack Jansenb3be2162001-11-30 14:16:36 +0000211 PyObject *v;
212 v = Py_BuildValue("(is)", 0, "Cannot open file");
213 if (v != NULL) {
214 PyErr_SetObject(PyExc_IOError, v);
215 Py_DECREF(v);
216 }
Jack Jansene08dea191995-04-23 22:12:47 +0000217 return NULL;
218 }
219#endif
Tim Peters2ea91112002-04-08 04:13:12 +0000220#ifdef _MSC_VER
221 /* MSVC 6 (Microsoft) leaves errno at 0 for bad mode strings,
222 * across all Windows flavors. When it sets EINVAL varies
223 * across Windows flavors, the exact conditions aren't
224 * documented, and the answer lies in the OS's implementation
225 * of Win32's CreateFile function (whose source is secret).
226 * Seems the best we can do is map EINVAL to ENOENT.
227 */
228 if (errno == 0) /* bad mode string */
229 errno = EINVAL;
230 else if (errno == EINVAL) /* unknown, but not a mode string */
231 errno = ENOENT;
232#endif
Jeremy Hylton41c83212001-11-09 16:17:24 +0000233 if (errno == EINVAL)
Tim Peters2ea91112002-04-08 04:13:12 +0000234 PyErr_Format(PyExc_IOError, "invalid mode: %s",
Jeremy Hylton41c83212001-11-09 16:17:24 +0000235 mode);
236 else
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000237#ifdef MS_WINDOWS
238 PyErr_SetFromErrnoWithFilenameObject(PyExc_IOError, f->f_name);
239#else
Jeremy Hylton41c83212001-11-09 16:17:24 +0000240 PyErr_SetFromErrnoWithFilename(PyExc_IOError, name);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000241#endif /* MS_WINDOWS */
Tim Peters59c9a642001-09-13 05:38:56 +0000242 f = NULL;
243 }
Tim Peters2ea91112002-04-08 04:13:12 +0000244 if (f != NULL)
Neil Schemenauered19b882002-03-23 02:06:50 +0000245 f = dircheck(f);
Tim Peters59c9a642001-09-13 05:38:56 +0000246 return (PyObject *)f;
247}
248
249PyObject *
250PyFile_FromFile(FILE *fp, char *name, char *mode, int (*close)(FILE *))
251{
Tim Peters44410012001-09-14 03:26:08 +0000252 PyFileObject *f = (PyFileObject *)PyFile_Type.tp_new(&PyFile_Type,
253 NULL, NULL);
Tim Peters59c9a642001-09-13 05:38:56 +0000254 if (f != NULL) {
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000255 if (fill_file_fields(f, fp, name, mode, close, NULL) == NULL) {
Tim Peters59c9a642001-09-13 05:38:56 +0000256 Py_DECREF(f);
257 f = NULL;
258 }
259 }
260 return (PyObject *) f;
261}
262
263PyObject *
264PyFile_FromString(char *name, char *mode)
265{
266 extern int fclose(FILE *);
267 PyFileObject *f;
268
269 f = (PyFileObject *)PyFile_FromFile((FILE *)NULL, name, mode, fclose);
270 if (f != NULL) {
271 if (open_the_file(f, name, mode) == NULL) {
272 Py_DECREF(f);
273 f = NULL;
274 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000275 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000276 return (PyObject *)f;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000277}
278
Guido van Rossumb6775db1994-08-01 11:34:53 +0000279void
Fred Drakefd99de62000-07-09 05:02:18 +0000280PyFile_SetBufSize(PyObject *f, int bufsize)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000281{
282 if (bufsize >= 0) {
283#ifdef HAVE_SETVBUF
284 int type;
285 switch (bufsize) {
286 case 0:
287 type = _IONBF;
288 break;
289 case 1:
290 type = _IOLBF;
291 bufsize = BUFSIZ;
292 break;
293 default:
294 type = _IOFBF;
295 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000296 setvbuf(((PyFileObject *)f)->f_fp, (char *)NULL,
297 type, bufsize);
Guido van Rossumf8b4de01998-03-06 15:32:40 +0000298#else /* !HAVE_SETVBUF */
299 if (bufsize <= 1)
300 setbuf(((PyFileObject *)f)->f_fp, (char *)NULL);
301#endif /* !HAVE_SETVBUF */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000302 }
303}
304
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000305static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000306err_closed(void)
Guido van Rossumd7297e61992-07-06 14:19:26 +0000307{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000308 PyErr_SetString(PyExc_ValueError, "I/O operation on closed file");
Guido van Rossumd7297e61992-07-06 14:19:26 +0000309 return NULL;
310}
311
Neal Norwitzd8b995f2002-08-06 21:50:54 +0000312static void drop_readahead(PyFileObject *);
Guido van Rossum7a6e9592002-08-06 15:55:28 +0000313
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000314/* Methods */
315
316static void
Fred Drakefd99de62000-07-09 05:02:18 +0000317file_dealloc(PyFileObject *f)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000318{
Guido van Rossumff4949e1992-08-05 19:58:53 +0000319 if (f->f_fp != NULL && f->f_close != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000320 Py_BEGIN_ALLOW_THREADS
Guido van Rossuma1ab7fa1991-06-04 19:37:39 +0000321 (*f->f_close)(f->f_fp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000322 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000323 }
Tim Peters44410012001-09-14 03:26:08 +0000324 Py_XDECREF(f->f_name);
325 Py_XDECREF(f->f_mode);
Guido van Rossum7a6e9592002-08-06 15:55:28 +0000326 drop_readahead(f);
Guido van Rossum9475a232001-10-05 20:51:39 +0000327 f->ob_type->tp_free((PyObject *)f);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000328}
329
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000330static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000331file_repr(PyFileObject *f)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000332{
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000333 if (PyUnicode_Check(f->f_name)) {
Martin v. Löwis0073f2e2002-11-21 23:52:35 +0000334#ifdef Py_USING_UNICODE
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000335 PyObject *ret = NULL;
336 PyObject *name;
337 name = PyUnicode_AsUnicodeEscapeString(f->f_name);
338 ret = PyString_FromFormat("<%s file u'%s', mode '%s' at %p>",
339 f->f_fp == NULL ? "closed" : "open",
340 PyString_AsString(name),
341 PyString_AsString(f->f_mode),
342 f);
343 Py_XDECREF(name);
344 return ret;
Martin v. Löwis0073f2e2002-11-21 23:52:35 +0000345#endif
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000346 } else {
347 return PyString_FromFormat("<%s file '%s', mode '%s' at %p>",
Barry Warsaw7ce36942001-08-24 18:34:26 +0000348 f->f_fp == NULL ? "closed" : "open",
349 PyString_AsString(f->f_name),
350 PyString_AsString(f->f_mode),
351 f);
Mark Hammondc2e85bd2002-10-03 05:10:39 +0000352 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000353}
354
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000355static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000356file_close(PyFileObject *f)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000357{
Guido van Rossuma1ab7fa1991-06-04 19:37:39 +0000358 int sts = 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000359 if (f->f_fp != NULL) {
Guido van Rossumff4949e1992-08-05 19:58:53 +0000360 if (f->f_close != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000361 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000362 errno = 0;
Guido van Rossuma1ab7fa1991-06-04 19:37:39 +0000363 sts = (*f->f_close)(f->f_fp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000364 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000365 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000366 f->f_fp = NULL;
367 }
Guido van Rossumfebd5511992-03-04 16:39:24 +0000368 if (sts == EOF)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000369 return PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossuma1ab7fa1991-06-04 19:37:39 +0000370 if (sts != 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000371 return PyInt_FromLong((long)sts);
372 Py_INCREF(Py_None);
373 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000374}
375
Trent Mickf29f47b2000-08-11 19:02:59 +0000376
Guido van Rossumb8552162001-09-05 14:58:11 +0000377/* Our very own off_t-like type, 64-bit if possible */
378#if !defined(HAVE_LARGEFILE_SUPPORT)
379typedef off_t Py_off_t;
380#elif SIZEOF_OFF_T >= 8
381typedef off_t Py_off_t;
382#elif SIZEOF_FPOS_T >= 8
Guido van Rossum4f53da02001-03-01 18:26:53 +0000383typedef fpos_t Py_off_t;
384#else
Guido van Rossumb8552162001-09-05 14:58:11 +0000385#error "Large file support, but neither off_t nor fpos_t is large enough."
Guido van Rossum4f53da02001-03-01 18:26:53 +0000386#endif
387
388
Trent Mickf29f47b2000-08-11 19:02:59 +0000389/* a portable fseek() function
390 return 0 on success, non-zero on failure (with errno set) */
Guido van Rossumf68d8e52001-04-14 17:55:09 +0000391static int
Guido van Rossum4f53da02001-03-01 18:26:53 +0000392_portable_fseek(FILE *fp, Py_off_t offset, int whence)
Trent Mickf29f47b2000-08-11 19:02:59 +0000393{
Guido van Rossumb8552162001-09-05 14:58:11 +0000394#if !defined(HAVE_LARGEFILE_SUPPORT)
395 return fseek(fp, offset, whence);
396#elif defined(HAVE_FSEEKO) && SIZEOF_OFF_T >= 8
Trent Mickf29f47b2000-08-11 19:02:59 +0000397 return fseeko(fp, offset, whence);
398#elif defined(HAVE_FSEEK64)
399 return fseek64(fp, offset, whence);
Fred Drakedb810ac2000-10-06 20:42:33 +0000400#elif defined(__BEOS__)
401 return _fseek(fp, offset, whence);
Guido van Rossumb8552162001-09-05 14:58:11 +0000402#elif SIZEOF_FPOS_T >= 8
Guido van Rossume54e0be2001-01-16 20:53:31 +0000403 /* lacking a 64-bit capable fseek(), use a 64-bit capable fsetpos()
404 and fgetpos() to implement fseek()*/
Trent Mickf29f47b2000-08-11 19:02:59 +0000405 fpos_t pos;
406 switch (whence) {
Guido van Rossume54e0be2001-01-16 20:53:31 +0000407 case SEEK_END:
Guido van Rossum8b4e43e2001-09-10 20:43:35 +0000408#ifdef MS_WINDOWS
409 fflush(fp);
410 if (_lseeki64(fileno(fp), 0, 2) == -1)
411 return -1;
412#else
Guido van Rossume54e0be2001-01-16 20:53:31 +0000413 if (fseek(fp, 0, SEEK_END) != 0)
414 return -1;
Guido van Rossum8b4e43e2001-09-10 20:43:35 +0000415#endif
Guido van Rossume54e0be2001-01-16 20:53:31 +0000416 /* fall through */
417 case SEEK_CUR:
418 if (fgetpos(fp, &pos) != 0)
419 return -1;
420 offset += pos;
421 break;
422 /* case SEEK_SET: break; */
Trent Mickf29f47b2000-08-11 19:02:59 +0000423 }
424 return fsetpos(fp, &offset);
425#else
Guido van Rossumb8552162001-09-05 14:58:11 +0000426#error "Large file support, but no way to fseek."
Trent Mickf29f47b2000-08-11 19:02:59 +0000427#endif
428}
429
430
431/* a portable ftell() function
432 Return -1 on failure with errno set appropriately, current file
433 position on success */
Guido van Rossumf68d8e52001-04-14 17:55:09 +0000434static Py_off_t
Fred Drake8ce159a2000-08-31 05:18:54 +0000435_portable_ftell(FILE* fp)
Trent Mickf29f47b2000-08-11 19:02:59 +0000436{
Guido van Rossumb8552162001-09-05 14:58:11 +0000437#if !defined(HAVE_LARGEFILE_SUPPORT)
438 return ftell(fp);
439#elif defined(HAVE_FTELLO) && SIZEOF_OFF_T >= 8
440 return ftello(fp);
441#elif defined(HAVE_FTELL64)
442 return ftell64(fp);
443#elif SIZEOF_FPOS_T >= 8
Trent Mickf29f47b2000-08-11 19:02:59 +0000444 fpos_t pos;
445 if (fgetpos(fp, &pos) != 0)
446 return -1;
447 return pos;
448#else
Guido van Rossumb8552162001-09-05 14:58:11 +0000449#error "Large file support, but no way to ftell."
Trent Mickf29f47b2000-08-11 19:02:59 +0000450#endif
451}
452
453
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000454static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000455file_seek(PyFileObject *f, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000456{
Guido van Rossumd7297e61992-07-06 14:19:26 +0000457 int whence;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000458 int ret;
Guido van Rossum4f53da02001-03-01 18:26:53 +0000459 Py_off_t offset;
Guido van Rossum3c9fe0c1999-01-06 18:51:17 +0000460 PyObject *offobj;
Tim Peters86821b22001-01-07 21:19:34 +0000461
Guido van Rossumd7297e61992-07-06 14:19:26 +0000462 if (f->f_fp == NULL)
463 return err_closed();
Guido van Rossum7a6e9592002-08-06 15:55:28 +0000464 drop_readahead(f);
Guido van Rossumd7297e61992-07-06 14:19:26 +0000465 whence = 0;
Guido van Rossum43713e52000-02-29 13:59:29 +0000466 if (!PyArg_ParseTuple(args, "O|i:seek", &offobj, &whence))
Guido van Rossum3c9fe0c1999-01-06 18:51:17 +0000467 return NULL;
468#if !defined(HAVE_LARGEFILE_SUPPORT)
469 offset = PyInt_AsLong(offobj);
470#else
471 offset = PyLong_Check(offobj) ?
472 PyLong_AsLongLong(offobj) : PyInt_AsLong(offobj);
473#endif
474 if (PyErr_Occurred())
Guido van Rossum88303191999-01-04 17:22:18 +0000475 return NULL;
Tim Peters86821b22001-01-07 21:19:34 +0000476
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000477 Py_BEGIN_ALLOW_THREADS
Guido van Rossumce5ba841991-03-06 13:06:18 +0000478 errno = 0;
Trent Mickf29f47b2000-08-11 19:02:59 +0000479 ret = _portable_fseek(f->f_fp, offset, whence);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000480 Py_END_ALLOW_THREADS
Trent Mickf29f47b2000-08-11 19:02:59 +0000481
Guido van Rossumff4949e1992-08-05 19:58:53 +0000482 if (ret != 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000483 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossumfebd5511992-03-04 16:39:24 +0000484 clearerr(f->f_fp);
485 return NULL;
Guido van Rossumce5ba841991-03-06 13:06:18 +0000486 }
Jack Jansen7b8c7542002-04-14 20:12:41 +0000487#ifdef WITH_UNIVERSAL_NEWLINES
488 f->f_skipnextlf = 0;
489#endif
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000490 Py_INCREF(Py_None);
491 return Py_None;
Guido van Rossumce5ba841991-03-06 13:06:18 +0000492}
493
Trent Mickf29f47b2000-08-11 19:02:59 +0000494
Guido van Rossumd7047b31995-01-02 19:07:15 +0000495#ifdef HAVE_FTRUNCATE
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000496static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000497file_truncate(PyFileObject *f, PyObject *args)
Guido van Rossumd7047b31995-01-02 19:07:15 +0000498{
Guido van Rossumd7047b31995-01-02 19:07:15 +0000499 int ret;
Guido van Rossum4f53da02001-03-01 18:26:53 +0000500 Py_off_t newsize;
Guido van Rossum3c9fe0c1999-01-06 18:51:17 +0000501 PyObject *newsizeobj;
Tim Peters86821b22001-01-07 21:19:34 +0000502
Guido van Rossumd7047b31995-01-02 19:07:15 +0000503 if (f->f_fp == NULL)
504 return err_closed();
Guido van Rossum3c9fe0c1999-01-06 18:51:17 +0000505 newsizeobj = NULL;
Raymond Hettingerea3fdf42002-12-29 16:33:45 +0000506 if (!PyArg_UnpackTuple(args, "truncate", 0, 1, &newsizeobj))
Guido van Rossum88303191999-01-04 17:22:18 +0000507 return NULL;
Tim Petersfb05db22002-03-11 00:24:00 +0000508
509 /* Set newsize to current postion if newsizeobj NULL, else to the
510 specified value. */
Guido van Rossum3c9fe0c1999-01-06 18:51:17 +0000511 if (newsizeobj != NULL) {
512#if !defined(HAVE_LARGEFILE_SUPPORT)
513 newsize = PyInt_AsLong(newsizeobj);
514#else
515 newsize = PyLong_Check(newsizeobj) ?
516 PyLong_AsLongLong(newsizeobj) :
517 PyInt_AsLong(newsizeobj);
518#endif
519 if (PyErr_Occurred())
520 return NULL;
Tim Petersfb05db22002-03-11 00:24:00 +0000521 }
522 else {
523 /* Default to current position. */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000524 Py_BEGIN_ALLOW_THREADS
Guido van Rossumd7047b31995-01-02 19:07:15 +0000525 errno = 0;
Trent Mickf29f47b2000-08-11 19:02:59 +0000526 newsize = _portable_ftell(f->f_fp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000527 Py_END_ALLOW_THREADS
Tim Petersfb05db22002-03-11 00:24:00 +0000528 if (newsize == -1)
529 goto onioerror;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000530 }
Tim Petersfb05db22002-03-11 00:24:00 +0000531
532 /* Flush the file. */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000533 Py_BEGIN_ALLOW_THREADS
Guido van Rossumd7047b31995-01-02 19:07:15 +0000534 errno = 0;
535 ret = fflush(f->f_fp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000536 Py_END_ALLOW_THREADS
Tim Petersfb05db22002-03-11 00:24:00 +0000537 if (ret != 0)
538 goto onioerror;
Trent Mickf29f47b2000-08-11 19:02:59 +0000539
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000540#ifdef MS_WINDOWS
Tim Petersfb05db22002-03-11 00:24:00 +0000541 /* MS _chsize doesn't work if newsize doesn't fit in 32 bits,
Tim Peters8f01b682002-03-12 03:04:44 +0000542 so don't even try using it. */
Tim Petersfb05db22002-03-11 00:24:00 +0000543 {
Tim Peters8f01b682002-03-12 03:04:44 +0000544 Py_off_t current; /* current file position */
Tim Petersfb05db22002-03-11 00:24:00 +0000545 HANDLE hFile;
546 int error;
547
Tim Peters8f01b682002-03-12 03:04:44 +0000548 /* current <- current file postion. */
549 if (newsizeobj == NULL)
550 current = newsize;
551 else {
Tim Petersfb05db22002-03-11 00:24:00 +0000552 Py_BEGIN_ALLOW_THREADS
553 errno = 0;
Tim Peters8f01b682002-03-12 03:04:44 +0000554 current = _portable_ftell(f->f_fp);
555 Py_END_ALLOW_THREADS
556 if (current == -1)
557 goto onioerror;
558 }
559
560 /* Move to newsize. */
561 if (current != newsize) {
562 Py_BEGIN_ALLOW_THREADS
563 errno = 0;
564 error = _portable_fseek(f->f_fp, newsize, SEEK_SET)
565 != 0;
Tim Petersfb05db22002-03-11 00:24:00 +0000566 Py_END_ALLOW_THREADS
567 if (error)
568 goto onioerror;
569 }
570
Tim Peters8f01b682002-03-12 03:04:44 +0000571 /* Truncate. Note that this may grow the file! */
572 Py_BEGIN_ALLOW_THREADS
573 errno = 0;
574 hFile = (HANDLE)_get_osfhandle(fileno(f->f_fp));
575 error = hFile == (HANDLE)-1;
576 if (!error) {
577 error = SetEndOfFile(hFile) == 0;
578 if (error)
579 errno = EACCES;
580 }
581 Py_END_ALLOW_THREADS
582 if (error)
583 goto onioerror;
584
585 /* Restore original file position. */
586 if (current != newsize) {
587 Py_BEGIN_ALLOW_THREADS
588 errno = 0;
589 error = _portable_fseek(f->f_fp, current, SEEK_SET)
590 != 0;
591 Py_END_ALLOW_THREADS
592 if (error)
593 goto onioerror;
594 }
Guido van Rossumd7047b31995-01-02 19:07:15 +0000595 }
Trent Mickf29f47b2000-08-11 19:02:59 +0000596#else
597 Py_BEGIN_ALLOW_THREADS
598 errno = 0;
599 ret = ftruncate(fileno(f->f_fp), newsize);
600 Py_END_ALLOW_THREADS
601 if (ret != 0) goto onioerror;
Martin v. Löwis6238d2b2002-06-30 15:26:10 +0000602#endif /* !MS_WINDOWS */
Tim Peters86821b22001-01-07 21:19:34 +0000603
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000604 Py_INCREF(Py_None);
605 return Py_None;
Trent Mickf29f47b2000-08-11 19:02:59 +0000606
607onioerror:
608 PyErr_SetFromErrno(PyExc_IOError);
609 clearerr(f->f_fp);
610 return NULL;
Guido van Rossumd7047b31995-01-02 19:07:15 +0000611}
612#endif /* HAVE_FTRUNCATE */
613
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000614static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000615file_tell(PyFileObject *f)
Guido van Rossumce5ba841991-03-06 13:06:18 +0000616{
Guido van Rossum4f53da02001-03-01 18:26:53 +0000617 Py_off_t pos;
Trent Mickf29f47b2000-08-11 19:02:59 +0000618
Guido van Rossumd7297e61992-07-06 14:19:26 +0000619 if (f->f_fp == NULL)
620 return err_closed();
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000621 Py_BEGIN_ALLOW_THREADS
Guido van Rossumce5ba841991-03-06 13:06:18 +0000622 errno = 0;
Trent Mickf29f47b2000-08-11 19:02:59 +0000623 pos = _portable_ftell(f->f_fp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000624 Py_END_ALLOW_THREADS
Trent Mickf29f47b2000-08-11 19:02:59 +0000625 if (pos == -1) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000626 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossumfebd5511992-03-04 16:39:24 +0000627 clearerr(f->f_fp);
628 return NULL;
Guido van Rossumce5ba841991-03-06 13:06:18 +0000629 }
Jack Jansen7b8c7542002-04-14 20:12:41 +0000630#ifdef WITH_UNIVERSAL_NEWLINES
631 if (f->f_skipnextlf) {
632 int c;
633 c = GETC(f->f_fp);
634 if (c == '\n') {
635 pos++;
636 f->f_skipnextlf = 0;
637 } else if (c != EOF) ungetc(c, f->f_fp);
638 }
639#endif
Guido van Rossum3c9fe0c1999-01-06 18:51:17 +0000640#if !defined(HAVE_LARGEFILE_SUPPORT)
Trent Mickf29f47b2000-08-11 19:02:59 +0000641 return PyInt_FromLong(pos);
Guido van Rossum3c9fe0c1999-01-06 18:51:17 +0000642#else
Trent Mickf29f47b2000-08-11 19:02:59 +0000643 return PyLong_FromLongLong(pos);
Guido van Rossum3c9fe0c1999-01-06 18:51:17 +0000644#endif
Guido van Rossumce5ba841991-03-06 13:06:18 +0000645}
646
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000647static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000648file_fileno(PyFileObject *f)
Guido van Rossumed233a51992-06-23 09:07:03 +0000649{
Guido van Rossumd7297e61992-07-06 14:19:26 +0000650 if (f->f_fp == NULL)
651 return err_closed();
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000652 return PyInt_FromLong((long) fileno(f->f_fp));
Guido van Rossumed233a51992-06-23 09:07:03 +0000653}
654
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000655static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000656file_flush(PyFileObject *f)
Guido van Rossumce5ba841991-03-06 13:06:18 +0000657{
Guido van Rossumff4949e1992-08-05 19:58:53 +0000658 int res;
Tim Peters86821b22001-01-07 21:19:34 +0000659
Guido van Rossumd7297e61992-07-06 14:19:26 +0000660 if (f->f_fp == NULL)
661 return err_closed();
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000662 Py_BEGIN_ALLOW_THREADS
Guido van Rossumce5ba841991-03-06 13:06:18 +0000663 errno = 0;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000664 res = fflush(f->f_fp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000665 Py_END_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000666 if (res != 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000667 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossumfebd5511992-03-04 16:39:24 +0000668 clearerr(f->f_fp);
669 return NULL;
Guido van Rossumce5ba841991-03-06 13:06:18 +0000670 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000671 Py_INCREF(Py_None);
672 return Py_None;
Guido van Rossumce5ba841991-03-06 13:06:18 +0000673}
674
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000675static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000676file_isatty(PyFileObject *f)
Guido van Rossuma1ab7fa1991-06-04 19:37:39 +0000677{
Guido van Rossumff4949e1992-08-05 19:58:53 +0000678 long res;
Guido van Rossumd7297e61992-07-06 14:19:26 +0000679 if (f->f_fp == NULL)
680 return err_closed();
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000681 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000682 res = isatty((int)fileno(f->f_fp));
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000683 Py_END_ALLOW_THREADS
Guido van Rossum7f7666f2002-04-07 06:28:00 +0000684 return PyBool_FromLong(res);
Guido van Rossuma1ab7fa1991-06-04 19:37:39 +0000685}
686
Guido van Rossumff7e83d1999-08-27 20:39:37 +0000687
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000688#if BUFSIZ < 8192
689#define SMALLCHUNK 8192
690#else
691#define SMALLCHUNK BUFSIZ
692#endif
693
Guido van Rossum3c259041999-01-14 19:00:14 +0000694#if SIZEOF_INT < 4
695#define BIGCHUNK (512 * 32)
696#else
697#define BIGCHUNK (512 * 1024)
698#endif
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000699
700static size_t
Fred Drakefd99de62000-07-09 05:02:18 +0000701new_buffersize(PyFileObject *f, size_t currentsize)
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000702{
703#ifdef HAVE_FSTAT
Fred Drake1bc8fab2001-07-19 21:49:38 +0000704 off_t pos, end;
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000705 struct stat st;
706 if (fstat(fileno(f->f_fp), &st) == 0) {
707 end = st.st_size;
Guido van Rossumcada2931998-12-11 20:44:56 +0000708 /* The following is not a bug: we really need to call lseek()
709 *and* ftell(). The reason is that some stdio libraries
710 mistakenly flush their buffer when ftell() is called and
711 the lseek() call it makes fails, thereby throwing away
712 data that cannot be recovered in any way. To avoid this,
713 we first test lseek(), and only call ftell() if lseek()
714 works. We can't use the lseek() value either, because we
715 need to take the amount of buffered data into account.
716 (Yet another reason why stdio stinks. :-) */
Jack Jansen2771b5b2001-10-10 22:03:27 +0000717#ifdef USE_GUSI2
718 pos = lseek(fileno(f->f_fp), 1L, SEEK_CUR);
719 pos = lseek(fileno(f->f_fp), -1L, SEEK_CUR);
720#else
Guido van Rossum91aaa921998-05-05 22:21:35 +0000721 pos = lseek(fileno(f->f_fp), 0L, SEEK_CUR);
Jack Jansen2771b5b2001-10-10 22:03:27 +0000722#endif
723 if (pos >= 0) {
Guido van Rossum91aaa921998-05-05 22:21:35 +0000724 pos = ftell(f->f_fp);
Jack Jansen2771b5b2001-10-10 22:03:27 +0000725 }
Guido van Rossumd30dc0a1998-04-27 19:01:08 +0000726 if (pos < 0)
727 clearerr(f->f_fp);
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000728 if (end > pos && pos >= 0)
Guido van Rossumcada2931998-12-11 20:44:56 +0000729 return currentsize + end - pos + 1;
Guido van Rossumdcb5e7f1998-03-03 22:36:10 +0000730 /* Add 1 so if the file were to grow we'd notice. */
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000731 }
732#endif
733 if (currentsize > SMALLCHUNK) {
734 /* Keep doubling until we reach BIGCHUNK;
735 then keep adding BIGCHUNK. */
736 if (currentsize <= BIGCHUNK)
737 return currentsize + currentsize;
738 else
739 return currentsize + BIGCHUNK;
740 }
741 return currentsize + SMALLCHUNK;
742}
743
Gustavo Niemeyer786ddb22002-12-16 18:12:53 +0000744#if defined(EWOULDBLOCK) && defined(EAGAIN) && EWOULDBLOCK != EAGAIN
745#define BLOCKED_ERRNO(x) ((x) == EWOULDBLOCK || (x) == EAGAIN)
746#else
747#ifdef EWOULDBLOCK
748#define BLOCKED_ERRNO(x) ((x) == EWOULDBLOCK)
749#else
750#ifdef EAGAIN
751#define BLOCKED_ERRNO(x) ((x) == EAGAIN)
752#else
753#define BLOCKED_ERRNO(x) 0
754#endif
755#endif
756#endif
757
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000758static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000759file_read(PyFileObject *f, PyObject *args)
Guido van Rossumce5ba841991-03-06 13:06:18 +0000760{
Guido van Rossum789a1611997-05-10 22:33:55 +0000761 long bytesrequested = -1;
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000762 size_t bytesread, buffersize, chunksize;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000763 PyObject *v;
Tim Peters86821b22001-01-07 21:19:34 +0000764
Guido van Rossumd7297e61992-07-06 14:19:26 +0000765 if (f->f_fp == NULL)
766 return err_closed();
Guido van Rossum43713e52000-02-29 13:59:29 +0000767 if (!PyArg_ParseTuple(args, "|l:read", &bytesrequested))
Guido van Rossum789a1611997-05-10 22:33:55 +0000768 return NULL;
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000769 if (bytesrequested < 0)
Guido van Rossumff1ccbf1999-04-10 15:48:23 +0000770 buffersize = new_buffersize(f, (size_t)0);
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000771 else
772 buffersize = bytesrequested;
Trent Mickf29f47b2000-08-11 19:02:59 +0000773 if (buffersize > INT_MAX) {
774 PyErr_SetString(PyExc_OverflowError,
Jeremy Hylton8b735422002-08-14 21:01:41 +0000775 "requested number of bytes is more than a Python string can hold");
Trent Mickf29f47b2000-08-11 19:02:59 +0000776 return NULL;
777 }
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000778 v = PyString_FromStringAndSize((char *)NULL, buffersize);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000779 if (v == NULL)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000780 return NULL;
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000781 bytesread = 0;
Guido van Rossumce5ba841991-03-06 13:06:18 +0000782 for (;;) {
Guido van Rossum6263d541997-05-10 22:07:25 +0000783 Py_BEGIN_ALLOW_THREADS
784 errno = 0;
Jack Jansen7b8c7542002-04-14 20:12:41 +0000785 chunksize = Py_UniversalNewlineFread(BUF(v) + bytesread,
Jeremy Hylton8b735422002-08-14 21:01:41 +0000786 buffersize - bytesread, f->f_fp, (PyObject *)f);
Guido van Rossum6263d541997-05-10 22:07:25 +0000787 Py_END_ALLOW_THREADS
788 if (chunksize == 0) {
789 if (!ferror(f->f_fp))
790 break;
Guido van Rossum6263d541997-05-10 22:07:25 +0000791 clearerr(f->f_fp);
Gustavo Niemeyer786ddb22002-12-16 18:12:53 +0000792 /* When in non-blocking mode, data shouldn't
793 * be discarded if a blocking signal was
794 * received. That will also happen if
795 * chunksize != 0, but bytesread < buffersize. */
796 if (bytesread > 0 && BLOCKED_ERRNO(errno))
797 break;
798 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossum6263d541997-05-10 22:07:25 +0000799 Py_DECREF(v);
800 return NULL;
801 }
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000802 bytesread += chunksize;
Gustavo Niemeyer786ddb22002-12-16 18:12:53 +0000803 if (bytesread < buffersize) {
804 clearerr(f->f_fp);
Guido van Rossumce5ba841991-03-06 13:06:18 +0000805 break;
Gustavo Niemeyer786ddb22002-12-16 18:12:53 +0000806 }
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000807 if (bytesrequested < 0) {
Guido van Rossumcada2931998-12-11 20:44:56 +0000808 buffersize = new_buffersize(f, buffersize);
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000809 if (_PyString_Resize(&v, buffersize) < 0)
Guido van Rossumce5ba841991-03-06 13:06:18 +0000810 return NULL;
Gustavo Niemeyer786ddb22002-12-16 18:12:53 +0000811 } else {
Gustavo Niemeyera080be82002-12-17 17:48:00 +0000812 /* Got what was requested. */
Gustavo Niemeyer786ddb22002-12-16 18:12:53 +0000813 break;
Guido van Rossumce5ba841991-03-06 13:06:18 +0000814 }
815 }
Guido van Rossum5449b6e1997-05-09 22:27:31 +0000816 if (bytesread != buffersize)
817 _PyString_Resize(&v, bytesread);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000818 return v;
819}
820
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000821static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +0000822file_readinto(PyFileObject *f, PyObject *args)
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000823{
824 char *ptr;
Guido van Rossum00ebd462001-10-23 21:25:24 +0000825 int ntodo;
826 size_t ndone, nnow;
Tim Peters86821b22001-01-07 21:19:34 +0000827
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000828 if (f->f_fp == NULL)
829 return err_closed();
Neal Norwitz62f5a9d2002-04-01 00:09:00 +0000830 if (!PyArg_ParseTuple(args, "w#", &ptr, &ntodo))
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000831 return NULL;
832 ndone = 0;
Guido van Rossum6263d541997-05-10 22:07:25 +0000833 while (ntodo > 0) {
834 Py_BEGIN_ALLOW_THREADS
835 errno = 0;
Jeremy Hylton8b735422002-08-14 21:01:41 +0000836 nnow = Py_UniversalNewlineFread(ptr+ndone, ntodo, f->f_fp,
837 (PyObject *)f);
Guido van Rossum6263d541997-05-10 22:07:25 +0000838 Py_END_ALLOW_THREADS
839 if (nnow == 0) {
840 if (!ferror(f->f_fp))
841 break;
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000842 PyErr_SetFromErrno(PyExc_IOError);
843 clearerr(f->f_fp);
844 return NULL;
845 }
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000846 ndone += nnow;
847 ntodo -= nnow;
848 }
Trent Mickf29f47b2000-08-11 19:02:59 +0000849 return PyInt_FromLong((long)ndone);
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000850}
851
Tim Peters86821b22001-01-07 21:19:34 +0000852/**************************************************************************
Tim Petersf29b64d2001-01-15 06:33:19 +0000853Routine to get next line using platform fgets().
Tim Peters86821b22001-01-07 21:19:34 +0000854
855Under MSVC 6:
856
Tim Peters1c733232001-01-08 04:02:07 +0000857+ MS threadsafe getc is very slow (multiple layers of function calls before+
858 after each character, to lock+unlock the stream).
859+ The stream-locking functions are MS-internal -- can't access them from user
860 code.
861+ There's nothing Tim could find in the MS C or platform SDK libraries that
862 can worm around this.
Tim Peters86821b22001-01-07 21:19:34 +0000863+ MS fgets locks/unlocks only once per line; it's the only hook we have.
864
865So we use fgets for speed(!), despite that it's painful.
866
867MS realloc is also slow.
868
Tim Petersf29b64d2001-01-15 06:33:19 +0000869Reports from other platforms on this method vs getc_unlocked (which MS doesn't
870have):
871 Linux a wash
872 Solaris a wash
873 Tru64 Unix getline_via_fgets significantly faster
Tim Peters86821b22001-01-07 21:19:34 +0000874
Tim Petersf29b64d2001-01-15 06:33:19 +0000875CAUTION: The C std isn't clear about this: in those cases where fgets
876writes something into the buffer, can it write into any position beyond the
877required trailing null byte? MSVC 6 fgets does not, and no platform is (yet)
878known on which it does; and it would be a strange way to code fgets. Still,
879getline_via_fgets may not work correctly if it does. The std test
880test_bufio.py should fail if platform fgets() routinely writes beyond the
881trailing null byte. #define DONT_USE_FGETS_IN_GETLINE to disable this code.
Tim Peters86821b22001-01-07 21:19:34 +0000882**************************************************************************/
883
Tim Petersf29b64d2001-01-15 06:33:19 +0000884/* Use this routine if told to, or by default on non-get_unlocked()
885 * platforms unless told not to. Yikes! Let's spell that out:
886 * On a platform with getc_unlocked():
887 * By default, use getc_unlocked().
888 * If you want to use fgets() instead, #define USE_FGETS_IN_GETLINE.
889 * On a platform without getc_unlocked():
890 * By default, use fgets().
891 * If you don't want to use fgets(), #define DONT_USE_FGETS_IN_GETLINE.
892 */
893#if !defined(USE_FGETS_IN_GETLINE) && !defined(HAVE_GETC_UNLOCKED)
894#define USE_FGETS_IN_GETLINE
Tim Peters86821b22001-01-07 21:19:34 +0000895#endif
896
Tim Petersf29b64d2001-01-15 06:33:19 +0000897#if defined(DONT_USE_FGETS_IN_GETLINE) && defined(USE_FGETS_IN_GETLINE)
898#undef USE_FGETS_IN_GETLINE
899#endif
900
901#ifdef USE_FGETS_IN_GETLINE
Tim Peters86821b22001-01-07 21:19:34 +0000902static PyObject*
Tim Petersf29b64d2001-01-15 06:33:19 +0000903getline_via_fgets(FILE *fp)
Tim Peters86821b22001-01-07 21:19:34 +0000904{
Tim Peters15b83852001-01-08 00:53:12 +0000905/* INITBUFSIZE is the maximum line length that lets us get away with the fast
Tim Peters142297a2001-01-15 10:36:56 +0000906 * no-realloc, one-fgets()-call path. Boosting it isn't free, because we have
907 * to fill this much of the buffer with a known value in order to figure out
908 * how much of the buffer fgets() overwrites. So if INITBUFSIZE is larger
909 * than "most" lines, we waste time filling unused buffer slots. 100 is
910 * surely adequate for most peoples' email archives, chewing over source code,
911 * etc -- "regular old text files".
912 * MAXBUFSIZE is the maximum line length that lets us get away with the less
913 * fast (but still zippy) no-realloc, two-fgets()-call path. See above for
914 * cautions about boosting that. 300 was chosen because the worst real-life
915 * text-crunching job reported on Python-Dev was a mail-log crawler where over
916 * half the lines were 254 chars.
Tim Peters15b83852001-01-08 00:53:12 +0000917 */
Tim Peters142297a2001-01-15 10:36:56 +0000918#define INITBUFSIZE 100
919#define MAXBUFSIZE 300
Tim Peters142297a2001-01-15 10:36:56 +0000920 char* p; /* temp */
921 char buf[MAXBUFSIZE];
Tim Peters86821b22001-01-07 21:19:34 +0000922 PyObject* v; /* the string object result */
Tim Peters86821b22001-01-07 21:19:34 +0000923 char* pvfree; /* address of next free slot */
924 char* pvend; /* address one beyond last free slot */
Tim Peters142297a2001-01-15 10:36:56 +0000925 size_t nfree; /* # of free buffer slots; pvend-pvfree */
926 size_t total_v_size; /* total # of slots in buffer */
Tim Petersddea2082002-03-23 10:03:50 +0000927 size_t increment; /* amount to increment the buffer */
Tim Peters86821b22001-01-07 21:19:34 +0000928
Tim Peters15b83852001-01-08 00:53:12 +0000929 /* Optimize for normal case: avoid _PyString_Resize if at all
Tim Peters142297a2001-01-15 10:36:56 +0000930 * possible via first reading into stack buffer "buf".
Tim Peters15b83852001-01-08 00:53:12 +0000931 */
Tim Peters142297a2001-01-15 10:36:56 +0000932 total_v_size = INITBUFSIZE; /* start small and pray */
933 pvfree = buf;
934 for (;;) {
935 Py_BEGIN_ALLOW_THREADS
936 pvend = buf + total_v_size;
937 nfree = pvend - pvfree;
938 memset(pvfree, '\n', nfree);
939 p = fgets(pvfree, nfree, fp);
940 Py_END_ALLOW_THREADS
Tim Peters15b83852001-01-08 00:53:12 +0000941
Tim Peters142297a2001-01-15 10:36:56 +0000942 if (p == NULL) {
943 clearerr(fp);
944 if (PyErr_CheckSignals())
945 return NULL;
946 v = PyString_FromStringAndSize(buf, pvfree - buf);
Tim Peters86821b22001-01-07 21:19:34 +0000947 return v;
948 }
Tim Peters142297a2001-01-15 10:36:56 +0000949 /* fgets read *something* */
950 p = memchr(pvfree, '\n', nfree);
951 if (p != NULL) {
952 /* Did the \n come from fgets or from us?
953 * Since fgets stops at the first \n, and then writes
954 * \0, if it's from fgets a \0 must be next. But if
955 * that's so, it could not have come from us, since
956 * the \n's we filled the buffer with have only more
957 * \n's to the right.
958 */
959 if (p+1 < pvend && *(p+1) == '\0') {
960 /* It's from fgets: we win! In particular,
961 * we haven't done any mallocs yet, and can
962 * build the final result on the first try.
963 */
964 ++p; /* include \n from fgets */
965 }
966 else {
967 /* Must be from us: fgets didn't fill the
968 * buffer and didn't find a newline, so it
969 * must be the last and newline-free line of
970 * the file.
971 */
972 assert(p > pvfree && *(p-1) == '\0');
973 --p; /* don't include \0 from fgets */
974 }
975 v = PyString_FromStringAndSize(buf, p - buf);
976 return v;
977 }
978 /* yuck: fgets overwrote all the newlines, i.e. the entire
979 * buffer. So this line isn't over yet, or maybe it is but
980 * we're exactly at EOF. If we haven't already, try using the
981 * rest of the stack buffer.
Tim Peters86821b22001-01-07 21:19:34 +0000982 */
Tim Peters142297a2001-01-15 10:36:56 +0000983 assert(*(pvend-1) == '\0');
984 if (pvfree == buf) {
985 pvfree = pvend - 1; /* overwrite trailing null */
986 total_v_size = MAXBUFSIZE;
987 }
988 else
989 break;
Tim Peters86821b22001-01-07 21:19:34 +0000990 }
Tim Peters142297a2001-01-15 10:36:56 +0000991
992 /* The stack buffer isn't big enough; malloc a string object and read
993 * into its buffer.
Tim Peters15b83852001-01-08 00:53:12 +0000994 */
Tim Petersddea2082002-03-23 10:03:50 +0000995 total_v_size = MAXBUFSIZE << 1;
Tim Peters1c733232001-01-08 04:02:07 +0000996 v = PyString_FromStringAndSize((char*)NULL, (int)total_v_size);
Tim Peters15b83852001-01-08 00:53:12 +0000997 if (v == NULL)
998 return v;
999 /* copy over everything except the last null byte */
Tim Peters142297a2001-01-15 10:36:56 +00001000 memcpy(BUF(v), buf, MAXBUFSIZE-1);
1001 pvfree = BUF(v) + MAXBUFSIZE - 1;
Tim Peters86821b22001-01-07 21:19:34 +00001002
1003 /* Keep reading stuff into v; if it ever ends successfully, break
Tim Peters15b83852001-01-08 00:53:12 +00001004 * after setting p one beyond the end of the line. The code here is
1005 * very much like the code above, except reads into v's buffer; see
1006 * the code above for detailed comments about the logic.
Tim Peters86821b22001-01-07 21:19:34 +00001007 */
1008 for (;;) {
Tim Peters86821b22001-01-07 21:19:34 +00001009 Py_BEGIN_ALLOW_THREADS
1010 pvend = BUF(v) + total_v_size;
1011 nfree = pvend - pvfree;
1012 memset(pvfree, '\n', nfree);
1013 p = fgets(pvfree, nfree, fp);
1014 Py_END_ALLOW_THREADS
1015
1016 if (p == NULL) {
1017 clearerr(fp);
1018 if (PyErr_CheckSignals()) {
1019 Py_DECREF(v);
1020 return NULL;
1021 }
1022 p = pvfree;
1023 break;
1024 }
Tim Peters86821b22001-01-07 21:19:34 +00001025 p = memchr(pvfree, '\n', nfree);
1026 if (p != NULL) {
1027 if (p+1 < pvend && *(p+1) == '\0') {
1028 /* \n came from fgets */
1029 ++p;
1030 break;
1031 }
1032 /* \n came from us; last line of file, no newline */
1033 assert(p > pvfree && *(p-1) == '\0');
1034 --p;
1035 break;
1036 }
1037 /* expand buffer and try again */
1038 assert(*(pvend-1) == '\0');
Tim Petersddea2082002-03-23 10:03:50 +00001039 increment = total_v_size >> 2; /* mild exponential growth */
1040 total_v_size += increment;
Tim Peters86821b22001-01-07 21:19:34 +00001041 if (total_v_size > INT_MAX) {
1042 PyErr_SetString(PyExc_OverflowError,
1043 "line is longer than a Python string can hold");
1044 Py_DECREF(v);
1045 return NULL;
1046 }
1047 if (_PyString_Resize(&v, (int)total_v_size) < 0)
1048 return NULL;
1049 /* overwrite the trailing null byte */
Tim Petersddea2082002-03-23 10:03:50 +00001050 pvfree = BUF(v) + (total_v_size - increment - 1);
Tim Peters86821b22001-01-07 21:19:34 +00001051 }
1052 if (BUF(v) + total_v_size != p)
1053 _PyString_Resize(&v, p - BUF(v));
1054 return v;
1055#undef INITBUFSIZE
Tim Peters142297a2001-01-15 10:36:56 +00001056#undef MAXBUFSIZE
Tim Peters86821b22001-01-07 21:19:34 +00001057}
Tim Petersf29b64d2001-01-15 06:33:19 +00001058#endif /* ifdef USE_FGETS_IN_GETLINE */
Guido van Rossumfdf95dd1997-05-05 22:15:02 +00001059
Guido van Rossum0bd24411991-04-04 15:21:57 +00001060/* Internal routine to get a line.
1061 Size argument interpretation:
1062 > 0: max length;
Guido van Rossum86282062001-01-08 01:26:47 +00001063 <= 0: read arbitrary line
Guido van Rossumce5ba841991-03-06 13:06:18 +00001064*/
1065
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001066static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +00001067get_line(PyFileObject *f, int n)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001068{
Guido van Rossum1187aa42001-01-05 14:43:05 +00001069 FILE *fp = f->f_fp;
1070 int c;
Andrew M. Kuchling4b2b4452000-11-29 02:53:22 +00001071 char *buf, *end;
Neil Schemenauer3a204a72002-03-23 19:41:34 +00001072 size_t total_v_size; /* total # of slots in buffer */
1073 size_t used_v_size; /* # used slots in buffer */
1074 size_t increment; /* amount to increment the buffer */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001075 PyObject *v;
Jack Jansen7b8c7542002-04-14 20:12:41 +00001076#ifdef WITH_UNIVERSAL_NEWLINES
1077 int newlinetypes = f->f_newlinetypes;
1078 int skipnextlf = f->f_skipnextlf;
1079 int univ_newline = f->f_univ_newline;
1080#endif
Guido van Rossum0bd24411991-04-04 15:21:57 +00001081
Jack Jansen7b8c7542002-04-14 20:12:41 +00001082#if defined(USE_FGETS_IN_GETLINE)
1083#ifdef WITH_UNIVERSAL_NEWLINES
1084 if (n <= 0 && !univ_newline )
1085#else
Guido van Rossum86282062001-01-08 01:26:47 +00001086 if (n <= 0)
Jack Jansen7b8c7542002-04-14 20:12:41 +00001087#endif
Tim Petersf29b64d2001-01-15 06:33:19 +00001088 return getline_via_fgets(fp);
Tim Peters86821b22001-01-07 21:19:34 +00001089#endif
Neil Schemenauer3a204a72002-03-23 19:41:34 +00001090 total_v_size = n > 0 ? n : 100;
1091 v = PyString_FromStringAndSize((char *)NULL, total_v_size);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001092 if (v == NULL)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001093 return NULL;
Guido van Rossumce5ba841991-03-06 13:06:18 +00001094 buf = BUF(v);
Neil Schemenauer3a204a72002-03-23 19:41:34 +00001095 end = buf + total_v_size;
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001096
Guido van Rossumce5ba841991-03-06 13:06:18 +00001097 for (;;) {
Guido van Rossum1187aa42001-01-05 14:43:05 +00001098 Py_BEGIN_ALLOW_THREADS
1099 FLOCKFILE(fp);
Jack Jansen7b8c7542002-04-14 20:12:41 +00001100#ifdef WITH_UNIVERSAL_NEWLINES
1101 if (univ_newline) {
1102 c = 'x'; /* Shut up gcc warning */
1103 while ( buf != end && (c = GETC(fp)) != EOF ) {
1104 if (skipnextlf ) {
1105 skipnextlf = 0;
1106 if (c == '\n') {
Jeremy Hylton8b735422002-08-14 21:01:41 +00001107 /* Seeing a \n here with
1108 * skipnextlf true means we
1109 * saw a \r before.
1110 */
Jack Jansen7b8c7542002-04-14 20:12:41 +00001111 newlinetypes |= NEWLINE_CRLF;
1112 c = GETC(fp);
1113 if (c == EOF) break;
1114 } else {
1115 newlinetypes |= NEWLINE_CR;
1116 }
1117 }
1118 if (c == '\r') {
1119 skipnextlf = 1;
1120 c = '\n';
1121 } else if ( c == '\n')
1122 newlinetypes |= NEWLINE_LF;
1123 *buf++ = c;
1124 if (c == '\n') break;
1125 }
1126 if ( c == EOF && skipnextlf )
1127 newlinetypes |= NEWLINE_CR;
1128 } else /* If not universal newlines use the normal loop */
1129#endif
Guido van Rossum1187aa42001-01-05 14:43:05 +00001130 while ((c = GETC(fp)) != EOF &&
1131 (*buf++ = c) != '\n' &&
1132 buf != end)
1133 ;
1134 FUNLOCKFILE(fp);
1135 Py_END_ALLOW_THREADS
Jack Jansen7b8c7542002-04-14 20:12:41 +00001136#ifdef WITH_UNIVERSAL_NEWLINES
1137 f->f_newlinetypes = newlinetypes;
1138 f->f_skipnextlf = skipnextlf;
1139#endif
Guido van Rossum1187aa42001-01-05 14:43:05 +00001140 if (c == '\n')
1141 break;
1142 if (c == EOF) {
Guido van Rossum29206bc2001-08-09 18:14:59 +00001143 if (ferror(fp)) {
1144 PyErr_SetFromErrno(PyExc_IOError);
1145 clearerr(fp);
1146 Py_DECREF(v);
1147 return NULL;
1148 }
Guido van Rossum76ad8ed1991-06-03 10:54:55 +00001149 clearerr(fp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001150 if (PyErr_CheckSignals()) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001151 Py_DECREF(v);
Guido van Rossum0bd24411991-04-04 15:21:57 +00001152 return NULL;
1153 }
Guido van Rossumce5ba841991-03-06 13:06:18 +00001154 break;
Guido van Rossum0bd24411991-04-04 15:21:57 +00001155 }
Guido van Rossum1187aa42001-01-05 14:43:05 +00001156 /* Must be because buf == end */
1157 if (n > 0)
Guido van Rossum0bd24411991-04-04 15:21:57 +00001158 break;
Neil Schemenauer3a204a72002-03-23 19:41:34 +00001159 used_v_size = total_v_size;
1160 increment = total_v_size >> 2; /* mild exponential growth */
1161 total_v_size += increment;
1162 if (total_v_size > INT_MAX) {
Guido van Rossum1187aa42001-01-05 14:43:05 +00001163 PyErr_SetString(PyExc_OverflowError,
1164 "line is longer than a Python string can hold");
Tim Peters86821b22001-01-07 21:19:34 +00001165 Py_DECREF(v);
Guido van Rossum1187aa42001-01-05 14:43:05 +00001166 return NULL;
Guido van Rossum0bd24411991-04-04 15:21:57 +00001167 }
Neil Schemenauer3a204a72002-03-23 19:41:34 +00001168 if (_PyString_Resize(&v, total_v_size) < 0)
Guido van Rossum1187aa42001-01-05 14:43:05 +00001169 return NULL;
Neil Schemenauer3a204a72002-03-23 19:41:34 +00001170 buf = BUF(v) + used_v_size;
1171 end = BUF(v) + total_v_size;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001172 }
Guido van Rossum1984f1e1992-08-04 12:41:02 +00001173
Neil Schemenauer3a204a72002-03-23 19:41:34 +00001174 used_v_size = buf - BUF(v);
1175 if (used_v_size != total_v_size)
1176 _PyString_Resize(&v, used_v_size);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001177 return v;
1178}
1179
Guido van Rossum0bd24411991-04-04 15:21:57 +00001180/* External C interface */
1181
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001182PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +00001183PyFile_GetLine(PyObject *f, int n)
Guido van Rossum0bd24411991-04-04 15:21:57 +00001184{
Guido van Rossum4ddf0a02001-01-07 20:51:39 +00001185 PyObject *result;
1186
Guido van Rossum3165fe61992-09-25 21:59:05 +00001187 if (f == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001188 PyErr_BadInternalCall();
Guido van Rossum0bd24411991-04-04 15:21:57 +00001189 return NULL;
1190 }
Guido van Rossum4ddf0a02001-01-07 20:51:39 +00001191
1192 if (PyFile_Check(f)) {
1193 if (((PyFileObject*)f)->f_fp == NULL)
1194 return err_closed();
1195 result = get_line((PyFileObject *)f, n);
1196 }
1197 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001198 PyObject *reader;
1199 PyObject *args;
Guido van Rossum4ddf0a02001-01-07 20:51:39 +00001200
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001201 reader = PyObject_GetAttrString(f, "readline");
Guido van Rossum3165fe61992-09-25 21:59:05 +00001202 if (reader == NULL)
1203 return NULL;
1204 if (n <= 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001205 args = Py_BuildValue("()");
Guido van Rossum3165fe61992-09-25 21:59:05 +00001206 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001207 args = Py_BuildValue("(i)", n);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001208 if (args == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001209 Py_DECREF(reader);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001210 return NULL;
1211 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001212 result = PyEval_CallObject(reader, args);
1213 Py_DECREF(reader);
1214 Py_DECREF(args);
1215 if (result != NULL && !PyString_Check(result)) {
1216 Py_DECREF(result);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001217 result = NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001218 PyErr_SetString(PyExc_TypeError,
Guido van Rossum3165fe61992-09-25 21:59:05 +00001219 "object.readline() returned non-string");
1220 }
Guido van Rossum4ddf0a02001-01-07 20:51:39 +00001221 }
1222
1223 if (n < 0 && result != NULL && PyString_Check(result)) {
1224 char *s = PyString_AS_STRING(result);
1225 int len = PyString_GET_SIZE(result);
1226 if (len == 0) {
1227 Py_DECREF(result);
1228 result = NULL;
1229 PyErr_SetString(PyExc_EOFError,
1230 "EOF when reading a line");
1231 }
1232 else if (s[len-1] == '\n') {
1233 if (result->ob_refcnt == 1)
1234 _PyString_Resize(&result, len-1);
1235 else {
1236 PyObject *v;
1237 v = PyString_FromStringAndSize(s, len-1);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001238 Py_DECREF(result);
Guido van Rossum4ddf0a02001-01-07 20:51:39 +00001239 result = v;
Guido van Rossum3165fe61992-09-25 21:59:05 +00001240 }
1241 }
Guido van Rossum3165fe61992-09-25 21:59:05 +00001242 }
Guido van Rossum4ddf0a02001-01-07 20:51:39 +00001243 return result;
Guido van Rossum0bd24411991-04-04 15:21:57 +00001244}
1245
1246/* Python method */
1247
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001248static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +00001249file_readline(PyFileObject *f, PyObject *args)
Guido van Rossum0bd24411991-04-04 15:21:57 +00001250{
Guido van Rossum789a1611997-05-10 22:33:55 +00001251 int n = -1;
Guido van Rossum0bd24411991-04-04 15:21:57 +00001252
Guido van Rossumd7297e61992-07-06 14:19:26 +00001253 if (f->f_fp == NULL)
1254 return err_closed();
Guido van Rossum43713e52000-02-29 13:59:29 +00001255 if (!PyArg_ParseTuple(args, "|i:readline", &n))
Guido van Rossum789a1611997-05-10 22:33:55 +00001256 return NULL;
1257 if (n == 0)
1258 return PyString_FromString("");
1259 if (n < 0)
1260 n = 0;
Marc-André Lemburg1f468602000-07-05 15:32:40 +00001261 return get_line(f, n);
Guido van Rossum0bd24411991-04-04 15:21:57 +00001262}
1263
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001264static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +00001265file_readlines(PyFileObject *f, PyObject *args)
Guido van Rossumce5ba841991-03-06 13:06:18 +00001266{
Guido van Rossum789a1611997-05-10 22:33:55 +00001267 long sizehint = 0;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001268 PyObject *list;
1269 PyObject *line;
Guido van Rossum6263d541997-05-10 22:07:25 +00001270 char small_buffer[SMALLCHUNK];
1271 char *buffer = small_buffer;
1272 size_t buffersize = SMALLCHUNK;
1273 PyObject *big_buffer = NULL;
1274 size_t nfilled = 0;
1275 size_t nread;
Guido van Rossum789a1611997-05-10 22:33:55 +00001276 size_t totalread = 0;
Guido van Rossum6263d541997-05-10 22:07:25 +00001277 char *p, *q, *end;
1278 int err;
Guido van Rossum79fd0fc2001-10-12 20:01:53 +00001279 int shortread = 0;
Guido van Rossum0bd24411991-04-04 15:21:57 +00001280
Guido van Rossumd7297e61992-07-06 14:19:26 +00001281 if (f->f_fp == NULL)
1282 return err_closed();
Guido van Rossum43713e52000-02-29 13:59:29 +00001283 if (!PyArg_ParseTuple(args, "|l:readlines", &sizehint))
Guido van Rossum0bd24411991-04-04 15:21:57 +00001284 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001285 if ((list = PyList_New(0)) == NULL)
Guido van Rossumce5ba841991-03-06 13:06:18 +00001286 return NULL;
1287 for (;;) {
Guido van Rossum79fd0fc2001-10-12 20:01:53 +00001288 if (shortread)
1289 nread = 0;
1290 else {
1291 Py_BEGIN_ALLOW_THREADS
1292 errno = 0;
Tim Peters058b1412002-04-21 07:29:14 +00001293 nread = Py_UniversalNewlineFread(buffer+nfilled,
Jack Jansen7b8c7542002-04-14 20:12:41 +00001294 buffersize-nfilled, f->f_fp, (PyObject *)f);
Guido van Rossum79fd0fc2001-10-12 20:01:53 +00001295 Py_END_ALLOW_THREADS
1296 shortread = (nread < buffersize-nfilled);
1297 }
Guido van Rossum6263d541997-05-10 22:07:25 +00001298 if (nread == 0) {
Guido van Rossum789a1611997-05-10 22:33:55 +00001299 sizehint = 0;
Guido van Rossum3da3fce1998-02-19 20:46:48 +00001300 if (!ferror(f->f_fp))
Guido van Rossum6263d541997-05-10 22:07:25 +00001301 break;
1302 PyErr_SetFromErrno(PyExc_IOError);
1303 clearerr(f->f_fp);
1304 error:
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001305 Py_DECREF(list);
Guido van Rossum6263d541997-05-10 22:07:25 +00001306 list = NULL;
1307 goto cleanup;
Guido van Rossumce5ba841991-03-06 13:06:18 +00001308 }
Guido van Rossum789a1611997-05-10 22:33:55 +00001309 totalread += nread;
Guido van Rossum6263d541997-05-10 22:07:25 +00001310 p = memchr(buffer+nfilled, '\n', nread);
1311 if (p == NULL) {
1312 /* Need a larger buffer to fit this line */
1313 nfilled += nread;
1314 buffersize *= 2;
Trent Mickf29f47b2000-08-11 19:02:59 +00001315 if (buffersize > INT_MAX) {
1316 PyErr_SetString(PyExc_OverflowError,
Guido van Rossume07d5cf2001-01-09 21:50:24 +00001317 "line is longer than a Python string can hold");
Trent Mickf29f47b2000-08-11 19:02:59 +00001318 goto error;
1319 }
Guido van Rossum6263d541997-05-10 22:07:25 +00001320 if (big_buffer == NULL) {
1321 /* Create the big buffer */
1322 big_buffer = PyString_FromStringAndSize(
1323 NULL, buffersize);
1324 if (big_buffer == NULL)
1325 goto error;
1326 buffer = PyString_AS_STRING(big_buffer);
1327 memcpy(buffer, small_buffer, nfilled);
1328 }
1329 else {
1330 /* Grow the big buffer */
Jack Jansen7b8c7542002-04-14 20:12:41 +00001331 if ( _PyString_Resize(&big_buffer, buffersize) < 0 )
1332 goto error;
Guido van Rossum6263d541997-05-10 22:07:25 +00001333 buffer = PyString_AS_STRING(big_buffer);
1334 }
1335 continue;
1336 }
1337 end = buffer+nfilled+nread;
1338 q = buffer;
1339 do {
1340 /* Process complete lines */
1341 p++;
1342 line = PyString_FromStringAndSize(q, p-q);
1343 if (line == NULL)
1344 goto error;
1345 err = PyList_Append(list, line);
1346 Py_DECREF(line);
1347 if (err != 0)
1348 goto error;
1349 q = p;
1350 p = memchr(q, '\n', end-q);
1351 } while (p != NULL);
1352 /* Move the remaining incomplete line to the start */
1353 nfilled = end-q;
1354 memmove(buffer, q, nfilled);
Guido van Rossum789a1611997-05-10 22:33:55 +00001355 if (sizehint > 0)
1356 if (totalread >= (size_t)sizehint)
1357 break;
Guido van Rossumce5ba841991-03-06 13:06:18 +00001358 }
Guido van Rossum6263d541997-05-10 22:07:25 +00001359 if (nfilled != 0) {
1360 /* Partial last line */
1361 line = PyString_FromStringAndSize(buffer, nfilled);
1362 if (line == NULL)
1363 goto error;
Guido van Rossum789a1611997-05-10 22:33:55 +00001364 if (sizehint > 0) {
1365 /* Need to complete the last line */
Marc-André Lemburg1f468602000-07-05 15:32:40 +00001366 PyObject *rest = get_line(f, 0);
Guido van Rossum789a1611997-05-10 22:33:55 +00001367 if (rest == NULL) {
1368 Py_DECREF(line);
1369 goto error;
1370 }
1371 PyString_Concat(&line, rest);
1372 Py_DECREF(rest);
1373 if (line == NULL)
1374 goto error;
1375 }
Guido van Rossum6263d541997-05-10 22:07:25 +00001376 err = PyList_Append(list, line);
1377 Py_DECREF(line);
1378 if (err != 0)
1379 goto error;
1380 }
1381 cleanup:
Tim Peters5de98422002-04-27 18:44:32 +00001382 Py_XDECREF(big_buffer);
Guido van Rossumce5ba841991-03-06 13:06:18 +00001383 return list;
1384}
1385
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001386static PyObject *
Fred Drakefd99de62000-07-09 05:02:18 +00001387file_write(PyFileObject *f, PyObject *args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001388{
Guido van Rossumd7297e61992-07-06 14:19:26 +00001389 char *s;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001390 int n, n2;
Guido van Rossumd7297e61992-07-06 14:19:26 +00001391 if (f->f_fp == NULL)
1392 return err_closed();
Michael W. Hudsone2ec3eb2001-10-31 18:51:01 +00001393 if (!PyArg_ParseTuple(args, f->f_binary ? "s#" : "t#", &s, &n))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001394 return NULL;
Guido van Rossumeb183da1991-04-04 10:44:06 +00001395 f->f_softspace = 0;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001396 Py_BEGIN_ALLOW_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001397 errno = 0;
Guido van Rossumd7297e61992-07-06 14:19:26 +00001398 n2 = fwrite(s, 1, n, f->f_fp);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001399 Py_END_ALLOW_THREADS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001400 if (n2 != n) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001401 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossumfebd5511992-03-04 16:39:24 +00001402 clearerr(f->f_fp);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001403 return NULL;
1404 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001405 Py_INCREF(Py_None);
1406 return Py_None;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001407}
1408
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001409static PyObject *
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001410file_writelines(PyFileObject *f, PyObject *seq)
Guido van Rossum5a2a6831993-10-25 09:59:04 +00001411{
Guido van Rossumee70ad12000-03-13 16:27:06 +00001412#define CHUNKSIZE 1000
1413 PyObject *list, *line;
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001414 PyObject *it; /* iter(seq) */
Guido van Rossumee70ad12000-03-13 16:27:06 +00001415 PyObject *result;
1416 int i, j, index, len, nwritten, islist;
1417
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001418 assert(seq != NULL);
Guido van Rossum5a2a6831993-10-25 09:59:04 +00001419 if (f->f_fp == NULL)
1420 return err_closed();
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001421
1422 result = NULL;
1423 list = NULL;
1424 islist = PyList_Check(seq);
1425 if (islist)
1426 it = NULL;
1427 else {
1428 it = PyObject_GetIter(seq);
1429 if (it == NULL) {
1430 PyErr_SetString(PyExc_TypeError,
1431 "writelines() requires an iterable argument");
1432 return NULL;
1433 }
1434 /* From here on, fail by going to error, to reclaim "it". */
1435 list = PyList_New(CHUNKSIZE);
1436 if (list == NULL)
1437 goto error;
Guido van Rossum5a2a6831993-10-25 09:59:04 +00001438 }
Guido van Rossumee70ad12000-03-13 16:27:06 +00001439
1440 /* Strategy: slurp CHUNKSIZE lines into a private list,
1441 checking that they are all strings, then write that list
1442 without holding the interpreter lock, then come back for more. */
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001443 for (index = 0; ; index += CHUNKSIZE) {
Guido van Rossumee70ad12000-03-13 16:27:06 +00001444 if (islist) {
1445 Py_XDECREF(list);
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001446 list = PyList_GetSlice(seq, index, index+CHUNKSIZE);
Guido van Rossumee70ad12000-03-13 16:27:06 +00001447 if (list == NULL)
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001448 goto error;
Guido van Rossumee70ad12000-03-13 16:27:06 +00001449 j = PyList_GET_SIZE(list);
1450 }
1451 else {
1452 for (j = 0; j < CHUNKSIZE; j++) {
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001453 line = PyIter_Next(it);
Guido van Rossumee70ad12000-03-13 16:27:06 +00001454 if (line == NULL) {
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001455 if (PyErr_Occurred())
1456 goto error;
1457 break;
Guido van Rossumee70ad12000-03-13 16:27:06 +00001458 }
Guido van Rossumee70ad12000-03-13 16:27:06 +00001459 PyList_SetItem(list, j, line);
1460 }
1461 }
1462 if (j == 0)
1463 break;
1464
Marc-André Lemburg6ef68b52000-08-25 22:39:50 +00001465 /* Check that all entries are indeed strings. If not,
1466 apply the same rules as for file.write() and
1467 convert the results to strings. This is slow, but
1468 seems to be the only way since all conversion APIs
1469 could potentially execute Python code. */
1470 for (i = 0; i < j; i++) {
1471 PyObject *v = PyList_GET_ITEM(list, i);
1472 if (!PyString_Check(v)) {
1473 const char *buffer;
1474 int len;
Tim Peters86821b22001-01-07 21:19:34 +00001475 if (((f->f_binary &&
Marc-André Lemburg6ef68b52000-08-25 22:39:50 +00001476 PyObject_AsReadBuffer(v,
1477 (const void**)&buffer,
1478 &len)) ||
1479 PyObject_AsCharBuffer(v,
1480 &buffer,
1481 &len))) {
1482 PyErr_SetString(PyExc_TypeError,
Jeremy Hylton8b735422002-08-14 21:01:41 +00001483 "writelines() argument must be a sequence of strings");
Marc-André Lemburg6ef68b52000-08-25 22:39:50 +00001484 goto error;
1485 }
1486 line = PyString_FromStringAndSize(buffer,
1487 len);
1488 if (line == NULL)
1489 goto error;
1490 Py_DECREF(v);
Marc-André Lemburgf5e96fa2000-08-25 22:49:05 +00001491 PyList_SET_ITEM(list, i, line);
Marc-André Lemburg6ef68b52000-08-25 22:39:50 +00001492 }
1493 }
1494
1495 /* Since we are releasing the global lock, the
1496 following code may *not* execute Python code. */
Guido van Rossumee70ad12000-03-13 16:27:06 +00001497 Py_BEGIN_ALLOW_THREADS
1498 f->f_softspace = 0;
1499 errno = 0;
1500 for (i = 0; i < j; i++) {
Marc-André Lemburg6ef68b52000-08-25 22:39:50 +00001501 line = PyList_GET_ITEM(list, i);
Guido van Rossumee70ad12000-03-13 16:27:06 +00001502 len = PyString_GET_SIZE(line);
1503 nwritten = fwrite(PyString_AS_STRING(line),
1504 1, len, f->f_fp);
1505 if (nwritten != len) {
1506 Py_BLOCK_THREADS
1507 PyErr_SetFromErrno(PyExc_IOError);
1508 clearerr(f->f_fp);
1509 goto error;
1510 }
1511 }
1512 Py_END_ALLOW_THREADS
1513
1514 if (j < CHUNKSIZE)
1515 break;
Guido van Rossumee70ad12000-03-13 16:27:06 +00001516 }
1517
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001518 Py_INCREF(Py_None);
Guido van Rossumee70ad12000-03-13 16:27:06 +00001519 result = Py_None;
1520 error:
1521 Py_XDECREF(list);
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001522 Py_XDECREF(it);
Guido van Rossumee70ad12000-03-13 16:27:06 +00001523 return result;
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001524#undef CHUNKSIZE
Guido van Rossum5a2a6831993-10-25 09:59:04 +00001525}
1526
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001527static PyObject *
1528file_getiter(PyFileObject *f)
1529{
1530 if (f->f_fp == NULL)
1531 return err_closed();
1532 Py_INCREF(f);
1533 return (PyObject *)f;
1534}
1535
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001536PyDoc_STRVAR(readline_doc,
Tim Petersefc3a3a2001-09-20 07:55:22 +00001537"readline([size]) -> next line from the file, as a string.\n"
1538"\n"
1539"Retain newline. A non-negative size argument limits the maximum\n"
1540"number of bytes to return (an incomplete line may be returned then).\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001541"Return an empty string at EOF.");
Tim Petersefc3a3a2001-09-20 07:55:22 +00001542
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001543PyDoc_STRVAR(read_doc,
Tim Petersefc3a3a2001-09-20 07:55:22 +00001544"read([size]) -> read at most size bytes, returned as a string.\n"
1545"\n"
Gustavo Niemeyer786ddb22002-12-16 18:12:53 +00001546"If the size argument is negative or omitted, read until EOF is reached.\n"
1547"Notice that when in non-blocking mode, less data than what was requested\n"
1548"may be returned, even if no size parameter was given.");
Tim Petersefc3a3a2001-09-20 07:55:22 +00001549
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001550PyDoc_STRVAR(write_doc,
Tim Petersefc3a3a2001-09-20 07:55:22 +00001551"write(str) -> None. Write string str to file.\n"
1552"\n"
1553"Note that due to buffering, flush() or close() may be needed before\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001554"the file on disk reflects the data written.");
Tim Petersefc3a3a2001-09-20 07:55:22 +00001555
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001556PyDoc_STRVAR(fileno_doc,
Tim Petersefc3a3a2001-09-20 07:55:22 +00001557"fileno() -> integer \"file descriptor\".\n"
1558"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001559"This is needed for lower-level file interfaces, such os.read().");
Tim Petersefc3a3a2001-09-20 07:55:22 +00001560
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001561PyDoc_STRVAR(seek_doc,
Tim Petersefc3a3a2001-09-20 07:55:22 +00001562"seek(offset[, whence]) -> None. Move to new file position.\n"
1563"\n"
1564"Argument offset is a byte count. Optional argument whence defaults to\n"
1565"0 (offset from start of file, offset should be >= 0); other values are 1\n"
1566"(move relative to current position, positive or negative), and 2 (move\n"
1567"relative to end of file, usually negative, although many platforms allow\n"
1568"seeking beyond the end of a file).\n"
1569"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001570"Note that not all file objects are seekable.");
Tim Petersefc3a3a2001-09-20 07:55:22 +00001571
Guido van Rossumd7047b31995-01-02 19:07:15 +00001572#ifdef HAVE_FTRUNCATE
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001573PyDoc_STRVAR(truncate_doc,
Tim Petersefc3a3a2001-09-20 07:55:22 +00001574"truncate([size]) -> None. Truncate the file to at most size bytes.\n"
1575"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001576"Size defaults to the current file position, as returned by tell().");
Guido van Rossumd7047b31995-01-02 19:07:15 +00001577#endif
Tim Petersefc3a3a2001-09-20 07:55:22 +00001578
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001579PyDoc_STRVAR(tell_doc,
1580"tell() -> current file position, an integer (may be a long integer).");
Tim Petersefc3a3a2001-09-20 07:55:22 +00001581
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001582PyDoc_STRVAR(readinto_doc,
1583"readinto() -> Undocumented. Don't use this; it may go away.");
Tim Petersefc3a3a2001-09-20 07:55:22 +00001584
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001585PyDoc_STRVAR(readlines_doc,
Tim Petersefc3a3a2001-09-20 07:55:22 +00001586"readlines([size]) -> list of strings, each a line from the file.\n"
1587"\n"
1588"Call readline() repeatedly and return a list of the lines so read.\n"
1589"The optional size argument, if given, is an approximate bound on the\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001590"total number of bytes in the lines returned.");
Tim Petersefc3a3a2001-09-20 07:55:22 +00001591
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001592PyDoc_STRVAR(xreadlines_doc,
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001593"xreadlines() -> returns self.\n"
Tim Petersefc3a3a2001-09-20 07:55:22 +00001594"\n"
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001595"For backward compatibility. File objects now include the performance\n"
1596"optimizations previously implemented in the xreadlines module.");
Tim Petersefc3a3a2001-09-20 07:55:22 +00001597
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001598PyDoc_STRVAR(writelines_doc,
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001599"writelines(sequence_of_strings) -> None. Write the strings to the file.\n"
Tim Petersefc3a3a2001-09-20 07:55:22 +00001600"\n"
Tim Peters2c9aa5e2001-09-23 04:06:05 +00001601"Note that newlines are not added. The sequence can be any iterable object\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001602"producing strings. This is equivalent to calling write() for each string.");
Tim Petersefc3a3a2001-09-20 07:55:22 +00001603
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001604PyDoc_STRVAR(flush_doc,
1605"flush() -> None. Flush the internal I/O buffer.");
Tim Petersefc3a3a2001-09-20 07:55:22 +00001606
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001607PyDoc_STRVAR(close_doc,
Tim Petersefc3a3a2001-09-20 07:55:22 +00001608"close() -> None or (perhaps) an integer. Close the file.\n"
1609"\n"
Guido van Rossum77f6a652002-04-03 22:41:51 +00001610"Sets data attribute .closed to True. A closed file cannot be used for\n"
Tim Petersefc3a3a2001-09-20 07:55:22 +00001611"further I/O operations. close() may be called more than once without\n"
1612"error. Some kinds of file objects (for example, opened by popen())\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001613"may return an exit status upon closing.");
Tim Petersefc3a3a2001-09-20 07:55:22 +00001614
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001615PyDoc_STRVAR(isatty_doc,
1616"isatty() -> true or false. True if the file is connected to a tty device.");
Tim Petersefc3a3a2001-09-20 07:55:22 +00001617
1618static PyMethodDef file_methods[] = {
Jeremy Hylton8b735422002-08-14 21:01:41 +00001619 {"readline", (PyCFunction)file_readline, METH_VARARGS, readline_doc},
1620 {"read", (PyCFunction)file_read, METH_VARARGS, read_doc},
1621 {"write", (PyCFunction)file_write, METH_VARARGS, write_doc},
1622 {"fileno", (PyCFunction)file_fileno, METH_NOARGS, fileno_doc},
1623 {"seek", (PyCFunction)file_seek, METH_VARARGS, seek_doc},
Tim Petersefc3a3a2001-09-20 07:55:22 +00001624#ifdef HAVE_FTRUNCATE
Jeremy Hylton8b735422002-08-14 21:01:41 +00001625 {"truncate", (PyCFunction)file_truncate, METH_VARARGS, truncate_doc},
Tim Petersefc3a3a2001-09-20 07:55:22 +00001626#endif
Jeremy Hylton8b735422002-08-14 21:01:41 +00001627 {"tell", (PyCFunction)file_tell, METH_NOARGS, tell_doc},
1628 {"readinto", (PyCFunction)file_readinto, METH_VARARGS, readinto_doc},
1629 {"readlines", (PyCFunction)file_readlines,METH_VARARGS, readlines_doc},
1630 {"xreadlines",(PyCFunction)file_getiter, METH_NOARGS, xreadlines_doc},
1631 {"writelines",(PyCFunction)file_writelines, METH_O, writelines_doc},
1632 {"flush", (PyCFunction)file_flush, METH_NOARGS, flush_doc},
1633 {"close", (PyCFunction)file_close, METH_NOARGS, close_doc},
1634 {"isatty", (PyCFunction)file_isatty, METH_NOARGS, isatty_doc},
1635 {NULL, NULL} /* sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001636};
1637
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001638#define OFF(x) offsetof(PyFileObject, x)
Guido van Rossumb6775db1994-08-01 11:34:53 +00001639
Guido van Rossum6f799372001-09-20 20:46:19 +00001640static PyMemberDef file_memberlist[] = {
1641 {"softspace", T_INT, OFF(f_softspace), 0,
1642 "flag indicating that a space needs to be printed; used by print"},
1643 {"mode", T_OBJECT, OFF(f_mode), RO,
Martin v. Löwis6233c9b2002-12-11 13:06:53 +00001644 "file mode ('r', 'U', 'w', 'a', possibly with 'b' or '+' added)"},
Guido van Rossum6f799372001-09-20 20:46:19 +00001645 {"name", T_OBJECT, OFF(f_name), RO,
1646 "file name"},
Guido van Rossumb6775db1994-08-01 11:34:53 +00001647 /* getattr(f, "closed") is implemented without this table */
Guido van Rossumb6775db1994-08-01 11:34:53 +00001648 {NULL} /* Sentinel */
1649};
1650
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001651static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00001652get_closed(PyFileObject *f, void *closure)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001653{
Guido van Rossum77f6a652002-04-03 22:41:51 +00001654 return PyBool_FromLong((long)(f->f_fp == 0));
Guido van Rossumb6775db1994-08-01 11:34:53 +00001655}
Jack Jansen7b8c7542002-04-14 20:12:41 +00001656#ifdef WITH_UNIVERSAL_NEWLINES
1657static PyObject *
1658get_newlines(PyFileObject *f, void *closure)
1659{
1660 switch (f->f_newlinetypes) {
1661 case NEWLINE_UNKNOWN:
1662 Py_INCREF(Py_None);
1663 return Py_None;
1664 case NEWLINE_CR:
1665 return PyString_FromString("\r");
1666 case NEWLINE_LF:
1667 return PyString_FromString("\n");
1668 case NEWLINE_CR|NEWLINE_LF:
1669 return Py_BuildValue("(ss)", "\r", "\n");
1670 case NEWLINE_CRLF:
1671 return PyString_FromString("\r\n");
1672 case NEWLINE_CR|NEWLINE_CRLF:
1673 return Py_BuildValue("(ss)", "\r", "\r\n");
1674 case NEWLINE_LF|NEWLINE_CRLF:
1675 return Py_BuildValue("(ss)", "\n", "\r\n");
1676 case NEWLINE_CR|NEWLINE_LF|NEWLINE_CRLF:
1677 return Py_BuildValue("(sss)", "\r", "\n", "\r\n");
1678 default:
Jeremy Hylton8b735422002-08-14 21:01:41 +00001679 PyErr_Format(PyExc_SystemError,
1680 "Unknown newlines value 0x%x\n",
1681 f->f_newlinetypes);
Jack Jansen7b8c7542002-04-14 20:12:41 +00001682 return NULL;
1683 }
1684}
1685#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +00001686
Guido van Rossum32d34c82001-09-20 21:45:26 +00001687static PyGetSetDef file_getsetlist[] = {
Guido van Rossum77f6a652002-04-03 22:41:51 +00001688 {"closed", (getter)get_closed, NULL, "True if the file is closed"},
Jack Jansen7b8c7542002-04-14 20:12:41 +00001689#ifdef WITH_UNIVERSAL_NEWLINES
Jeremy Hylton8b735422002-08-14 21:01:41 +00001690 {"newlines", (getter)get_newlines, NULL,
1691 "end-of-line convention used in this file"},
Jack Jansen7b8c7542002-04-14 20:12:41 +00001692#endif
Tim Peters6d6c1a32001-08-02 04:15:00 +00001693 {0},
1694};
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001695
Neal Norwitzd8b995f2002-08-06 21:50:54 +00001696static void
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001697drop_readahead(PyFileObject *f)
Guido van Rossum65967252001-04-21 13:20:18 +00001698{
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001699 if (f->f_buf != NULL) {
1700 PyMem_Free(f->f_buf);
1701 f->f_buf = NULL;
1702 }
Guido van Rossum65967252001-04-21 13:20:18 +00001703}
1704
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001705/* Make sure that file has a readahead buffer with at least one byte
1706 (unless at EOF) and no more than bufsize. Returns negative value on
1707 error */
Neal Norwitzd8b995f2002-08-06 21:50:54 +00001708static int
1709readahead(PyFileObject *f, int bufsize)
1710{
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001711 int chunksize;
1712
1713 if (f->f_buf != NULL) {
1714 if( (f->f_bufend - f->f_bufptr) >= 1)
1715 return 0;
1716 else
1717 drop_readahead(f);
1718 }
1719 if ((f->f_buf = PyMem_Malloc(bufsize)) == NULL) {
1720 return -1;
1721 }
1722 Py_BEGIN_ALLOW_THREADS
1723 errno = 0;
1724 chunksize = Py_UniversalNewlineFread(
1725 f->f_buf, bufsize, f->f_fp, (PyObject *)f);
1726 Py_END_ALLOW_THREADS
1727 if (chunksize == 0) {
1728 if (ferror(f->f_fp)) {
1729 PyErr_SetFromErrno(PyExc_IOError);
1730 clearerr(f->f_fp);
1731 drop_readahead(f);
1732 return -1;
1733 }
1734 }
1735 f->f_bufptr = f->f_buf;
1736 f->f_bufend = f->f_buf + chunksize;
1737 return 0;
1738}
1739
1740/* Used by file_iternext. The returned string will start with 'skip'
1741 uninitialized bytes followed by the remainder of the line. Don't be
1742 horrified by the recursive call: maximum recursion depth is limited by
1743 logarithmic buffer growth to about 50 even when reading a 1gb line. */
1744
Neal Norwitzd8b995f2002-08-06 21:50:54 +00001745static PyStringObject *
1746readahead_get_line_skip(PyFileObject *f, int skip, int bufsize)
1747{
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001748 PyStringObject* s;
1749 char *bufptr;
1750 char *buf;
1751 int len;
1752
1753 if (f->f_buf == NULL)
1754 if (readahead(f, bufsize) < 0)
1755 return NULL;
1756
1757 len = f->f_bufend - f->f_bufptr;
1758 if (len == 0)
1759 return (PyStringObject *)
1760 PyString_FromStringAndSize(NULL, skip);
1761 bufptr = memchr(f->f_bufptr, '\n', len);
1762 if (bufptr != NULL) {
1763 bufptr++; /* Count the '\n' */
1764 len = bufptr - f->f_bufptr;
1765 s = (PyStringObject *)
1766 PyString_FromStringAndSize(NULL, skip+len);
1767 if (s == NULL)
1768 return NULL;
1769 memcpy(PyString_AS_STRING(s)+skip, f->f_bufptr, len);
1770 f->f_bufptr = bufptr;
1771 if (bufptr == f->f_bufend)
1772 drop_readahead(f);
1773 } else {
1774 bufptr = f->f_bufptr;
1775 buf = f->f_buf;
1776 f->f_buf = NULL; /* Force new readahead buffer */
1777 s = readahead_get_line_skip(
1778 f, skip+len, bufsize + (bufsize>>2) );
1779 if (s == NULL) {
1780 PyMem_Free(buf);
1781 return NULL;
1782 }
1783 memcpy(PyString_AS_STRING(s)+skip, bufptr, len);
1784 PyMem_Free(buf);
1785 }
1786 return s;
1787}
1788
1789/* A larger buffer size may actually decrease performance. */
1790#define READAHEAD_BUFSIZE 8192
1791
1792static PyObject *
1793file_iternext(PyFileObject *f)
1794{
1795 PyStringObject* l;
1796
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001797 if (f->f_fp == NULL)
1798 return err_closed();
1799
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001800 l = readahead_get_line_skip(f, 0, READAHEAD_BUFSIZE);
1801 if (l == NULL || PyString_GET_SIZE(l) == 0) {
1802 Py_XDECREF(l);
1803 return NULL;
1804 }
1805 return (PyObject *)l;
1806}
1807
1808
Tim Peters59c9a642001-09-13 05:38:56 +00001809static PyObject *
1810file_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1811{
Tim Peters44410012001-09-14 03:26:08 +00001812 PyObject *self;
1813 static PyObject *not_yet_string;
1814
1815 assert(type != NULL && type->tp_alloc != NULL);
1816
1817 if (not_yet_string == NULL) {
1818 not_yet_string = PyString_FromString("<uninitialized file>");
1819 if (not_yet_string == NULL)
1820 return NULL;
1821 }
1822
1823 self = type->tp_alloc(type, 0);
1824 if (self != NULL) {
1825 /* Always fill in the name and mode, so that nobody else
1826 needs to special-case NULLs there. */
1827 Py_INCREF(not_yet_string);
1828 ((PyFileObject *)self)->f_name = not_yet_string;
1829 Py_INCREF(not_yet_string);
1830 ((PyFileObject *)self)->f_mode = not_yet_string;
1831 }
1832 return self;
1833}
1834
1835static int
1836file_init(PyObject *self, PyObject *args, PyObject *kwds)
1837{
1838 PyFileObject *foself = (PyFileObject *)self;
1839 int ret = 0;
Tim Peters59c9a642001-09-13 05:38:56 +00001840 static char *kwlist[] = {"name", "mode", "buffering", 0};
1841 char *name = NULL;
1842 char *mode = "r";
1843 int bufsize = -1;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001844 int wideargument = 0;
Tim Peters44410012001-09-14 03:26:08 +00001845
1846 assert(PyFile_Check(self));
1847 if (foself->f_fp != NULL) {
1848 /* Have to close the existing file first. */
1849 PyObject *closeresult = file_close(foself);
1850 if (closeresult == NULL)
1851 return -1;
1852 Py_DECREF(closeresult);
1853 }
Tim Peters59c9a642001-09-13 05:38:56 +00001854
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001855#ifdef Py_WIN_WIDE_FILENAMES
1856 if (GetVersion() < 0x80000000) { /* On NT, so wide API available */
1857 PyObject *po;
1858 if (PyArg_ParseTupleAndKeywords(args, kwds, "U|si:file",
1859 kwlist, &po, &mode, &bufsize)) {
1860 wideargument = 1;
1861 if (fill_file_fields(foself, NULL, name, mode,
1862 fclose, po) == NULL)
1863 goto Error;
1864 } else {
1865 /* Drop the argument parsing error as narrow
1866 strings are also valid. */
1867 PyErr_Clear();
1868 }
1869 }
1870#endif
1871
1872 if (!wideargument) {
1873 if (!PyArg_ParseTupleAndKeywords(args, kwds, "et|si:file", kwlist,
1874 Py_FileSystemDefaultEncoding,
1875 &name,
1876 &mode, &bufsize))
1877 return -1;
1878 if (fill_file_fields(foself, NULL, name, mode,
1879 fclose, NULL) == NULL)
1880 goto Error;
1881 }
Tim Peters44410012001-09-14 03:26:08 +00001882 if (open_the_file(foself, name, mode) == NULL)
1883 goto Error;
1884 PyFile_SetBufSize(self, bufsize);
1885 goto Done;
1886
1887Error:
1888 ret = -1;
1889 /* fall through */
1890Done:
Tim Peters59c9a642001-09-13 05:38:56 +00001891 PyMem_Free(name); /* free the encoded string */
Tim Peters44410012001-09-14 03:26:08 +00001892 return ret;
Tim Peters59c9a642001-09-13 05:38:56 +00001893}
1894
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001895PyDoc_VAR(file_doc) =
1896PyDoc_STR(
Tim Peters59c9a642001-09-13 05:38:56 +00001897"file(name[, mode[, buffering]]) -> file object\n"
1898"\n"
1899"Open a file. The mode can be 'r', 'w' or 'a' for reading (default),\n"
1900"writing or appending. The file will be created if it doesn't exist\n"
1901"when opened for writing or appending; it will be truncated when\n"
1902"opened for writing. Add a 'b' to the mode for binary files.\n"
1903"Add a '+' to the mode to allow simultaneous reading and writing.\n"
1904"If the buffering argument is given, 0 means unbuffered, 1 means line\n"
Tim Peters742dfd62001-09-13 21:49:44 +00001905"buffered, and larger numbers specify the buffer size.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001906)
Barry Warsaw4be55b52002-05-22 20:37:53 +00001907#ifdef WITH_UNIVERSAL_NEWLINES
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001908PyDoc_STR(
Barry Warsaw4be55b52002-05-22 20:37:53 +00001909"Add a 'U' to mode to open the file for input with universal newline\n"
1910"support. Any line ending in the input file will be seen as a '\\n'\n"
1911"in Python. Also, a file so opened gains the attribute 'newlines';\n"
1912"the value for this attribute is one of None (no newline read yet),\n"
1913"'\\r', '\\n', '\\r\\n' or a tuple containing all the newline types seen.\n"
1914"\n"
1915"'U' cannot be combined with 'w' or '+' mode.\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001916)
Barry Warsaw4be55b52002-05-22 20:37:53 +00001917#endif /* WITH_UNIVERSAL_NEWLINES */
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001918PyDoc_STR(
Barry Warsaw4be55b52002-05-22 20:37:53 +00001919"\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001920"Note: open() is an alias for file()."
1921);
Tim Peters59c9a642001-09-13 05:38:56 +00001922
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001923PyTypeObject PyFile_Type = {
1924 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001925 0,
1926 "file",
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001927 sizeof(PyFileObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001928 0,
Guido van Rossum65967252001-04-21 13:20:18 +00001929 (destructor)file_dealloc, /* tp_dealloc */
1930 0, /* tp_print */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001931 0, /* tp_getattr */
1932 0, /* tp_setattr */
Guido van Rossum65967252001-04-21 13:20:18 +00001933 0, /* tp_compare */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001934 (reprfunc)file_repr, /* tp_repr */
Guido van Rossum65967252001-04-21 13:20:18 +00001935 0, /* tp_as_number */
1936 0, /* tp_as_sequence */
1937 0, /* tp_as_mapping */
1938 0, /* tp_hash */
1939 0, /* tp_call */
1940 0, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001941 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossum65967252001-04-21 13:20:18 +00001942 0, /* tp_setattro */
1943 0, /* tp_as_buffer */
Guido van Rossum9475a232001-10-05 20:51:39 +00001944 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
Tim Peters59c9a642001-09-13 05:38:56 +00001945 file_doc, /* tp_doc */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001946 0, /* tp_traverse */
1947 0, /* tp_clear */
Guido van Rossum65967252001-04-21 13:20:18 +00001948 0, /* tp_richcompare */
1949 0, /* tp_weaklistoffset */
Guido van Rossum7a6e9592002-08-06 15:55:28 +00001950 (getiterfunc)file_getiter, /* tp_iter */
1951 (iternextfunc)file_iternext, /* tp_iternext */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001952 file_methods, /* tp_methods */
1953 file_memberlist, /* tp_members */
1954 file_getsetlist, /* tp_getset */
1955 0, /* tp_base */
1956 0, /* tp_dict */
Tim Peters59c9a642001-09-13 05:38:56 +00001957 0, /* tp_descr_get */
1958 0, /* tp_descr_set */
1959 0, /* tp_dictoffset */
Tim Peters44410012001-09-14 03:26:08 +00001960 (initproc)file_init, /* tp_init */
1961 PyType_GenericAlloc, /* tp_alloc */
Tim Peters59c9a642001-09-13 05:38:56 +00001962 file_new, /* tp_new */
Neil Schemenaueraa769ae2002-04-12 02:44:10 +00001963 PyObject_Del, /* tp_free */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001964};
Guido van Rossumeb183da1991-04-04 10:44:06 +00001965
1966/* Interface for the 'soft space' between print items. */
1967
1968int
Fred Drakefd99de62000-07-09 05:02:18 +00001969PyFile_SoftSpace(PyObject *f, int newflag)
Guido van Rossumeb183da1991-04-04 10:44:06 +00001970{
1971 int oldflag = 0;
Guido van Rossum3165fe61992-09-25 21:59:05 +00001972 if (f == NULL) {
1973 /* Do nothing */
1974 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001975 else if (PyFile_Check(f)) {
1976 oldflag = ((PyFileObject *)f)->f_softspace;
1977 ((PyFileObject *)f)->f_softspace = newflag;
Guido van Rossumeb183da1991-04-04 10:44:06 +00001978 }
Guido van Rossum3165fe61992-09-25 21:59:05 +00001979 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001980 PyObject *v;
1981 v = PyObject_GetAttrString(f, "softspace");
Guido van Rossum3165fe61992-09-25 21:59:05 +00001982 if (v == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001983 PyErr_Clear();
Guido van Rossum3165fe61992-09-25 21:59:05 +00001984 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001985 if (PyInt_Check(v))
1986 oldflag = PyInt_AsLong(v);
1987 Py_DECREF(v);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001988 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001989 v = PyInt_FromLong((long)newflag);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001990 if (v == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001991 PyErr_Clear();
Guido van Rossum3165fe61992-09-25 21:59:05 +00001992 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001993 if (PyObject_SetAttrString(f, "softspace", v) != 0)
1994 PyErr_Clear();
1995 Py_DECREF(v);
Guido van Rossum3165fe61992-09-25 21:59:05 +00001996 }
1997 }
Guido van Rossumeb183da1991-04-04 10:44:06 +00001998 return oldflag;
1999}
Guido van Rossum3165fe61992-09-25 21:59:05 +00002000
2001/* Interfaces to write objects/strings to file-like objects */
2002
2003int
Fred Drakefd99de62000-07-09 05:02:18 +00002004PyFile_WriteObject(PyObject *v, PyObject *f, int flags)
Guido van Rossum3165fe61992-09-25 21:59:05 +00002005{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002006 PyObject *writer, *value, *args, *result;
Guido van Rossum3165fe61992-09-25 21:59:05 +00002007 if (f == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002008 PyErr_SetString(PyExc_TypeError, "writeobject with NULL file");
Guido van Rossum3165fe61992-09-25 21:59:05 +00002009 return -1;
2010 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002011 else if (PyFile_Check(f)) {
2012 FILE *fp = PyFile_AsFile(f);
Guido van Rossum3165fe61992-09-25 21:59:05 +00002013 if (fp == NULL) {
2014 err_closed();
2015 return -1;
2016 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002017 return PyObject_Print(v, fp, flags);
Guido van Rossum3165fe61992-09-25 21:59:05 +00002018 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002019 writer = PyObject_GetAttrString(f, "write");
Guido van Rossum3165fe61992-09-25 21:59:05 +00002020 if (writer == NULL)
2021 return -1;
Martin v. Löwis2777c022001-09-19 13:47:32 +00002022 if (flags & Py_PRINT_RAW) {
2023 if (PyUnicode_Check(v)) {
2024 value = v;
2025 Py_INCREF(value);
2026 } else
2027 value = PyObject_Str(v);
2028 }
2029 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002030 value = PyObject_Repr(v);
Guido van Rossumc6004111993-11-05 10:22:19 +00002031 if (value == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002032 Py_DECREF(writer);
Guido van Rossumc6004111993-11-05 10:22:19 +00002033 return -1;
Guido van Rossum3165fe61992-09-25 21:59:05 +00002034 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002035 args = Py_BuildValue("(O)", value);
Guido van Rossume9eec541997-05-22 14:02:25 +00002036 if (args == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002037 Py_DECREF(value);
2038 Py_DECREF(writer);
Guido van Rossumd3f9a1a1995-07-10 23:32:26 +00002039 return -1;
2040 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002041 result = PyEval_CallObject(writer, args);
2042 Py_DECREF(args);
2043 Py_DECREF(value);
2044 Py_DECREF(writer);
Guido van Rossum3165fe61992-09-25 21:59:05 +00002045 if (result == NULL)
2046 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002047 Py_DECREF(result);
Guido van Rossum3165fe61992-09-25 21:59:05 +00002048 return 0;
2049}
2050
Guido van Rossum27a60b11997-05-22 22:25:11 +00002051int
Tim Petersc1bbcb82001-11-28 22:13:25 +00002052PyFile_WriteString(const char *s, PyObject *f)
Guido van Rossum3165fe61992-09-25 21:59:05 +00002053{
2054 if (f == NULL) {
Guido van Rossum27a60b11997-05-22 22:25:11 +00002055 /* Should be caused by a pre-existing error */
Fred Drakefd99de62000-07-09 05:02:18 +00002056 if (!PyErr_Occurred())
Guido van Rossum27a60b11997-05-22 22:25:11 +00002057 PyErr_SetString(PyExc_SystemError,
2058 "null file for PyFile_WriteString");
2059 return -1;
Guido van Rossum3165fe61992-09-25 21:59:05 +00002060 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002061 else if (PyFile_Check(f)) {
2062 FILE *fp = PyFile_AsFile(f);
Guido van Rossum27a60b11997-05-22 22:25:11 +00002063 if (fp == NULL) {
2064 err_closed();
2065 return -1;
2066 }
2067 fputs(s, fp);
2068 return 0;
Guido van Rossum3165fe61992-09-25 21:59:05 +00002069 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002070 else if (!PyErr_Occurred()) {
2071 PyObject *v = PyString_FromString(s);
Guido van Rossum27a60b11997-05-22 22:25:11 +00002072 int err;
2073 if (v == NULL)
2074 return -1;
2075 err = PyFile_WriteObject(v, f, Py_PRINT_RAW);
2076 Py_DECREF(v);
2077 return err;
Guido van Rossum3165fe61992-09-25 21:59:05 +00002078 }
Guido van Rossum74ba2471997-07-13 03:56:50 +00002079 else
2080 return -1;
Guido van Rossum3165fe61992-09-25 21:59:05 +00002081}
Andrew M. Kuchling06051ed2000-07-13 23:56:54 +00002082
2083/* Try to get a file-descriptor from a Python object. If the object
2084 is an integer or long integer, its value is returned. If not, the
2085 object's fileno() method is called if it exists; the method must return
2086 an integer or long integer, which is returned as the file descriptor value.
2087 -1 is returned on failure.
2088*/
2089
2090int PyObject_AsFileDescriptor(PyObject *o)
2091{
2092 int fd;
2093 PyObject *meth;
2094
2095 if (PyInt_Check(o)) {
2096 fd = PyInt_AsLong(o);
2097 }
2098 else if (PyLong_Check(o)) {
2099 fd = PyLong_AsLong(o);
2100 }
2101 else if ((meth = PyObject_GetAttrString(o, "fileno")) != NULL)
2102 {
2103 PyObject *fno = PyEval_CallObject(meth, NULL);
2104 Py_DECREF(meth);
2105 if (fno == NULL)
2106 return -1;
Tim Peters86821b22001-01-07 21:19:34 +00002107
Andrew M. Kuchling06051ed2000-07-13 23:56:54 +00002108 if (PyInt_Check(fno)) {
2109 fd = PyInt_AsLong(fno);
2110 Py_DECREF(fno);
2111 }
2112 else if (PyLong_Check(fno)) {
2113 fd = PyLong_AsLong(fno);
2114 Py_DECREF(fno);
2115 }
2116 else {
2117 PyErr_SetString(PyExc_TypeError,
2118 "fileno() returned a non-integer");
2119 Py_DECREF(fno);
2120 return -1;
2121 }
2122 }
2123 else {
2124 PyErr_SetString(PyExc_TypeError,
2125 "argument must be an int, or have a fileno() method.");
2126 return -1;
2127 }
2128
2129 if (fd < 0) {
2130 PyErr_Format(PyExc_ValueError,
2131 "file descriptor cannot be a negative integer (%i)",
2132 fd);
2133 return -1;
2134 }
2135 return fd;
2136}
Jack Jansen7b8c7542002-04-14 20:12:41 +00002137
2138#ifdef WITH_UNIVERSAL_NEWLINES
2139/* From here on we need access to the real fgets and fread */
2140#undef fgets
2141#undef fread
2142
2143/*
2144** Py_UniversalNewlineFgets is an fgets variation that understands
2145** all of \r, \n and \r\n conventions.
2146** The stream should be opened in binary mode.
2147** If fobj is NULL the routine always does newline conversion, and
2148** it may peek one char ahead to gobble the second char in \r\n.
2149** If fobj is non-NULL it must be a PyFileObject. In this case there
2150** is no readahead but in stead a flag is used to skip a following
2151** \n on the next read. Also, if the file is open in binary mode
2152** the whole conversion is skipped. Finally, the routine keeps track of
2153** the different types of newlines seen.
2154** Note that we need no error handling: fgets() treats error and eof
2155** identically.
2156*/
2157char *
2158Py_UniversalNewlineFgets(char *buf, int n, FILE *stream, PyObject *fobj)
2159{
2160 char *p = buf;
2161 int c;
2162 int newlinetypes = 0;
2163 int skipnextlf = 0;
2164 int univ_newline = 1;
Tim Peters058b1412002-04-21 07:29:14 +00002165
Jack Jansen7b8c7542002-04-14 20:12:41 +00002166 if (fobj) {
2167 if (!PyFile_Check(fobj)) {
2168 errno = ENXIO; /* What can you do... */
2169 return NULL;
2170 }
2171 univ_newline = ((PyFileObject *)fobj)->f_univ_newline;
2172 if ( !univ_newline )
2173 return fgets(buf, n, stream);
2174 newlinetypes = ((PyFileObject *)fobj)->f_newlinetypes;
2175 skipnextlf = ((PyFileObject *)fobj)->f_skipnextlf;
2176 }
2177 FLOCKFILE(stream);
2178 c = 'x'; /* Shut up gcc warning */
2179 while (--n > 0 && (c = GETC(stream)) != EOF ) {
2180 if (skipnextlf ) {
2181 skipnextlf = 0;
2182 if (c == '\n') {
2183 /* Seeing a \n here with skipnextlf true
2184 ** means we saw a \r before.
2185 */
2186 newlinetypes |= NEWLINE_CRLF;
2187 c = GETC(stream);
2188 if (c == EOF) break;
2189 } else {
2190 /*
2191 ** Note that c == EOF also brings us here,
2192 ** so we're okay if the last char in the file
2193 ** is a CR.
2194 */
2195 newlinetypes |= NEWLINE_CR;
2196 }
2197 }
2198 if (c == '\r') {
2199 /* A \r is translated into a \n, and we skip
2200 ** an adjacent \n, if any. We don't set the
2201 ** newlinetypes flag until we've seen the next char.
2202 */
2203 skipnextlf = 1;
2204 c = '\n';
2205 } else if ( c == '\n') {
2206 newlinetypes |= NEWLINE_LF;
2207 }
2208 *p++ = c;
2209 if (c == '\n') break;
2210 }
2211 if ( c == EOF && skipnextlf )
2212 newlinetypes |= NEWLINE_CR;
2213 FUNLOCKFILE(stream);
2214 *p = '\0';
2215 if (fobj) {
2216 ((PyFileObject *)fobj)->f_newlinetypes = newlinetypes;
2217 ((PyFileObject *)fobj)->f_skipnextlf = skipnextlf;
2218 } else if ( skipnextlf ) {
2219 /* If we have no file object we cannot save the
2220 ** skipnextlf flag. We have to readahead, which
2221 ** will cause a pause if we're reading from an
2222 ** interactive stream, but that is very unlikely
2223 ** unless we're doing something silly like
2224 ** execfile("/dev/tty").
2225 */
2226 c = GETC(stream);
2227 if ( c != '\n' )
2228 ungetc(c, stream);
2229 }
2230 if (p == buf)
2231 return NULL;
2232 return buf;
2233}
2234
2235/*
2236** Py_UniversalNewlineFread is an fread variation that understands
2237** all of \r, \n and \r\n conventions.
2238** The stream should be opened in binary mode.
2239** fobj must be a PyFileObject. In this case there
2240** is no readahead but in stead a flag is used to skip a following
2241** \n on the next read. Also, if the file is open in binary mode
2242** the whole conversion is skipped. Finally, the routine keeps track of
2243** the different types of newlines seen.
2244*/
2245size_t
Tim Peters058b1412002-04-21 07:29:14 +00002246Py_UniversalNewlineFread(char *buf, size_t n,
Jack Jansen7b8c7542002-04-14 20:12:41 +00002247 FILE *stream, PyObject *fobj)
2248{
Tim Peters058b1412002-04-21 07:29:14 +00002249 char *dst = buf;
2250 PyFileObject *f = (PyFileObject *)fobj;
2251 int newlinetypes, skipnextlf;
2252
2253 assert(buf != NULL);
2254 assert(stream != NULL);
2255
Jack Jansen7b8c7542002-04-14 20:12:41 +00002256 if (!fobj || !PyFile_Check(fobj)) {
2257 errno = ENXIO; /* What can you do... */
2258 return -1;
2259 }
Tim Peters058b1412002-04-21 07:29:14 +00002260 if (!f->f_univ_newline)
Jack Jansen7b8c7542002-04-14 20:12:41 +00002261 return fread(buf, 1, n, stream);
Tim Peters058b1412002-04-21 07:29:14 +00002262 newlinetypes = f->f_newlinetypes;
2263 skipnextlf = f->f_skipnextlf;
2264 /* Invariant: n is the number of bytes remaining to be filled
2265 * in the buffer.
2266 */
2267 while (n) {
2268 size_t nread;
2269 int shortread;
2270 char *src = dst;
2271
2272 nread = fread(dst, 1, n, stream);
2273 assert(nread <= n);
Tim Peterse1682a82002-04-21 18:15:20 +00002274 n -= nread; /* assuming 1 byte out for each in; will adjust */
2275 shortread = n != 0; /* true iff EOF or error */
Tim Peters058b1412002-04-21 07:29:14 +00002276 while (nread--) {
2277 char c = *src++;
Jack Jansen7b8c7542002-04-14 20:12:41 +00002278 if (c == '\r') {
Tim Peters058b1412002-04-21 07:29:14 +00002279 /* Save as LF and set flag to skip next LF. */
Jack Jansen7b8c7542002-04-14 20:12:41 +00002280 *dst++ = '\n';
2281 skipnextlf = 1;
Tim Peters058b1412002-04-21 07:29:14 +00002282 }
2283 else if (skipnextlf && c == '\n') {
2284 /* Skip LF, and remember we saw CR LF. */
Jack Jansen7b8c7542002-04-14 20:12:41 +00002285 skipnextlf = 0;
2286 newlinetypes |= NEWLINE_CRLF;
Tim Peterse1682a82002-04-21 18:15:20 +00002287 ++n;
Tim Peters058b1412002-04-21 07:29:14 +00002288 }
2289 else {
2290 /* Normal char to be stored in buffer. Also
2291 * update the newlinetypes flag if either this
2292 * is an LF or the previous char was a CR.
2293 */
Jack Jansen7b8c7542002-04-14 20:12:41 +00002294 if (c == '\n')
2295 newlinetypes |= NEWLINE_LF;
2296 else if (skipnextlf)
2297 newlinetypes |= NEWLINE_CR;
2298 *dst++ = c;
2299 skipnextlf = 0;
2300 }
2301 }
Tim Peters058b1412002-04-21 07:29:14 +00002302 if (shortread) {
2303 /* If this is EOF, update type flags. */
2304 if (skipnextlf && feof(stream))
2305 newlinetypes |= NEWLINE_CR;
2306 break;
2307 }
Jack Jansen7b8c7542002-04-14 20:12:41 +00002308 }
Tim Peters058b1412002-04-21 07:29:14 +00002309 f->f_newlinetypes = newlinetypes;
2310 f->f_skipnextlf = skipnextlf;
2311 return dst - buf;
Jack Jansen7b8c7542002-04-14 20:12:41 +00002312}
2313#endif