1 /* File object implementation */
4 #include "structmember.h"
6 #ifndef DONT_HAVE_SYS_TYPES_H
8 #endif /* DONT_HAVE_SYS_TYPES_H */
11 #define fileno _fileno
12 /* can simulate truncate with Win32 API functions; see file_truncate */
13 #define HAVE_FTRUNCATE
14 #define WIN32_LEAN_AND_MEAN
19 /* Need GetVersion to see if on NT so safe to use _wfopen */
20 #define WIN32_LEAN_AND_MEAN
26 #define HAVE_FTRUNCATE
31 /* Mwerks fopen() doesn't always set errno */
32 #define NO_FOPEN_ERRNO
35 #if defined(PYOS_OS2) && defined(PYCC_GCC)
39 #define BUF(v) PyString_AS_STRING((PyStringObject *)v)
41 #ifndef DONT_HAVE_ERRNO_H
45 #ifdef HAVE_GETC_UNLOCKED
46 #define GETC(f) getc_unlocked(f)
47 #define FLOCKFILE(f) flockfile(f)
48 #define FUNLOCKFILE(f) funlockfile(f)
50 #define GETC(f) getc(f)
52 #define FUNLOCKFILE(f)
55 #ifdef WITH_UNIVERSAL_NEWLINES
56 /* Bits in f_newlinetypes */
57 #define NEWLINE_UNKNOWN 0 /* No newline seen, yet */
58 #define NEWLINE_CR 1 /* \r newline seen */
59 #define NEWLINE_LF 2 /* \n newline seen */
60 #define NEWLINE_CRLF 4 /* \r\n newline seen */
64 PyFile_AsFile(PyObject
*f
)
66 if (f
== NULL
|| !PyFile_Check(f
))
69 return ((PyFileObject
*)f
)->f_fp
;
73 PyFile_Name(PyObject
*f
)
75 if (f
== NULL
|| !PyFile_Check(f
))
78 return ((PyFileObject
*)f
)->f_name
;
81 /* On Unix, fopen will succeed for directories.
82 In Python, there should be no file objects referring to
83 directories, so we need a check. */
86 dircheck(PyFileObject
* f
)
88 #if defined(HAVE_FSTAT) && defined(S_IFDIR) && defined(EISDIR)
92 if (fstat(fileno(f
->f_fp
), &buf
) == 0 &&
93 S_ISDIR(buf
.st_mode
)) {
95 char *msg
= strerror(EISDIR
);
97 char *msg
= "Is a directory";
99 PyObject
*exc
= PyObject_CallFunction(PyExc_IOError
, "(is)",
101 PyErr_SetObject(PyExc_IOError
, exc
);
111 fill_file_fields(PyFileObject
*f
, FILE *fp
, char *name
, char *mode
,
112 int (*close
)(FILE *), PyObject
*wname
)
115 assert(PyFile_Check(f
));
116 assert(f
->f_fp
== NULL
);
118 Py_DECREF(f
->f_name
);
119 Py_DECREF(f
->f_mode
);
120 Py_DECREF(f
->f_encoding
);
121 #ifdef Py_USING_UNICODE
123 f
->f_name
= PyUnicode_FromObject(wname
);
126 f
->f_name
= PyString_FromString(name
);
127 f
->f_mode
= PyString_FromString(mode
);
131 f
->f_binary
= strchr(mode
,'b') != NULL
;
133 #ifdef WITH_UNIVERSAL_NEWLINES
134 f
->f_univ_newline
= (strchr(mode
, 'U') != NULL
);
135 f
->f_newlinetypes
= NEWLINE_UNKNOWN
;
139 f
->f_encoding
= Py_None
;
141 if (f
->f_name
== NULL
|| f
->f_mode
== NULL
)
145 return (PyObject
*) f
;
149 open_the_file(PyFileObject
*f
, char *name
, char *mode
)
152 assert(PyFile_Check(f
));
154 /* windows ignores the passed name in order to support Unicode */
155 assert(f
->f_name
!= NULL
);
157 assert(name
!= NULL
);
159 assert(mode
!= NULL
);
160 assert(f
->f_fp
== NULL
);
162 /* rexec.py can't stop a user from getting the file() constructor --
163 all they have to do is get *any* file object f, and then do
164 type(f). Here we prevent them from doing damage with it. */
165 if (PyEval_GetRestricted()) {
166 PyErr_SetString(PyExc_IOError
,
167 "file() constructor not accessible in restricted mode");
174 f
->f_fp
= fopenRF(name
, mode
+1);
179 #ifdef WITH_UNIVERSAL_NEWLINES
180 if (strcmp(mode
, "U") == 0 || strcmp(mode
, "rU") == 0)
183 /* Compatibility: specifying U in a Python without universal
184 ** newlines is allowed, and the file is opened as a normal text
187 if (strcmp(mode
, "U") == 0 || strcmp(mode
, "rU") == 0)
191 if (PyUnicode_Check(f
->f_name
)) {
193 wmode
= PyUnicode_DecodeASCII(mode
, strlen(mode
), NULL
);
194 if (f
->f_name
&& wmode
) {
195 Py_BEGIN_ALLOW_THREADS
196 /* PyUnicode_AS_UNICODE OK without thread
197 lock as it is a simple dereference. */
198 f
->f_fp
= _wfopen(PyUnicode_AS_UNICODE(f
->f_name
),
199 PyUnicode_AS_UNICODE(wmode
));
205 if (NULL
== f
->f_fp
&& NULL
!= name
) {
206 Py_BEGIN_ALLOW_THREADS
207 f
->f_fp
= fopen(name
, mode
);
211 if (f
->f_fp
== NULL
) {
212 #ifdef NO_FOPEN_ERRNO
213 /* Metroworks only, wich does not always sets errno */
216 v
= Py_BuildValue("(is)", 0, "Cannot open file");
218 PyErr_SetObject(PyExc_IOError
, v
);
225 /* MSVC 6 (Microsoft) leaves errno at 0 for bad mode strings,
226 * across all Windows flavors. When it sets EINVAL varies
227 * across Windows flavors, the exact conditions aren't
228 * documented, and the answer lies in the OS's implementation
229 * of Win32's CreateFile function (whose source is secret).
230 * Seems the best we can do is map EINVAL to ENOENT.
232 if (errno
== 0) /* bad mode string */
234 else if (errno
== EINVAL
) /* unknown, but not a mode string */
238 PyErr_Format(PyExc_IOError
, "invalid mode: %s",
242 PyErr_SetFromErrnoWithFilenameObject(PyExc_IOError
, f
->f_name
);
244 PyErr_SetFromErrnoWithFilename(PyExc_IOError
, name
);
245 #endif /* MS_WINDOWS */
250 return (PyObject
*)f
;
254 PyFile_FromFile(FILE *fp
, char *name
, char *mode
, int (*close
)(FILE *))
256 PyFileObject
*f
= (PyFileObject
*)PyFile_Type
.tp_new(&PyFile_Type
,
259 if (fill_file_fields(f
, fp
, name
, mode
, close
, NULL
) == NULL
) {
264 return (PyObject
*) f
;
268 PyFile_FromString(char *name
, char *mode
)
270 extern int fclose(FILE *);
273 f
= (PyFileObject
*)PyFile_FromFile((FILE *)NULL
, name
, mode
, fclose
);
275 if (open_the_file(f
, name
, mode
) == NULL
) {
280 return (PyObject
*)f
;
284 PyFile_SetBufSize(PyObject
*f
, int bufsize
)
286 PyFileObject
*file
= (PyFileObject
*)f
;
307 if (type
== _IONBF
) {
308 PyMem_Free(file
->f_setbuf
);
309 file
->f_setbuf
= NULL
;
311 file
->f_setbuf
= PyMem_Realloc(file
->f_setbuf
, bufsize
);
314 setvbuf(file
->f_fp
, file
->f_setbuf
, type
, bufsize
);
315 #else /* !HAVE_SETVBUF */
316 setbuf(file
->f_fp
, file
->f_setbuf
);
317 #endif /* !HAVE_SETVBUF */
321 /* Set the encoding used to output Unicode strings.
322 Returh 1 on success, 0 on failure. */
325 PyFile_SetEncoding(PyObject
*f
, const char *enc
)
327 PyFileObject
*file
= (PyFileObject
*)f
;
328 PyObject
*str
= PyString_FromString(enc
);
331 Py_DECREF(file
->f_encoding
);
332 file
->f_encoding
= str
;
339 PyErr_SetString(PyExc_ValueError
, "I/O operation on closed file");
343 static void drop_readahead(PyFileObject
*);
348 file_dealloc(PyFileObject
*f
)
350 if (f
->f_fp
!= NULL
&& f
->f_close
!= NULL
) {
351 Py_BEGIN_ALLOW_THREADS
352 (*f
->f_close
)(f
->f_fp
);
355 PyMem_Free(f
->f_setbuf
);
356 Py_XDECREF(f
->f_name
);
357 Py_XDECREF(f
->f_mode
);
358 Py_XDECREF(f
->f_encoding
);
360 f
->ob_type
->tp_free((PyObject
*)f
);
364 file_repr(PyFileObject
*f
)
366 if (PyUnicode_Check(f
->f_name
)) {
367 #ifdef Py_USING_UNICODE
368 PyObject
*ret
= NULL
;
370 name
= PyUnicode_AsUnicodeEscapeString(f
->f_name
);
371 ret
= PyString_FromFormat("<%s file u'%s', mode '%s' at %p>",
372 f
->f_fp
== NULL
? "closed" : "open",
373 PyString_AsString(name
),
374 PyString_AsString(f
->f_mode
),
380 return PyString_FromFormat("<%s file '%s', mode '%s' at %p>",
381 f
->f_fp
== NULL
? "closed" : "open",
382 PyString_AsString(f
->f_name
),
383 PyString_AsString(f
->f_mode
),
389 file_close(PyFileObject
*f
)
392 if (f
->f_fp
!= NULL
) {
393 if (f
->f_close
!= NULL
) {
394 Py_BEGIN_ALLOW_THREADS
396 sts
= (*f
->f_close
)(f
->f_fp
);
401 PyMem_Free(f
->f_setbuf
);
404 return PyErr_SetFromErrno(PyExc_IOError
);
406 return PyInt_FromLong((long)sts
);
412 /* Our very own off_t-like type, 64-bit if possible */
413 #if !defined(HAVE_LARGEFILE_SUPPORT)
414 typedef off_t Py_off_t
;
415 #elif SIZEOF_OFF_T >= 8
416 typedef off_t Py_off_t
;
417 #elif SIZEOF_FPOS_T >= 8
418 typedef fpos_t Py_off_t
;
420 #error "Large file support, but neither off_t nor fpos_t is large enough."
424 /* a portable fseek() function
425 return 0 on success, non-zero on failure (with errno set) */
427 _portable_fseek(FILE *fp
, Py_off_t offset
, int whence
)
429 #if !defined(HAVE_LARGEFILE_SUPPORT)
430 return fseek(fp
, offset
, whence
);
431 #elif defined(HAVE_FSEEKO) && SIZEOF_OFF_T >= 8
432 return fseeko(fp
, offset
, whence
);
433 #elif defined(HAVE_FSEEK64)
434 return fseek64(fp
, offset
, whence
);
435 #elif defined(__BEOS__)
436 return _fseek(fp
, offset
, whence
);
437 #elif SIZEOF_FPOS_T >= 8
438 /* lacking a 64-bit capable fseek(), use a 64-bit capable fsetpos()
439 and fgetpos() to implement fseek()*/
445 if (_lseeki64(fileno(fp
), 0, 2) == -1)
448 if (fseek(fp
, 0, SEEK_END
) != 0)
453 if (fgetpos(fp
, &pos
) != 0)
457 /* case SEEK_SET: break; */
459 return fsetpos(fp
, &offset
);
461 #error "Large file support, but no way to fseek."
466 /* a portable ftell() function
467 Return -1 on failure with errno set appropriately, current file
468 position on success */
470 _portable_ftell(FILE* fp
)
472 #if !defined(HAVE_LARGEFILE_SUPPORT)
474 #elif defined(HAVE_FTELLO) && SIZEOF_OFF_T >= 8
476 #elif defined(HAVE_FTELL64)
478 #elif SIZEOF_FPOS_T >= 8
480 if (fgetpos(fp
, &pos
) != 0)
484 #error "Large file support, but no way to ftell."
490 file_seek(PyFileObject
*f
, PyObject
*args
)
501 if (!PyArg_ParseTuple(args
, "O|i:seek", &offobj
, &whence
))
503 #if !defined(HAVE_LARGEFILE_SUPPORT)
504 offset
= PyInt_AsLong(offobj
);
506 offset
= PyLong_Check(offobj
) ?
507 PyLong_AsLongLong(offobj
) : PyInt_AsLong(offobj
);
509 if (PyErr_Occurred())
512 Py_BEGIN_ALLOW_THREADS
514 ret
= _portable_fseek(f
->f_fp
, offset
, whence
);
518 PyErr_SetFromErrno(PyExc_IOError
);
522 #ifdef WITH_UNIVERSAL_NEWLINES
530 #ifdef HAVE_FTRUNCATE
532 file_truncate(PyFileObject
*f
, PyObject
*args
)
536 PyObject
*newsizeobj
;
541 if (!PyArg_UnpackTuple(args
, "truncate", 0, 1, &newsizeobj
))
544 /* Set newsize to current postion if newsizeobj NULL, else to the
546 if (newsizeobj
!= NULL
) {
547 #if !defined(HAVE_LARGEFILE_SUPPORT)
548 newsize
= PyInt_AsLong(newsizeobj
);
550 newsize
= PyLong_Check(newsizeobj
) ?
551 PyLong_AsLongLong(newsizeobj
) :
552 PyInt_AsLong(newsizeobj
);
554 if (PyErr_Occurred())
558 /* Default to current position. */
559 Py_BEGIN_ALLOW_THREADS
561 newsize
= _portable_ftell(f
->f_fp
);
567 /* Flush the file. */
568 Py_BEGIN_ALLOW_THREADS
570 ret
= fflush(f
->f_fp
);
576 /* MS _chsize doesn't work if newsize doesn't fit in 32 bits,
577 so don't even try using it. */
579 Py_off_t current
; /* current file position */
583 /* current <- current file postion. */
584 if (newsizeobj
== NULL
)
587 Py_BEGIN_ALLOW_THREADS
589 current
= _portable_ftell(f
->f_fp
);
595 /* Move to newsize. */
596 if (current
!= newsize
) {
597 Py_BEGIN_ALLOW_THREADS
599 error
= _portable_fseek(f
->f_fp
, newsize
, SEEK_SET
)
606 /* Truncate. Note that this may grow the file! */
607 Py_BEGIN_ALLOW_THREADS
609 hFile
= (HANDLE
)_get_osfhandle(fileno(f
->f_fp
));
610 error
= hFile
== (HANDLE
)-1;
612 error
= SetEndOfFile(hFile
) == 0;
620 /* Restore original file position. */
621 if (current
!= newsize
) {
622 Py_BEGIN_ALLOW_THREADS
624 error
= _portable_fseek(f
->f_fp
, current
, SEEK_SET
)
632 Py_BEGIN_ALLOW_THREADS
634 ret
= ftruncate(fileno(f
->f_fp
), newsize
);
636 if (ret
!= 0) goto onioerror
;
637 #endif /* !MS_WINDOWS */
643 PyErr_SetFromErrno(PyExc_IOError
);
647 #endif /* HAVE_FTRUNCATE */
650 file_tell(PyFileObject
*f
)
656 Py_BEGIN_ALLOW_THREADS
658 pos
= _portable_ftell(f
->f_fp
);
661 PyErr_SetFromErrno(PyExc_IOError
);
665 #ifdef WITH_UNIVERSAL_NEWLINES
666 if (f
->f_skipnextlf
) {
672 } else if (c
!= EOF
) ungetc(c
, f
->f_fp
);
675 #if !defined(HAVE_LARGEFILE_SUPPORT)
676 return PyInt_FromLong(pos
);
678 return PyLong_FromLongLong(pos
);
683 file_fileno(PyFileObject
*f
)
687 return PyInt_FromLong((long) fileno(f
->f_fp
));
691 file_flush(PyFileObject
*f
)
697 Py_BEGIN_ALLOW_THREADS
699 res
= fflush(f
->f_fp
);
702 PyErr_SetFromErrno(PyExc_IOError
);
711 file_isatty(PyFileObject
*f
)
716 Py_BEGIN_ALLOW_THREADS
717 res
= isatty((int)fileno(f
->f_fp
));
719 return PyBool_FromLong(res
);
724 #define SMALLCHUNK 8192
726 #define SMALLCHUNK BUFSIZ
730 #define BIGCHUNK (512 * 32)
732 #define BIGCHUNK (512 * 1024)
736 new_buffersize(PyFileObject
*f
, size_t currentsize
)
741 if (fstat(fileno(f
->f_fp
), &st
) == 0) {
743 /* The following is not a bug: we really need to call lseek()
744 *and* ftell(). The reason is that some stdio libraries
745 mistakenly flush their buffer when ftell() is called and
746 the lseek() call it makes fails, thereby throwing away
747 data that cannot be recovered in any way. To avoid this,
748 we first test lseek(), and only call ftell() if lseek()
749 works. We can't use the lseek() value either, because we
750 need to take the amount of buffered data into account.
751 (Yet another reason why stdio stinks. :-) */
753 pos
= lseek(fileno(f
->f_fp
), 1L, SEEK_CUR
);
754 pos
= lseek(fileno(f
->f_fp
), -1L, SEEK_CUR
);
756 pos
= lseek(fileno(f
->f_fp
), 0L, SEEK_CUR
);
759 pos
= ftell(f
->f_fp
);
763 if (end
> pos
&& pos
>= 0)
764 return currentsize
+ end
- pos
+ 1;
765 /* Add 1 so if the file were to grow we'd notice. */
768 if (currentsize
> SMALLCHUNK
) {
769 /* Keep doubling until we reach BIGCHUNK;
770 then keep adding BIGCHUNK. */
771 if (currentsize
<= BIGCHUNK
)
772 return currentsize
+ currentsize
;
774 return currentsize
+ BIGCHUNK
;
776 return currentsize
+ SMALLCHUNK
;
779 #if defined(EWOULDBLOCK) && defined(EAGAIN) && EWOULDBLOCK != EAGAIN
780 #define BLOCKED_ERRNO(x) ((x) == EWOULDBLOCK || (x) == EAGAIN)
783 #define BLOCKED_ERRNO(x) ((x) == EWOULDBLOCK)
786 #define BLOCKED_ERRNO(x) ((x) == EAGAIN)
788 #define BLOCKED_ERRNO(x) 0
794 file_read(PyFileObject
*f
, PyObject
*args
)
796 long bytesrequested
= -1;
797 size_t bytesread
, buffersize
, chunksize
;
802 if (!PyArg_ParseTuple(args
, "|l:read", &bytesrequested
))
804 if (bytesrequested
< 0)
805 buffersize
= new_buffersize(f
, (size_t)0);
807 buffersize
= bytesrequested
;
808 if (buffersize
> INT_MAX
) {
809 PyErr_SetString(PyExc_OverflowError
,
810 "requested number of bytes is more than a Python string can hold");
813 v
= PyString_FromStringAndSize((char *)NULL
, buffersize
);
818 Py_BEGIN_ALLOW_THREADS
820 chunksize
= Py_UniversalNewlineFread(BUF(v
) + bytesread
,
821 buffersize
- bytesread
, f
->f_fp
, (PyObject
*)f
);
823 if (chunksize
== 0) {
824 if (!ferror(f
->f_fp
))
827 /* When in non-blocking mode, data shouldn't
828 * be discarded if a blocking signal was
829 * received. That will also happen if
830 * chunksize != 0, but bytesread < buffersize. */
831 if (bytesread
> 0 && BLOCKED_ERRNO(errno
))
833 PyErr_SetFromErrno(PyExc_IOError
);
837 bytesread
+= chunksize
;
838 if (bytesread
< buffersize
) {
842 if (bytesrequested
< 0) {
843 buffersize
= new_buffersize(f
, buffersize
);
844 if (_PyString_Resize(&v
, buffersize
) < 0)
847 /* Got what was requested. */
851 if (bytesread
!= buffersize
)
852 _PyString_Resize(&v
, bytesread
);
857 file_readinto(PyFileObject
*f
, PyObject
*args
)
865 if (!PyArg_ParseTuple(args
, "w#", &ptr
, &ntodo
))
869 Py_BEGIN_ALLOW_THREADS
871 nnow
= Py_UniversalNewlineFread(ptr
+ndone
, ntodo
, f
->f_fp
,
875 if (!ferror(f
->f_fp
))
877 PyErr_SetFromErrno(PyExc_IOError
);
884 return PyInt_FromLong((long)ndone
);
887 /**************************************************************************
888 Routine to get next line using platform fgets().
892 + MS threadsafe getc is very slow (multiple layers of function calls before+
893 after each character, to lock+unlock the stream).
894 + The stream-locking functions are MS-internal -- can't access them from user
896 + There's nothing Tim could find in the MS C or platform SDK libraries that
897 can worm around this.
898 + MS fgets locks/unlocks only once per line; it's the only hook we have.
900 So we use fgets for speed(!), despite that it's painful.
902 MS realloc is also slow.
904 Reports from other platforms on this method vs getc_unlocked (which MS doesn't
908 Tru64 Unix getline_via_fgets significantly faster
910 CAUTION: The C std isn't clear about this: in those cases where fgets
911 writes something into the buffer, can it write into any position beyond the
912 required trailing null byte? MSVC 6 fgets does not, and no platform is (yet)
913 known on which it does; and it would be a strange way to code fgets. Still,
914 getline_via_fgets may not work correctly if it does. The std test
915 test_bufio.py should fail if platform fgets() routinely writes beyond the
916 trailing null byte. #define DONT_USE_FGETS_IN_GETLINE to disable this code.
917 **************************************************************************/
919 /* Use this routine if told to, or by default on non-get_unlocked()
920 * platforms unless told not to. Yikes! Let's spell that out:
921 * On a platform with getc_unlocked():
922 * By default, use getc_unlocked().
923 * If you want to use fgets() instead, #define USE_FGETS_IN_GETLINE.
924 * On a platform without getc_unlocked():
925 * By default, use fgets().
926 * If you don't want to use fgets(), #define DONT_USE_FGETS_IN_GETLINE.
928 #if !defined(USE_FGETS_IN_GETLINE) && !defined(HAVE_GETC_UNLOCKED)
929 #define USE_FGETS_IN_GETLINE
932 #if defined(DONT_USE_FGETS_IN_GETLINE) && defined(USE_FGETS_IN_GETLINE)
933 #undef USE_FGETS_IN_GETLINE
936 #ifdef USE_FGETS_IN_GETLINE
938 getline_via_fgets(FILE *fp
)
940 /* INITBUFSIZE is the maximum line length that lets us get away with the fast
941 * no-realloc, one-fgets()-call path. Boosting it isn't free, because we have
942 * to fill this much of the buffer with a known value in order to figure out
943 * how much of the buffer fgets() overwrites. So if INITBUFSIZE is larger
944 * than "most" lines, we waste time filling unused buffer slots. 100 is
945 * surely adequate for most peoples' email archives, chewing over source code,
946 * etc -- "regular old text files".
947 * MAXBUFSIZE is the maximum line length that lets us get away with the less
948 * fast (but still zippy) no-realloc, two-fgets()-call path. See above for
949 * cautions about boosting that. 300 was chosen because the worst real-life
950 * text-crunching job reported on Python-Dev was a mail-log crawler where over
951 * half the lines were 254 chars.
953 #define INITBUFSIZE 100
954 #define MAXBUFSIZE 300
956 char buf
[MAXBUFSIZE
];
957 PyObject
* v
; /* the string object result */
958 char* pvfree
; /* address of next free slot */
959 char* pvend
; /* address one beyond last free slot */
960 size_t nfree
; /* # of free buffer slots; pvend-pvfree */
961 size_t total_v_size
; /* total # of slots in buffer */
962 size_t increment
; /* amount to increment the buffer */
964 /* Optimize for normal case: avoid _PyString_Resize if at all
965 * possible via first reading into stack buffer "buf".
967 total_v_size
= INITBUFSIZE
; /* start small and pray */
970 Py_BEGIN_ALLOW_THREADS
971 pvend
= buf
+ total_v_size
;
972 nfree
= pvend
- pvfree
;
973 memset(pvfree
, '\n', nfree
);
974 p
= fgets(pvfree
, nfree
, fp
);
979 if (PyErr_CheckSignals())
981 v
= PyString_FromStringAndSize(buf
, pvfree
- buf
);
984 /* fgets read *something* */
985 p
= memchr(pvfree
, '\n', nfree
);
987 /* Did the \n come from fgets or from us?
988 * Since fgets stops at the first \n, and then writes
989 * \0, if it's from fgets a \0 must be next. But if
990 * that's so, it could not have come from us, since
991 * the \n's we filled the buffer with have only more
994 if (p
+1 < pvend
&& *(p
+1) == '\0') {
995 /* It's from fgets: we win! In particular,
996 * we haven't done any mallocs yet, and can
997 * build the final result on the first try.
999 ++p
; /* include \n from fgets */
1002 /* Must be from us: fgets didn't fill the
1003 * buffer and didn't find a newline, so it
1004 * must be the last and newline-free line of
1007 assert(p
> pvfree
&& *(p
-1) == '\0');
1008 --p
; /* don't include \0 from fgets */
1010 v
= PyString_FromStringAndSize(buf
, p
- buf
);
1013 /* yuck: fgets overwrote all the newlines, i.e. the entire
1014 * buffer. So this line isn't over yet, or maybe it is but
1015 * we're exactly at EOF. If we haven't already, try using the
1016 * rest of the stack buffer.
1018 assert(*(pvend
-1) == '\0');
1019 if (pvfree
== buf
) {
1020 pvfree
= pvend
- 1; /* overwrite trailing null */
1021 total_v_size
= MAXBUFSIZE
;
1027 /* The stack buffer isn't big enough; malloc a string object and read
1030 total_v_size
= MAXBUFSIZE
<< 1;
1031 v
= PyString_FromStringAndSize((char*)NULL
, (int)total_v_size
);
1034 /* copy over everything except the last null byte */
1035 memcpy(BUF(v
), buf
, MAXBUFSIZE
-1);
1036 pvfree
= BUF(v
) + MAXBUFSIZE
- 1;
1038 /* Keep reading stuff into v; if it ever ends successfully, break
1039 * after setting p one beyond the end of the line. The code here is
1040 * very much like the code above, except reads into v's buffer; see
1041 * the code above for detailed comments about the logic.
1044 Py_BEGIN_ALLOW_THREADS
1045 pvend
= BUF(v
) + total_v_size
;
1046 nfree
= pvend
- pvfree
;
1047 memset(pvfree
, '\n', nfree
);
1048 p
= fgets(pvfree
, nfree
, fp
);
1049 Py_END_ALLOW_THREADS
1053 if (PyErr_CheckSignals()) {
1060 p
= memchr(pvfree
, '\n', nfree
);
1062 if (p
+1 < pvend
&& *(p
+1) == '\0') {
1063 /* \n came from fgets */
1067 /* \n came from us; last line of file, no newline */
1068 assert(p
> pvfree
&& *(p
-1) == '\0');
1072 /* expand buffer and try again */
1073 assert(*(pvend
-1) == '\0');
1074 increment
= total_v_size
>> 2; /* mild exponential growth */
1075 total_v_size
+= increment
;
1076 if (total_v_size
> INT_MAX
) {
1077 PyErr_SetString(PyExc_OverflowError
,
1078 "line is longer than a Python string can hold");
1082 if (_PyString_Resize(&v
, (int)total_v_size
) < 0)
1084 /* overwrite the trailing null byte */
1085 pvfree
= BUF(v
) + (total_v_size
- increment
- 1);
1087 if (BUF(v
) + total_v_size
!= p
)
1088 _PyString_Resize(&v
, p
- BUF(v
));
1093 #endif /* ifdef USE_FGETS_IN_GETLINE */
1095 /* Internal routine to get a line.
1096 Size argument interpretation:
1098 <= 0: read arbitrary line
1102 get_line(PyFileObject
*f
, int n
)
1107 size_t total_v_size
; /* total # of slots in buffer */
1108 size_t used_v_size
; /* # used slots in buffer */
1109 size_t increment
; /* amount to increment the buffer */
1111 #ifdef WITH_UNIVERSAL_NEWLINES
1112 int newlinetypes
= f
->f_newlinetypes
;
1113 int skipnextlf
= f
->f_skipnextlf
;
1114 int univ_newline
= f
->f_univ_newline
;
1117 #if defined(USE_FGETS_IN_GETLINE)
1118 #ifdef WITH_UNIVERSAL_NEWLINES
1119 if (n
<= 0 && !univ_newline
)
1123 return getline_via_fgets(fp
);
1125 total_v_size
= n
> 0 ? n
: 100;
1126 v
= PyString_FromStringAndSize((char *)NULL
, total_v_size
);
1130 end
= buf
+ total_v_size
;
1133 Py_BEGIN_ALLOW_THREADS
1135 #ifdef WITH_UNIVERSAL_NEWLINES
1137 c
= 'x'; /* Shut up gcc warning */
1138 while ( buf
!= end
&& (c
= GETC(fp
)) != EOF
) {
1142 /* Seeing a \n here with
1143 * skipnextlf true means we
1146 newlinetypes
|= NEWLINE_CRLF
;
1148 if (c
== EOF
) break;
1150 newlinetypes
|= NEWLINE_CR
;
1156 } else if ( c
== '\n')
1157 newlinetypes
|= NEWLINE_LF
;
1159 if (c
== '\n') break;
1161 if ( c
== EOF
&& skipnextlf
)
1162 newlinetypes
|= NEWLINE_CR
;
1163 } else /* If not universal newlines use the normal loop */
1165 while ((c
= GETC(fp
)) != EOF
&&
1166 (*buf
++ = c
) != '\n' &&
1170 Py_END_ALLOW_THREADS
1171 #ifdef WITH_UNIVERSAL_NEWLINES
1172 f
->f_newlinetypes
= newlinetypes
;
1173 f
->f_skipnextlf
= skipnextlf
;
1179 PyErr_SetFromErrno(PyExc_IOError
);
1185 if (PyErr_CheckSignals()) {
1191 /* Must be because buf == end */
1194 used_v_size
= total_v_size
;
1195 increment
= total_v_size
>> 2; /* mild exponential growth */
1196 total_v_size
+= increment
;
1197 if (total_v_size
> INT_MAX
) {
1198 PyErr_SetString(PyExc_OverflowError
,
1199 "line is longer than a Python string can hold");
1203 if (_PyString_Resize(&v
, total_v_size
) < 0)
1205 buf
= BUF(v
) + used_v_size
;
1206 end
= BUF(v
) + total_v_size
;
1209 used_v_size
= buf
- BUF(v
);
1210 if (used_v_size
!= total_v_size
)
1211 _PyString_Resize(&v
, used_v_size
);
1215 /* External C interface */
1218 PyFile_GetLine(PyObject
*f
, int n
)
1223 PyErr_BadInternalCall();
1227 if (PyFile_Check(f
)) {
1228 if (((PyFileObject
*)f
)->f_fp
== NULL
)
1229 return err_closed();
1230 result
= get_line((PyFileObject
*)f
, n
);
1236 reader
= PyObject_GetAttrString(f
, "readline");
1240 args
= Py_BuildValue("()");
1242 args
= Py_BuildValue("(i)", n
);
1247 result
= PyEval_CallObject(reader
, args
);
1250 if (result
!= NULL
&& !PyString_Check(result
) &&
1251 !PyUnicode_Check(result
)) {
1254 PyErr_SetString(PyExc_TypeError
,
1255 "object.readline() returned non-string");
1259 if (n
< 0 && result
!= NULL
&& PyString_Check(result
)) {
1260 char *s
= PyString_AS_STRING(result
);
1261 int len
= PyString_GET_SIZE(result
);
1265 PyErr_SetString(PyExc_EOFError
,
1266 "EOF when reading a line");
1268 else if (s
[len
-1] == '\n') {
1269 if (result
->ob_refcnt
== 1)
1270 _PyString_Resize(&result
, len
-1);
1273 v
= PyString_FromStringAndSize(s
, len
-1);
1279 #ifdef Py_USING_UNICODE
1280 if (n
< 0 && result
!= NULL
&& PyUnicode_Check(result
)) {
1281 Py_UNICODE
*s
= PyUnicode_AS_UNICODE(result
);
1282 int len
= PyUnicode_GET_SIZE(result
);
1286 PyErr_SetString(PyExc_EOFError
,
1287 "EOF when reading a line");
1289 else if (s
[len
-1] == '\n') {
1290 if (result
->ob_refcnt
== 1)
1291 PyUnicode_Resize(&result
, len
-1);
1294 v
= PyUnicode_FromUnicode(s
, len
-1);
1307 file_readline(PyFileObject
*f
, PyObject
*args
)
1311 if (f
->f_fp
== NULL
)
1312 return err_closed();
1313 if (!PyArg_ParseTuple(args
, "|i:readline", &n
))
1316 return PyString_FromString("");
1319 return get_line(f
, n
);
1323 file_readlines(PyFileObject
*f
, PyObject
*args
)
1328 char small_buffer
[SMALLCHUNK
];
1329 char *buffer
= small_buffer
;
1330 size_t buffersize
= SMALLCHUNK
;
1331 PyObject
*big_buffer
= NULL
;
1334 size_t totalread
= 0;
1339 if (f
->f_fp
== NULL
)
1340 return err_closed();
1341 if (!PyArg_ParseTuple(args
, "|l:readlines", &sizehint
))
1343 if ((list
= PyList_New(0)) == NULL
)
1349 Py_BEGIN_ALLOW_THREADS
1351 nread
= Py_UniversalNewlineFread(buffer
+nfilled
,
1352 buffersize
-nfilled
, f
->f_fp
, (PyObject
*)f
);
1353 Py_END_ALLOW_THREADS
1354 shortread
= (nread
< buffersize
-nfilled
);
1358 if (!ferror(f
->f_fp
))
1360 PyErr_SetFromErrno(PyExc_IOError
);
1368 p
= memchr(buffer
+nfilled
, '\n', nread
);
1370 /* Need a larger buffer to fit this line */
1373 if (buffersize
> INT_MAX
) {
1374 PyErr_SetString(PyExc_OverflowError
,
1375 "line is longer than a Python string can hold");
1378 if (big_buffer
== NULL
) {
1379 /* Create the big buffer */
1380 big_buffer
= PyString_FromStringAndSize(
1382 if (big_buffer
== NULL
)
1384 buffer
= PyString_AS_STRING(big_buffer
);
1385 memcpy(buffer
, small_buffer
, nfilled
);
1388 /* Grow the big buffer */
1389 if ( _PyString_Resize(&big_buffer
, buffersize
) < 0 )
1391 buffer
= PyString_AS_STRING(big_buffer
);
1395 end
= buffer
+nfilled
+nread
;
1398 /* Process complete lines */
1400 line
= PyString_FromStringAndSize(q
, p
-q
);
1403 err
= PyList_Append(list
, line
);
1408 p
= memchr(q
, '\n', end
-q
);
1409 } while (p
!= NULL
);
1410 /* Move the remaining incomplete line to the start */
1412 memmove(buffer
, q
, nfilled
);
1414 if (totalread
>= (size_t)sizehint
)
1418 /* Partial last line */
1419 line
= PyString_FromStringAndSize(buffer
, nfilled
);
1423 /* Need to complete the last line */
1424 PyObject
*rest
= get_line(f
, 0);
1429 PyString_Concat(&line
, rest
);
1434 err
= PyList_Append(list
, line
);
1440 Py_XDECREF(big_buffer
);
1445 file_write(PyFileObject
*f
, PyObject
*args
)
1449 if (f
->f_fp
== NULL
)
1450 return err_closed();
1451 if (!PyArg_ParseTuple(args
, f
->f_binary
? "s#" : "t#", &s
, &n
))
1454 Py_BEGIN_ALLOW_THREADS
1456 n2
= fwrite(s
, 1, n
, f
->f_fp
);
1457 Py_END_ALLOW_THREADS
1459 PyErr_SetFromErrno(PyExc_IOError
);
1468 file_writelines(PyFileObject
*f
, PyObject
*seq
)
1470 #define CHUNKSIZE 1000
1471 PyObject
*list
, *line
;
1472 PyObject
*it
; /* iter(seq) */
1474 int i
, j
, index
, len
, nwritten
, islist
;
1476 assert(seq
!= NULL
);
1477 if (f
->f_fp
== NULL
)
1478 return err_closed();
1482 islist
= PyList_Check(seq
);
1486 it
= PyObject_GetIter(seq
);
1488 PyErr_SetString(PyExc_TypeError
,
1489 "writelines() requires an iterable argument");
1492 /* From here on, fail by going to error, to reclaim "it". */
1493 list
= PyList_New(CHUNKSIZE
);
1498 /* Strategy: slurp CHUNKSIZE lines into a private list,
1499 checking that they are all strings, then write that list
1500 without holding the interpreter lock, then come back for more. */
1501 for (index
= 0; ; index
+= CHUNKSIZE
) {
1504 list
= PyList_GetSlice(seq
, index
, index
+CHUNKSIZE
);
1507 j
= PyList_GET_SIZE(list
);
1510 for (j
= 0; j
< CHUNKSIZE
; j
++) {
1511 line
= PyIter_Next(it
);
1513 if (PyErr_Occurred())
1517 PyList_SetItem(list
, j
, line
);
1523 /* Check that all entries are indeed strings. If not,
1524 apply the same rules as for file.write() and
1525 convert the results to strings. This is slow, but
1526 seems to be the only way since all conversion APIs
1527 could potentially execute Python code. */
1528 for (i
= 0; i
< j
; i
++) {
1529 PyObject
*v
= PyList_GET_ITEM(list
, i
);
1530 if (!PyString_Check(v
)) {
1533 if (((f
->f_binary
&&
1534 PyObject_AsReadBuffer(v
,
1535 (const void**)&buffer
,
1537 PyObject_AsCharBuffer(v
,
1540 PyErr_SetString(PyExc_TypeError
,
1541 "writelines() argument must be a sequence of strings");
1544 line
= PyString_FromStringAndSize(buffer
,
1549 PyList_SET_ITEM(list
, i
, line
);
1553 /* Since we are releasing the global lock, the
1554 following code may *not* execute Python code. */
1555 Py_BEGIN_ALLOW_THREADS
1558 for (i
= 0; i
< j
; i
++) {
1559 line
= PyList_GET_ITEM(list
, i
);
1560 len
= PyString_GET_SIZE(line
);
1561 nwritten
= fwrite(PyString_AS_STRING(line
),
1563 if (nwritten
!= len
) {
1565 PyErr_SetFromErrno(PyExc_IOError
);
1570 Py_END_ALLOW_THREADS
1586 file_getiter(PyFileObject
*f
)
1588 if (f
->f_fp
== NULL
)
1589 return err_closed();
1591 return (PyObject
*)f
;
1594 PyDoc_STRVAR(readline_doc
,
1595 "readline([size]) -> next line from the file, as a string.\n"
1597 "Retain newline. A non-negative size argument limits the maximum\n"
1598 "number of bytes to return (an incomplete line may be returned then).\n"
1599 "Return an empty string at EOF.");
1601 PyDoc_STRVAR(read_doc
,
1602 "read([size]) -> read at most size bytes, returned as a string.\n"
1604 "If the size argument is negative or omitted, read until EOF is reached.\n"
1605 "Notice that when in non-blocking mode, less data than what was requested\n"
1606 "may be returned, even if no size parameter was given.");
1608 PyDoc_STRVAR(write_doc
,
1609 "write(str) -> None. Write string str to file.\n"
1611 "Note that due to buffering, flush() or close() may be needed before\n"
1612 "the file on disk reflects the data written.");
1614 PyDoc_STRVAR(fileno_doc
,
1615 "fileno() -> integer \"file descriptor\".\n"
1617 "This is needed for lower-level file interfaces, such os.read().");
1619 PyDoc_STRVAR(seek_doc
,
1620 "seek(offset[, whence]) -> None. Move to new file position.\n"
1622 "Argument offset is a byte count. Optional argument whence defaults to\n"
1623 "0 (offset from start of file, offset should be >= 0); other values are 1\n"
1624 "(move relative to current position, positive or negative), and 2 (move\n"
1625 "relative to end of file, usually negative, although many platforms allow\n"
1626 "seeking beyond the end of a file). If the file is opened in text mode,\n"
1627 "only offsets returned by tell() are legal. Use of other offsets causes\n"
1628 "undefined behavior."
1630 "Note that not all file objects are seekable.");
1632 #ifdef HAVE_FTRUNCATE
1633 PyDoc_STRVAR(truncate_doc
,
1634 "truncate([size]) -> None. Truncate the file to at most size bytes.\n"
1636 "Size defaults to the current file position, as returned by tell().");
1639 PyDoc_STRVAR(tell_doc
,
1640 "tell() -> current file position, an integer (may be a long integer).");
1642 PyDoc_STRVAR(readinto_doc
,
1643 "readinto() -> Undocumented. Don't use this; it may go away.");
1645 PyDoc_STRVAR(readlines_doc
,
1646 "readlines([size]) -> list of strings, each a line from the file.\n"
1648 "Call readline() repeatedly and return a list of the lines so read.\n"
1649 "The optional size argument, if given, is an approximate bound on the\n"
1650 "total number of bytes in the lines returned.");
1652 PyDoc_STRVAR(xreadlines_doc
,
1653 "xreadlines() -> returns self.\n"
1655 "For backward compatibility. File objects now include the performance\n"
1656 "optimizations previously implemented in the xreadlines module.");
1658 PyDoc_STRVAR(writelines_doc
,
1659 "writelines(sequence_of_strings) -> None. Write the strings to the file.\n"
1661 "Note that newlines are not added. The sequence can be any iterable object\n"
1662 "producing strings. This is equivalent to calling write() for each string.");
1664 PyDoc_STRVAR(flush_doc
,
1665 "flush() -> None. Flush the internal I/O buffer.");
1667 PyDoc_STRVAR(close_doc
,
1668 "close() -> None or (perhaps) an integer. Close the file.\n"
1670 "Sets data attribute .closed to True. A closed file cannot be used for\n"
1671 "further I/O operations. close() may be called more than once without\n"
1672 "error. Some kinds of file objects (for example, opened by popen())\n"
1673 "may return an exit status upon closing.");
1675 PyDoc_STRVAR(isatty_doc
,
1676 "isatty() -> true or false. True if the file is connected to a tty device.");
1678 static PyMethodDef file_methods
[] = {
1679 {"readline", (PyCFunction
)file_readline
, METH_VARARGS
, readline_doc
},
1680 {"read", (PyCFunction
)file_read
, METH_VARARGS
, read_doc
},
1681 {"write", (PyCFunction
)file_write
, METH_VARARGS
, write_doc
},
1682 {"fileno", (PyCFunction
)file_fileno
, METH_NOARGS
, fileno_doc
},
1683 {"seek", (PyCFunction
)file_seek
, METH_VARARGS
, seek_doc
},
1684 #ifdef HAVE_FTRUNCATE
1685 {"truncate", (PyCFunction
)file_truncate
, METH_VARARGS
, truncate_doc
},
1687 {"tell", (PyCFunction
)file_tell
, METH_NOARGS
, tell_doc
},
1688 {"readinto", (PyCFunction
)file_readinto
, METH_VARARGS
, readinto_doc
},
1689 {"readlines", (PyCFunction
)file_readlines
,METH_VARARGS
, readlines_doc
},
1690 {"xreadlines",(PyCFunction
)file_getiter
, METH_NOARGS
, xreadlines_doc
},
1691 {"writelines",(PyCFunction
)file_writelines
, METH_O
, writelines_doc
},
1692 {"flush", (PyCFunction
)file_flush
, METH_NOARGS
, flush_doc
},
1693 {"close", (PyCFunction
)file_close
, METH_NOARGS
, close_doc
},
1694 {"isatty", (PyCFunction
)file_isatty
, METH_NOARGS
, isatty_doc
},
1695 {NULL
, NULL
} /* sentinel */
1698 #define OFF(x) offsetof(PyFileObject, x)
1700 static PyMemberDef file_memberlist
[] = {
1701 {"softspace", T_INT
, OFF(f_softspace
), 0,
1702 "flag indicating that a space needs to be printed; used by print"},
1703 {"mode", T_OBJECT
, OFF(f_mode
), RO
,
1704 "file mode ('r', 'U', 'w', 'a', possibly with 'b' or '+' added)"},
1705 {"name", T_OBJECT
, OFF(f_name
), RO
,
1707 {"encoding", T_OBJECT
, OFF(f_encoding
), RO
,
1709 /* getattr(f, "closed") is implemented without this table */
1710 {NULL
} /* Sentinel */
1714 get_closed(PyFileObject
*f
, void *closure
)
1716 return PyBool_FromLong((long)(f
->f_fp
== 0));
1718 #ifdef WITH_UNIVERSAL_NEWLINES
1720 get_newlines(PyFileObject
*f
, void *closure
)
1722 switch (f
->f_newlinetypes
) {
1723 case NEWLINE_UNKNOWN
:
1727 return PyString_FromString("\r");
1729 return PyString_FromString("\n");
1730 case NEWLINE_CR
|NEWLINE_LF
:
1731 return Py_BuildValue("(ss)", "\r", "\n");
1733 return PyString_FromString("\r\n");
1734 case NEWLINE_CR
|NEWLINE_CRLF
:
1735 return Py_BuildValue("(ss)", "\r", "\r\n");
1736 case NEWLINE_LF
|NEWLINE_CRLF
:
1737 return Py_BuildValue("(ss)", "\n", "\r\n");
1738 case NEWLINE_CR
|NEWLINE_LF
|NEWLINE_CRLF
:
1739 return Py_BuildValue("(sss)", "\r", "\n", "\r\n");
1741 PyErr_Format(PyExc_SystemError
,
1742 "Unknown newlines value 0x%x\n",
1749 static PyGetSetDef file_getsetlist
[] = {
1750 {"closed", (getter
)get_closed
, NULL
, "True if the file is closed"},
1751 #ifdef WITH_UNIVERSAL_NEWLINES
1752 {"newlines", (getter
)get_newlines
, NULL
,
1753 "end-of-line convention used in this file"},
1759 drop_readahead(PyFileObject
*f
)
1761 if (f
->f_buf
!= NULL
) {
1762 PyMem_Free(f
->f_buf
);
1767 /* Make sure that file has a readahead buffer with at least one byte
1768 (unless at EOF) and no more than bufsize. Returns negative value on
1771 readahead(PyFileObject
*f
, int bufsize
)
1775 if (f
->f_buf
!= NULL
) {
1776 if( (f
->f_bufend
- f
->f_bufptr
) >= 1)
1781 if ((f
->f_buf
= PyMem_Malloc(bufsize
)) == NULL
) {
1784 Py_BEGIN_ALLOW_THREADS
1786 chunksize
= Py_UniversalNewlineFread(
1787 f
->f_buf
, bufsize
, f
->f_fp
, (PyObject
*)f
);
1788 Py_END_ALLOW_THREADS
1789 if (chunksize
== 0) {
1790 if (ferror(f
->f_fp
)) {
1791 PyErr_SetFromErrno(PyExc_IOError
);
1797 f
->f_bufptr
= f
->f_buf
;
1798 f
->f_bufend
= f
->f_buf
+ chunksize
;
1802 /* Used by file_iternext. The returned string will start with 'skip'
1803 uninitialized bytes followed by the remainder of the line. Don't be
1804 horrified by the recursive call: maximum recursion depth is limited by
1805 logarithmic buffer growth to about 50 even when reading a 1gb line. */
1807 static PyStringObject
*
1808 readahead_get_line_skip(PyFileObject
*f
, int skip
, int bufsize
)
1815 if (f
->f_buf
== NULL
)
1816 if (readahead(f
, bufsize
) < 0)
1819 len
= f
->f_bufend
- f
->f_bufptr
;
1821 return (PyStringObject
*)
1822 PyString_FromStringAndSize(NULL
, skip
);
1823 bufptr
= memchr(f
->f_bufptr
, '\n', len
);
1824 if (bufptr
!= NULL
) {
1825 bufptr
++; /* Count the '\n' */
1826 len
= bufptr
- f
->f_bufptr
;
1827 s
= (PyStringObject
*)
1828 PyString_FromStringAndSize(NULL
, skip
+len
);
1831 memcpy(PyString_AS_STRING(s
)+skip
, f
->f_bufptr
, len
);
1832 f
->f_bufptr
= bufptr
;
1833 if (bufptr
== f
->f_bufend
)
1836 bufptr
= f
->f_bufptr
;
1838 f
->f_buf
= NULL
; /* Force new readahead buffer */
1839 s
= readahead_get_line_skip(
1840 f
, skip
+len
, bufsize
+ (bufsize
>>2) );
1845 memcpy(PyString_AS_STRING(s
)+skip
, bufptr
, len
);
1851 /* A larger buffer size may actually decrease performance. */
1852 #define READAHEAD_BUFSIZE 8192
1855 file_iternext(PyFileObject
*f
)
1859 if (f
->f_fp
== NULL
)
1860 return err_closed();
1862 l
= readahead_get_line_skip(f
, 0, READAHEAD_BUFSIZE
);
1863 if (l
== NULL
|| PyString_GET_SIZE(l
) == 0) {
1867 return (PyObject
*)l
;
1872 file_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
1875 static PyObject
*not_yet_string
;
1877 assert(type
!= NULL
&& type
->tp_alloc
!= NULL
);
1879 if (not_yet_string
== NULL
) {
1880 not_yet_string
= PyString_FromString("<uninitialized file>");
1881 if (not_yet_string
== NULL
)
1885 self
= type
->tp_alloc(type
, 0);
1887 /* Always fill in the name and mode, so that nobody else
1888 needs to special-case NULLs there. */
1889 Py_INCREF(not_yet_string
);
1890 ((PyFileObject
*)self
)->f_name
= not_yet_string
;
1891 Py_INCREF(not_yet_string
);
1892 ((PyFileObject
*)self
)->f_mode
= not_yet_string
;
1894 ((PyFileObject
*)self
)->f_encoding
= Py_None
;
1900 file_init(PyObject
*self
, PyObject
*args
, PyObject
*kwds
)
1902 PyFileObject
*foself
= (PyFileObject
*)self
;
1904 static char *kwlist
[] = {"name", "mode", "buffering", 0};
1908 int wideargument
= 0;
1910 assert(PyFile_Check(self
));
1911 if (foself
->f_fp
!= NULL
) {
1912 /* Have to close the existing file first. */
1913 PyObject
*closeresult
= file_close(foself
);
1914 if (closeresult
== NULL
)
1916 Py_DECREF(closeresult
);
1919 #ifdef Py_WIN_WIDE_FILENAMES
1920 if (GetVersion() < 0x80000000) { /* On NT, so wide API available */
1922 if (PyArg_ParseTupleAndKeywords(args
, kwds
, "U|si:file",
1923 kwlist
, &po
, &mode
, &bufsize
)) {
1925 if (fill_file_fields(foself
, NULL
, name
, mode
,
1926 fclose
, po
) == NULL
)
1929 /* Drop the argument parsing error as narrow
1930 strings are also valid. */
1936 if (!wideargument
) {
1937 if (!PyArg_ParseTupleAndKeywords(args
, kwds
, "et|si:file", kwlist
,
1938 Py_FileSystemDefaultEncoding
,
1942 if (fill_file_fields(foself
, NULL
, name
, mode
,
1943 fclose
, NULL
) == NULL
)
1946 if (open_the_file(foself
, name
, mode
) == NULL
)
1948 foself
->f_setbuf
= NULL
;
1949 PyFile_SetBufSize(self
, bufsize
);
1956 PyMem_Free(name
); /* free the encoded string */
1960 PyDoc_VAR(file_doc
) =
1962 "file(name[, mode[, buffering]]) -> file object\n"
1964 "Open a file. The mode can be 'r', 'w' or 'a' for reading (default),\n"
1965 "writing or appending. The file will be created if it doesn't exist\n"
1966 "when opened for writing or appending; it will be truncated when\n"
1967 "opened for writing. Add a 'b' to the mode for binary files.\n"
1968 "Add a '+' to the mode to allow simultaneous reading and writing.\n"
1969 "If the buffering argument is given, 0 means unbuffered, 1 means line\n"
1970 "buffered, and larger numbers specify the buffer size.\n"
1972 #ifdef WITH_UNIVERSAL_NEWLINES
1974 "Add a 'U' to mode to open the file for input with universal newline\n"
1975 "support. Any line ending in the input file will be seen as a '\\n'\n"
1976 "in Python. Also, a file so opened gains the attribute 'newlines';\n"
1977 "the value for this attribute is one of None (no newline read yet),\n"
1978 "'\\r', '\\n', '\\r\\n' or a tuple containing all the newline types seen.\n"
1980 "'U' cannot be combined with 'w' or '+' mode.\n"
1982 #endif /* WITH_UNIVERSAL_NEWLINES */
1985 "Note: open() is an alias for file()."
1988 PyTypeObject PyFile_Type
= {
1989 PyObject_HEAD_INIT(&PyType_Type
)
1992 sizeof(PyFileObject
),
1994 (destructor
)file_dealloc
, /* tp_dealloc */
1999 (reprfunc
)file_repr
, /* tp_repr */
2000 0, /* tp_as_number */
2001 0, /* tp_as_sequence */
2002 0, /* tp_as_mapping */
2006 PyObject_GenericGetAttr
, /* tp_getattro */
2007 /* softspace is writable: we must supply tp_setattro */
2008 PyObject_GenericSetAttr
, /* tp_setattro */
2009 0, /* tp_as_buffer */
2010 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
, /* tp_flags */
2011 file_doc
, /* tp_doc */
2012 0, /* tp_traverse */
2014 0, /* tp_richcompare */
2015 0, /* tp_weaklistoffset */
2016 (getiterfunc
)file_getiter
, /* tp_iter */
2017 (iternextfunc
)file_iternext
, /* tp_iternext */
2018 file_methods
, /* tp_methods */
2019 file_memberlist
, /* tp_members */
2020 file_getsetlist
, /* tp_getset */
2023 0, /* tp_descr_get */
2024 0, /* tp_descr_set */
2025 0, /* tp_dictoffset */
2026 (initproc
)file_init
, /* tp_init */
2027 PyType_GenericAlloc
, /* tp_alloc */
2028 file_new
, /* tp_new */
2029 PyObject_Del
, /* tp_free */
2032 /* Interface for the 'soft space' between print items. */
2035 PyFile_SoftSpace(PyObject
*f
, int newflag
)
2041 else if (PyFile_Check(f
)) {
2042 oldflag
= ((PyFileObject
*)f
)->f_softspace
;
2043 ((PyFileObject
*)f
)->f_softspace
= newflag
;
2047 v
= PyObject_GetAttrString(f
, "softspace");
2052 oldflag
= PyInt_AsLong(v
);
2055 v
= PyInt_FromLong((long)newflag
);
2059 if (PyObject_SetAttrString(f
, "softspace", v
) != 0)
2067 /* Interfaces to write objects/strings to file-like objects */
2070 PyFile_WriteObject(PyObject
*v
, PyObject
*f
, int flags
)
2072 PyObject
*writer
, *value
, *args
, *result
;
2074 PyErr_SetString(PyExc_TypeError
, "writeobject with NULL file");
2077 else if (PyFile_Check(f
)) {
2078 FILE *fp
= PyFile_AsFile(f
);
2079 #ifdef Py_USING_UNICODE
2080 PyObject
*enc
= ((PyFileObject
*)f
)->f_encoding
;
2087 #ifdef Py_USING_UNICODE
2088 if ((flags
& Py_PRINT_RAW
) &&
2089 PyUnicode_Check(v
) && enc
!= Py_None
) {
2090 char *cenc
= PyString_AS_STRING(enc
);
2091 value
= PyUnicode_AsEncodedString(v
, cenc
, "strict");
2098 result
= PyObject_Print(value
, fp
, flags
);
2102 return PyObject_Print(v
, fp
, flags
);
2105 writer
= PyObject_GetAttrString(f
, "write");
2108 if (flags
& Py_PRINT_RAW
) {
2109 if (PyUnicode_Check(v
)) {
2113 value
= PyObject_Str(v
);
2116 value
= PyObject_Repr(v
);
2117 if (value
== NULL
) {
2121 args
= Py_BuildValue("(O)", value
);
2127 result
= PyEval_CallObject(writer
, args
);
2138 PyFile_WriteString(const char *s
, PyObject
*f
)
2141 /* Should be caused by a pre-existing error */
2142 if (!PyErr_Occurred())
2143 PyErr_SetString(PyExc_SystemError
,
2144 "null file for PyFile_WriteString");
2147 else if (PyFile_Check(f
)) {
2148 FILE *fp
= PyFile_AsFile(f
);
2156 else if (!PyErr_Occurred()) {
2157 PyObject
*v
= PyString_FromString(s
);
2161 err
= PyFile_WriteObject(v
, f
, Py_PRINT_RAW
);
2169 /* Try to get a file-descriptor from a Python object. If the object
2170 is an integer or long integer, its value is returned. If not, the
2171 object's fileno() method is called if it exists; the method must return
2172 an integer or long integer, which is returned as the file descriptor value.
2173 -1 is returned on failure.
2176 int PyObject_AsFileDescriptor(PyObject
*o
)
2181 if (PyInt_Check(o
)) {
2182 fd
= PyInt_AsLong(o
);
2184 else if (PyLong_Check(o
)) {
2185 fd
= PyLong_AsLong(o
);
2187 else if ((meth
= PyObject_GetAttrString(o
, "fileno")) != NULL
)
2189 PyObject
*fno
= PyEval_CallObject(meth
, NULL
);
2194 if (PyInt_Check(fno
)) {
2195 fd
= PyInt_AsLong(fno
);
2198 else if (PyLong_Check(fno
)) {
2199 fd
= PyLong_AsLong(fno
);
2203 PyErr_SetString(PyExc_TypeError
,
2204 "fileno() returned a non-integer");
2210 PyErr_SetString(PyExc_TypeError
,
2211 "argument must be an int, or have a fileno() method.");
2216 PyErr_Format(PyExc_ValueError
,
2217 "file descriptor cannot be a negative integer (%i)",
2224 #ifdef WITH_UNIVERSAL_NEWLINES
2225 /* From here on we need access to the real fgets and fread */
2230 ** Py_UniversalNewlineFgets is an fgets variation that understands
2231 ** all of \r, \n and \r\n conventions.
2232 ** The stream should be opened in binary mode.
2233 ** If fobj is NULL the routine always does newline conversion, and
2234 ** it may peek one char ahead to gobble the second char in \r\n.
2235 ** If fobj is non-NULL it must be a PyFileObject. In this case there
2236 ** is no readahead but in stead a flag is used to skip a following
2237 ** \n on the next read. Also, if the file is open in binary mode
2238 ** the whole conversion is skipped. Finally, the routine keeps track of
2239 ** the different types of newlines seen.
2240 ** Note that we need no error handling: fgets() treats error and eof
2244 Py_UniversalNewlineFgets(char *buf
, int n
, FILE *stream
, PyObject
*fobj
)
2248 int newlinetypes
= 0;
2250 int univ_newline
= 1;
2253 if (!PyFile_Check(fobj
)) {
2254 errno
= ENXIO
; /* What can you do... */
2257 univ_newline
= ((PyFileObject
*)fobj
)->f_univ_newline
;
2258 if ( !univ_newline
)
2259 return fgets(buf
, n
, stream
);
2260 newlinetypes
= ((PyFileObject
*)fobj
)->f_newlinetypes
;
2261 skipnextlf
= ((PyFileObject
*)fobj
)->f_skipnextlf
;
2264 c
= 'x'; /* Shut up gcc warning */
2265 while (--n
> 0 && (c
= GETC(stream
)) != EOF
) {
2269 /* Seeing a \n here with skipnextlf true
2270 ** means we saw a \r before.
2272 newlinetypes
|= NEWLINE_CRLF
;
2274 if (c
== EOF
) break;
2277 ** Note that c == EOF also brings us here,
2278 ** so we're okay if the last char in the file
2281 newlinetypes
|= NEWLINE_CR
;
2285 /* A \r is translated into a \n, and we skip
2286 ** an adjacent \n, if any. We don't set the
2287 ** newlinetypes flag until we've seen the next char.
2291 } else if ( c
== '\n') {
2292 newlinetypes
|= NEWLINE_LF
;
2295 if (c
== '\n') break;
2297 if ( c
== EOF
&& skipnextlf
)
2298 newlinetypes
|= NEWLINE_CR
;
2299 FUNLOCKFILE(stream
);
2302 ((PyFileObject
*)fobj
)->f_newlinetypes
= newlinetypes
;
2303 ((PyFileObject
*)fobj
)->f_skipnextlf
= skipnextlf
;
2304 } else if ( skipnextlf
) {
2305 /* If we have no file object we cannot save the
2306 ** skipnextlf flag. We have to readahead, which
2307 ** will cause a pause if we're reading from an
2308 ** interactive stream, but that is very unlikely
2309 ** unless we're doing something silly like
2310 ** execfile("/dev/tty").
2322 ** Py_UniversalNewlineFread is an fread variation that understands
2323 ** all of \r, \n and \r\n conventions.
2324 ** The stream should be opened in binary mode.
2325 ** fobj must be a PyFileObject. In this case there
2326 ** is no readahead but in stead a flag is used to skip a following
2327 ** \n on the next read. Also, if the file is open in binary mode
2328 ** the whole conversion is skipped. Finally, the routine keeps track of
2329 ** the different types of newlines seen.
2332 Py_UniversalNewlineFread(char *buf
, size_t n
,
2333 FILE *stream
, PyObject
*fobj
)
2336 PyFileObject
*f
= (PyFileObject
*)fobj
;
2337 int newlinetypes
, skipnextlf
;
2339 assert(buf
!= NULL
);
2340 assert(stream
!= NULL
);
2342 if (!fobj
|| !PyFile_Check(fobj
)) {
2343 errno
= ENXIO
; /* What can you do... */
2346 if (!f
->f_univ_newline
)
2347 return fread(buf
, 1, n
, stream
);
2348 newlinetypes
= f
->f_newlinetypes
;
2349 skipnextlf
= f
->f_skipnextlf
;
2350 /* Invariant: n is the number of bytes remaining to be filled
2358 nread
= fread(dst
, 1, n
, stream
);
2363 n
-= nread
; /* assuming 1 byte out for each in; will adjust */
2364 shortread
= n
!= 0; /* true iff EOF or error */
2368 /* Save as LF and set flag to skip next LF. */
2372 else if (skipnextlf
&& c
== '\n') {
2373 /* Skip LF, and remember we saw CR LF. */
2375 newlinetypes
|= NEWLINE_CRLF
;
2379 /* Normal char to be stored in buffer. Also
2380 * update the newlinetypes flag if either this
2381 * is an LF or the previous char was a CR.
2384 newlinetypes
|= NEWLINE_LF
;
2385 else if (skipnextlf
)
2386 newlinetypes
|= NEWLINE_CR
;
2392 /* If this is EOF, update type flags. */
2393 if (skipnextlf
&& feof(stream
))
2394 newlinetypes
|= NEWLINE_CR
;
2398 f
->f_newlinetypes
= newlinetypes
;
2399 f
->f_skipnextlf
= skipnextlf
;