2 /* File object implementation */
5 #include "structmember.h"
7 #ifndef DONT_HAVE_SYS_TYPES_H
9 #endif /* DONT_HAVE_SYS_TYPES_H */
16 #define fileno _fileno
17 /* can (almost fully) duplicate with _chsize, see file_truncate */
18 #define HAVE_FTRUNCATE
23 #define HAVE_FTRUNCATE
28 /* Mwerks fopen() doesn't always set errno */
29 #define NO_FOPEN_ERRNO
32 #define BUF(v) PyString_AS_STRING((PyStringObject *)v)
34 #ifndef DONT_HAVE_ERRNO_H
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 */
51 PyFile_AsFile(PyObject
*f
)
53 if (f
== NULL
|| !PyFile_Check(f
))
56 return ((PyFileObject
*)f
)->f_fp
;
60 PyFile_Name(PyObject
*f
)
62 if (f
== NULL
|| !PyFile_Check(f
))
65 return ((PyFileObject
*)f
)->f_name
;
69 PyFile_FromFile(FILE *fp
, char *name
, char *mode
, int (*close
)(FILE *))
71 PyFileObject
*f
= PyObject_NEW(PyFileObject
, &PyFile_Type
);
75 f
->f_name
= PyString_FromString(name
);
76 f
->f_mode
= PyString_FromString(mode
);
79 if (strchr(mode
,'b') != NULL
)
83 if (f
->f_name
== NULL
|| f
->f_mode
== NULL
) {
88 return (PyObject
*) f
;
92 PyFile_FromString(char *name
, char *mode
)
94 extern int fclose(FILE *);
96 f
= (PyFileObject
*) PyFile_FromFile((FILE *)NULL
, name
, mode
, fclose
);
102 f
->f_fp
= fopenRF(name
, mode
+1);
107 Py_BEGIN_ALLOW_THREADS
108 f
->f_fp
= fopen(name
, mode
);
111 if (f
->f_fp
== NULL
) {
112 #ifdef NO_FOPEN_ERRNO
113 /* Metroworks only, not testable, so unchanged */
115 PyErr_SetString(PyExc_IOError
, "Cannot open file");
120 PyErr_SetFromErrnoWithFilename(PyExc_IOError
, name
);
124 return (PyObject
*)f
;
128 PyFile_SetBufSize(PyObject
*f
, int bufsize
)
144 setvbuf(((PyFileObject
*)f
)->f_fp
, (char *)NULL
,
146 #else /* !HAVE_SETVBUF */
148 setbuf(((PyFileObject
*)f
)->f_fp
, (char *)NULL
);
149 #endif /* !HAVE_SETVBUF */
156 PyErr_SetString(PyExc_ValueError
, "I/O operation on closed file");
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
);
170 if (f
->f_name
!= NULL
) {
171 Py_DECREF(f
->f_name
);
173 if (f
->f_mode
!= NULL
) {
174 Py_DECREF(f
->f_mode
);
180 file_repr(PyFileObject
*f
)
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
);
192 file_close(PyFileObject
*f
, PyObject
*args
)
195 if (!PyArg_NoArgs(args
))
197 if (f
->f_fp
!= NULL
) {
198 if (f
->f_close
!= NULL
) {
199 Py_BEGIN_ALLOW_THREADS
201 sts
= (*f
->f_close
)(f
->f_fp
);
207 return PyErr_SetFromErrno(PyExc_IOError
);
209 return PyInt_FromLong((long)sts
);
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
;
219 typedef off_t Py_off_t
;
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()*/
240 if (fseek(fp
, 0, SEEK_END
) != 0)
244 if (fgetpos(fp
, &pos
) != 0)
248 /* case SEEK_SET: break; */
250 return fsetpos(fp
, &offset
);
252 return fseek(fp
, offset
, whence
);
257 /* a portable ftell() function
258 Return -1 on failure with errno set appropriately, current file
259 position on success */
261 _portable_ftell(FILE* fp
)
263 #if SIZEOF_FPOS_T >= 8 && defined(HAVE_LARGEFILE_SUPPORT)
265 if (fgetpos(fp
, &pos
) != 0)
268 #elif defined(HAVE_FTELLO) && defined(HAVE_LARGEFILE_SUPPORT)
270 #elif defined(HAVE_FTELL64) && defined(HAVE_LARGEFILE_SUPPORT)
279 file_seek(PyFileObject
*f
, PyObject
*args
)
289 if (!PyArg_ParseTuple(args
, "O|i:seek", &offobj
, &whence
))
291 #if !defined(HAVE_LARGEFILE_SUPPORT)
292 offset
= PyInt_AsLong(offobj
);
294 offset
= PyLong_Check(offobj
) ?
295 PyLong_AsLongLong(offobj
) : PyInt_AsLong(offobj
);
297 if (PyErr_Occurred())
300 Py_BEGIN_ALLOW_THREADS
302 ret
= _portable_fseek(f
->f_fp
, offset
, whence
);
306 PyErr_SetFromErrno(PyExc_IOError
);
315 #ifdef HAVE_FTRUNCATE
317 file_truncate(PyFileObject
*f
, PyObject
*args
)
321 PyObject
*newsizeobj
;
326 if (!PyArg_ParseTuple(args
, "|O:truncate", &newsizeobj
))
328 if (newsizeobj
!= NULL
) {
329 #if !defined(HAVE_LARGEFILE_SUPPORT)
330 newsize
= PyInt_AsLong(newsizeobj
);
332 newsize
= PyLong_Check(newsizeobj
) ?
333 PyLong_AsLongLong(newsizeobj
) :
334 PyInt_AsLong(newsizeobj
);
336 if (PyErr_Occurred())
339 /* Default to current position*/
340 Py_BEGIN_ALLOW_THREADS
342 newsize
= _portable_ftell(f
->f_fp
);
345 PyErr_SetFromErrno(PyExc_IOError
);
350 Py_BEGIN_ALLOW_THREADS
352 ret
= fflush(f
->f_fp
);
354 if (ret
!= 0) goto onioerror
;
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)");
364 Py_BEGIN_ALLOW_THREADS
366 ret
= _chsize(fileno(f
->f_fp
), newsize
);
368 if (ret
!= 0) goto onioerror
;
371 Py_BEGIN_ALLOW_THREADS
373 ret
= ftruncate(fileno(f
->f_fp
), newsize
);
375 if (ret
!= 0) goto onioerror
;
376 #endif /* !MS_WIN32 */
382 PyErr_SetFromErrno(PyExc_IOError
);
386 #endif /* HAVE_FTRUNCATE */
389 file_tell(PyFileObject
*f
, PyObject
*args
)
395 if (!PyArg_NoArgs(args
))
397 Py_BEGIN_ALLOW_THREADS
399 pos
= _portable_ftell(f
->f_fp
);
402 PyErr_SetFromErrno(PyExc_IOError
);
406 #if !defined(HAVE_LARGEFILE_SUPPORT)
407 return PyInt_FromLong(pos
);
409 return PyLong_FromLongLong(pos
);
414 file_fileno(PyFileObject
*f
, PyObject
*args
)
418 if (!PyArg_NoArgs(args
))
420 return PyInt_FromLong((long) fileno(f
->f_fp
));
424 file_flush(PyFileObject
*f
, PyObject
*args
)
430 if (!PyArg_NoArgs(args
))
432 Py_BEGIN_ALLOW_THREADS
434 res
= fflush(f
->f_fp
);
437 PyErr_SetFromErrno(PyExc_IOError
);
446 file_isatty(PyFileObject
*f
, PyObject
*args
)
451 if (!PyArg_NoArgs(args
))
453 Py_BEGIN_ALLOW_THREADS
454 res
= isatty((int)fileno(f
->f_fp
));
456 return PyInt_FromLong(res
);
461 #define SMALLCHUNK 8192
463 #define SMALLCHUNK BUFSIZ
467 #define BIGCHUNK (512 * 32)
469 #define BIGCHUNK (512 * 1024)
473 new_buffersize(PyFileObject
*f
, size_t currentsize
)
478 if (fstat(fileno(f
->f_fp
), &st
) == 0) {
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
);
491 pos
= ftell(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. */
499 if (currentsize
> SMALLCHUNK
) {
500 /* Keep doubling until we reach BIGCHUNK;
501 then keep adding BIGCHUNK. */
502 if (currentsize
<= BIGCHUNK
)
503 return currentsize
+ currentsize
;
505 return currentsize
+ BIGCHUNK
;
507 return currentsize
+ SMALLCHUNK
;
511 file_read(PyFileObject
*f
, PyObject
*args
)
513 long bytesrequested
= -1;
514 size_t bytesread
, buffersize
, chunksize
;
519 if (!PyArg_ParseTuple(args
, "|l:read", &bytesrequested
))
521 if (bytesrequested
< 0)
522 buffersize
= new_buffersize(f
, (size_t)0);
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");
530 v
= PyString_FromStringAndSize((char *)NULL
, buffersize
);
535 Py_BEGIN_ALLOW_THREADS
537 chunksize
= fread(BUF(v
) + bytesread
, 1,
538 buffersize
- bytesread
, f
->f_fp
);
540 if (chunksize
== 0) {
541 if (!ferror(f
->f_fp
))
543 PyErr_SetFromErrno(PyExc_IOError
);
548 bytesread
+= chunksize
;
549 if (bytesread
< buffersize
)
551 if (bytesrequested
< 0) {
552 buffersize
= new_buffersize(f
, buffersize
);
553 if (_PyString_Resize(&v
, buffersize
) < 0)
557 if (bytesread
!= buffersize
)
558 _PyString_Resize(&v
, bytesread
);
563 file_readinto(PyFileObject
*f
, PyObject
*args
)
566 size_t ntodo
, ndone
, nnow
;
570 if (!PyArg_Parse(args
, "w#", &ptr
, &ntodo
))
574 Py_BEGIN_ALLOW_THREADS
576 nnow
= fread(ptr
+ndone
, 1, ntodo
, f
->f_fp
);
579 if (!ferror(f
->f_fp
))
581 PyErr_SetFromErrno(PyExc_IOError
);
588 return PyInt_FromLong((long)ndone
);
591 /**************************************************************************
592 Routine to get next line using platform fgets().
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
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
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
636 #if defined(DONT_USE_FGETS_IN_GETLINE) && defined(USE_FGETS_IN_GETLINE)
637 #undef USE_FGETS_IN_GETLINE
640 #ifdef USE_FGETS_IN_GETLINE
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
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 */
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
);
686 if (PyErr_CheckSignals())
688 v
= PyString_FromStringAndSize(buf
, pvfree
- buf
);
691 /* fgets read *something* */
692 p
= memchr(pvfree
, '\n', nfree
);
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
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 */
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
714 assert(p
> pvfree
&& *(p
-1) == '\0');
715 --p
; /* don't include \0 from fgets */
717 v
= PyString_FromStringAndSize(buf
, p
- buf
);
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');
727 pvfree
= pvend
- 1; /* overwrite trailing null */
728 total_v_size
= MAXBUFSIZE
;
734 /* The stack buffer isn't big enough; malloc a string object and read
737 total_v_size
= MAXBUFSIZE
+ INCBUFSIZE
;
738 v
= PyString_FromStringAndSize((char*)NULL
, (int)total_v_size
);
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.
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
);
760 if (PyErr_CheckSignals()) {
767 p
= memchr(pvfree
, '\n', nfree
);
769 if (p
+1 < pvend
&& *(p
+1) == '\0') {
770 /* \n came from fgets */
774 /* \n came from us; last line of file, no newline */
775 assert(p
> pvfree
&& *(p
-1) == '\0');
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");
788 if (_PyString_Resize(&v
, (int)total_v_size
) < 0)
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
));
800 #endif /* ifdef USE_FGETS_IN_GETLINE */
802 /* Internal routine to get a line.
803 Size argument interpretation:
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)
813 #define GETC(f) getc(f)
815 #define FUNLOCKFILE(f)
819 get_line(PyFileObject
*f
, int n
)
827 #ifdef USE_FGETS_IN_GETLINE
829 return getline_via_fgets(fp
);
831 n2
= n
> 0 ? n
: 100;
832 v
= PyString_FromStringAndSize((char *)NULL
, n2
);
839 Py_BEGIN_ALLOW_THREADS
841 while ((c
= GETC(fp
)) != EOF
&&
842 (*buf
++ = c
) != '\n' &&
851 if (PyErr_CheckSignals()) {
857 /* Must be because buf == end */
863 PyErr_SetString(PyExc_OverflowError
,
864 "line is longer than a Python string can hold");
868 if (_PyString_Resize(&v
, n2
) < 0)
876 _PyString_Resize(&v
, n1
);
880 /* External C interface */
883 PyFile_GetLine(PyObject
*f
, int n
)
888 PyErr_BadInternalCall();
892 if (PyFile_Check(f
)) {
893 if (((PyFileObject
*)f
)->f_fp
== NULL
)
895 result
= get_line((PyFileObject
*)f
, n
);
901 reader
= PyObject_GetAttrString(f
, "readline");
905 args
= Py_BuildValue("()");
907 args
= Py_BuildValue("(i)", n
);
912 result
= PyEval_CallObject(reader
, args
);
915 if (result
!= NULL
&& !PyString_Check(result
)) {
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
);
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);
937 v
= PyString_FromStringAndSize(s
, len
-1);
949 file_readline(PyFileObject
*f
, PyObject
*args
)
955 if (!PyArg_ParseTuple(args
, "|i:readline", &n
))
958 return PyString_FromString("");
961 return get_line(f
, n
);
965 file_xreadlines(PyFileObject
*f
, PyObject
*args
)
967 static PyObject
* xreadlines_function
= NULL
;
969 if (!PyArg_ParseTuple(args
, ":xreadlines"))
972 if (!xreadlines_function
) {
973 PyObject
*xreadlines_module
=
974 PyImport_ImportModule("xreadlines");
975 if(!xreadlines_module
)
978 xreadlines_function
= PyObject_GetAttrString(xreadlines_module
,
980 Py_DECREF(xreadlines_module
);
981 if(!xreadlines_function
)
984 return PyObject_CallFunction(xreadlines_function
, "(O)", f
);
988 file_readlines(PyFileObject
*f
, PyObject
*args
)
993 char small_buffer
[SMALLCHUNK
];
994 char *buffer
= small_buffer
;
995 size_t buffersize
= SMALLCHUNK
;
996 PyObject
*big_buffer
= NULL
;
999 size_t totalread
= 0;
1003 if (f
->f_fp
== NULL
)
1004 return err_closed();
1005 if (!PyArg_ParseTuple(args
, "|l:readlines", &sizehint
))
1007 if ((list
= PyList_New(0)) == NULL
)
1010 Py_BEGIN_ALLOW_THREADS
1012 nread
= fread(buffer
+nfilled
, 1, buffersize
-nfilled
, f
->f_fp
);
1013 Py_END_ALLOW_THREADS
1016 if (!ferror(f
->f_fp
))
1018 PyErr_SetFromErrno(PyExc_IOError
);
1026 p
= memchr(buffer
+nfilled
, '\n', nread
);
1028 /* Need a larger buffer to fit this line */
1031 if (buffersize
> INT_MAX
) {
1032 PyErr_SetString(PyExc_OverflowError
,
1033 "line is longer than a Python string can hold");
1036 if (big_buffer
== NULL
) {
1037 /* Create the big buffer */
1038 big_buffer
= PyString_FromStringAndSize(
1040 if (big_buffer
== NULL
)
1042 buffer
= PyString_AS_STRING(big_buffer
);
1043 memcpy(buffer
, small_buffer
, nfilled
);
1046 /* Grow the big buffer */
1047 _PyString_Resize(&big_buffer
, buffersize
);
1048 buffer
= PyString_AS_STRING(big_buffer
);
1052 end
= buffer
+nfilled
+nread
;
1055 /* Process complete lines */
1057 line
= PyString_FromStringAndSize(q
, p
-q
);
1060 err
= PyList_Append(list
, line
);
1065 p
= memchr(q
, '\n', end
-q
);
1066 } while (p
!= NULL
);
1067 /* Move the remaining incomplete line to the start */
1069 memmove(buffer
, q
, nfilled
);
1071 if (totalread
>= (size_t)sizehint
)
1075 /* Partial last line */
1076 line
= PyString_FromStringAndSize(buffer
, nfilled
);
1080 /* Need to complete the last line */
1081 PyObject
*rest
= get_line(f
, 0);
1086 PyString_Concat(&line
, rest
);
1091 err
= PyList_Append(list
, line
);
1098 Py_DECREF(big_buffer
);
1104 file_write(PyFileObject
*f
, PyObject
*args
)
1108 if (f
->f_fp
== NULL
)
1109 return err_closed();
1110 if (!PyArg_Parse(args
, f
->f_binary
? "s#" : "t#", &s
, &n
))
1113 Py_BEGIN_ALLOW_THREADS
1115 n2
= fwrite(s
, 1, n
, f
->f_fp
);
1116 Py_END_ALLOW_THREADS
1118 PyErr_SetFromErrno(PyExc_IOError
);
1127 file_writelines(PyFileObject
*f
, PyObject
*args
)
1129 #define CHUNKSIZE 1000
1130 PyObject
*list
, *line
;
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");
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. */
1150 list
= PyList_New(CHUNKSIZE
);
1159 list
= PyList_GetSlice(args
, index
, index
+CHUNKSIZE
);
1162 j
= PyList_GET_SIZE(list
);
1165 for (j
= 0; j
< CHUNKSIZE
; j
++) {
1166 line
= PySequence_GetItem(args
, index
+j
);
1168 if (PyErr_ExceptionMatches(
1169 PyExc_IndexError
)) {
1173 /* Some other error occurred.
1174 XXX We may lose some output. */
1177 PyList_SetItem(list
, j
, line
);
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
)) {
1193 if (((f
->f_binary
&&
1194 PyObject_AsReadBuffer(v
,
1195 (const void**)&buffer
,
1197 PyObject_AsCharBuffer(v
,
1200 PyErr_SetString(PyExc_TypeError
,
1201 "writelines() argument must be a sequence of strings");
1204 line
= PyString_FromStringAndSize(buffer
,
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
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
),
1223 if (nwritten
!= len
) {
1225 PyErr_SetFromErrno(PyExc_IOError
);
1230 Py_END_ALLOW_THREADS
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},
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 */
1276 file_getattr(PyFileObject
*f
, char *name
)
1280 res
= Py_FindMethod(file_methods
, (PyObject
*)f
, name
);
1284 if (strcmp(name
, "closed") == 0)
1285 return PyInt_FromLong((long)(f
->f_fp
== 0));
1286 return PyMember_Get((char *)f
, file_memberlist
, name
);
1290 file_setattr(PyFileObject
*f
, char *name
, PyObject
*v
)
1293 PyErr_SetString(PyExc_AttributeError
,
1294 "can't delete file attributes");
1297 return PyMember_Set((char *)f
, file_memberlist
, name
, v
);
1300 PyTypeObject PyFile_Type
= {
1301 PyObject_HEAD_INIT(&PyType_Type
)
1304 sizeof(PyFileObject
),
1306 (destructor
)file_dealloc
, /*tp_dealloc*/
1308 (getattrfunc
)file_getattr
, /*tp_getattr*/
1309 (setattrfunc
)file_setattr
, /*tp_setattr*/
1311 (reprfunc
)file_repr
, /*tp_repr*/
1314 /* Interface for the 'soft space' between print items. */
1317 PyFile_SoftSpace(PyObject
*f
, int newflag
)
1323 else if (PyFile_Check(f
)) {
1324 oldflag
= ((PyFileObject
*)f
)->f_softspace
;
1325 ((PyFileObject
*)f
)->f_softspace
= newflag
;
1329 v
= PyObject_GetAttrString(f
, "softspace");
1334 oldflag
= PyInt_AsLong(v
);
1337 v
= PyInt_FromLong((long)newflag
);
1341 if (PyObject_SetAttrString(f
, "softspace", v
) != 0)
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
;
1356 PyErr_SetString(PyExc_TypeError
, "writeobject with NULL file");
1359 else if (PyFile_Check(f
)) {
1360 FILE *fp
= PyFile_AsFile(f
);
1365 return PyObject_Print(v
, fp
, flags
);
1367 writer
= PyObject_GetAttrString(f
, "write");
1370 if (flags
& Py_PRINT_RAW
)
1371 value
= PyObject_Str(v
);
1373 value
= PyObject_Repr(v
);
1374 if (value
== NULL
) {
1378 args
= Py_BuildValue("(O)", value
);
1384 result
= PyEval_CallObject(writer
, args
);
1395 PyFile_WriteString(char *s
, PyObject
*f
)
1398 /* Should be caused by a pre-existing error */
1399 if (!PyErr_Occurred())
1400 PyErr_SetString(PyExc_SystemError
,
1401 "null file for PyFile_WriteString");
1404 else if (PyFile_Check(f
)) {
1405 FILE *fp
= PyFile_AsFile(f
);
1413 else if (!PyErr_Occurred()) {
1414 PyObject
*v
= PyString_FromString(s
);
1418 err
= PyFile_WriteObject(v
, f
, Py_PRINT_RAW
);
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
)
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
);
1451 if (PyInt_Check(fno
)) {
1452 fd
= PyInt_AsLong(fno
);
1455 else if (PyLong_Check(fno
)) {
1456 fd
= PyLong_AsLong(fno
);
1460 PyErr_SetString(PyExc_TypeError
,
1461 "fileno() returned a non-integer");
1467 PyErr_SetString(PyExc_TypeError
,
1468 "argument must be an int, or have a fileno() method.");
1473 PyErr_Format(PyExc_ValueError
,
1474 "file descriptor cannot be a negative integer (%i)",