This commit was manufactured by cvs2svn to create tag 'r221'.
[python/dscho.git] / Objects / fileobject.c
blob7ac6efbbc831c40ba53568767279baf0f9d6e826
2 /* File object implementation */
4 #include "Python.h"
5 #include "structmember.h"
7 #ifndef DONT_HAVE_SYS_TYPES_H
8 #include <sys/types.h>
9 #endif /* DONT_HAVE_SYS_TYPES_H */
11 #ifdef MS_WIN32
12 #define fileno _fileno
13 /* can (almost fully) duplicate with _chsize, see file_truncate */
14 #define HAVE_FTRUNCATE
15 #endif
17 #ifdef macintosh
18 #ifdef USE_GUSI
19 #define HAVE_FTRUNCATE
20 #endif
21 #endif
23 #ifdef __MWERKS__
24 /* Mwerks fopen() doesn't always set errno */
25 #define NO_FOPEN_ERRNO
26 #endif
28 #define BUF(v) PyString_AS_STRING((PyStringObject *)v)
30 #ifndef DONT_HAVE_ERRNO_H
31 #include <errno.h>
32 #endif
35 typedef struct {
36 PyObject_HEAD
37 FILE *f_fp;
38 PyObject *f_name;
39 PyObject *f_mode;
40 int (*f_close)(FILE *);
41 int f_softspace; /* Flag used by 'print' command */
42 int f_binary; /* Flag which indicates whether the file is open
43 open in binary (1) or test (0) mode */
44 } PyFileObject;
46 FILE *
47 PyFile_AsFile(PyObject *f)
49 if (f == NULL || !PyFile_Check(f))
50 return NULL;
51 else
52 return ((PyFileObject *)f)->f_fp;
55 PyObject *
56 PyFile_Name(PyObject *f)
58 if (f == NULL || !PyFile_Check(f))
59 return NULL;
60 else
61 return ((PyFileObject *)f)->f_name;
65 static PyObject *
66 fill_file_fields(PyFileObject *f, FILE *fp, char *name, char *mode,
67 int (*close)(FILE *))
69 assert(f != NULL);
70 assert(PyFile_Check(f));
71 assert(f->f_fp == NULL);
73 Py_DECREF(f->f_name);
74 Py_DECREF(f->f_mode);
75 f->f_name = PyString_FromString(name);
76 f->f_mode = PyString_FromString(mode);
78 f->f_close = close;
79 f->f_softspace = 0;
80 f->f_binary = strchr(mode,'b') != NULL;
82 if (f->f_name == NULL || f->f_mode == NULL)
83 return NULL;
84 f->f_fp = fp;
85 return (PyObject *) f;
88 static PyObject *
89 open_the_file(PyFileObject *f, char *name, char *mode)
91 assert(f != NULL);
92 assert(PyFile_Check(f));
93 assert(name != NULL);
94 assert(mode != NULL);
95 assert(f->f_fp == NULL);
97 /* rexec.py can't stop a user from getting the file() constructor --
98 all they have to do is get *any* file object f, and then do
99 type(f). Here we prevent them from doing damage with it. */
100 if (PyEval_GetRestricted()) {
101 PyErr_SetString(PyExc_IOError,
102 "file() constructor not accessible in restricted mode");
103 return NULL;
105 errno = 0;
106 #ifdef HAVE_FOPENRF
107 if (*mode == '*') {
108 FILE *fopenRF();
109 f->f_fp = fopenRF(name, mode+1);
111 else
112 #endif
114 Py_BEGIN_ALLOW_THREADS
115 f->f_fp = fopen(name, mode);
116 Py_END_ALLOW_THREADS
118 if (f->f_fp == NULL) {
119 #ifdef NO_FOPEN_ERRNO
120 /* Metroworks only, wich does not always sets errno */
121 if (errno == 0) {
122 PyObject *v;
123 v = Py_BuildValue("(is)", 0, "Cannot open file");
124 if (v != NULL) {
125 PyErr_SetObject(PyExc_IOError, v);
126 Py_DECREF(v);
128 return NULL;
130 #endif
131 #ifdef _MSC_VER
132 /* MSVC 6 (Microsoft) leaves errno at 0 for bad mode strings,
133 * across all Windows flavors. When it sets EINVAL varies
134 * across Windows flavors, the exact conditions aren't
135 * documented, and the answer lies in the OS's implementation
136 * of Win32's CreateFile function (whose source is secret).
137 * Seems the best we can do is map EINVAL to ENOENT.
139 if (errno == 0) /* bad mode string */
140 errno = EINVAL;
141 else if (errno == EINVAL) /* unknown, but not a mode string */
142 errno = ENOENT;
143 #endif
144 if (errno == EINVAL)
145 PyErr_Format(PyExc_IOError, "invalid mode: %s",
146 mode);
147 else
148 PyErr_SetFromErrnoWithFilename(PyExc_IOError, name);
149 f = NULL;
151 return (PyObject *)f;
154 PyObject *
155 PyFile_FromFile(FILE *fp, char *name, char *mode, int (*close)(FILE *))
157 PyFileObject *f = (PyFileObject *)PyFile_Type.tp_new(&PyFile_Type,
158 NULL, NULL);
159 if (f != NULL) {
160 if (fill_file_fields(f, fp, name, mode, close) == NULL) {
161 Py_DECREF(f);
162 f = NULL;
165 return (PyObject *) f;
168 PyObject *
169 PyFile_FromString(char *name, char *mode)
171 extern int fclose(FILE *);
172 PyFileObject *f;
174 f = (PyFileObject *)PyFile_FromFile((FILE *)NULL, name, mode, fclose);
175 if (f != NULL) {
176 if (open_the_file(f, name, mode) == NULL) {
177 Py_DECREF(f);
178 f = NULL;
181 return (PyObject *)f;
184 void
185 PyFile_SetBufSize(PyObject *f, int bufsize)
187 if (bufsize >= 0) {
188 #ifdef HAVE_SETVBUF
189 int type;
190 switch (bufsize) {
191 case 0:
192 type = _IONBF;
193 break;
194 case 1:
195 type = _IOLBF;
196 bufsize = BUFSIZ;
197 break;
198 default:
199 type = _IOFBF;
201 setvbuf(((PyFileObject *)f)->f_fp, (char *)NULL,
202 type, bufsize);
203 #else /* !HAVE_SETVBUF */
204 if (bufsize <= 1)
205 setbuf(((PyFileObject *)f)->f_fp, (char *)NULL);
206 #endif /* !HAVE_SETVBUF */
210 static PyObject *
211 err_closed(void)
213 PyErr_SetString(PyExc_ValueError, "I/O operation on closed file");
214 return NULL;
217 /* Methods */
219 static void
220 file_dealloc(PyFileObject *f)
222 if (f->f_fp != NULL && f->f_close != NULL) {
223 Py_BEGIN_ALLOW_THREADS
224 (*f->f_close)(f->f_fp);
225 Py_END_ALLOW_THREADS
227 Py_XDECREF(f->f_name);
228 Py_XDECREF(f->f_mode);
229 f->ob_type->tp_free((PyObject *)f);
232 static PyObject *
233 file_repr(PyFileObject *f)
235 return PyString_FromFormat("<%s file '%s', mode '%s' at %p>",
236 f->f_fp == NULL ? "closed" : "open",
237 PyString_AsString(f->f_name),
238 PyString_AsString(f->f_mode),
242 static PyObject *
243 file_close(PyFileObject *f)
245 int sts = 0;
246 if (f->f_fp != NULL) {
247 if (f->f_close != NULL) {
248 Py_BEGIN_ALLOW_THREADS
249 errno = 0;
250 sts = (*f->f_close)(f->f_fp);
251 Py_END_ALLOW_THREADS
253 f->f_fp = NULL;
255 if (sts == EOF)
256 return PyErr_SetFromErrno(PyExc_IOError);
257 if (sts != 0)
258 return PyInt_FromLong((long)sts);
259 Py_INCREF(Py_None);
260 return Py_None;
264 /* Our very own off_t-like type, 64-bit if possible */
265 #if !defined(HAVE_LARGEFILE_SUPPORT)
266 typedef off_t Py_off_t;
267 #elif SIZEOF_OFF_T >= 8
268 typedef off_t Py_off_t;
269 #elif SIZEOF_FPOS_T >= 8
270 typedef fpos_t Py_off_t;
271 #else
272 #error "Large file support, but neither off_t nor fpos_t is large enough."
273 #endif
276 /* a portable fseek() function
277 return 0 on success, non-zero on failure (with errno set) */
278 static int
279 _portable_fseek(FILE *fp, Py_off_t offset, int whence)
281 #if !defined(HAVE_LARGEFILE_SUPPORT)
282 return fseek(fp, offset, whence);
283 #elif defined(HAVE_FSEEKO) && SIZEOF_OFF_T >= 8
284 return fseeko(fp, offset, whence);
285 #elif defined(HAVE_FSEEK64)
286 return fseek64(fp, offset, whence);
287 #elif defined(__BEOS__)
288 return _fseek(fp, offset, whence);
289 #elif SIZEOF_FPOS_T >= 8
290 /* lacking a 64-bit capable fseek(), use a 64-bit capable fsetpos()
291 and fgetpos() to implement fseek()*/
292 fpos_t pos;
293 switch (whence) {
294 case SEEK_END:
295 #ifdef MS_WINDOWS
296 fflush(fp);
297 if (_lseeki64(fileno(fp), 0, 2) == -1)
298 return -1;
299 #else
300 if (fseek(fp, 0, SEEK_END) != 0)
301 return -1;
302 #endif
303 /* fall through */
304 case SEEK_CUR:
305 if (fgetpos(fp, &pos) != 0)
306 return -1;
307 offset += pos;
308 break;
309 /* case SEEK_SET: break; */
311 return fsetpos(fp, &offset);
312 #else
313 #error "Large file support, but no way to fseek."
314 #endif
318 /* a portable ftell() function
319 Return -1 on failure with errno set appropriately, current file
320 position on success */
321 static Py_off_t
322 _portable_ftell(FILE* fp)
324 #if !defined(HAVE_LARGEFILE_SUPPORT)
325 return ftell(fp);
326 #elif defined(HAVE_FTELLO) && SIZEOF_OFF_T >= 8
327 return ftello(fp);
328 #elif defined(HAVE_FTELL64)
329 return ftell64(fp);
330 #elif SIZEOF_FPOS_T >= 8
331 fpos_t pos;
332 if (fgetpos(fp, &pos) != 0)
333 return -1;
334 return pos;
335 #else
336 #error "Large file support, but no way to ftell."
337 #endif
341 static PyObject *
342 file_seek(PyFileObject *f, PyObject *args)
344 int whence;
345 int ret;
346 Py_off_t offset;
347 PyObject *offobj;
349 if (f->f_fp == NULL)
350 return err_closed();
351 whence = 0;
352 if (!PyArg_ParseTuple(args, "O|i:seek", &offobj, &whence))
353 return NULL;
354 #if !defined(HAVE_LARGEFILE_SUPPORT)
355 offset = PyInt_AsLong(offobj);
356 #else
357 offset = PyLong_Check(offobj) ?
358 PyLong_AsLongLong(offobj) : PyInt_AsLong(offobj);
359 #endif
360 if (PyErr_Occurred())
361 return NULL;
363 Py_BEGIN_ALLOW_THREADS
364 errno = 0;
365 ret = _portable_fseek(f->f_fp, offset, whence);
366 Py_END_ALLOW_THREADS
368 if (ret != 0) {
369 PyErr_SetFromErrno(PyExc_IOError);
370 clearerr(f->f_fp);
371 return NULL;
373 Py_INCREF(Py_None);
374 return Py_None;
378 #ifdef HAVE_FTRUNCATE
379 static PyObject *
380 file_truncate(PyFileObject *f, PyObject *args)
382 int ret;
383 Py_off_t newsize;
384 PyObject *newsizeobj;
386 if (f->f_fp == NULL)
387 return err_closed();
388 newsizeobj = NULL;
389 if (!PyArg_ParseTuple(args, "|O:truncate", &newsizeobj))
390 return NULL;
391 if (newsizeobj != NULL) {
392 #if !defined(HAVE_LARGEFILE_SUPPORT)
393 newsize = PyInt_AsLong(newsizeobj);
394 #else
395 newsize = PyLong_Check(newsizeobj) ?
396 PyLong_AsLongLong(newsizeobj) :
397 PyInt_AsLong(newsizeobj);
398 #endif
399 if (PyErr_Occurred())
400 return NULL;
401 } else {
402 /* Default to current position*/
403 Py_BEGIN_ALLOW_THREADS
404 errno = 0;
405 newsize = _portable_ftell(f->f_fp);
406 Py_END_ALLOW_THREADS
407 if (newsize == -1) {
408 PyErr_SetFromErrno(PyExc_IOError);
409 clearerr(f->f_fp);
410 return NULL;
413 Py_BEGIN_ALLOW_THREADS
414 errno = 0;
415 ret = fflush(f->f_fp);
416 Py_END_ALLOW_THREADS
417 if (ret != 0) goto onioerror;
419 #ifdef MS_WIN32
420 /* can use _chsize; if, however, the newsize overflows 32-bits then
421 _chsize is *not* adequate; in this case, an OverflowError is raised */
422 if (newsize > LONG_MAX) {
423 PyErr_SetString(PyExc_OverflowError,
424 "the new size is too long for _chsize (it is limited to 32-bit values)");
425 return NULL;
426 } else {
427 Py_BEGIN_ALLOW_THREADS
428 errno = 0;
429 ret = _chsize(fileno(f->f_fp), (long)newsize);
430 Py_END_ALLOW_THREADS
431 if (ret != 0) goto onioerror;
433 #else
434 Py_BEGIN_ALLOW_THREADS
435 errno = 0;
436 ret = ftruncate(fileno(f->f_fp), newsize);
437 Py_END_ALLOW_THREADS
438 if (ret != 0) goto onioerror;
439 #endif /* !MS_WIN32 */
441 Py_INCREF(Py_None);
442 return Py_None;
444 onioerror:
445 PyErr_SetFromErrno(PyExc_IOError);
446 clearerr(f->f_fp);
447 return NULL;
449 #endif /* HAVE_FTRUNCATE */
451 static PyObject *
452 file_tell(PyFileObject *f)
454 Py_off_t pos;
456 if (f->f_fp == NULL)
457 return err_closed();
458 Py_BEGIN_ALLOW_THREADS
459 errno = 0;
460 pos = _portable_ftell(f->f_fp);
461 Py_END_ALLOW_THREADS
462 if (pos == -1) {
463 PyErr_SetFromErrno(PyExc_IOError);
464 clearerr(f->f_fp);
465 return NULL;
467 #if !defined(HAVE_LARGEFILE_SUPPORT)
468 return PyInt_FromLong(pos);
469 #else
470 return PyLong_FromLongLong(pos);
471 #endif
474 static PyObject *
475 file_fileno(PyFileObject *f)
477 if (f->f_fp == NULL)
478 return err_closed();
479 return PyInt_FromLong((long) fileno(f->f_fp));
482 static PyObject *
483 file_flush(PyFileObject *f)
485 int res;
487 if (f->f_fp == NULL)
488 return err_closed();
489 Py_BEGIN_ALLOW_THREADS
490 errno = 0;
491 res = fflush(f->f_fp);
492 Py_END_ALLOW_THREADS
493 if (res != 0) {
494 PyErr_SetFromErrno(PyExc_IOError);
495 clearerr(f->f_fp);
496 return NULL;
498 Py_INCREF(Py_None);
499 return Py_None;
502 static PyObject *
503 file_isatty(PyFileObject *f)
505 long res;
506 if (f->f_fp == NULL)
507 return err_closed();
508 Py_BEGIN_ALLOW_THREADS
509 res = isatty((int)fileno(f->f_fp));
510 Py_END_ALLOW_THREADS
511 return PyInt_FromLong(res);
515 #if BUFSIZ < 8192
516 #define SMALLCHUNK 8192
517 #else
518 #define SMALLCHUNK BUFSIZ
519 #endif
521 #if SIZEOF_INT < 4
522 #define BIGCHUNK (512 * 32)
523 #else
524 #define BIGCHUNK (512 * 1024)
525 #endif
527 static size_t
528 new_buffersize(PyFileObject *f, size_t currentsize)
530 #ifdef HAVE_FSTAT
531 off_t pos, end;
532 struct stat st;
533 if (fstat(fileno(f->f_fp), &st) == 0) {
534 end = st.st_size;
535 /* The following is not a bug: we really need to call lseek()
536 *and* ftell(). The reason is that some stdio libraries
537 mistakenly flush their buffer when ftell() is called and
538 the lseek() call it makes fails, thereby throwing away
539 data that cannot be recovered in any way. To avoid this,
540 we first test lseek(), and only call ftell() if lseek()
541 works. We can't use the lseek() value either, because we
542 need to take the amount of buffered data into account.
543 (Yet another reason why stdio stinks. :-) */
544 #ifdef USE_GUSI2
545 pos = lseek(fileno(f->f_fp), 1L, SEEK_CUR);
546 pos = lseek(fileno(f->f_fp), -1L, SEEK_CUR);
547 #else
548 pos = lseek(fileno(f->f_fp), 0L, SEEK_CUR);
549 #endif
550 if (pos >= 0) {
551 pos = ftell(f->f_fp);
553 if (pos < 0)
554 clearerr(f->f_fp);
555 if (end > pos && pos >= 0)
556 return currentsize + end - pos + 1;
557 /* Add 1 so if the file were to grow we'd notice. */
559 #endif
560 if (currentsize > SMALLCHUNK) {
561 /* Keep doubling until we reach BIGCHUNK;
562 then keep adding BIGCHUNK. */
563 if (currentsize <= BIGCHUNK)
564 return currentsize + currentsize;
565 else
566 return currentsize + BIGCHUNK;
568 return currentsize + SMALLCHUNK;
571 static PyObject *
572 file_read(PyFileObject *f, PyObject *args)
574 long bytesrequested = -1;
575 size_t bytesread, buffersize, chunksize;
576 PyObject *v;
578 if (f->f_fp == NULL)
579 return err_closed();
580 if (!PyArg_ParseTuple(args, "|l:read", &bytesrequested))
581 return NULL;
582 if (bytesrequested < 0)
583 buffersize = new_buffersize(f, (size_t)0);
584 else
585 buffersize = bytesrequested;
586 if (buffersize > INT_MAX) {
587 PyErr_SetString(PyExc_OverflowError,
588 "requested number of bytes is more than a Python string can hold");
589 return NULL;
591 v = PyString_FromStringAndSize((char *)NULL, buffersize);
592 if (v == NULL)
593 return NULL;
594 bytesread = 0;
595 for (;;) {
596 Py_BEGIN_ALLOW_THREADS
597 errno = 0;
598 chunksize = fread(BUF(v) + bytesread, 1,
599 buffersize - bytesread, f->f_fp);
600 Py_END_ALLOW_THREADS
601 if (chunksize == 0) {
602 if (!ferror(f->f_fp))
603 break;
604 PyErr_SetFromErrno(PyExc_IOError);
605 clearerr(f->f_fp);
606 Py_DECREF(v);
607 return NULL;
609 bytesread += chunksize;
610 if (bytesread < buffersize)
611 break;
612 if (bytesrequested < 0) {
613 buffersize = new_buffersize(f, buffersize);
614 if (_PyString_Resize(&v, buffersize) < 0)
615 return NULL;
618 if (bytesread != buffersize)
619 _PyString_Resize(&v, bytesread);
620 return v;
623 static PyObject *
624 file_readinto(PyFileObject *f, PyObject *args)
626 char *ptr;
627 int ntodo;
628 size_t ndone, nnow;
630 if (f->f_fp == NULL)
631 return err_closed();
632 if (!PyArg_Parse(args, "w#", &ptr, &ntodo))
633 return NULL;
634 ndone = 0;
635 while (ntodo > 0) {
636 Py_BEGIN_ALLOW_THREADS
637 errno = 0;
638 nnow = fread(ptr+ndone, 1, ntodo, f->f_fp);
639 Py_END_ALLOW_THREADS
640 if (nnow == 0) {
641 if (!ferror(f->f_fp))
642 break;
643 PyErr_SetFromErrno(PyExc_IOError);
644 clearerr(f->f_fp);
645 return NULL;
647 ndone += nnow;
648 ntodo -= nnow;
650 return PyInt_FromLong((long)ndone);
653 /**************************************************************************
654 Routine to get next line using platform fgets().
656 Under MSVC 6:
658 + MS threadsafe getc is very slow (multiple layers of function calls before+
659 after each character, to lock+unlock the stream).
660 + The stream-locking functions are MS-internal -- can't access them from user
661 code.
662 + There's nothing Tim could find in the MS C or platform SDK libraries that
663 can worm around this.
664 + MS fgets locks/unlocks only once per line; it's the only hook we have.
666 So we use fgets for speed(!), despite that it's painful.
668 MS realloc is also slow.
670 Reports from other platforms on this method vs getc_unlocked (which MS doesn't
671 have):
672 Linux a wash
673 Solaris a wash
674 Tru64 Unix getline_via_fgets significantly faster
676 CAUTION: The C std isn't clear about this: in those cases where fgets
677 writes something into the buffer, can it write into any position beyond the
678 required trailing null byte? MSVC 6 fgets does not, and no platform is (yet)
679 known on which it does; and it would be a strange way to code fgets. Still,
680 getline_via_fgets may not work correctly if it does. The std test
681 test_bufio.py should fail if platform fgets() routinely writes beyond the
682 trailing null byte. #define DONT_USE_FGETS_IN_GETLINE to disable this code.
683 **************************************************************************/
685 /* Use this routine if told to, or by default on non-get_unlocked()
686 * platforms unless told not to. Yikes! Let's spell that out:
687 * On a platform with getc_unlocked():
688 * By default, use getc_unlocked().
689 * If you want to use fgets() instead, #define USE_FGETS_IN_GETLINE.
690 * On a platform without getc_unlocked():
691 * By default, use fgets().
692 * If you don't want to use fgets(), #define DONT_USE_FGETS_IN_GETLINE.
694 #if !defined(USE_FGETS_IN_GETLINE) && !defined(HAVE_GETC_UNLOCKED)
695 #define USE_FGETS_IN_GETLINE
696 #endif
698 #if defined(DONT_USE_FGETS_IN_GETLINE) && defined(USE_FGETS_IN_GETLINE)
699 #undef USE_FGETS_IN_GETLINE
700 #endif
702 #ifdef USE_FGETS_IN_GETLINE
703 static PyObject*
704 getline_via_fgets(FILE *fp)
706 /* INITBUFSIZE is the maximum line length that lets us get away with the fast
707 * no-realloc, one-fgets()-call path. Boosting it isn't free, because we have
708 * to fill this much of the buffer with a known value in order to figure out
709 * how much of the buffer fgets() overwrites. So if INITBUFSIZE is larger
710 * than "most" lines, we waste time filling unused buffer slots. 100 is
711 * surely adequate for most peoples' email archives, chewing over source code,
712 * etc -- "regular old text files".
713 * MAXBUFSIZE is the maximum line length that lets us get away with the less
714 * fast (but still zippy) no-realloc, two-fgets()-call path. See above for
715 * cautions about boosting that. 300 was chosen because the worst real-life
716 * text-crunching job reported on Python-Dev was a mail-log crawler where over
717 * half the lines were 254 chars.
718 * INCBUFSIZE is the amount by which we grow the buffer, if MAXBUFSIZE isn't
719 * enough. It doesn't much matter what this is set to: we only get here for
720 * absurdly long lines anyway.
722 #define INITBUFSIZE 100
723 #define MAXBUFSIZE 300
724 #define INCBUFSIZE 1000
725 char* p; /* temp */
726 char buf[MAXBUFSIZE];
727 PyObject* v; /* the string object result */
728 char* pvfree; /* address of next free slot */
729 char* pvend; /* address one beyond last free slot */
730 size_t nfree; /* # of free buffer slots; pvend-pvfree */
731 size_t total_v_size; /* total # of slots in buffer */
733 /* Optimize for normal case: avoid _PyString_Resize if at all
734 * possible via first reading into stack buffer "buf".
736 total_v_size = INITBUFSIZE; /* start small and pray */
737 pvfree = buf;
738 for (;;) {
739 Py_BEGIN_ALLOW_THREADS
740 pvend = buf + total_v_size;
741 nfree = pvend - pvfree;
742 memset(pvfree, '\n', nfree);
743 p = fgets(pvfree, nfree, fp);
744 Py_END_ALLOW_THREADS
746 if (p == NULL) {
747 clearerr(fp);
748 if (PyErr_CheckSignals())
749 return NULL;
750 v = PyString_FromStringAndSize(buf, pvfree - buf);
751 return v;
753 /* fgets read *something* */
754 p = memchr(pvfree, '\n', nfree);
755 if (p != NULL) {
756 /* Did the \n come from fgets or from us?
757 * Since fgets stops at the first \n, and then writes
758 * \0, if it's from fgets a \0 must be next. But if
759 * that's so, it could not have come from us, since
760 * the \n's we filled the buffer with have only more
761 * \n's to the right.
763 if (p+1 < pvend && *(p+1) == '\0') {
764 /* It's from fgets: we win! In particular,
765 * we haven't done any mallocs yet, and can
766 * build the final result on the first try.
768 ++p; /* include \n from fgets */
770 else {
771 /* Must be from us: fgets didn't fill the
772 * buffer and didn't find a newline, so it
773 * must be the last and newline-free line of
774 * the file.
776 assert(p > pvfree && *(p-1) == '\0');
777 --p; /* don't include \0 from fgets */
779 v = PyString_FromStringAndSize(buf, p - buf);
780 return v;
782 /* yuck: fgets overwrote all the newlines, i.e. the entire
783 * buffer. So this line isn't over yet, or maybe it is but
784 * we're exactly at EOF. If we haven't already, try using the
785 * rest of the stack buffer.
787 assert(*(pvend-1) == '\0');
788 if (pvfree == buf) {
789 pvfree = pvend - 1; /* overwrite trailing null */
790 total_v_size = MAXBUFSIZE;
792 else
793 break;
796 /* The stack buffer isn't big enough; malloc a string object and read
797 * into its buffer.
799 total_v_size = MAXBUFSIZE + INCBUFSIZE;
800 v = PyString_FromStringAndSize((char*)NULL, (int)total_v_size);
801 if (v == NULL)
802 return v;
803 /* copy over everything except the last null byte */
804 memcpy(BUF(v), buf, MAXBUFSIZE-1);
805 pvfree = BUF(v) + MAXBUFSIZE - 1;
807 /* Keep reading stuff into v; if it ever ends successfully, break
808 * after setting p one beyond the end of the line. The code here is
809 * very much like the code above, except reads into v's buffer; see
810 * the code above for detailed comments about the logic.
812 for (;;) {
813 Py_BEGIN_ALLOW_THREADS
814 pvend = BUF(v) + total_v_size;
815 nfree = pvend - pvfree;
816 memset(pvfree, '\n', nfree);
817 p = fgets(pvfree, nfree, fp);
818 Py_END_ALLOW_THREADS
820 if (p == NULL) {
821 clearerr(fp);
822 if (PyErr_CheckSignals()) {
823 Py_DECREF(v);
824 return NULL;
826 p = pvfree;
827 break;
829 p = memchr(pvfree, '\n', nfree);
830 if (p != NULL) {
831 if (p+1 < pvend && *(p+1) == '\0') {
832 /* \n came from fgets */
833 ++p;
834 break;
836 /* \n came from us; last line of file, no newline */
837 assert(p > pvfree && *(p-1) == '\0');
838 --p;
839 break;
841 /* expand buffer and try again */
842 assert(*(pvend-1) == '\0');
843 total_v_size += INCBUFSIZE;
844 if (total_v_size > INT_MAX) {
845 PyErr_SetString(PyExc_OverflowError,
846 "line is longer than a Python string can hold");
847 Py_DECREF(v);
848 return NULL;
850 if (_PyString_Resize(&v, (int)total_v_size) < 0)
851 return NULL;
852 /* overwrite the trailing null byte */
853 pvfree = BUF(v) + (total_v_size - INCBUFSIZE - 1);
855 if (BUF(v) + total_v_size != p)
856 _PyString_Resize(&v, p - BUF(v));
857 return v;
858 #undef INITBUFSIZE
859 #undef MAXBUFSIZE
860 #undef INCBUFSIZE
862 #endif /* ifdef USE_FGETS_IN_GETLINE */
864 /* Internal routine to get a line.
865 Size argument interpretation:
866 > 0: max length;
867 <= 0: read arbitrary line
870 #ifdef HAVE_GETC_UNLOCKED
871 #define GETC(f) getc_unlocked(f)
872 #define FLOCKFILE(f) flockfile(f)
873 #define FUNLOCKFILE(f) funlockfile(f)
874 #else
875 #define GETC(f) getc(f)
876 #define FLOCKFILE(f)
877 #define FUNLOCKFILE(f)
878 #endif
880 static PyObject *
881 get_line(PyFileObject *f, int n)
883 FILE *fp = f->f_fp;
884 int c;
885 char *buf, *end;
886 size_t n1, n2;
887 PyObject *v;
889 #ifdef USE_FGETS_IN_GETLINE
890 if (n <= 0)
891 return getline_via_fgets(fp);
892 #endif
893 n2 = n > 0 ? n : 100;
894 v = PyString_FromStringAndSize((char *)NULL, n2);
895 if (v == NULL)
896 return NULL;
897 buf = BUF(v);
898 end = buf + n2;
900 for (;;) {
901 Py_BEGIN_ALLOW_THREADS
902 FLOCKFILE(fp);
903 while ((c = GETC(fp)) != EOF &&
904 (*buf++ = c) != '\n' &&
905 buf != end)
907 FUNLOCKFILE(fp);
908 Py_END_ALLOW_THREADS
909 if (c == '\n')
910 break;
911 if (c == EOF) {
912 if (ferror(fp)) {
913 PyErr_SetFromErrno(PyExc_IOError);
914 clearerr(fp);
915 Py_DECREF(v);
916 return NULL;
918 clearerr(fp);
919 if (PyErr_CheckSignals()) {
920 Py_DECREF(v);
921 return NULL;
923 break;
925 /* Must be because buf == end */
926 if (n > 0)
927 break;
928 n1 = n2;
929 n2 += 1000;
930 if (n2 > INT_MAX) {
931 PyErr_SetString(PyExc_OverflowError,
932 "line is longer than a Python string can hold");
933 Py_DECREF(v);
934 return NULL;
936 if (_PyString_Resize(&v, n2) < 0)
937 return NULL;
938 buf = BUF(v) + n1;
939 end = BUF(v) + n2;
942 n1 = buf - BUF(v);
943 if (n1 != n2)
944 _PyString_Resize(&v, n1);
945 return v;
948 /* External C interface */
950 PyObject *
951 PyFile_GetLine(PyObject *f, int n)
953 PyObject *result;
955 if (f == NULL) {
956 PyErr_BadInternalCall();
957 return NULL;
960 if (PyFile_Check(f)) {
961 if (((PyFileObject*)f)->f_fp == NULL)
962 return err_closed();
963 result = get_line((PyFileObject *)f, n);
965 else {
966 PyObject *reader;
967 PyObject *args;
969 reader = PyObject_GetAttrString(f, "readline");
970 if (reader == NULL)
971 return NULL;
972 if (n <= 0)
973 args = Py_BuildValue("()");
974 else
975 args = Py_BuildValue("(i)", n);
976 if (args == NULL) {
977 Py_DECREF(reader);
978 return NULL;
980 result = PyEval_CallObject(reader, args);
981 Py_DECREF(reader);
982 Py_DECREF(args);
983 if (result != NULL && !PyString_Check(result)) {
984 Py_DECREF(result);
985 result = NULL;
986 PyErr_SetString(PyExc_TypeError,
987 "object.readline() returned non-string");
991 if (n < 0 && result != NULL && PyString_Check(result)) {
992 char *s = PyString_AS_STRING(result);
993 int len = PyString_GET_SIZE(result);
994 if (len == 0) {
995 Py_DECREF(result);
996 result = NULL;
997 PyErr_SetString(PyExc_EOFError,
998 "EOF when reading a line");
1000 else if (s[len-1] == '\n') {
1001 if (result->ob_refcnt == 1)
1002 _PyString_Resize(&result, len-1);
1003 else {
1004 PyObject *v;
1005 v = PyString_FromStringAndSize(s, len-1);
1006 Py_DECREF(result);
1007 result = v;
1011 return result;
1014 /* Python method */
1016 static PyObject *
1017 file_readline(PyFileObject *f, PyObject *args)
1019 int n = -1;
1021 if (f->f_fp == NULL)
1022 return err_closed();
1023 if (!PyArg_ParseTuple(args, "|i:readline", &n))
1024 return NULL;
1025 if (n == 0)
1026 return PyString_FromString("");
1027 if (n < 0)
1028 n = 0;
1029 return get_line(f, n);
1032 static PyObject *
1033 file_xreadlines(PyFileObject *f)
1035 static PyObject* xreadlines_function = NULL;
1037 if (!xreadlines_function) {
1038 PyObject *xreadlines_module =
1039 PyImport_ImportModule("xreadlines");
1040 if(!xreadlines_module)
1041 return NULL;
1043 xreadlines_function = PyObject_GetAttrString(xreadlines_module,
1044 "xreadlines");
1045 Py_DECREF(xreadlines_module);
1046 if(!xreadlines_function)
1047 return NULL;
1049 return PyObject_CallFunction(xreadlines_function, "(O)", f);
1052 static PyObject *
1053 file_readlines(PyFileObject *f, PyObject *args)
1055 long sizehint = 0;
1056 PyObject *list;
1057 PyObject *line;
1058 char small_buffer[SMALLCHUNK];
1059 char *buffer = small_buffer;
1060 size_t buffersize = SMALLCHUNK;
1061 PyObject *big_buffer = NULL;
1062 size_t nfilled = 0;
1063 size_t nread;
1064 size_t totalread = 0;
1065 char *p, *q, *end;
1066 int err;
1067 int shortread = 0;
1069 if (f->f_fp == NULL)
1070 return err_closed();
1071 if (!PyArg_ParseTuple(args, "|l:readlines", &sizehint))
1072 return NULL;
1073 if ((list = PyList_New(0)) == NULL)
1074 return NULL;
1075 for (;;) {
1076 if (shortread)
1077 nread = 0;
1078 else {
1079 Py_BEGIN_ALLOW_THREADS
1080 errno = 0;
1081 nread = fread(buffer+nfilled, 1,
1082 buffersize-nfilled, f->f_fp);
1083 Py_END_ALLOW_THREADS
1084 shortread = (nread < buffersize-nfilled);
1086 if (nread == 0) {
1087 sizehint = 0;
1088 if (!ferror(f->f_fp))
1089 break;
1090 PyErr_SetFromErrno(PyExc_IOError);
1091 clearerr(f->f_fp);
1092 error:
1093 Py_DECREF(list);
1094 list = NULL;
1095 goto cleanup;
1097 totalread += nread;
1098 p = memchr(buffer+nfilled, '\n', nread);
1099 if (p == NULL) {
1100 /* Need a larger buffer to fit this line */
1101 nfilled += nread;
1102 buffersize *= 2;
1103 if (buffersize > INT_MAX) {
1104 PyErr_SetString(PyExc_OverflowError,
1105 "line is longer than a Python string can hold");
1106 goto error;
1108 if (big_buffer == NULL) {
1109 /* Create the big buffer */
1110 big_buffer = PyString_FromStringAndSize(
1111 NULL, buffersize);
1112 if (big_buffer == NULL)
1113 goto error;
1114 buffer = PyString_AS_STRING(big_buffer);
1115 memcpy(buffer, small_buffer, nfilled);
1117 else {
1118 /* Grow the big buffer */
1119 _PyString_Resize(&big_buffer, buffersize);
1120 buffer = PyString_AS_STRING(big_buffer);
1122 continue;
1124 end = buffer+nfilled+nread;
1125 q = buffer;
1126 do {
1127 /* Process complete lines */
1128 p++;
1129 line = PyString_FromStringAndSize(q, p-q);
1130 if (line == NULL)
1131 goto error;
1132 err = PyList_Append(list, line);
1133 Py_DECREF(line);
1134 if (err != 0)
1135 goto error;
1136 q = p;
1137 p = memchr(q, '\n', end-q);
1138 } while (p != NULL);
1139 /* Move the remaining incomplete line to the start */
1140 nfilled = end-q;
1141 memmove(buffer, q, nfilled);
1142 if (sizehint > 0)
1143 if (totalread >= (size_t)sizehint)
1144 break;
1146 if (nfilled != 0) {
1147 /* Partial last line */
1148 line = PyString_FromStringAndSize(buffer, nfilled);
1149 if (line == NULL)
1150 goto error;
1151 if (sizehint > 0) {
1152 /* Need to complete the last line */
1153 PyObject *rest = get_line(f, 0);
1154 if (rest == NULL) {
1155 Py_DECREF(line);
1156 goto error;
1158 PyString_Concat(&line, rest);
1159 Py_DECREF(rest);
1160 if (line == NULL)
1161 goto error;
1163 err = PyList_Append(list, line);
1164 Py_DECREF(line);
1165 if (err != 0)
1166 goto error;
1168 cleanup:
1169 if (big_buffer) {
1170 Py_DECREF(big_buffer);
1172 return list;
1175 static PyObject *
1176 file_write(PyFileObject *f, PyObject *args)
1178 char *s;
1179 int n, n2;
1180 if (f->f_fp == NULL)
1181 return err_closed();
1182 if (!PyArg_ParseTuple(args, f->f_binary ? "s#" : "t#", &s, &n))
1183 return NULL;
1184 f->f_softspace = 0;
1185 Py_BEGIN_ALLOW_THREADS
1186 errno = 0;
1187 n2 = fwrite(s, 1, n, f->f_fp);
1188 Py_END_ALLOW_THREADS
1189 if (n2 != n) {
1190 PyErr_SetFromErrno(PyExc_IOError);
1191 clearerr(f->f_fp);
1192 return NULL;
1194 Py_INCREF(Py_None);
1195 return Py_None;
1198 static PyObject *
1199 file_writelines(PyFileObject *f, PyObject *seq)
1201 #define CHUNKSIZE 1000
1202 PyObject *list, *line;
1203 PyObject *it; /* iter(seq) */
1204 PyObject *result;
1205 int i, j, index, len, nwritten, islist;
1207 assert(seq != NULL);
1208 if (f->f_fp == NULL)
1209 return err_closed();
1211 result = NULL;
1212 list = NULL;
1213 islist = PyList_Check(seq);
1214 if (islist)
1215 it = NULL;
1216 else {
1217 it = PyObject_GetIter(seq);
1218 if (it == NULL) {
1219 PyErr_SetString(PyExc_TypeError,
1220 "writelines() requires an iterable argument");
1221 return NULL;
1223 /* From here on, fail by going to error, to reclaim "it". */
1224 list = PyList_New(CHUNKSIZE);
1225 if (list == NULL)
1226 goto error;
1229 /* Strategy: slurp CHUNKSIZE lines into a private list,
1230 checking that they are all strings, then write that list
1231 without holding the interpreter lock, then come back for more. */
1232 for (index = 0; ; index += CHUNKSIZE) {
1233 if (islist) {
1234 Py_XDECREF(list);
1235 list = PyList_GetSlice(seq, index, index+CHUNKSIZE);
1236 if (list == NULL)
1237 goto error;
1238 j = PyList_GET_SIZE(list);
1240 else {
1241 for (j = 0; j < CHUNKSIZE; j++) {
1242 line = PyIter_Next(it);
1243 if (line == NULL) {
1244 if (PyErr_Occurred())
1245 goto error;
1246 break;
1248 PyList_SetItem(list, j, line);
1251 if (j == 0)
1252 break;
1254 /* Check that all entries are indeed strings. If not,
1255 apply the same rules as for file.write() and
1256 convert the results to strings. This is slow, but
1257 seems to be the only way since all conversion APIs
1258 could potentially execute Python code. */
1259 for (i = 0; i < j; i++) {
1260 PyObject *v = PyList_GET_ITEM(list, i);
1261 if (!PyString_Check(v)) {
1262 const char *buffer;
1263 int len;
1264 if (((f->f_binary &&
1265 PyObject_AsReadBuffer(v,
1266 (const void**)&buffer,
1267 &len)) ||
1268 PyObject_AsCharBuffer(v,
1269 &buffer,
1270 &len))) {
1271 PyErr_SetString(PyExc_TypeError,
1272 "writelines() argument must be a sequence of strings");
1273 goto error;
1275 line = PyString_FromStringAndSize(buffer,
1276 len);
1277 if (line == NULL)
1278 goto error;
1279 Py_DECREF(v);
1280 PyList_SET_ITEM(list, i, line);
1284 /* Since we are releasing the global lock, the
1285 following code may *not* execute Python code. */
1286 Py_BEGIN_ALLOW_THREADS
1287 f->f_softspace = 0;
1288 errno = 0;
1289 for (i = 0; i < j; i++) {
1290 line = PyList_GET_ITEM(list, i);
1291 len = PyString_GET_SIZE(line);
1292 nwritten = fwrite(PyString_AS_STRING(line),
1293 1, len, f->f_fp);
1294 if (nwritten != len) {
1295 Py_BLOCK_THREADS
1296 PyErr_SetFromErrno(PyExc_IOError);
1297 clearerr(f->f_fp);
1298 goto error;
1301 Py_END_ALLOW_THREADS
1303 if (j < CHUNKSIZE)
1304 break;
1307 Py_INCREF(Py_None);
1308 result = Py_None;
1309 error:
1310 Py_XDECREF(list);
1311 Py_XDECREF(it);
1312 return result;
1313 #undef CHUNKSIZE
1316 static char readline_doc[] =
1317 "readline([size]) -> next line from the file, as a string.\n"
1318 "\n"
1319 "Retain newline. A non-negative size argument limits the maximum\n"
1320 "number of bytes to return (an incomplete line may be returned then).\n"
1321 "Return an empty string at EOF.";
1323 static char read_doc[] =
1324 "read([size]) -> read at most size bytes, returned as a string.\n"
1325 "\n"
1326 "If the size argument is negative or omitted, read until EOF is reached.";
1328 static char write_doc[] =
1329 "write(str) -> None. Write string str to file.\n"
1330 "\n"
1331 "Note that due to buffering, flush() or close() may be needed before\n"
1332 "the file on disk reflects the data written.";
1334 static char fileno_doc[] =
1335 "fileno() -> integer \"file descriptor\".\n"
1336 "\n"
1337 "This is needed for lower-level file interfaces, such os.read().";
1339 static char seek_doc[] =
1340 "seek(offset[, whence]) -> None. Move to new file position.\n"
1341 "\n"
1342 "Argument offset is a byte count. Optional argument whence defaults to\n"
1343 "0 (offset from start of file, offset should be >= 0); other values are 1\n"
1344 "(move relative to current position, positive or negative), and 2 (move\n"
1345 "relative to end of file, usually negative, although many platforms allow\n"
1346 "seeking beyond the end of a file).\n"
1347 "\n"
1348 "Note that not all file objects are seekable.";
1350 #ifdef HAVE_FTRUNCATE
1351 static char truncate_doc[] =
1352 "truncate([size]) -> None. Truncate the file to at most size bytes.\n"
1353 "\n"
1354 "Size defaults to the current file position, as returned by tell().";
1355 #endif
1357 static char tell_doc[] =
1358 "tell() -> current file position, an integer (may be a long integer).";
1360 static char readinto_doc[] =
1361 "readinto() -> Undocumented. Don't use this; it may go away.";
1363 static char readlines_doc[] =
1364 "readlines([size]) -> list of strings, each a line from the file.\n"
1365 "\n"
1366 "Call readline() repeatedly and return a list of the lines so read.\n"
1367 "The optional size argument, if given, is an approximate bound on the\n"
1368 "total number of bytes in the lines returned.";
1370 static char xreadlines_doc[] =
1371 "xreadlines() -> next line from the file, as a string.\n"
1372 "\n"
1373 "Equivalent to xreadlines.xreadlines(file). This is like readline(), but\n"
1374 "often quicker, due to reading ahead internally.";
1376 static char writelines_doc[] =
1377 "writelines(sequence_of_strings) -> None. Write the strings to the file.\n"
1378 "\n"
1379 "Note that newlines are not added. The sequence can be any iterable object\n"
1380 "producing strings. This is equivalent to calling write() for each string.";
1382 static char flush_doc[] =
1383 "flush() -> None. Flush the internal I/O buffer.";
1385 static char close_doc[] =
1386 "close() -> None or (perhaps) an integer. Close the file.\n"
1387 "\n"
1388 "Sets data attribute .closed to true. A closed file cannot be used for\n"
1389 "further I/O operations. close() may be called more than once without\n"
1390 "error. Some kinds of file objects (for example, opened by popen())\n"
1391 "may return an exit status upon closing.";
1393 static char isatty_doc[] =
1394 "isatty() -> true or false. True if the file is connected to a tty device.";
1396 static PyMethodDef file_methods[] = {
1397 {"readline", (PyCFunction)file_readline, METH_VARARGS, readline_doc},
1398 {"read", (PyCFunction)file_read, METH_VARARGS, read_doc},
1399 {"write", (PyCFunction)file_write, METH_VARARGS, write_doc},
1400 {"fileno", (PyCFunction)file_fileno, METH_NOARGS, fileno_doc},
1401 {"seek", (PyCFunction)file_seek, METH_VARARGS, seek_doc},
1402 #ifdef HAVE_FTRUNCATE
1403 {"truncate", (PyCFunction)file_truncate, METH_VARARGS, truncate_doc},
1404 #endif
1405 {"tell", (PyCFunction)file_tell, METH_NOARGS, tell_doc},
1406 {"readinto", (PyCFunction)file_readinto, METH_OLDARGS, readinto_doc},
1407 {"readlines", (PyCFunction)file_readlines, METH_VARARGS, readlines_doc},
1408 {"xreadlines", (PyCFunction)file_xreadlines, METH_NOARGS, xreadlines_doc},
1409 {"writelines", (PyCFunction)file_writelines, METH_O, writelines_doc},
1410 {"flush", (PyCFunction)file_flush, METH_NOARGS, flush_doc},
1411 {"close", (PyCFunction)file_close, METH_NOARGS, close_doc},
1412 {"isatty", (PyCFunction)file_isatty, METH_NOARGS, isatty_doc},
1413 {NULL, NULL} /* sentinel */
1416 #define OFF(x) offsetof(PyFileObject, x)
1418 static PyMemberDef file_memberlist[] = {
1419 {"softspace", T_INT, OFF(f_softspace), 0,
1420 "flag indicating that a space needs to be printed; used by print"},
1421 {"mode", T_OBJECT, OFF(f_mode), RO,
1422 "file mode ('r', 'w', 'a', possibly with 'b' or '+' added)"},
1423 {"name", T_OBJECT, OFF(f_name), RO,
1424 "file name"},
1425 /* getattr(f, "closed") is implemented without this table */
1426 {NULL} /* Sentinel */
1429 static PyObject *
1430 get_closed(PyFileObject *f, void *closure)
1432 return PyInt_FromLong((long)(f->f_fp == 0));
1435 static PyGetSetDef file_getsetlist[] = {
1436 {"closed", (getter)get_closed, NULL, "flag set if the file is closed"},
1437 {0},
1440 static PyObject *
1441 file_getiter(PyObject *f)
1443 return PyObject_CallMethod(f, "xreadlines", "");
1446 static PyObject *
1447 file_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1449 PyObject *self;
1450 static PyObject *not_yet_string;
1452 assert(type != NULL && type->tp_alloc != NULL);
1454 if (not_yet_string == NULL) {
1455 not_yet_string = PyString_FromString("<uninitialized file>");
1456 if (not_yet_string == NULL)
1457 return NULL;
1460 self = type->tp_alloc(type, 0);
1461 if (self != NULL) {
1462 /* Always fill in the name and mode, so that nobody else
1463 needs to special-case NULLs there. */
1464 Py_INCREF(not_yet_string);
1465 ((PyFileObject *)self)->f_name = not_yet_string;
1466 Py_INCREF(not_yet_string);
1467 ((PyFileObject *)self)->f_mode = not_yet_string;
1469 return self;
1472 static int
1473 file_init(PyObject *self, PyObject *args, PyObject *kwds)
1475 PyFileObject *foself = (PyFileObject *)self;
1476 int ret = 0;
1477 static char *kwlist[] = {"name", "mode", "buffering", 0};
1478 char *name = NULL;
1479 char *mode = "r";
1480 int bufsize = -1;
1482 assert(PyFile_Check(self));
1483 if (foself->f_fp != NULL) {
1484 /* Have to close the existing file first. */
1485 PyObject *closeresult = file_close(foself);
1486 if (closeresult == NULL)
1487 return -1;
1488 Py_DECREF(closeresult);
1491 if (!PyArg_ParseTupleAndKeywords(args, kwds, "et|si:file", kwlist,
1492 Py_FileSystemDefaultEncoding, &name,
1493 &mode, &bufsize))
1494 return -1;
1495 if (fill_file_fields(foself, NULL, name, mode, fclose) == NULL)
1496 goto Error;
1497 if (open_the_file(foself, name, mode) == NULL)
1498 goto Error;
1499 PyFile_SetBufSize(self, bufsize);
1500 goto Done;
1502 Error:
1503 ret = -1;
1504 /* fall through */
1505 Done:
1506 PyMem_Free(name); /* free the encoded string */
1507 return ret;
1510 static char file_doc[] =
1511 "file(name[, mode[, buffering]]) -> file object\n"
1512 "\n"
1513 "Open a file. The mode can be 'r', 'w' or 'a' for reading (default),\n"
1514 "writing or appending. The file will be created if it doesn't exist\n"
1515 "when opened for writing or appending; it will be truncated when\n"
1516 "opened for writing. Add a 'b' to the mode for binary files.\n"
1517 "Add a '+' to the mode to allow simultaneous reading and writing.\n"
1518 "If the buffering argument is given, 0 means unbuffered, 1 means line\n"
1519 "buffered, and larger numbers specify the buffer size.\n"
1520 "Note: open() is an alias for file().\n";
1522 PyTypeObject PyFile_Type = {
1523 PyObject_HEAD_INIT(&PyType_Type)
1525 "file",
1526 sizeof(PyFileObject),
1528 (destructor)file_dealloc, /* tp_dealloc */
1529 0, /* tp_print */
1530 0, /* tp_getattr */
1531 0, /* tp_setattr */
1532 0, /* tp_compare */
1533 (reprfunc)file_repr, /* tp_repr */
1534 0, /* tp_as_number */
1535 0, /* tp_as_sequence */
1536 0, /* tp_as_mapping */
1537 0, /* tp_hash */
1538 0, /* tp_call */
1539 0, /* tp_str */
1540 PyObject_GenericGetAttr, /* tp_getattro */
1541 0, /* tp_setattro */
1542 0, /* tp_as_buffer */
1543 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
1544 file_doc, /* tp_doc */
1545 0, /* tp_traverse */
1546 0, /* tp_clear */
1547 0, /* tp_richcompare */
1548 0, /* tp_weaklistoffset */
1549 file_getiter, /* tp_iter */
1550 0, /* tp_iternext */
1551 file_methods, /* tp_methods */
1552 file_memberlist, /* tp_members */
1553 file_getsetlist, /* tp_getset */
1554 0, /* tp_base */
1555 0, /* tp_dict */
1556 0, /* tp_descr_get */
1557 0, /* tp_descr_set */
1558 0, /* tp_dictoffset */
1559 (initproc)file_init, /* tp_init */
1560 PyType_GenericAlloc, /* tp_alloc */
1561 file_new, /* tp_new */
1562 _PyObject_Del, /* tp_free */
1565 /* Interface for the 'soft space' between print items. */
1568 PyFile_SoftSpace(PyObject *f, int newflag)
1570 int oldflag = 0;
1571 if (f == NULL) {
1572 /* Do nothing */
1574 else if (PyFile_Check(f)) {
1575 oldflag = ((PyFileObject *)f)->f_softspace;
1576 ((PyFileObject *)f)->f_softspace = newflag;
1578 else {
1579 PyObject *v;
1580 v = PyObject_GetAttrString(f, "softspace");
1581 if (v == NULL)
1582 PyErr_Clear();
1583 else {
1584 if (PyInt_Check(v))
1585 oldflag = PyInt_AsLong(v);
1586 Py_DECREF(v);
1588 v = PyInt_FromLong((long)newflag);
1589 if (v == NULL)
1590 PyErr_Clear();
1591 else {
1592 if (PyObject_SetAttrString(f, "softspace", v) != 0)
1593 PyErr_Clear();
1594 Py_DECREF(v);
1597 return oldflag;
1600 /* Interfaces to write objects/strings to file-like objects */
1603 PyFile_WriteObject(PyObject *v, PyObject *f, int flags)
1605 PyObject *writer, *value, *args, *result;
1606 if (f == NULL) {
1607 PyErr_SetString(PyExc_TypeError, "writeobject with NULL file");
1608 return -1;
1610 else if (PyFile_Check(f)) {
1611 FILE *fp = PyFile_AsFile(f);
1612 if (fp == NULL) {
1613 err_closed();
1614 return -1;
1616 return PyObject_Print(v, fp, flags);
1618 writer = PyObject_GetAttrString(f, "write");
1619 if (writer == NULL)
1620 return -1;
1621 if (flags & Py_PRINT_RAW) {
1622 if (PyUnicode_Check(v)) {
1623 value = v;
1624 Py_INCREF(value);
1625 } else
1626 value = PyObject_Str(v);
1628 else
1629 value = PyObject_Repr(v);
1630 if (value == NULL) {
1631 Py_DECREF(writer);
1632 return -1;
1634 args = Py_BuildValue("(O)", value);
1635 if (args == NULL) {
1636 Py_DECREF(value);
1637 Py_DECREF(writer);
1638 return -1;
1640 result = PyEval_CallObject(writer, args);
1641 Py_DECREF(args);
1642 Py_DECREF(value);
1643 Py_DECREF(writer);
1644 if (result == NULL)
1645 return -1;
1646 Py_DECREF(result);
1647 return 0;
1651 PyFile_WriteString(const char *s, PyObject *f)
1653 if (f == NULL) {
1654 /* Should be caused by a pre-existing error */
1655 if (!PyErr_Occurred())
1656 PyErr_SetString(PyExc_SystemError,
1657 "null file for PyFile_WriteString");
1658 return -1;
1660 else if (PyFile_Check(f)) {
1661 FILE *fp = PyFile_AsFile(f);
1662 if (fp == NULL) {
1663 err_closed();
1664 return -1;
1666 fputs(s, fp);
1667 return 0;
1669 else if (!PyErr_Occurred()) {
1670 PyObject *v = PyString_FromString(s);
1671 int err;
1672 if (v == NULL)
1673 return -1;
1674 err = PyFile_WriteObject(v, f, Py_PRINT_RAW);
1675 Py_DECREF(v);
1676 return err;
1678 else
1679 return -1;
1682 /* Try to get a file-descriptor from a Python object. If the object
1683 is an integer or long integer, its value is returned. If not, the
1684 object's fileno() method is called if it exists; the method must return
1685 an integer or long integer, which is returned as the file descriptor value.
1686 -1 is returned on failure.
1689 int PyObject_AsFileDescriptor(PyObject *o)
1691 int fd;
1692 PyObject *meth;
1694 if (PyInt_Check(o)) {
1695 fd = PyInt_AsLong(o);
1697 else if (PyLong_Check(o)) {
1698 fd = PyLong_AsLong(o);
1700 else if ((meth = PyObject_GetAttrString(o, "fileno")) != NULL)
1702 PyObject *fno = PyEval_CallObject(meth, NULL);
1703 Py_DECREF(meth);
1704 if (fno == NULL)
1705 return -1;
1707 if (PyInt_Check(fno)) {
1708 fd = PyInt_AsLong(fno);
1709 Py_DECREF(fno);
1711 else if (PyLong_Check(fno)) {
1712 fd = PyLong_AsLong(fno);
1713 Py_DECREF(fno);
1715 else {
1716 PyErr_SetString(PyExc_TypeError,
1717 "fileno() returned a non-integer");
1718 Py_DECREF(fno);
1719 return -1;
1722 else {
1723 PyErr_SetString(PyExc_TypeError,
1724 "argument must be an int, or have a fileno() method.");
1725 return -1;
1728 if (fd < 0) {
1729 PyErr_Format(PyExc_ValueError,
1730 "file descriptor cannot be a negative integer (%i)",
1731 fd);
1732 return -1;
1734 return fd;