2 /* File object implementation */
5 #include "structmember.h"
7 #ifndef DONT_HAVE_SYS_TYPES_H
9 #endif /* DONT_HAVE_SYS_TYPES_H */
12 #define fileno _fileno
13 /* can (almost fully) duplicate with _chsize, see file_truncate */
14 #define HAVE_FTRUNCATE
19 #define HAVE_FTRUNCATE
24 /* Mwerks fopen() doesn't always set errno */
25 #define NO_FOPEN_ERRNO
28 #define BUF(v) PyString_AS_STRING((PyStringObject *)v)
30 #ifndef DONT_HAVE_ERRNO_H
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 */
47 PyFile_AsFile(PyObject
*f
)
49 if (f
== NULL
|| !PyFile_Check(f
))
52 return ((PyFileObject
*)f
)->f_fp
;
56 PyFile_Name(PyObject
*f
)
58 if (f
== NULL
|| !PyFile_Check(f
))
61 return ((PyFileObject
*)f
)->f_name
;
66 fill_file_fields(PyFileObject
*f
, FILE *fp
, char *name
, char *mode
,
70 assert(PyFile_Check(f
));
71 assert(f
->f_fp
== NULL
);
75 f
->f_name
= PyString_FromString(name
);
76 f
->f_mode
= PyString_FromString(mode
);
80 f
->f_binary
= strchr(mode
,'b') != NULL
;
82 if (f
->f_name
== NULL
|| f
->f_mode
== NULL
)
85 return (PyObject
*) f
;
89 open_the_file(PyFileObject
*f
, char *name
, char *mode
)
92 assert(PyFile_Check(f
));
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");
109 f
->f_fp
= fopenRF(name
, mode
+1);
114 Py_BEGIN_ALLOW_THREADS
115 f
->f_fp
= fopen(name
, mode
);
118 if (f
->f_fp
== NULL
) {
119 #ifdef NO_FOPEN_ERRNO
120 /* Metroworks only, wich does not always sets errno */
123 v
= Py_BuildValue("(is)", 0, "Cannot open file");
125 PyErr_SetObject(PyExc_IOError
, v
);
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 */
141 else if (errno
== EINVAL
) /* unknown, but not a mode string */
145 PyErr_Format(PyExc_IOError
, "invalid mode: %s",
148 PyErr_SetFromErrnoWithFilename(PyExc_IOError
, name
);
151 return (PyObject
*)f
;
155 PyFile_FromFile(FILE *fp
, char *name
, char *mode
, int (*close
)(FILE *))
157 PyFileObject
*f
= (PyFileObject
*)PyFile_Type
.tp_new(&PyFile_Type
,
160 if (fill_file_fields(f
, fp
, name
, mode
, close
) == NULL
) {
165 return (PyObject
*) f
;
169 PyFile_FromString(char *name
, char *mode
)
171 extern int fclose(FILE *);
174 f
= (PyFileObject
*)PyFile_FromFile((FILE *)NULL
, name
, mode
, fclose
);
176 if (open_the_file(f
, name
, mode
) == NULL
) {
181 return (PyObject
*)f
;
185 PyFile_SetBufSize(PyObject
*f
, int bufsize
)
201 setvbuf(((PyFileObject
*)f
)->f_fp
, (char *)NULL
,
203 #else /* !HAVE_SETVBUF */
205 setbuf(((PyFileObject
*)f
)->f_fp
, (char *)NULL
);
206 #endif /* !HAVE_SETVBUF */
213 PyErr_SetString(PyExc_ValueError
, "I/O operation on closed file");
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
);
227 Py_XDECREF(f
->f_name
);
228 Py_XDECREF(f
->f_mode
);
229 f
->ob_type
->tp_free((PyObject
*)f
);
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
),
243 file_close(PyFileObject
*f
)
246 if (f
->f_fp
!= NULL
) {
247 if (f
->f_close
!= NULL
) {
248 Py_BEGIN_ALLOW_THREADS
250 sts
= (*f
->f_close
)(f
->f_fp
);
256 return PyErr_SetFromErrno(PyExc_IOError
);
258 return PyInt_FromLong((long)sts
);
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
;
272 #error "Large file support, but neither off_t nor fpos_t is large enough."
276 /* a portable fseek() function
277 return 0 on success, non-zero on failure (with errno set) */
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()*/
297 if (_lseeki64(fileno(fp
), 0, 2) == -1)
300 if (fseek(fp
, 0, SEEK_END
) != 0)
305 if (fgetpos(fp
, &pos
) != 0)
309 /* case SEEK_SET: break; */
311 return fsetpos(fp
, &offset
);
313 #error "Large file support, but no way to fseek."
318 /* a portable ftell() function
319 Return -1 on failure with errno set appropriately, current file
320 position on success */
322 _portable_ftell(FILE* fp
)
324 #if !defined(HAVE_LARGEFILE_SUPPORT)
326 #elif defined(HAVE_FTELLO) && SIZEOF_OFF_T >= 8
328 #elif defined(HAVE_FTELL64)
330 #elif SIZEOF_FPOS_T >= 8
332 if (fgetpos(fp
, &pos
) != 0)
336 #error "Large file support, but no way to ftell."
342 file_seek(PyFileObject
*f
, PyObject
*args
)
352 if (!PyArg_ParseTuple(args
, "O|i:seek", &offobj
, &whence
))
354 #if !defined(HAVE_LARGEFILE_SUPPORT)
355 offset
= PyInt_AsLong(offobj
);
357 offset
= PyLong_Check(offobj
) ?
358 PyLong_AsLongLong(offobj
) : PyInt_AsLong(offobj
);
360 if (PyErr_Occurred())
363 Py_BEGIN_ALLOW_THREADS
365 ret
= _portable_fseek(f
->f_fp
, offset
, whence
);
369 PyErr_SetFromErrno(PyExc_IOError
);
378 #ifdef HAVE_FTRUNCATE
380 file_truncate(PyFileObject
*f
, PyObject
*args
)
384 PyObject
*newsizeobj
;
389 if (!PyArg_ParseTuple(args
, "|O:truncate", &newsizeobj
))
391 if (newsizeobj
!= NULL
) {
392 #if !defined(HAVE_LARGEFILE_SUPPORT)
393 newsize
= PyInt_AsLong(newsizeobj
);
395 newsize
= PyLong_Check(newsizeobj
) ?
396 PyLong_AsLongLong(newsizeobj
) :
397 PyInt_AsLong(newsizeobj
);
399 if (PyErr_Occurred())
402 /* Default to current position*/
403 Py_BEGIN_ALLOW_THREADS
405 newsize
= _portable_ftell(f
->f_fp
);
408 PyErr_SetFromErrno(PyExc_IOError
);
413 Py_BEGIN_ALLOW_THREADS
415 ret
= fflush(f
->f_fp
);
417 if (ret
!= 0) goto onioerror
;
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)");
427 Py_BEGIN_ALLOW_THREADS
429 ret
= _chsize(fileno(f
->f_fp
), (long)newsize
);
431 if (ret
!= 0) goto onioerror
;
434 Py_BEGIN_ALLOW_THREADS
436 ret
= ftruncate(fileno(f
->f_fp
), newsize
);
438 if (ret
!= 0) goto onioerror
;
439 #endif /* !MS_WIN32 */
445 PyErr_SetFromErrno(PyExc_IOError
);
449 #endif /* HAVE_FTRUNCATE */
452 file_tell(PyFileObject
*f
)
458 Py_BEGIN_ALLOW_THREADS
460 pos
= _portable_ftell(f
->f_fp
);
463 PyErr_SetFromErrno(PyExc_IOError
);
467 #if !defined(HAVE_LARGEFILE_SUPPORT)
468 return PyInt_FromLong(pos
);
470 return PyLong_FromLongLong(pos
);
475 file_fileno(PyFileObject
*f
)
479 return PyInt_FromLong((long) fileno(f
->f_fp
));
483 file_flush(PyFileObject
*f
)
489 Py_BEGIN_ALLOW_THREADS
491 res
= fflush(f
->f_fp
);
494 PyErr_SetFromErrno(PyExc_IOError
);
503 file_isatty(PyFileObject
*f
)
508 Py_BEGIN_ALLOW_THREADS
509 res
= isatty((int)fileno(f
->f_fp
));
511 return PyInt_FromLong(res
);
516 #define SMALLCHUNK 8192
518 #define SMALLCHUNK BUFSIZ
522 #define BIGCHUNK (512 * 32)
524 #define BIGCHUNK (512 * 1024)
528 new_buffersize(PyFileObject
*f
, size_t currentsize
)
533 if (fstat(fileno(f
->f_fp
), &st
) == 0) {
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. :-) */
545 pos
= lseek(fileno(f
->f_fp
), 1L, SEEK_CUR
);
546 pos
= lseek(fileno(f
->f_fp
), -1L, SEEK_CUR
);
548 pos
= lseek(fileno(f
->f_fp
), 0L, SEEK_CUR
);
551 pos
= ftell(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. */
560 if (currentsize
> SMALLCHUNK
) {
561 /* Keep doubling until we reach BIGCHUNK;
562 then keep adding BIGCHUNK. */
563 if (currentsize
<= BIGCHUNK
)
564 return currentsize
+ currentsize
;
566 return currentsize
+ BIGCHUNK
;
568 return currentsize
+ SMALLCHUNK
;
572 file_read(PyFileObject
*f
, PyObject
*args
)
574 long bytesrequested
= -1;
575 size_t bytesread
, buffersize
, chunksize
;
580 if (!PyArg_ParseTuple(args
, "|l:read", &bytesrequested
))
582 if (bytesrequested
< 0)
583 buffersize
= new_buffersize(f
, (size_t)0);
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");
591 v
= PyString_FromStringAndSize((char *)NULL
, buffersize
);
596 Py_BEGIN_ALLOW_THREADS
598 chunksize
= fread(BUF(v
) + bytesread
, 1,
599 buffersize
- bytesread
, f
->f_fp
);
601 if (chunksize
== 0) {
602 if (!ferror(f
->f_fp
))
604 PyErr_SetFromErrno(PyExc_IOError
);
609 bytesread
+= chunksize
;
610 if (bytesread
< buffersize
)
612 if (bytesrequested
< 0) {
613 buffersize
= new_buffersize(f
, buffersize
);
614 if (_PyString_Resize(&v
, buffersize
) < 0)
618 if (bytesread
!= buffersize
)
619 _PyString_Resize(&v
, bytesread
);
624 file_readinto(PyFileObject
*f
, PyObject
*args
)
632 if (!PyArg_Parse(args
, "w#", &ptr
, &ntodo
))
636 Py_BEGIN_ALLOW_THREADS
638 nnow
= fread(ptr
+ndone
, 1, ntodo
, f
->f_fp
);
641 if (!ferror(f
->f_fp
))
643 PyErr_SetFromErrno(PyExc_IOError
);
650 return PyInt_FromLong((long)ndone
);
653 /**************************************************************************
654 Routine to get next line using platform fgets().
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
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
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
698 #if defined(DONT_USE_FGETS_IN_GETLINE) && defined(USE_FGETS_IN_GETLINE)
699 #undef USE_FGETS_IN_GETLINE
702 #ifdef USE_FGETS_IN_GETLINE
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
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 */
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
);
748 if (PyErr_CheckSignals())
750 v
= PyString_FromStringAndSize(buf
, pvfree
- buf
);
753 /* fgets read *something* */
754 p
= memchr(pvfree
, '\n', nfree
);
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
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 */
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
776 assert(p
> pvfree
&& *(p
-1) == '\0');
777 --p
; /* don't include \0 from fgets */
779 v
= PyString_FromStringAndSize(buf
, p
- buf
);
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');
789 pvfree
= pvend
- 1; /* overwrite trailing null */
790 total_v_size
= MAXBUFSIZE
;
796 /* The stack buffer isn't big enough; malloc a string object and read
799 total_v_size
= MAXBUFSIZE
+ INCBUFSIZE
;
800 v
= PyString_FromStringAndSize((char*)NULL
, (int)total_v_size
);
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.
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
);
822 if (PyErr_CheckSignals()) {
829 p
= memchr(pvfree
, '\n', nfree
);
831 if (p
+1 < pvend
&& *(p
+1) == '\0') {
832 /* \n came from fgets */
836 /* \n came from us; last line of file, no newline */
837 assert(p
> pvfree
&& *(p
-1) == '\0');
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");
850 if (_PyString_Resize(&v
, (int)total_v_size
) < 0)
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
));
862 #endif /* ifdef USE_FGETS_IN_GETLINE */
864 /* Internal routine to get a line.
865 Size argument interpretation:
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)
875 #define GETC(f) getc(f)
877 #define FUNLOCKFILE(f)
881 get_line(PyFileObject
*f
, int n
)
889 #ifdef USE_FGETS_IN_GETLINE
891 return getline_via_fgets(fp
);
893 n2
= n
> 0 ? n
: 100;
894 v
= PyString_FromStringAndSize((char *)NULL
, n2
);
901 Py_BEGIN_ALLOW_THREADS
903 while ((c
= GETC(fp
)) != EOF
&&
904 (*buf
++ = c
) != '\n' &&
913 PyErr_SetFromErrno(PyExc_IOError
);
919 if (PyErr_CheckSignals()) {
925 /* Must be because buf == end */
931 PyErr_SetString(PyExc_OverflowError
,
932 "line is longer than a Python string can hold");
936 if (_PyString_Resize(&v
, n2
) < 0)
944 _PyString_Resize(&v
, n1
);
948 /* External C interface */
951 PyFile_GetLine(PyObject
*f
, int n
)
956 PyErr_BadInternalCall();
960 if (PyFile_Check(f
)) {
961 if (((PyFileObject
*)f
)->f_fp
== NULL
)
963 result
= get_line((PyFileObject
*)f
, n
);
969 reader
= PyObject_GetAttrString(f
, "readline");
973 args
= Py_BuildValue("()");
975 args
= Py_BuildValue("(i)", n
);
980 result
= PyEval_CallObject(reader
, args
);
983 if (result
!= NULL
&& !PyString_Check(result
)) {
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
);
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);
1005 v
= PyString_FromStringAndSize(s
, len
-1);
1017 file_readline(PyFileObject
*f
, PyObject
*args
)
1021 if (f
->f_fp
== NULL
)
1022 return err_closed();
1023 if (!PyArg_ParseTuple(args
, "|i:readline", &n
))
1026 return PyString_FromString("");
1029 return get_line(f
, n
);
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
)
1043 xreadlines_function
= PyObject_GetAttrString(xreadlines_module
,
1045 Py_DECREF(xreadlines_module
);
1046 if(!xreadlines_function
)
1049 return PyObject_CallFunction(xreadlines_function
, "(O)", f
);
1053 file_readlines(PyFileObject
*f
, PyObject
*args
)
1058 char small_buffer
[SMALLCHUNK
];
1059 char *buffer
= small_buffer
;
1060 size_t buffersize
= SMALLCHUNK
;
1061 PyObject
*big_buffer
= NULL
;
1064 size_t totalread
= 0;
1069 if (f
->f_fp
== NULL
)
1070 return err_closed();
1071 if (!PyArg_ParseTuple(args
, "|l:readlines", &sizehint
))
1073 if ((list
= PyList_New(0)) == NULL
)
1079 Py_BEGIN_ALLOW_THREADS
1081 nread
= fread(buffer
+nfilled
, 1,
1082 buffersize
-nfilled
, f
->f_fp
);
1083 Py_END_ALLOW_THREADS
1084 shortread
= (nread
< buffersize
-nfilled
);
1088 if (!ferror(f
->f_fp
))
1090 PyErr_SetFromErrno(PyExc_IOError
);
1098 p
= memchr(buffer
+nfilled
, '\n', nread
);
1100 /* Need a larger buffer to fit this line */
1103 if (buffersize
> INT_MAX
) {
1104 PyErr_SetString(PyExc_OverflowError
,
1105 "line is longer than a Python string can hold");
1108 if (big_buffer
== NULL
) {
1109 /* Create the big buffer */
1110 big_buffer
= PyString_FromStringAndSize(
1112 if (big_buffer
== NULL
)
1114 buffer
= PyString_AS_STRING(big_buffer
);
1115 memcpy(buffer
, small_buffer
, nfilled
);
1118 /* Grow the big buffer */
1119 _PyString_Resize(&big_buffer
, buffersize
);
1120 buffer
= PyString_AS_STRING(big_buffer
);
1124 end
= buffer
+nfilled
+nread
;
1127 /* Process complete lines */
1129 line
= PyString_FromStringAndSize(q
, p
-q
);
1132 err
= PyList_Append(list
, line
);
1137 p
= memchr(q
, '\n', end
-q
);
1138 } while (p
!= NULL
);
1139 /* Move the remaining incomplete line to the start */
1141 memmove(buffer
, q
, nfilled
);
1143 if (totalread
>= (size_t)sizehint
)
1147 /* Partial last line */
1148 line
= PyString_FromStringAndSize(buffer
, nfilled
);
1152 /* Need to complete the last line */
1153 PyObject
*rest
= get_line(f
, 0);
1158 PyString_Concat(&line
, rest
);
1163 err
= PyList_Append(list
, line
);
1170 Py_DECREF(big_buffer
);
1176 file_write(PyFileObject
*f
, PyObject
*args
)
1180 if (f
->f_fp
== NULL
)
1181 return err_closed();
1182 if (!PyArg_ParseTuple(args
, f
->f_binary
? "s#" : "t#", &s
, &n
))
1185 Py_BEGIN_ALLOW_THREADS
1187 n2
= fwrite(s
, 1, n
, f
->f_fp
);
1188 Py_END_ALLOW_THREADS
1190 PyErr_SetFromErrno(PyExc_IOError
);
1199 file_writelines(PyFileObject
*f
, PyObject
*seq
)
1201 #define CHUNKSIZE 1000
1202 PyObject
*list
, *line
;
1203 PyObject
*it
; /* iter(seq) */
1205 int i
, j
, index
, len
, nwritten
, islist
;
1207 assert(seq
!= NULL
);
1208 if (f
->f_fp
== NULL
)
1209 return err_closed();
1213 islist
= PyList_Check(seq
);
1217 it
= PyObject_GetIter(seq
);
1219 PyErr_SetString(PyExc_TypeError
,
1220 "writelines() requires an iterable argument");
1223 /* From here on, fail by going to error, to reclaim "it". */
1224 list
= PyList_New(CHUNKSIZE
);
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
) {
1235 list
= PyList_GetSlice(seq
, index
, index
+CHUNKSIZE
);
1238 j
= PyList_GET_SIZE(list
);
1241 for (j
= 0; j
< CHUNKSIZE
; j
++) {
1242 line
= PyIter_Next(it
);
1244 if (PyErr_Occurred())
1248 PyList_SetItem(list
, j
, line
);
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
)) {
1264 if (((f
->f_binary
&&
1265 PyObject_AsReadBuffer(v
,
1266 (const void**)&buffer
,
1268 PyObject_AsCharBuffer(v
,
1271 PyErr_SetString(PyExc_TypeError
,
1272 "writelines() argument must be a sequence of strings");
1275 line
= PyString_FromStringAndSize(buffer
,
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
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
),
1294 if (nwritten
!= len
) {
1296 PyErr_SetFromErrno(PyExc_IOError
);
1301 Py_END_ALLOW_THREADS
1316 static char readline_doc
[] =
1317 "readline([size]) -> next line from the file, as a string.\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"
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"
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"
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"
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"
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"
1354 "Size defaults to the current file position, as returned by tell().";
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"
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"
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"
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"
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
},
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
,
1425 /* getattr(f, "closed") is implemented without this table */
1426 {NULL
} /* Sentinel */
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"},
1441 file_getiter(PyObject
*f
)
1443 return PyObject_CallMethod(f
, "xreadlines", "");
1447 file_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
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
)
1460 self
= type
->tp_alloc(type
, 0);
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
;
1473 file_init(PyObject
*self
, PyObject
*args
, PyObject
*kwds
)
1475 PyFileObject
*foself
= (PyFileObject
*)self
;
1477 static char *kwlist
[] = {"name", "mode", "buffering", 0};
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
)
1488 Py_DECREF(closeresult
);
1491 if (!PyArg_ParseTupleAndKeywords(args
, kwds
, "et|si:file", kwlist
,
1492 Py_FileSystemDefaultEncoding
, &name
,
1495 if (fill_file_fields(foself
, NULL
, name
, mode
, fclose
) == NULL
)
1497 if (open_the_file(foself
, name
, mode
) == NULL
)
1499 PyFile_SetBufSize(self
, bufsize
);
1506 PyMem_Free(name
); /* free the encoded string */
1510 static char file_doc
[] =
1511 "file(name[, mode[, buffering]]) -> file object\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
)
1526 sizeof(PyFileObject
),
1528 (destructor
)file_dealloc
, /* tp_dealloc */
1533 (reprfunc
)file_repr
, /* tp_repr */
1534 0, /* tp_as_number */
1535 0, /* tp_as_sequence */
1536 0, /* tp_as_mapping */
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 */
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 */
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
)
1574 else if (PyFile_Check(f
)) {
1575 oldflag
= ((PyFileObject
*)f
)->f_softspace
;
1576 ((PyFileObject
*)f
)->f_softspace
= newflag
;
1580 v
= PyObject_GetAttrString(f
, "softspace");
1585 oldflag
= PyInt_AsLong(v
);
1588 v
= PyInt_FromLong((long)newflag
);
1592 if (PyObject_SetAttrString(f
, "softspace", v
) != 0)
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
;
1607 PyErr_SetString(PyExc_TypeError
, "writeobject with NULL file");
1610 else if (PyFile_Check(f
)) {
1611 FILE *fp
= PyFile_AsFile(f
);
1616 return PyObject_Print(v
, fp
, flags
);
1618 writer
= PyObject_GetAttrString(f
, "write");
1621 if (flags
& Py_PRINT_RAW
) {
1622 if (PyUnicode_Check(v
)) {
1626 value
= PyObject_Str(v
);
1629 value
= PyObject_Repr(v
);
1630 if (value
== NULL
) {
1634 args
= Py_BuildValue("(O)", value
);
1640 result
= PyEval_CallObject(writer
, args
);
1651 PyFile_WriteString(const char *s
, PyObject
*f
)
1654 /* Should be caused by a pre-existing error */
1655 if (!PyErr_Occurred())
1656 PyErr_SetString(PyExc_SystemError
,
1657 "null file for PyFile_WriteString");
1660 else if (PyFile_Check(f
)) {
1661 FILE *fp
= PyFile_AsFile(f
);
1669 else if (!PyErr_Occurred()) {
1670 PyObject
*v
= PyString_FromString(s
);
1674 err
= PyFile_WriteObject(v
, f
, Py_PRINT_RAW
);
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
)
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
);
1707 if (PyInt_Check(fno
)) {
1708 fd
= PyInt_AsLong(fno
);
1711 else if (PyLong_Check(fno
)) {
1712 fd
= PyLong_AsLong(fno
);
1716 PyErr_SetString(PyExc_TypeError
,
1717 "fileno() returned a non-integer");
1723 PyErr_SetString(PyExc_TypeError
,
1724 "argument must be an int, or have a fileno() method.");
1729 PyErr_Format(PyExc_ValueError
,
1730 "file descriptor cannot be a negative integer (%i)",