Files for 2.1b1 distribution.
[python/dscho.git] / Objects / fileobject.c
blob27e21de154ba646e51d8ffb5043acec8e5d64626
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 HAVE_UNISTD_H
12 #include <unistd.h>
13 #endif
15 #ifdef MS_WIN32
16 #define fileno _fileno
17 /* can (almost fully) duplicate with _chsize, see file_truncate */
18 #define HAVE_FTRUNCATE
19 #endif
21 #ifdef macintosh
22 #ifdef USE_GUSI
23 #define HAVE_FTRUNCATE
24 #endif
25 #endif
27 #ifdef __MWERKS__
28 /* Mwerks fopen() doesn't always set errno */
29 #define NO_FOPEN_ERRNO
30 #endif
32 #define BUF(v) PyString_AS_STRING((PyStringObject *)v)
34 #ifndef DONT_HAVE_ERRNO_H
35 #include <errno.h>
36 #endif
39 typedef struct {
40 PyObject_HEAD
41 FILE *f_fp;
42 PyObject *f_name;
43 PyObject *f_mode;
44 int (*f_close)(FILE *);
45 int f_softspace; /* Flag used by 'print' command */
46 int f_binary; /* Flag which indicates whether the file is open
47 open in binary (1) or test (0) mode */
48 } PyFileObject;
50 FILE *
51 PyFile_AsFile(PyObject *f)
53 if (f == NULL || !PyFile_Check(f))
54 return NULL;
55 else
56 return ((PyFileObject *)f)->f_fp;
59 PyObject *
60 PyFile_Name(PyObject *f)
62 if (f == NULL || !PyFile_Check(f))
63 return NULL;
64 else
65 return ((PyFileObject *)f)->f_name;
68 PyObject *
69 PyFile_FromFile(FILE *fp, char *name, char *mode, int (*close)(FILE *))
71 PyFileObject *f = PyObject_NEW(PyFileObject, &PyFile_Type);
72 if (f == NULL)
73 return NULL;
74 f->f_fp = NULL;
75 f->f_name = PyString_FromString(name);
76 f->f_mode = PyString_FromString(mode);
77 f->f_close = close;
78 f->f_softspace = 0;
79 if (strchr(mode,'b') != NULL)
80 f->f_binary = 1;
81 else
82 f->f_binary = 0;
83 if (f->f_name == NULL || f->f_mode == NULL) {
84 Py_DECREF(f);
85 return NULL;
87 f->f_fp = fp;
88 return (PyObject *) f;
91 PyObject *
92 PyFile_FromString(char *name, char *mode)
94 extern int fclose(FILE *);
95 PyFileObject *f;
96 f = (PyFileObject *) PyFile_FromFile((FILE *)NULL, name, mode, fclose);
97 if (f == NULL)
98 return NULL;
99 #ifdef HAVE_FOPENRF
100 if (*mode == '*') {
101 FILE *fopenRF();
102 f->f_fp = fopenRF(name, mode+1);
104 else
105 #endif
107 Py_BEGIN_ALLOW_THREADS
108 f->f_fp = fopen(name, mode);
109 Py_END_ALLOW_THREADS
111 if (f->f_fp == NULL) {
112 #ifdef NO_FOPEN_ERRNO
113 /* Metroworks only, not testable, so unchanged */
114 if ( errno == 0 ) {
115 PyErr_SetString(PyExc_IOError, "Cannot open file");
116 Py_DECREF(f);
117 return NULL;
119 #endif
120 PyErr_SetFromErrnoWithFilename(PyExc_IOError, name);
121 Py_DECREF(f);
122 return NULL;
124 return (PyObject *)f;
127 void
128 PyFile_SetBufSize(PyObject *f, int bufsize)
130 if (bufsize >= 0) {
131 #ifdef HAVE_SETVBUF
132 int type;
133 switch (bufsize) {
134 case 0:
135 type = _IONBF;
136 break;
137 case 1:
138 type = _IOLBF;
139 bufsize = BUFSIZ;
140 break;
141 default:
142 type = _IOFBF;
144 setvbuf(((PyFileObject *)f)->f_fp, (char *)NULL,
145 type, bufsize);
146 #else /* !HAVE_SETVBUF */
147 if (bufsize <= 1)
148 setbuf(((PyFileObject *)f)->f_fp, (char *)NULL);
149 #endif /* !HAVE_SETVBUF */
153 static PyObject *
154 err_closed(void)
156 PyErr_SetString(PyExc_ValueError, "I/O operation on closed file");
157 return NULL;
160 /* Methods */
162 static void
163 file_dealloc(PyFileObject *f)
165 if (f->f_fp != NULL && f->f_close != NULL) {
166 Py_BEGIN_ALLOW_THREADS
167 (*f->f_close)(f->f_fp);
168 Py_END_ALLOW_THREADS
170 if (f->f_name != NULL) {
171 Py_DECREF(f->f_name);
173 if (f->f_mode != NULL) {
174 Py_DECREF(f->f_mode);
176 PyObject_DEL(f);
179 static PyObject *
180 file_repr(PyFileObject *f)
182 char buf[300];
183 sprintf(buf, "<%s file '%.256s', mode '%.10s' at %p>",
184 f->f_fp == NULL ? "closed" : "open",
185 PyString_AsString(f->f_name),
186 PyString_AsString(f->f_mode),
188 return PyString_FromString(buf);
191 static PyObject *
192 file_close(PyFileObject *f, PyObject *args)
194 int sts = 0;
195 if (!PyArg_NoArgs(args))
196 return NULL;
197 if (f->f_fp != NULL) {
198 if (f->f_close != NULL) {
199 Py_BEGIN_ALLOW_THREADS
200 errno = 0;
201 sts = (*f->f_close)(f->f_fp);
202 Py_END_ALLOW_THREADS
204 f->f_fp = NULL;
206 if (sts == EOF)
207 return PyErr_SetFromErrno(PyExc_IOError);
208 if (sts != 0)
209 return PyInt_FromLong((long)sts);
210 Py_INCREF(Py_None);
211 return Py_None;
215 /* An 8-byte off_t-like type */
216 #if defined(HAVE_LARGEFILE_SUPPORT) && SIZEOF_OFF_T < 8 && SIZEOF_FPOS_T >= 8
217 typedef fpos_t Py_off_t;
218 #else
219 typedef off_t Py_off_t;
220 #endif
223 /* a portable fseek() function
224 return 0 on success, non-zero on failure (with errno set) */
226 _portable_fseek(FILE *fp, Py_off_t offset, int whence)
228 #if defined(HAVE_FSEEKO)
229 return fseeko(fp, offset, whence);
230 #elif defined(HAVE_FSEEK64)
231 return fseek64(fp, offset, whence);
232 #elif defined(__BEOS__)
233 return _fseek(fp, offset, whence);
234 #elif defined(HAVE_LARGEFILE_SUPPORT) && SIZEOF_FPOS_T >= 8
235 /* lacking a 64-bit capable fseek(), use a 64-bit capable fsetpos()
236 and fgetpos() to implement fseek()*/
237 fpos_t pos;
238 switch (whence) {
239 case SEEK_END:
240 if (fseek(fp, 0, SEEK_END) != 0)
241 return -1;
242 /* fall through */
243 case SEEK_CUR:
244 if (fgetpos(fp, &pos) != 0)
245 return -1;
246 offset += pos;
247 break;
248 /* case SEEK_SET: break; */
250 return fsetpos(fp, &offset);
251 #else
252 return fseek(fp, offset, whence);
253 #endif
257 /* a portable ftell() function
258 Return -1 on failure with errno set appropriately, current file
259 position on success */
260 Py_off_t
261 _portable_ftell(FILE* fp)
263 #if SIZEOF_FPOS_T >= 8 && defined(HAVE_LARGEFILE_SUPPORT)
264 fpos_t pos;
265 if (fgetpos(fp, &pos) != 0)
266 return -1;
267 return pos;
268 #elif defined(HAVE_FTELLO) && defined(HAVE_LARGEFILE_SUPPORT)
269 return ftello(fp);
270 #elif defined(HAVE_FTELL64) && defined(HAVE_LARGEFILE_SUPPORT)
271 return ftell64(fp);
272 #else
273 return ftell(fp);
274 #endif
278 static PyObject *
279 file_seek(PyFileObject *f, PyObject *args)
281 int whence;
282 int ret;
283 Py_off_t offset;
284 PyObject *offobj;
286 if (f->f_fp == NULL)
287 return err_closed();
288 whence = 0;
289 if (!PyArg_ParseTuple(args, "O|i:seek", &offobj, &whence))
290 return NULL;
291 #if !defined(HAVE_LARGEFILE_SUPPORT)
292 offset = PyInt_AsLong(offobj);
293 #else
294 offset = PyLong_Check(offobj) ?
295 PyLong_AsLongLong(offobj) : PyInt_AsLong(offobj);
296 #endif
297 if (PyErr_Occurred())
298 return NULL;
300 Py_BEGIN_ALLOW_THREADS
301 errno = 0;
302 ret = _portable_fseek(f->f_fp, offset, whence);
303 Py_END_ALLOW_THREADS
305 if (ret != 0) {
306 PyErr_SetFromErrno(PyExc_IOError);
307 clearerr(f->f_fp);
308 return NULL;
310 Py_INCREF(Py_None);
311 return Py_None;
315 #ifdef HAVE_FTRUNCATE
316 static PyObject *
317 file_truncate(PyFileObject *f, PyObject *args)
319 int ret;
320 Py_off_t newsize;
321 PyObject *newsizeobj;
323 if (f->f_fp == NULL)
324 return err_closed();
325 newsizeobj = NULL;
326 if (!PyArg_ParseTuple(args, "|O:truncate", &newsizeobj))
327 return NULL;
328 if (newsizeobj != NULL) {
329 #if !defined(HAVE_LARGEFILE_SUPPORT)
330 newsize = PyInt_AsLong(newsizeobj);
331 #else
332 newsize = PyLong_Check(newsizeobj) ?
333 PyLong_AsLongLong(newsizeobj) :
334 PyInt_AsLong(newsizeobj);
335 #endif
336 if (PyErr_Occurred())
337 return NULL;
338 } else {
339 /* Default to current position*/
340 Py_BEGIN_ALLOW_THREADS
341 errno = 0;
342 newsize = _portable_ftell(f->f_fp);
343 Py_END_ALLOW_THREADS
344 if (newsize == -1) {
345 PyErr_SetFromErrno(PyExc_IOError);
346 clearerr(f->f_fp);
347 return NULL;
350 Py_BEGIN_ALLOW_THREADS
351 errno = 0;
352 ret = fflush(f->f_fp);
353 Py_END_ALLOW_THREADS
354 if (ret != 0) goto onioerror;
356 #ifdef MS_WIN32
357 /* can use _chsize; if, however, the newsize overflows 32-bits then
358 _chsize is *not* adequate; in this case, an OverflowError is raised */
359 if (newsize > LONG_MAX) {
360 PyErr_SetString(PyExc_OverflowError,
361 "the new size is too long for _chsize (it is limited to 32-bit values)");
362 return NULL;
363 } else {
364 Py_BEGIN_ALLOW_THREADS
365 errno = 0;
366 ret = _chsize(fileno(f->f_fp), newsize);
367 Py_END_ALLOW_THREADS
368 if (ret != 0) goto onioerror;
370 #else
371 Py_BEGIN_ALLOW_THREADS
372 errno = 0;
373 ret = ftruncate(fileno(f->f_fp), newsize);
374 Py_END_ALLOW_THREADS
375 if (ret != 0) goto onioerror;
376 #endif /* !MS_WIN32 */
378 Py_INCREF(Py_None);
379 return Py_None;
381 onioerror:
382 PyErr_SetFromErrno(PyExc_IOError);
383 clearerr(f->f_fp);
384 return NULL;
386 #endif /* HAVE_FTRUNCATE */
388 static PyObject *
389 file_tell(PyFileObject *f, PyObject *args)
391 Py_off_t pos;
393 if (f->f_fp == NULL)
394 return err_closed();
395 if (!PyArg_NoArgs(args))
396 return NULL;
397 Py_BEGIN_ALLOW_THREADS
398 errno = 0;
399 pos = _portable_ftell(f->f_fp);
400 Py_END_ALLOW_THREADS
401 if (pos == -1) {
402 PyErr_SetFromErrno(PyExc_IOError);
403 clearerr(f->f_fp);
404 return NULL;
406 #if !defined(HAVE_LARGEFILE_SUPPORT)
407 return PyInt_FromLong(pos);
408 #else
409 return PyLong_FromLongLong(pos);
410 #endif
413 static PyObject *
414 file_fileno(PyFileObject *f, PyObject *args)
416 if (f->f_fp == NULL)
417 return err_closed();
418 if (!PyArg_NoArgs(args))
419 return NULL;
420 return PyInt_FromLong((long) fileno(f->f_fp));
423 static PyObject *
424 file_flush(PyFileObject *f, PyObject *args)
426 int res;
428 if (f->f_fp == NULL)
429 return err_closed();
430 if (!PyArg_NoArgs(args))
431 return NULL;
432 Py_BEGIN_ALLOW_THREADS
433 errno = 0;
434 res = fflush(f->f_fp);
435 Py_END_ALLOW_THREADS
436 if (res != 0) {
437 PyErr_SetFromErrno(PyExc_IOError);
438 clearerr(f->f_fp);
439 return NULL;
441 Py_INCREF(Py_None);
442 return Py_None;
445 static PyObject *
446 file_isatty(PyFileObject *f, PyObject *args)
448 long res;
449 if (f->f_fp == NULL)
450 return err_closed();
451 if (!PyArg_NoArgs(args))
452 return NULL;
453 Py_BEGIN_ALLOW_THREADS
454 res = isatty((int)fileno(f->f_fp));
455 Py_END_ALLOW_THREADS
456 return PyInt_FromLong(res);
460 #if BUFSIZ < 8192
461 #define SMALLCHUNK 8192
462 #else
463 #define SMALLCHUNK BUFSIZ
464 #endif
466 #if SIZEOF_INT < 4
467 #define BIGCHUNK (512 * 32)
468 #else
469 #define BIGCHUNK (512 * 1024)
470 #endif
472 static size_t
473 new_buffersize(PyFileObject *f, size_t currentsize)
475 #ifdef HAVE_FSTAT
476 long pos, end;
477 struct stat st;
478 if (fstat(fileno(f->f_fp), &st) == 0) {
479 end = st.st_size;
480 /* The following is not a bug: we really need to call lseek()
481 *and* ftell(). The reason is that some stdio libraries
482 mistakenly flush their buffer when ftell() is called and
483 the lseek() call it makes fails, thereby throwing away
484 data that cannot be recovered in any way. To avoid this,
485 we first test lseek(), and only call ftell() if lseek()
486 works. We can't use the lseek() value either, because we
487 need to take the amount of buffered data into account.
488 (Yet another reason why stdio stinks. :-) */
489 pos = lseek(fileno(f->f_fp), 0L, SEEK_CUR);
490 if (pos >= 0)
491 pos = ftell(f->f_fp);
492 if (pos < 0)
493 clearerr(f->f_fp);
494 if (end > pos && pos >= 0)
495 return currentsize + end - pos + 1;
496 /* Add 1 so if the file were to grow we'd notice. */
498 #endif
499 if (currentsize > SMALLCHUNK) {
500 /* Keep doubling until we reach BIGCHUNK;
501 then keep adding BIGCHUNK. */
502 if (currentsize <= BIGCHUNK)
503 return currentsize + currentsize;
504 else
505 return currentsize + BIGCHUNK;
507 return currentsize + SMALLCHUNK;
510 static PyObject *
511 file_read(PyFileObject *f, PyObject *args)
513 long bytesrequested = -1;
514 size_t bytesread, buffersize, chunksize;
515 PyObject *v;
517 if (f->f_fp == NULL)
518 return err_closed();
519 if (!PyArg_ParseTuple(args, "|l:read", &bytesrequested))
520 return NULL;
521 if (bytesrequested < 0)
522 buffersize = new_buffersize(f, (size_t)0);
523 else
524 buffersize = bytesrequested;
525 if (buffersize > INT_MAX) {
526 PyErr_SetString(PyExc_OverflowError,
527 "requested number of bytes is more than a Python string can hold");
528 return NULL;
530 v = PyString_FromStringAndSize((char *)NULL, buffersize);
531 if (v == NULL)
532 return NULL;
533 bytesread = 0;
534 for (;;) {
535 Py_BEGIN_ALLOW_THREADS
536 errno = 0;
537 chunksize = fread(BUF(v) + bytesread, 1,
538 buffersize - bytesread, f->f_fp);
539 Py_END_ALLOW_THREADS
540 if (chunksize == 0) {
541 if (!ferror(f->f_fp))
542 break;
543 PyErr_SetFromErrno(PyExc_IOError);
544 clearerr(f->f_fp);
545 Py_DECREF(v);
546 return NULL;
548 bytesread += chunksize;
549 if (bytesread < buffersize)
550 break;
551 if (bytesrequested < 0) {
552 buffersize = new_buffersize(f, buffersize);
553 if (_PyString_Resize(&v, buffersize) < 0)
554 return NULL;
557 if (bytesread != buffersize)
558 _PyString_Resize(&v, bytesread);
559 return v;
562 static PyObject *
563 file_readinto(PyFileObject *f, PyObject *args)
565 char *ptr;
566 size_t ntodo, ndone, nnow;
568 if (f->f_fp == NULL)
569 return err_closed();
570 if (!PyArg_Parse(args, "w#", &ptr, &ntodo))
571 return NULL;
572 ndone = 0;
573 while (ntodo > 0) {
574 Py_BEGIN_ALLOW_THREADS
575 errno = 0;
576 nnow = fread(ptr+ndone, 1, ntodo, f->f_fp);
577 Py_END_ALLOW_THREADS
578 if (nnow == 0) {
579 if (!ferror(f->f_fp))
580 break;
581 PyErr_SetFromErrno(PyExc_IOError);
582 clearerr(f->f_fp);
583 return NULL;
585 ndone += nnow;
586 ntodo -= nnow;
588 return PyInt_FromLong((long)ndone);
591 /**************************************************************************
592 Routine to get next line using platform fgets().
594 Under MSVC 6:
596 + MS threadsafe getc is very slow (multiple layers of function calls before+
597 after each character, to lock+unlock the stream).
598 + The stream-locking functions are MS-internal -- can't access them from user
599 code.
600 + There's nothing Tim could find in the MS C or platform SDK libraries that
601 can worm around this.
602 + MS fgets locks/unlocks only once per line; it's the only hook we have.
604 So we use fgets for speed(!), despite that it's painful.
606 MS realloc is also slow.
608 Reports from other platforms on this method vs getc_unlocked (which MS doesn't
609 have):
610 Linux a wash
611 Solaris a wash
612 Tru64 Unix getline_via_fgets significantly faster
614 CAUTION: The C std isn't clear about this: in those cases where fgets
615 writes something into the buffer, can it write into any position beyond the
616 required trailing null byte? MSVC 6 fgets does not, and no platform is (yet)
617 known on which it does; and it would be a strange way to code fgets. Still,
618 getline_via_fgets may not work correctly if it does. The std test
619 test_bufio.py should fail if platform fgets() routinely writes beyond the
620 trailing null byte. #define DONT_USE_FGETS_IN_GETLINE to disable this code.
621 **************************************************************************/
623 /* Use this routine if told to, or by default on non-get_unlocked()
624 * platforms unless told not to. Yikes! Let's spell that out:
625 * On a platform with getc_unlocked():
626 * By default, use getc_unlocked().
627 * If you want to use fgets() instead, #define USE_FGETS_IN_GETLINE.
628 * On a platform without getc_unlocked():
629 * By default, use fgets().
630 * If you don't want to use fgets(), #define DONT_USE_FGETS_IN_GETLINE.
632 #if !defined(USE_FGETS_IN_GETLINE) && !defined(HAVE_GETC_UNLOCKED)
633 #define USE_FGETS_IN_GETLINE
634 #endif
636 #if defined(DONT_USE_FGETS_IN_GETLINE) && defined(USE_FGETS_IN_GETLINE)
637 #undef USE_FGETS_IN_GETLINE
638 #endif
640 #ifdef USE_FGETS_IN_GETLINE
641 static PyObject*
642 getline_via_fgets(FILE *fp)
644 /* INITBUFSIZE is the maximum line length that lets us get away with the fast
645 * no-realloc, one-fgets()-call path. Boosting it isn't free, because we have
646 * to fill this much of the buffer with a known value in order to figure out
647 * how much of the buffer fgets() overwrites. So if INITBUFSIZE is larger
648 * than "most" lines, we waste time filling unused buffer slots. 100 is
649 * surely adequate for most peoples' email archives, chewing over source code,
650 * etc -- "regular old text files".
651 * MAXBUFSIZE is the maximum line length that lets us get away with the less
652 * fast (but still zippy) no-realloc, two-fgets()-call path. See above for
653 * cautions about boosting that. 300 was chosen because the worst real-life
654 * text-crunching job reported on Python-Dev was a mail-log crawler where over
655 * half the lines were 254 chars.
656 * INCBUFSIZE is the amount by which we grow the buffer, if MAXBUFSIZE isn't
657 * enough. It doesn't much matter what this is set to: we only get here for
658 * absurdly long lines anyway.
660 #define INITBUFSIZE 100
661 #define MAXBUFSIZE 300
662 #define INCBUFSIZE 1000
663 char* p; /* temp */
664 char buf[MAXBUFSIZE];
665 PyObject* v; /* the string object result */
666 char* pvfree; /* address of next free slot */
667 char* pvend; /* address one beyond last free slot */
668 size_t nfree; /* # of free buffer slots; pvend-pvfree */
669 size_t total_v_size; /* total # of slots in buffer */
671 /* Optimize for normal case: avoid _PyString_Resize if at all
672 * possible via first reading into stack buffer "buf".
674 total_v_size = INITBUFSIZE; /* start small and pray */
675 pvfree = buf;
676 for (;;) {
677 Py_BEGIN_ALLOW_THREADS
678 pvend = buf + total_v_size;
679 nfree = pvend - pvfree;
680 memset(pvfree, '\n', nfree);
681 p = fgets(pvfree, nfree, fp);
682 Py_END_ALLOW_THREADS
684 if (p == NULL) {
685 clearerr(fp);
686 if (PyErr_CheckSignals())
687 return NULL;
688 v = PyString_FromStringAndSize(buf, pvfree - buf);
689 return v;
691 /* fgets read *something* */
692 p = memchr(pvfree, '\n', nfree);
693 if (p != NULL) {
694 /* Did the \n come from fgets or from us?
695 * Since fgets stops at the first \n, and then writes
696 * \0, if it's from fgets a \0 must be next. But if
697 * that's so, it could not have come from us, since
698 * the \n's we filled the buffer with have only more
699 * \n's to the right.
701 if (p+1 < pvend && *(p+1) == '\0') {
702 /* It's from fgets: we win! In particular,
703 * we haven't done any mallocs yet, and can
704 * build the final result on the first try.
706 ++p; /* include \n from fgets */
708 else {
709 /* Must be from us: fgets didn't fill the
710 * buffer and didn't find a newline, so it
711 * must be the last and newline-free line of
712 * the file.
714 assert(p > pvfree && *(p-1) == '\0');
715 --p; /* don't include \0 from fgets */
717 v = PyString_FromStringAndSize(buf, p - buf);
718 return v;
720 /* yuck: fgets overwrote all the newlines, i.e. the entire
721 * buffer. So this line isn't over yet, or maybe it is but
722 * we're exactly at EOF. If we haven't already, try using the
723 * rest of the stack buffer.
725 assert(*(pvend-1) == '\0');
726 if (pvfree == buf) {
727 pvfree = pvend - 1; /* overwrite trailing null */
728 total_v_size = MAXBUFSIZE;
730 else
731 break;
734 /* The stack buffer isn't big enough; malloc a string object and read
735 * into its buffer.
737 total_v_size = MAXBUFSIZE + INCBUFSIZE;
738 v = PyString_FromStringAndSize((char*)NULL, (int)total_v_size);
739 if (v == NULL)
740 return v;
741 /* copy over everything except the last null byte */
742 memcpy(BUF(v), buf, MAXBUFSIZE-1);
743 pvfree = BUF(v) + MAXBUFSIZE - 1;
745 /* Keep reading stuff into v; if it ever ends successfully, break
746 * after setting p one beyond the end of the line. The code here is
747 * very much like the code above, except reads into v's buffer; see
748 * the code above for detailed comments about the logic.
750 for (;;) {
751 Py_BEGIN_ALLOW_THREADS
752 pvend = BUF(v) + total_v_size;
753 nfree = pvend - pvfree;
754 memset(pvfree, '\n', nfree);
755 p = fgets(pvfree, nfree, fp);
756 Py_END_ALLOW_THREADS
758 if (p == NULL) {
759 clearerr(fp);
760 if (PyErr_CheckSignals()) {
761 Py_DECREF(v);
762 return NULL;
764 p = pvfree;
765 break;
767 p = memchr(pvfree, '\n', nfree);
768 if (p != NULL) {
769 if (p+1 < pvend && *(p+1) == '\0') {
770 /* \n came from fgets */
771 ++p;
772 break;
774 /* \n came from us; last line of file, no newline */
775 assert(p > pvfree && *(p-1) == '\0');
776 --p;
777 break;
779 /* expand buffer and try again */
780 assert(*(pvend-1) == '\0');
781 total_v_size += INCBUFSIZE;
782 if (total_v_size > INT_MAX) {
783 PyErr_SetString(PyExc_OverflowError,
784 "line is longer than a Python string can hold");
785 Py_DECREF(v);
786 return NULL;
788 if (_PyString_Resize(&v, (int)total_v_size) < 0)
789 return NULL;
790 /* overwrite the trailing null byte */
791 pvfree = BUF(v) + (total_v_size - INCBUFSIZE - 1);
793 if (BUF(v) + total_v_size != p)
794 _PyString_Resize(&v, p - BUF(v));
795 return v;
796 #undef INITBUFSIZE
797 #undef MAXBUFSIZE
798 #undef INCBUFSIZE
800 #endif /* ifdef USE_FGETS_IN_GETLINE */
802 /* Internal routine to get a line.
803 Size argument interpretation:
804 > 0: max length;
805 <= 0: read arbitrary line
808 #ifdef HAVE_GETC_UNLOCKED
809 #define GETC(f) getc_unlocked(f)
810 #define FLOCKFILE(f) flockfile(f)
811 #define FUNLOCKFILE(f) funlockfile(f)
812 #else
813 #define GETC(f) getc(f)
814 #define FLOCKFILE(f)
815 #define FUNLOCKFILE(f)
816 #endif
818 static PyObject *
819 get_line(PyFileObject *f, int n)
821 FILE *fp = f->f_fp;
822 int c;
823 char *buf, *end;
824 size_t n1, n2;
825 PyObject *v;
827 #ifdef USE_FGETS_IN_GETLINE
828 if (n <= 0)
829 return getline_via_fgets(fp);
830 #endif
831 n2 = n > 0 ? n : 100;
832 v = PyString_FromStringAndSize((char *)NULL, n2);
833 if (v == NULL)
834 return NULL;
835 buf = BUF(v);
836 end = buf + n2;
838 for (;;) {
839 Py_BEGIN_ALLOW_THREADS
840 FLOCKFILE(fp);
841 while ((c = GETC(fp)) != EOF &&
842 (*buf++ = c) != '\n' &&
843 buf != end)
845 FUNLOCKFILE(fp);
846 Py_END_ALLOW_THREADS
847 if (c == '\n')
848 break;
849 if (c == EOF) {
850 clearerr(fp);
851 if (PyErr_CheckSignals()) {
852 Py_DECREF(v);
853 return NULL;
855 break;
857 /* Must be because buf == end */
858 if (n > 0)
859 break;
860 n1 = n2;
861 n2 += 1000;
862 if (n2 > INT_MAX) {
863 PyErr_SetString(PyExc_OverflowError,
864 "line is longer than a Python string can hold");
865 Py_DECREF(v);
866 return NULL;
868 if (_PyString_Resize(&v, n2) < 0)
869 return NULL;
870 buf = BUF(v) + n1;
871 end = BUF(v) + n2;
874 n1 = buf - BUF(v);
875 if (n1 != n2)
876 _PyString_Resize(&v, n1);
877 return v;
880 /* External C interface */
882 PyObject *
883 PyFile_GetLine(PyObject *f, int n)
885 PyObject *result;
887 if (f == NULL) {
888 PyErr_BadInternalCall();
889 return NULL;
892 if (PyFile_Check(f)) {
893 if (((PyFileObject*)f)->f_fp == NULL)
894 return err_closed();
895 result = get_line((PyFileObject *)f, n);
897 else {
898 PyObject *reader;
899 PyObject *args;
901 reader = PyObject_GetAttrString(f, "readline");
902 if (reader == NULL)
903 return NULL;
904 if (n <= 0)
905 args = Py_BuildValue("()");
906 else
907 args = Py_BuildValue("(i)", n);
908 if (args == NULL) {
909 Py_DECREF(reader);
910 return NULL;
912 result = PyEval_CallObject(reader, args);
913 Py_DECREF(reader);
914 Py_DECREF(args);
915 if (result != NULL && !PyString_Check(result)) {
916 Py_DECREF(result);
917 result = NULL;
918 PyErr_SetString(PyExc_TypeError,
919 "object.readline() returned non-string");
923 if (n < 0 && result != NULL && PyString_Check(result)) {
924 char *s = PyString_AS_STRING(result);
925 int len = PyString_GET_SIZE(result);
926 if (len == 0) {
927 Py_DECREF(result);
928 result = NULL;
929 PyErr_SetString(PyExc_EOFError,
930 "EOF when reading a line");
932 else if (s[len-1] == '\n') {
933 if (result->ob_refcnt == 1)
934 _PyString_Resize(&result, len-1);
935 else {
936 PyObject *v;
937 v = PyString_FromStringAndSize(s, len-1);
938 Py_DECREF(result);
939 result = v;
943 return result;
946 /* Python method */
948 static PyObject *
949 file_readline(PyFileObject *f, PyObject *args)
951 int n = -1;
953 if (f->f_fp == NULL)
954 return err_closed();
955 if (!PyArg_ParseTuple(args, "|i:readline", &n))
956 return NULL;
957 if (n == 0)
958 return PyString_FromString("");
959 if (n < 0)
960 n = 0;
961 return get_line(f, n);
964 static PyObject *
965 file_xreadlines(PyFileObject *f, PyObject *args)
967 static PyObject* xreadlines_function = NULL;
969 if (!PyArg_ParseTuple(args, ":xreadlines"))
970 return NULL;
972 if (!xreadlines_function) {
973 PyObject *xreadlines_module =
974 PyImport_ImportModule("xreadlines");
975 if(!xreadlines_module)
976 return NULL;
978 xreadlines_function = PyObject_GetAttrString(xreadlines_module,
979 "xreadlines");
980 Py_DECREF(xreadlines_module);
981 if(!xreadlines_function)
982 return NULL;
984 return PyObject_CallFunction(xreadlines_function, "(O)", f);
987 static PyObject *
988 file_readlines(PyFileObject *f, PyObject *args)
990 long sizehint = 0;
991 PyObject *list;
992 PyObject *line;
993 char small_buffer[SMALLCHUNK];
994 char *buffer = small_buffer;
995 size_t buffersize = SMALLCHUNK;
996 PyObject *big_buffer = NULL;
997 size_t nfilled = 0;
998 size_t nread;
999 size_t totalread = 0;
1000 char *p, *q, *end;
1001 int err;
1003 if (f->f_fp == NULL)
1004 return err_closed();
1005 if (!PyArg_ParseTuple(args, "|l:readlines", &sizehint))
1006 return NULL;
1007 if ((list = PyList_New(0)) == NULL)
1008 return NULL;
1009 for (;;) {
1010 Py_BEGIN_ALLOW_THREADS
1011 errno = 0;
1012 nread = fread(buffer+nfilled, 1, buffersize-nfilled, f->f_fp);
1013 Py_END_ALLOW_THREADS
1014 if (nread == 0) {
1015 sizehint = 0;
1016 if (!ferror(f->f_fp))
1017 break;
1018 PyErr_SetFromErrno(PyExc_IOError);
1019 clearerr(f->f_fp);
1020 error:
1021 Py_DECREF(list);
1022 list = NULL;
1023 goto cleanup;
1025 totalread += nread;
1026 p = memchr(buffer+nfilled, '\n', nread);
1027 if (p == NULL) {
1028 /* Need a larger buffer to fit this line */
1029 nfilled += nread;
1030 buffersize *= 2;
1031 if (buffersize > INT_MAX) {
1032 PyErr_SetString(PyExc_OverflowError,
1033 "line is longer than a Python string can hold");
1034 goto error;
1036 if (big_buffer == NULL) {
1037 /* Create the big buffer */
1038 big_buffer = PyString_FromStringAndSize(
1039 NULL, buffersize);
1040 if (big_buffer == NULL)
1041 goto error;
1042 buffer = PyString_AS_STRING(big_buffer);
1043 memcpy(buffer, small_buffer, nfilled);
1045 else {
1046 /* Grow the big buffer */
1047 _PyString_Resize(&big_buffer, buffersize);
1048 buffer = PyString_AS_STRING(big_buffer);
1050 continue;
1052 end = buffer+nfilled+nread;
1053 q = buffer;
1054 do {
1055 /* Process complete lines */
1056 p++;
1057 line = PyString_FromStringAndSize(q, p-q);
1058 if (line == NULL)
1059 goto error;
1060 err = PyList_Append(list, line);
1061 Py_DECREF(line);
1062 if (err != 0)
1063 goto error;
1064 q = p;
1065 p = memchr(q, '\n', end-q);
1066 } while (p != NULL);
1067 /* Move the remaining incomplete line to the start */
1068 nfilled = end-q;
1069 memmove(buffer, q, nfilled);
1070 if (sizehint > 0)
1071 if (totalread >= (size_t)sizehint)
1072 break;
1074 if (nfilled != 0) {
1075 /* Partial last line */
1076 line = PyString_FromStringAndSize(buffer, nfilled);
1077 if (line == NULL)
1078 goto error;
1079 if (sizehint > 0) {
1080 /* Need to complete the last line */
1081 PyObject *rest = get_line(f, 0);
1082 if (rest == NULL) {
1083 Py_DECREF(line);
1084 goto error;
1086 PyString_Concat(&line, rest);
1087 Py_DECREF(rest);
1088 if (line == NULL)
1089 goto error;
1091 err = PyList_Append(list, line);
1092 Py_DECREF(line);
1093 if (err != 0)
1094 goto error;
1096 cleanup:
1097 if (big_buffer) {
1098 Py_DECREF(big_buffer);
1100 return list;
1103 static PyObject *
1104 file_write(PyFileObject *f, PyObject *args)
1106 char *s;
1107 int n, n2;
1108 if (f->f_fp == NULL)
1109 return err_closed();
1110 if (!PyArg_Parse(args, f->f_binary ? "s#" : "t#", &s, &n))
1111 return NULL;
1112 f->f_softspace = 0;
1113 Py_BEGIN_ALLOW_THREADS
1114 errno = 0;
1115 n2 = fwrite(s, 1, n, f->f_fp);
1116 Py_END_ALLOW_THREADS
1117 if (n2 != n) {
1118 PyErr_SetFromErrno(PyExc_IOError);
1119 clearerr(f->f_fp);
1120 return NULL;
1122 Py_INCREF(Py_None);
1123 return Py_None;
1126 static PyObject *
1127 file_writelines(PyFileObject *f, PyObject *args)
1129 #define CHUNKSIZE 1000
1130 PyObject *list, *line;
1131 PyObject *result;
1132 int i, j, index, len, nwritten, islist;
1134 if (f->f_fp == NULL)
1135 return err_closed();
1136 if (args == NULL || !PySequence_Check(args)) {
1137 PyErr_SetString(PyExc_TypeError,
1138 "writelines() argument must be a sequence of strings");
1139 return NULL;
1141 islist = PyList_Check(args);
1143 /* Strategy: slurp CHUNKSIZE lines into a private list,
1144 checking that they are all strings, then write that list
1145 without holding the interpreter lock, then come back for more. */
1146 index = 0;
1147 if (islist)
1148 list = NULL;
1149 else {
1150 list = PyList_New(CHUNKSIZE);
1151 if (list == NULL)
1152 return NULL;
1154 result = NULL;
1156 for (;;) {
1157 if (islist) {
1158 Py_XDECREF(list);
1159 list = PyList_GetSlice(args, index, index+CHUNKSIZE);
1160 if (list == NULL)
1161 return NULL;
1162 j = PyList_GET_SIZE(list);
1164 else {
1165 for (j = 0; j < CHUNKSIZE; j++) {
1166 line = PySequence_GetItem(args, index+j);
1167 if (line == NULL) {
1168 if (PyErr_ExceptionMatches(
1169 PyExc_IndexError)) {
1170 PyErr_Clear();
1171 break;
1173 /* Some other error occurred.
1174 XXX We may lose some output. */
1175 goto error;
1177 PyList_SetItem(list, j, line);
1180 if (j == 0)
1181 break;
1183 /* Check that all entries are indeed strings. If not,
1184 apply the same rules as for file.write() and
1185 convert the results to strings. This is slow, but
1186 seems to be the only way since all conversion APIs
1187 could potentially execute Python code. */
1188 for (i = 0; i < j; i++) {
1189 PyObject *v = PyList_GET_ITEM(list, i);
1190 if (!PyString_Check(v)) {
1191 const char *buffer;
1192 int len;
1193 if (((f->f_binary &&
1194 PyObject_AsReadBuffer(v,
1195 (const void**)&buffer,
1196 &len)) ||
1197 PyObject_AsCharBuffer(v,
1198 &buffer,
1199 &len))) {
1200 PyErr_SetString(PyExc_TypeError,
1201 "writelines() argument must be a sequence of strings");
1202 goto error;
1204 line = PyString_FromStringAndSize(buffer,
1205 len);
1206 if (line == NULL)
1207 goto error;
1208 Py_DECREF(v);
1209 PyList_SET_ITEM(list, i, line);
1213 /* Since we are releasing the global lock, the
1214 following code may *not* execute Python code. */
1215 Py_BEGIN_ALLOW_THREADS
1216 f->f_softspace = 0;
1217 errno = 0;
1218 for (i = 0; i < j; i++) {
1219 line = PyList_GET_ITEM(list, i);
1220 len = PyString_GET_SIZE(line);
1221 nwritten = fwrite(PyString_AS_STRING(line),
1222 1, len, f->f_fp);
1223 if (nwritten != len) {
1224 Py_BLOCK_THREADS
1225 PyErr_SetFromErrno(PyExc_IOError);
1226 clearerr(f->f_fp);
1227 goto error;
1230 Py_END_ALLOW_THREADS
1232 if (j < CHUNKSIZE)
1233 break;
1234 index += CHUNKSIZE;
1237 Py_INCREF(Py_None);
1238 result = Py_None;
1239 error:
1240 Py_XDECREF(list);
1241 return result;
1244 static PyMethodDef file_methods[] = {
1245 {"readline", (PyCFunction)file_readline, 1},
1246 {"read", (PyCFunction)file_read, 1},
1247 {"write", (PyCFunction)file_write, 0},
1248 {"fileno", (PyCFunction)file_fileno, 0},
1249 {"seek", (PyCFunction)file_seek, 1},
1250 #ifdef HAVE_FTRUNCATE
1251 {"truncate", (PyCFunction)file_truncate, 1},
1252 #endif
1253 {"tell", (PyCFunction)file_tell, 0},
1254 {"readinto", (PyCFunction)file_readinto, 0},
1255 {"readlines", (PyCFunction)file_readlines, 1},
1256 {"xreadlines", (PyCFunction)file_xreadlines, 1},
1257 {"writelines", (PyCFunction)file_writelines, 0},
1258 {"flush", (PyCFunction)file_flush, 0},
1259 {"close", (PyCFunction)file_close, 0},
1260 {"isatty", (PyCFunction)file_isatty, 0},
1261 {NULL, NULL} /* sentinel */
1264 #define OFF(x) offsetof(PyFileObject, x)
1266 static struct memberlist file_memberlist[] = {
1267 {"softspace", T_INT, OFF(f_softspace)},
1268 {"mode", T_OBJECT, OFF(f_mode), RO},
1269 {"name", T_OBJECT, OFF(f_name), RO},
1270 /* getattr(f, "closed") is implemented without this table */
1271 {"closed", T_INT, 0, RO},
1272 {NULL} /* Sentinel */
1275 static PyObject *
1276 file_getattr(PyFileObject *f, char *name)
1278 PyObject *res;
1280 res = Py_FindMethod(file_methods, (PyObject *)f, name);
1281 if (res != NULL)
1282 return res;
1283 PyErr_Clear();
1284 if (strcmp(name, "closed") == 0)
1285 return PyInt_FromLong((long)(f->f_fp == 0));
1286 return PyMember_Get((char *)f, file_memberlist, name);
1289 static int
1290 file_setattr(PyFileObject *f, char *name, PyObject *v)
1292 if (v == NULL) {
1293 PyErr_SetString(PyExc_AttributeError,
1294 "can't delete file attributes");
1295 return -1;
1297 return PyMember_Set((char *)f, file_memberlist, name, v);
1300 PyTypeObject PyFile_Type = {
1301 PyObject_HEAD_INIT(&PyType_Type)
1303 "file",
1304 sizeof(PyFileObject),
1306 (destructor)file_dealloc, /*tp_dealloc*/
1307 0, /*tp_print*/
1308 (getattrfunc)file_getattr, /*tp_getattr*/
1309 (setattrfunc)file_setattr, /*tp_setattr*/
1310 0, /*tp_compare*/
1311 (reprfunc)file_repr, /*tp_repr*/
1314 /* Interface for the 'soft space' between print items. */
1317 PyFile_SoftSpace(PyObject *f, int newflag)
1319 int oldflag = 0;
1320 if (f == NULL) {
1321 /* Do nothing */
1323 else if (PyFile_Check(f)) {
1324 oldflag = ((PyFileObject *)f)->f_softspace;
1325 ((PyFileObject *)f)->f_softspace = newflag;
1327 else {
1328 PyObject *v;
1329 v = PyObject_GetAttrString(f, "softspace");
1330 if (v == NULL)
1331 PyErr_Clear();
1332 else {
1333 if (PyInt_Check(v))
1334 oldflag = PyInt_AsLong(v);
1335 Py_DECREF(v);
1337 v = PyInt_FromLong((long)newflag);
1338 if (v == NULL)
1339 PyErr_Clear();
1340 else {
1341 if (PyObject_SetAttrString(f, "softspace", v) != 0)
1342 PyErr_Clear();
1343 Py_DECREF(v);
1346 return oldflag;
1349 /* Interfaces to write objects/strings to file-like objects */
1352 PyFile_WriteObject(PyObject *v, PyObject *f, int flags)
1354 PyObject *writer, *value, *args, *result;
1355 if (f == NULL) {
1356 PyErr_SetString(PyExc_TypeError, "writeobject with NULL file");
1357 return -1;
1359 else if (PyFile_Check(f)) {
1360 FILE *fp = PyFile_AsFile(f);
1361 if (fp == NULL) {
1362 err_closed();
1363 return -1;
1365 return PyObject_Print(v, fp, flags);
1367 writer = PyObject_GetAttrString(f, "write");
1368 if (writer == NULL)
1369 return -1;
1370 if (flags & Py_PRINT_RAW)
1371 value = PyObject_Str(v);
1372 else
1373 value = PyObject_Repr(v);
1374 if (value == NULL) {
1375 Py_DECREF(writer);
1376 return -1;
1378 args = Py_BuildValue("(O)", value);
1379 if (args == NULL) {
1380 Py_DECREF(value);
1381 Py_DECREF(writer);
1382 return -1;
1384 result = PyEval_CallObject(writer, args);
1385 Py_DECREF(args);
1386 Py_DECREF(value);
1387 Py_DECREF(writer);
1388 if (result == NULL)
1389 return -1;
1390 Py_DECREF(result);
1391 return 0;
1395 PyFile_WriteString(char *s, PyObject *f)
1397 if (f == NULL) {
1398 /* Should be caused by a pre-existing error */
1399 if (!PyErr_Occurred())
1400 PyErr_SetString(PyExc_SystemError,
1401 "null file for PyFile_WriteString");
1402 return -1;
1404 else if (PyFile_Check(f)) {
1405 FILE *fp = PyFile_AsFile(f);
1406 if (fp == NULL) {
1407 err_closed();
1408 return -1;
1410 fputs(s, fp);
1411 return 0;
1413 else if (!PyErr_Occurred()) {
1414 PyObject *v = PyString_FromString(s);
1415 int err;
1416 if (v == NULL)
1417 return -1;
1418 err = PyFile_WriteObject(v, f, Py_PRINT_RAW);
1419 Py_DECREF(v);
1420 return err;
1422 else
1423 return -1;
1426 /* Try to get a file-descriptor from a Python object. If the object
1427 is an integer or long integer, its value is returned. If not, the
1428 object's fileno() method is called if it exists; the method must return
1429 an integer or long integer, which is returned as the file descriptor value.
1430 -1 is returned on failure.
1433 int PyObject_AsFileDescriptor(PyObject *o)
1435 int fd;
1436 PyObject *meth;
1438 if (PyInt_Check(o)) {
1439 fd = PyInt_AsLong(o);
1441 else if (PyLong_Check(o)) {
1442 fd = PyLong_AsLong(o);
1444 else if ((meth = PyObject_GetAttrString(o, "fileno")) != NULL)
1446 PyObject *fno = PyEval_CallObject(meth, NULL);
1447 Py_DECREF(meth);
1448 if (fno == NULL)
1449 return -1;
1451 if (PyInt_Check(fno)) {
1452 fd = PyInt_AsLong(fno);
1453 Py_DECREF(fno);
1455 else if (PyLong_Check(fno)) {
1456 fd = PyLong_AsLong(fno);
1457 Py_DECREF(fno);
1459 else {
1460 PyErr_SetString(PyExc_TypeError,
1461 "fileno() returned a non-integer");
1462 Py_DECREF(fno);
1463 return -1;
1466 else {
1467 PyErr_SetString(PyExc_TypeError,
1468 "argument must be an int, or have a fileno() method.");
1469 return -1;
1472 if (fd < 0) {
1473 PyErr_Format(PyExc_ValueError,
1474 "file descriptor cannot be a negative integer (%i)",
1475 fd);
1476 return -1;
1478 return fd;