1 /* File object implementation */
3 #define PY_SSIZE_T_CLEAN
5 #include "structmember.h"
7 #ifdef HAVE_SYS_TYPES_H
9 #endif /* HAVE_SYS_TYPES_H */
12 #define fileno _fileno
13 /* can simulate truncate with Win32 API functions; see file_truncate */
14 #define HAVE_FTRUNCATE
15 #define WIN32_LEAN_AND_MEAN
19 #if defined(PYOS_OS2) && defined(PYCC_GCC)
23 #define BUF(v) PyString_AS_STRING((PyStringObject *)v)
25 #ifndef DONT_HAVE_ERRNO_H
29 #ifdef HAVE_GETC_UNLOCKED
30 #define GETC(f) getc_unlocked(f)
31 #define FLOCKFILE(f) flockfile(f)
32 #define FUNLOCKFILE(f) funlockfile(f)
34 #define GETC(f) getc(f)
36 #define FUNLOCKFILE(f)
39 /* Bits in f_newlinetypes */
40 #define NEWLINE_UNKNOWN 0 /* No newline seen, yet */
41 #define NEWLINE_CR 1 /* \r newline seen */
42 #define NEWLINE_LF 2 /* \n newline seen */
43 #define NEWLINE_CRLF 4 /* \r\n newline seen */
46 * These macros release the GIL while preventing the f_close() function being
47 * called in the interval between them. For that purpose, a running total of
48 * the number of currently running unlocked code sections is kept in
49 * the unlocked_count field of the PyFileObject. The close() method raises
50 * an IOError if that field is non-zero. See issue #815646, #595601.
53 #define FILE_BEGIN_ALLOW_THREADS(fobj) \
55 fobj->unlocked_count++; \
56 Py_BEGIN_ALLOW_THREADS
58 #define FILE_END_ALLOW_THREADS(fobj) \
59 Py_END_ALLOW_THREADS \
60 fobj->unlocked_count--; \
61 assert(fobj->unlocked_count >= 0); \
64 #define FILE_ABORT_ALLOW_THREADS(fobj) \
66 fobj->unlocked_count--; \
67 assert(fobj->unlocked_count >= 0);
74 PyFile_AsFile(PyObject
*f
)
76 if (f
== NULL
|| !PyFile_Check(f
))
79 return ((PyFileObject
*)f
)->f_fp
;
82 void PyFile_IncUseCount(PyFileObject
*fobj
)
84 fobj
->unlocked_count
++;
87 void PyFile_DecUseCount(PyFileObject
*fobj
)
89 fobj
->unlocked_count
--;
90 assert(fobj
->unlocked_count
>= 0);
94 PyFile_Name(PyObject
*f
)
96 if (f
== NULL
|| !PyFile_Check(f
))
99 return ((PyFileObject
*)f
)->f_name
;
102 /* This is a safe wrapper around PyObject_Print to print to the FILE
103 of a PyFileObject. PyObject_Print releases the GIL but knows nothing
104 about PyFileObject. */
106 file_PyObject_Print(PyObject
*op
, PyFileObject
*f
, int flags
)
109 PyFile_IncUseCount(f
);
110 result
= PyObject_Print(op
, f
->f_fp
, flags
);
111 PyFile_DecUseCount(f
);
115 /* On Unix, fopen will succeed for directories.
116 In Python, there should be no file objects referring to
117 directories, so we need a check. */
120 dircheck(PyFileObject
* f
)
122 #if defined(HAVE_FSTAT) && defined(S_IFDIR) && defined(EISDIR)
126 if (fstat(fileno(f
->f_fp
), &buf
) == 0 &&
127 S_ISDIR(buf
.st_mode
)) {
128 char *msg
= strerror(EISDIR
);
129 PyObject
*exc
= PyObject_CallFunction(PyExc_IOError
, "(isO)",
130 EISDIR
, msg
, f
->f_name
);
131 PyErr_SetObject(PyExc_IOError
, exc
);
141 fill_file_fields(PyFileObject
*f
, FILE *fp
, PyObject
*name
, char *mode
,
142 int (*close
)(FILE *))
144 assert(name
!= NULL
);
146 assert(PyFile_Check(f
));
147 assert(f
->f_fp
== NULL
);
149 Py_DECREF(f
->f_name
);
150 Py_DECREF(f
->f_mode
);
151 Py_DECREF(f
->f_encoding
);
152 Py_DECREF(f
->f_errors
);
157 f
->f_mode
= PyString_FromString(mode
);
161 f
->f_binary
= strchr(mode
,'b') != NULL
;
163 f
->f_univ_newline
= (strchr(mode
, 'U') != NULL
);
164 f
->f_newlinetypes
= NEWLINE_UNKNOWN
;
167 f
->f_encoding
= Py_None
;
169 f
->f_errors
= Py_None
;
171 if (f
->f_mode
== NULL
)
175 return (PyObject
*) f
;
178 #if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__)
179 #define Py_VERIFY_WINNT
180 /* The CRT on windows compiled with Visual Studio 2005 and higher may
181 * assert if given invalid mode strings. This is all fine and well
182 * in static languages like C where the mode string is typcially hard
183 * coded. But in Python, were we pass in the mode string from the user,
184 * we need to verify it first manually
186 static int _PyVerify_Mode_WINNT(const char *mode
)
188 /* See if mode string is valid on Windows to avoid hard assertions */
189 /* remove leading spacese */
195 while(*mode
== ' ') /* strip initial spaces */
197 if (!strchr("rwa", *mode
)) /* must start with one of these */
200 if (*mode
== ' ' || *mode
== 'N') /* ignore spaces and N */
202 s
= "+TD"; /* each of this can appear only once */
203 c
= strchr(s
, *mode
);
206 if (singles
& (1<<idx
))
211 s
= "btcnSR"; /* only one of each letter in the pairs allowed */
212 c
= strchr(s
, *mode
);
214 ptrdiff_t idx
= (s
-c
)/2;
215 if (pairs
& (1<<idx
))
224 return 0; /* found an invalid char */
228 char *e
[] = {"UTF-8", "UTF-16LE", "UNICODE"};
232 if (strncmp(mode
, "ccs", 3))
241 for(encoding
= 0; encoding
<_countof(e
); ++encoding
) {
242 size_t l
= strlen(e
[encoding
]);
243 if (!strncmp(mode
, e
[encoding
], l
)) {
244 mode
+= l
; /* found a valid encoding */
248 if (encoding
== _countof(e
))
251 /* skip trailing spaces */
255 return *mode
== '\0'; /* must be at the end of the string */
259 /* check for known incorrect mode strings - problem is, platforms are
260 free to accept any mode characters they like and are supposed to
261 ignore stuff they don't understand... write or append mode with
262 universal newline support is expressly forbidden by PEP 278.
263 Additionally, remove the 'U' from the mode string as platforms
264 won't know what it is. Non-zero return signals an exception */
266 _PyFile_SanitizeMode(char *mode
)
269 size_t len
= strlen(mode
);
272 PyErr_SetString(PyExc_ValueError
, "empty mode string");
276 upos
= strchr(mode
, 'U');
278 memmove(upos
, upos
+1, len
-(upos
-mode
)); /* incl null char */
280 if (mode
[0] == 'w' || mode
[0] == 'a') {
281 PyErr_Format(PyExc_ValueError
, "universal newline "
282 "mode can only be used with modes "
283 "starting with 'r'");
287 if (mode
[0] != 'r') {
288 memmove(mode
+1, mode
, strlen(mode
)+1);
292 if (!strchr(mode
, 'b')) {
293 memmove(mode
+2, mode
+1, strlen(mode
));
296 } else if (mode
[0] != 'r' && mode
[0] != 'w' && mode
[0] != 'a') {
297 PyErr_Format(PyExc_ValueError
, "mode string must begin with "
298 "one of 'r', 'w', 'a' or 'U', not '%.200s'", mode
);
301 #ifdef Py_VERIFY_WINNT
302 /* additional checks on NT with visual studio 2005 and higher */
303 if (!_PyVerify_Mode_WINNT(mode
)) {
304 PyErr_Format(PyExc_ValueError
, "Invalid mode ('%.50s')", mode
);
312 open_the_file(PyFileObject
*f
, char *name
, char *mode
)
316 assert(PyFile_Check(f
));
318 /* windows ignores the passed name in order to support Unicode */
319 assert(f
->f_name
!= NULL
);
321 assert(name
!= NULL
);
323 assert(mode
!= NULL
);
324 assert(f
->f_fp
== NULL
);
326 /* probably need to replace 'U' by 'rb' */
327 newmode
= PyMem_MALLOC(strlen(mode
) + 3);
332 strcpy(newmode
, mode
);
334 if (_PyFile_SanitizeMode(newmode
)) {
339 /* rexec.py can't stop a user from getting the file() constructor --
340 all they have to do is get *any* file object f, and then do
341 type(f). Here we prevent them from doing damage with it. */
342 if (PyEval_GetRestricted()) {
343 PyErr_SetString(PyExc_IOError
,
344 "file() constructor not accessible in restricted mode");
351 if (PyUnicode_Check(f
->f_name
)) {
353 wmode
= PyUnicode_DecodeASCII(newmode
, strlen(newmode
), NULL
);
354 if (f
->f_name
&& wmode
) {
355 FILE_BEGIN_ALLOW_THREADS(f
)
356 /* PyUnicode_AS_UNICODE OK without thread
357 lock as it is a simple dereference. */
358 f
->f_fp
= _wfopen(PyUnicode_AS_UNICODE(f
->f_name
),
359 PyUnicode_AS_UNICODE(wmode
));
360 FILE_END_ALLOW_THREADS(f
)
365 if (NULL
== f
->f_fp
&& NULL
!= name
) {
366 FILE_BEGIN_ALLOW_THREADS(f
)
367 f
->f_fp
= fopen(name
, newmode
);
368 FILE_END_ALLOW_THREADS(f
)
371 if (f
->f_fp
== NULL
) {
372 #if defined _MSC_VER && (_MSC_VER < 1400 || !defined(__STDC_SECURE_LIB__))
373 /* MSVC 6 (Microsoft) leaves errno at 0 for bad mode strings,
374 * across all Windows flavors. When it sets EINVAL varies
375 * across Windows flavors, the exact conditions aren't
376 * documented, and the answer lies in the OS's implementation
377 * of Win32's CreateFile function (whose source is secret).
378 * Seems the best we can do is map EINVAL to ENOENT.
379 * Starting with Visual Studio .NET 2005, EINVAL is correctly
380 * set by our CRT error handler (set in exceptions.c.)
382 if (errno
== 0) /* bad mode string */
384 else if (errno
== EINVAL
) /* unknown, but not a mode string */
387 /* EINVAL is returned when an invalid filename or
388 * an invalid mode is supplied. */
389 if (errno
== EINVAL
) {
392 PyOS_snprintf(message
, 100,
393 "invalid mode ('%.50s') or filename", mode
);
394 v
= Py_BuildValue("(isO)", errno
, message
, f
->f_name
);
396 PyErr_SetObject(PyExc_IOError
, v
);
401 PyErr_SetFromErrnoWithFilenameObject(PyExc_IOError
, f
->f_name
);
410 return (PyObject
*)f
;
414 close_the_file(PyFileObject
*f
)
417 int (*local_close
)(FILE *);
418 FILE *local_fp
= f
->f_fp
;
419 if (local_fp
!= NULL
) {
420 local_close
= f
->f_close
;
421 if (local_close
!= NULL
&& f
->unlocked_count
> 0) {
422 if (f
->ob_refcnt
> 0) {
423 PyErr_SetString(PyExc_IOError
,
424 "close() called during concurrent "
425 "operation on the same file object.");
427 /* This should not happen unless someone is
428 * carelessly playing with the PyFileObject
429 * struct fields and/or its associated FILE
431 PyErr_SetString(PyExc_SystemError
,
432 "PyFileObject locking error in "
433 "destructor (refcnt <= 0 at close).");
437 /* NULL out the FILE pointer before releasing the GIL, because
438 * it will not be valid anymore after the close() function is
441 if (local_close
!= NULL
) {
442 Py_BEGIN_ALLOW_THREADS
444 sts
= (*local_close
)(local_fp
);
447 return PyErr_SetFromErrno(PyExc_IOError
);
449 return PyInt_FromLong((long)sts
);
456 PyFile_FromFile(FILE *fp
, char *name
, char *mode
, int (*close
)(FILE *))
458 PyFileObject
*f
= (PyFileObject
*)PyFile_Type
.tp_new(&PyFile_Type
,
461 PyObject
*o_name
= PyString_FromString(name
);
464 if (fill_file_fields(f
, fp
, o_name
, mode
, close
) == NULL
) {
470 return (PyObject
*) f
;
474 PyFile_FromString(char *name
, char *mode
)
476 extern int fclose(FILE *);
479 f
= (PyFileObject
*)PyFile_FromFile((FILE *)NULL
, name
, mode
, fclose
);
481 if (open_the_file(f
, name
, mode
) == NULL
) {
486 return (PyObject
*)f
;
490 PyFile_SetBufSize(PyObject
*f
, int bufsize
)
492 PyFileObject
*file
= (PyFileObject
*)f
;
513 if (type
== _IONBF
) {
514 PyMem_Free(file
->f_setbuf
);
515 file
->f_setbuf
= NULL
;
517 file
->f_setbuf
= (char *)PyMem_Realloc(file
->f_setbuf
,
521 setvbuf(file
->f_fp
, file
->f_setbuf
, type
, bufsize
);
522 #else /* !HAVE_SETVBUF */
523 setbuf(file
->f_fp
, file
->f_setbuf
);
524 #endif /* !HAVE_SETVBUF */
528 /* Set the encoding used to output Unicode strings.
529 Return 1 on success, 0 on failure. */
532 PyFile_SetEncoding(PyObject
*f
, const char *enc
)
534 return PyFile_SetEncodingAndErrors(f
, enc
, NULL
);
538 PyFile_SetEncodingAndErrors(PyObject
*f
, const char *enc
, char* errors
)
540 PyFileObject
*file
= (PyFileObject
*)f
;
541 PyObject
*str
, *oerrors
;
543 assert(PyFile_Check(f
));
544 str
= PyString_FromString(enc
);
548 oerrors
= PyString_FromString(errors
);
557 Py_DECREF(file
->f_encoding
);
558 file
->f_encoding
= str
;
559 Py_DECREF(file
->f_errors
);
560 file
->f_errors
= oerrors
;
567 PyErr_SetString(PyExc_ValueError
, "I/O operation on closed file");
571 /* Refuse regular file I/O if there's data in the iteration-buffer.
572 * Mixing them would cause data to arrive out of order, as the read*
573 * methods don't use the iteration buffer. */
575 err_iterbuffered(void)
577 PyErr_SetString(PyExc_ValueError
,
578 "Mixing iteration and read methods would lose data");
582 static void drop_readahead(PyFileObject
*);
587 file_dealloc(PyFileObject
*f
)
590 if (f
->weakreflist
!= NULL
)
591 PyObject_ClearWeakRefs((PyObject
*) f
);
592 ret
= close_the_file(f
);
594 PySys_WriteStderr("close failed in file object destructor:\n");
600 PyMem_Free(f
->f_setbuf
);
601 Py_XDECREF(f
->f_name
);
602 Py_XDECREF(f
->f_mode
);
603 Py_XDECREF(f
->f_encoding
);
604 Py_XDECREF(f
->f_errors
);
606 Py_TYPE(f
)->tp_free((PyObject
*)f
);
610 file_repr(PyFileObject
*f
)
612 if (PyUnicode_Check(f
->f_name
)) {
613 #ifdef Py_USING_UNICODE
614 PyObject
*ret
= NULL
;
615 PyObject
*name
= PyUnicode_AsUnicodeEscapeString(f
->f_name
);
616 const char *name_str
= name
? PyString_AsString(name
) : "?";
617 ret
= PyString_FromFormat("<%s file u'%s', mode '%s' at %p>",
618 f
->f_fp
== NULL
? "closed" : "open",
620 PyString_AsString(f
->f_mode
),
626 return PyString_FromFormat("<%s file '%s', mode '%s' at %p>",
627 f
->f_fp
== NULL
? "closed" : "open",
628 PyString_AsString(f
->f_name
),
629 PyString_AsString(f
->f_mode
),
635 file_close(PyFileObject
*f
)
637 PyObject
*sts
= close_the_file(f
);
638 PyMem_Free(f
->f_setbuf
);
644 /* Our very own off_t-like type, 64-bit if possible */
645 #if !defined(HAVE_LARGEFILE_SUPPORT)
646 typedef off_t Py_off_t
;
647 #elif SIZEOF_OFF_T >= 8
648 typedef off_t Py_off_t
;
649 #elif SIZEOF_FPOS_T >= 8
650 typedef fpos_t Py_off_t
;
652 #error "Large file support, but neither off_t nor fpos_t is large enough."
656 /* a portable fseek() function
657 return 0 on success, non-zero on failure (with errno set) */
659 _portable_fseek(FILE *fp
, Py_off_t offset
, int whence
)
661 #if !defined(HAVE_LARGEFILE_SUPPORT)
662 return fseek(fp
, offset
, whence
);
663 #elif defined(HAVE_FSEEKO) && SIZEOF_OFF_T >= 8
664 return fseeko(fp
, offset
, whence
);
665 #elif defined(HAVE_FSEEK64)
666 return fseek64(fp
, offset
, whence
);
667 #elif defined(__BEOS__)
668 return _fseek(fp
, offset
, whence
);
669 #elif SIZEOF_FPOS_T >= 8
670 /* lacking a 64-bit capable fseek(), use a 64-bit capable fsetpos()
671 and fgetpos() to implement fseek()*/
677 if (_lseeki64(fileno(fp
), 0, 2) == -1)
680 if (fseek(fp
, 0, SEEK_END
) != 0)
685 if (fgetpos(fp
, &pos
) != 0)
689 /* case SEEK_SET: break; */
691 return fsetpos(fp
, &offset
);
693 #error "Large file support, but no way to fseek."
698 /* a portable ftell() function
699 Return -1 on failure with errno set appropriately, current file
700 position on success */
702 _portable_ftell(FILE* fp
)
704 #if !defined(HAVE_LARGEFILE_SUPPORT)
706 #elif defined(HAVE_FTELLO) && SIZEOF_OFF_T >= 8
708 #elif defined(HAVE_FTELL64)
710 #elif SIZEOF_FPOS_T >= 8
712 if (fgetpos(fp
, &pos
) != 0)
716 #error "Large file support, but no way to ftell."
722 file_seek(PyFileObject
*f
, PyObject
*args
)
727 PyObject
*offobj
, *off_index
;
733 if (!PyArg_ParseTuple(args
, "O|i:seek", &offobj
, &whence
))
735 off_index
= PyNumber_Index(offobj
);
737 if (!PyFloat_Check(offobj
))
739 /* Deprecated in 2.6 */
741 if (PyErr_WarnEx(PyExc_DeprecationWarning
,
742 "integer argument expected, got float",
748 #if !defined(HAVE_LARGEFILE_SUPPORT)
749 offset
= PyInt_AsLong(off_index
);
751 offset
= PyLong_Check(off_index
) ?
752 PyLong_AsLongLong(off_index
) : PyInt_AsLong(off_index
);
754 Py_DECREF(off_index
);
755 if (PyErr_Occurred())
758 FILE_BEGIN_ALLOW_THREADS(f
)
760 ret
= _portable_fseek(f
->f_fp
, offset
, whence
);
761 FILE_END_ALLOW_THREADS(f
)
764 PyErr_SetFromErrno(PyExc_IOError
);
774 #ifdef HAVE_FTRUNCATE
776 file_truncate(PyFileObject
*f
, PyObject
*args
)
779 PyObject
*newsizeobj
= NULL
;
785 if (!PyArg_UnpackTuple(args
, "truncate", 0, 1, &newsizeobj
))
788 /* Get current file position. If the file happens to be open for
789 * update and the last operation was an input operation, C doesn't
790 * define what the later fflush() will do, but we promise truncate()
791 * won't change the current position (and fflush() *does* change it
792 * then at least on Windows). The easiest thing is to capture
793 * current pos now and seek back to it at the end.
795 FILE_BEGIN_ALLOW_THREADS(f
)
797 initialpos
= _portable_ftell(f
->f_fp
);
798 FILE_END_ALLOW_THREADS(f
)
799 if (initialpos
== -1)
802 /* Set newsize to current postion if newsizeobj NULL, else to the
805 if (newsizeobj
!= NULL
) {
806 #if !defined(HAVE_LARGEFILE_SUPPORT)
807 newsize
= PyInt_AsLong(newsizeobj
);
809 newsize
= PyLong_Check(newsizeobj
) ?
810 PyLong_AsLongLong(newsizeobj
) :
811 PyInt_AsLong(newsizeobj
);
813 if (PyErr_Occurred())
816 else /* default to current position */
817 newsize
= initialpos
;
819 /* Flush the stream. We're mixing stream-level I/O with lower-level
820 * I/O, and a flush may be necessary to synch both platform views
821 * of the current file state.
823 FILE_BEGIN_ALLOW_THREADS(f
)
825 ret
= fflush(f
->f_fp
);
826 FILE_END_ALLOW_THREADS(f
)
831 /* MS _chsize doesn't work if newsize doesn't fit in 32 bits,
832 so don't even try using it. */
836 /* Have to move current pos to desired endpoint on Windows. */
837 FILE_BEGIN_ALLOW_THREADS(f
)
839 ret
= _portable_fseek(f
->f_fp
, newsize
, SEEK_SET
) != 0;
840 FILE_END_ALLOW_THREADS(f
)
844 /* Truncate. Note that this may grow the file! */
845 FILE_BEGIN_ALLOW_THREADS(f
)
847 hFile
= (HANDLE
)_get_osfhandle(fileno(f
->f_fp
));
848 ret
= hFile
== (HANDLE
)-1;
850 ret
= SetEndOfFile(hFile
) == 0;
854 FILE_END_ALLOW_THREADS(f
)
859 FILE_BEGIN_ALLOW_THREADS(f
)
861 ret
= ftruncate(fileno(f
->f_fp
), newsize
);
862 FILE_END_ALLOW_THREADS(f
)
865 #endif /* !MS_WINDOWS */
867 /* Restore original file position. */
868 FILE_BEGIN_ALLOW_THREADS(f
)
870 ret
= _portable_fseek(f
->f_fp
, initialpos
, SEEK_SET
) != 0;
871 FILE_END_ALLOW_THREADS(f
)
879 PyErr_SetFromErrno(PyExc_IOError
);
883 #endif /* HAVE_FTRUNCATE */
886 file_tell(PyFileObject
*f
)
892 FILE_BEGIN_ALLOW_THREADS(f
)
894 pos
= _portable_ftell(f
->f_fp
);
895 FILE_END_ALLOW_THREADS(f
)
898 PyErr_SetFromErrno(PyExc_IOError
);
902 if (f
->f_skipnextlf
) {
906 f
->f_newlinetypes
|= NEWLINE_CRLF
;
909 } else if (c
!= EOF
) ungetc(c
, f
->f_fp
);
911 #if !defined(HAVE_LARGEFILE_SUPPORT)
912 return PyInt_FromLong(pos
);
914 return PyLong_FromLongLong(pos
);
919 file_fileno(PyFileObject
*f
)
923 return PyInt_FromLong((long) fileno(f
->f_fp
));
927 file_flush(PyFileObject
*f
)
933 FILE_BEGIN_ALLOW_THREADS(f
)
935 res
= fflush(f
->f_fp
);
936 FILE_END_ALLOW_THREADS(f
)
938 PyErr_SetFromErrno(PyExc_IOError
);
947 file_isatty(PyFileObject
*f
)
952 FILE_BEGIN_ALLOW_THREADS(f
)
953 res
= isatty((int)fileno(f
->f_fp
));
954 FILE_END_ALLOW_THREADS(f
)
955 return PyBool_FromLong(res
);
960 #define SMALLCHUNK 8192
962 #define SMALLCHUNK BUFSIZ
966 #define BIGCHUNK (512 * 32)
968 #define BIGCHUNK (512 * 1024)
972 new_buffersize(PyFileObject
*f
, size_t currentsize
)
977 if (fstat(fileno(f
->f_fp
), &st
) == 0) {
979 /* The following is not a bug: we really need to call lseek()
980 *and* ftell(). The reason is that some stdio libraries
981 mistakenly flush their buffer when ftell() is called and
982 the lseek() call it makes fails, thereby throwing away
983 data that cannot be recovered in any way. To avoid this,
984 we first test lseek(), and only call ftell() if lseek()
985 works. We can't use the lseek() value either, because we
986 need to take the amount of buffered data into account.
987 (Yet another reason why stdio stinks. :-) */
988 pos
= lseek(fileno(f
->f_fp
), 0L, SEEK_CUR
);
990 pos
= ftell(f
->f_fp
);
994 if (end
> pos
&& pos
>= 0)
995 return currentsize
+ end
- pos
+ 1;
996 /* Add 1 so if the file were to grow we'd notice. */
999 if (currentsize
> SMALLCHUNK
) {
1000 /* Keep doubling until we reach BIGCHUNK;
1001 then keep adding BIGCHUNK. */
1002 if (currentsize
<= BIGCHUNK
)
1003 return currentsize
+ currentsize
;
1005 return currentsize
+ BIGCHUNK
;
1007 return currentsize
+ SMALLCHUNK
;
1010 #if defined(EWOULDBLOCK) && defined(EAGAIN) && EWOULDBLOCK != EAGAIN
1011 #define BLOCKED_ERRNO(x) ((x) == EWOULDBLOCK || (x) == EAGAIN)
1014 #define BLOCKED_ERRNO(x) ((x) == EWOULDBLOCK)
1017 #define BLOCKED_ERRNO(x) ((x) == EAGAIN)
1019 #define BLOCKED_ERRNO(x) 0
1025 file_read(PyFileObject
*f
, PyObject
*args
)
1027 long bytesrequested
= -1;
1028 size_t bytesread
, buffersize
, chunksize
;
1031 if (f
->f_fp
== NULL
)
1032 return err_closed();
1033 /* refuse to mix with f.next() */
1034 if (f
->f_buf
!= NULL
&&
1035 (f
->f_bufend
- f
->f_bufptr
) > 0 &&
1036 f
->f_buf
[0] != '\0')
1037 return err_iterbuffered();
1038 if (!PyArg_ParseTuple(args
, "|l:read", &bytesrequested
))
1040 if (bytesrequested
< 0)
1041 buffersize
= new_buffersize(f
, (size_t)0);
1043 buffersize
= bytesrequested
;
1044 if (buffersize
> PY_SSIZE_T_MAX
) {
1045 PyErr_SetString(PyExc_OverflowError
,
1046 "requested number of bytes is more than a Python string can hold");
1049 v
= PyString_FromStringAndSize((char *)NULL
, buffersize
);
1054 FILE_BEGIN_ALLOW_THREADS(f
)
1056 chunksize
= Py_UniversalNewlineFread(BUF(v
) + bytesread
,
1057 buffersize
- bytesread
, f
->f_fp
, (PyObject
*)f
);
1058 FILE_END_ALLOW_THREADS(f
)
1059 if (chunksize
== 0) {
1060 if (!ferror(f
->f_fp
))
1063 /* When in non-blocking mode, data shouldn't
1064 * be discarded if a blocking signal was
1065 * received. That will also happen if
1066 * chunksize != 0, but bytesread < buffersize. */
1067 if (bytesread
> 0 && BLOCKED_ERRNO(errno
))
1069 PyErr_SetFromErrno(PyExc_IOError
);
1073 bytesread
+= chunksize
;
1074 if (bytesread
< buffersize
) {
1078 if (bytesrequested
< 0) {
1079 buffersize
= new_buffersize(f
, buffersize
);
1080 if (_PyString_Resize(&v
, buffersize
) < 0)
1083 /* Got what was requested. */
1087 if (bytesread
!= buffersize
)
1088 _PyString_Resize(&v
, bytesread
);
1093 file_readinto(PyFileObject
*f
, PyObject
*args
)
1097 Py_ssize_t ndone
, nnow
;
1100 if (f
->f_fp
== NULL
)
1101 return err_closed();
1102 /* refuse to mix with f.next() */
1103 if (f
->f_buf
!= NULL
&&
1104 (f
->f_bufend
- f
->f_bufptr
) > 0 &&
1105 f
->f_buf
[0] != '\0')
1106 return err_iterbuffered();
1107 if (!PyArg_ParseTuple(args
, "w*", &pbuf
))
1113 FILE_BEGIN_ALLOW_THREADS(f
)
1115 nnow
= Py_UniversalNewlineFread(ptr
+ndone
, ntodo
, f
->f_fp
,
1117 FILE_END_ALLOW_THREADS(f
)
1119 if (!ferror(f
->f_fp
))
1121 PyErr_SetFromErrno(PyExc_IOError
);
1123 PyBuffer_Release(&pbuf
);
1129 PyBuffer_Release(&pbuf
);
1130 return PyInt_FromSsize_t(ndone
);
1133 /**************************************************************************
1134 Routine to get next line using platform fgets().
1138 + MS threadsafe getc is very slow (multiple layers of function calls before+
1139 after each character, to lock+unlock the stream).
1140 + The stream-locking functions are MS-internal -- can't access them from user
1142 + There's nothing Tim could find in the MS C or platform SDK libraries that
1143 can worm around this.
1144 + MS fgets locks/unlocks only once per line; it's the only hook we have.
1146 So we use fgets for speed(!), despite that it's painful.
1148 MS realloc is also slow.
1150 Reports from other platforms on this method vs getc_unlocked (which MS doesn't
1154 Tru64 Unix getline_via_fgets significantly faster
1156 CAUTION: The C std isn't clear about this: in those cases where fgets
1157 writes something into the buffer, can it write into any position beyond the
1158 required trailing null byte? MSVC 6 fgets does not, and no platform is (yet)
1159 known on which it does; and it would be a strange way to code fgets. Still,
1160 getline_via_fgets may not work correctly if it does. The std test
1161 test_bufio.py should fail if platform fgets() routinely writes beyond the
1162 trailing null byte. #define DONT_USE_FGETS_IN_GETLINE to disable this code.
1163 **************************************************************************/
1165 /* Use this routine if told to, or by default on non-get_unlocked()
1166 * platforms unless told not to. Yikes! Let's spell that out:
1167 * On a platform with getc_unlocked():
1168 * By default, use getc_unlocked().
1169 * If you want to use fgets() instead, #define USE_FGETS_IN_GETLINE.
1170 * On a platform without getc_unlocked():
1171 * By default, use fgets().
1172 * If you don't want to use fgets(), #define DONT_USE_FGETS_IN_GETLINE.
1174 #if !defined(USE_FGETS_IN_GETLINE) && !defined(HAVE_GETC_UNLOCKED)
1175 #define USE_FGETS_IN_GETLINE
1178 #if defined(DONT_USE_FGETS_IN_GETLINE) && defined(USE_FGETS_IN_GETLINE)
1179 #undef USE_FGETS_IN_GETLINE
1182 #ifdef USE_FGETS_IN_GETLINE
1184 getline_via_fgets(PyFileObject
*f
, FILE *fp
)
1186 /* INITBUFSIZE is the maximum line length that lets us get away with the fast
1187 * no-realloc, one-fgets()-call path. Boosting it isn't free, because we have
1188 * to fill this much of the buffer with a known value in order to figure out
1189 * how much of the buffer fgets() overwrites. So if INITBUFSIZE is larger
1190 * than "most" lines, we waste time filling unused buffer slots. 100 is
1191 * surely adequate for most peoples' email archives, chewing over source code,
1192 * etc -- "regular old text files".
1193 * MAXBUFSIZE is the maximum line length that lets us get away with the less
1194 * fast (but still zippy) no-realloc, two-fgets()-call path. See above for
1195 * cautions about boosting that. 300 was chosen because the worst real-life
1196 * text-crunching job reported on Python-Dev was a mail-log crawler where over
1197 * half the lines were 254 chars.
1199 #define INITBUFSIZE 100
1200 #define MAXBUFSIZE 300
1202 char buf
[MAXBUFSIZE
];
1203 PyObject
* v
; /* the string object result */
1204 char* pvfree
; /* address of next free slot */
1205 char* pvend
; /* address one beyond last free slot */
1206 size_t nfree
; /* # of free buffer slots; pvend-pvfree */
1207 size_t total_v_size
; /* total # of slots in buffer */
1208 size_t increment
; /* amount to increment the buffer */
1211 /* Optimize for normal case: avoid _PyString_Resize if at all
1212 * possible via first reading into stack buffer "buf".
1214 total_v_size
= INITBUFSIZE
; /* start small and pray */
1217 FILE_BEGIN_ALLOW_THREADS(f
)
1218 pvend
= buf
+ total_v_size
;
1219 nfree
= pvend
- pvfree
;
1220 memset(pvfree
, '\n', nfree
);
1221 assert(nfree
< INT_MAX
); /* Should be atmost MAXBUFSIZE */
1222 p
= fgets(pvfree
, (int)nfree
, fp
);
1223 FILE_END_ALLOW_THREADS(f
)
1227 if (PyErr_CheckSignals())
1229 v
= PyString_FromStringAndSize(buf
, pvfree
- buf
);
1232 /* fgets read *something* */
1233 p
= memchr(pvfree
, '\n', nfree
);
1235 /* Did the \n come from fgets or from us?
1236 * Since fgets stops at the first \n, and then writes
1237 * \0, if it's from fgets a \0 must be next. But if
1238 * that's so, it could not have come from us, since
1239 * the \n's we filled the buffer with have only more
1240 * \n's to the right.
1242 if (p
+1 < pvend
&& *(p
+1) == '\0') {
1243 /* It's from fgets: we win! In particular,
1244 * we haven't done any mallocs yet, and can
1245 * build the final result on the first try.
1247 ++p
; /* include \n from fgets */
1250 /* Must be from us: fgets didn't fill the
1251 * buffer and didn't find a newline, so it
1252 * must be the last and newline-free line of
1255 assert(p
> pvfree
&& *(p
-1) == '\0');
1256 --p
; /* don't include \0 from fgets */
1258 v
= PyString_FromStringAndSize(buf
, p
- buf
);
1261 /* yuck: fgets overwrote all the newlines, i.e. the entire
1262 * buffer. So this line isn't over yet, or maybe it is but
1263 * we're exactly at EOF. If we haven't already, try using the
1264 * rest of the stack buffer.
1266 assert(*(pvend
-1) == '\0');
1267 if (pvfree
== buf
) {
1268 pvfree
= pvend
- 1; /* overwrite trailing null */
1269 total_v_size
= MAXBUFSIZE
;
1275 /* The stack buffer isn't big enough; malloc a string object and read
1278 total_v_size
= MAXBUFSIZE
<< 1;
1279 v
= PyString_FromStringAndSize((char*)NULL
, (int)total_v_size
);
1282 /* copy over everything except the last null byte */
1283 memcpy(BUF(v
), buf
, MAXBUFSIZE
-1);
1284 pvfree
= BUF(v
) + MAXBUFSIZE
- 1;
1286 /* Keep reading stuff into v; if it ever ends successfully, break
1287 * after setting p one beyond the end of the line. The code here is
1288 * very much like the code above, except reads into v's buffer; see
1289 * the code above for detailed comments about the logic.
1292 FILE_BEGIN_ALLOW_THREADS(f
)
1293 pvend
= BUF(v
) + total_v_size
;
1294 nfree
= pvend
- pvfree
;
1295 memset(pvfree
, '\n', nfree
);
1296 assert(nfree
< INT_MAX
);
1297 p
= fgets(pvfree
, (int)nfree
, fp
);
1298 FILE_END_ALLOW_THREADS(f
)
1302 if (PyErr_CheckSignals()) {
1309 p
= memchr(pvfree
, '\n', nfree
);
1311 if (p
+1 < pvend
&& *(p
+1) == '\0') {
1312 /* \n came from fgets */
1316 /* \n came from us; last line of file, no newline */
1317 assert(p
> pvfree
&& *(p
-1) == '\0');
1321 /* expand buffer and try again */
1322 assert(*(pvend
-1) == '\0');
1323 increment
= total_v_size
>> 2; /* mild exponential growth */
1324 prev_v_size
= total_v_size
;
1325 total_v_size
+= increment
;
1326 /* check for overflow */
1327 if (total_v_size
<= prev_v_size
||
1328 total_v_size
> PY_SSIZE_T_MAX
) {
1329 PyErr_SetString(PyExc_OverflowError
,
1330 "line is longer than a Python string can hold");
1334 if (_PyString_Resize(&v
, (int)total_v_size
) < 0)
1336 /* overwrite the trailing null byte */
1337 pvfree
= BUF(v
) + (prev_v_size
- 1);
1339 if (BUF(v
) + total_v_size
!= p
)
1340 _PyString_Resize(&v
, p
- BUF(v
));
1345 #endif /* ifdef USE_FGETS_IN_GETLINE */
1347 /* Internal routine to get a line.
1348 Size argument interpretation:
1350 <= 0: read arbitrary line
1354 get_line(PyFileObject
*f
, int n
)
1359 size_t total_v_size
; /* total # of slots in buffer */
1360 size_t used_v_size
; /* # used slots in buffer */
1361 size_t increment
; /* amount to increment the buffer */
1363 int newlinetypes
= f
->f_newlinetypes
;
1364 int skipnextlf
= f
->f_skipnextlf
;
1365 int univ_newline
= f
->f_univ_newline
;
1367 #if defined(USE_FGETS_IN_GETLINE)
1368 if (n
<= 0 && !univ_newline
)
1369 return getline_via_fgets(f
, fp
);
1371 total_v_size
= n
> 0 ? n
: 100;
1372 v
= PyString_FromStringAndSize((char *)NULL
, total_v_size
);
1376 end
= buf
+ total_v_size
;
1379 FILE_BEGIN_ALLOW_THREADS(f
)
1382 c
= 'x'; /* Shut up gcc warning */
1383 while ( buf
!= end
&& (c
= GETC(fp
)) != EOF
) {
1387 /* Seeing a \n here with
1388 * skipnextlf true means we
1391 newlinetypes
|= NEWLINE_CRLF
;
1393 if (c
== EOF
) break;
1395 newlinetypes
|= NEWLINE_CR
;
1401 } else if ( c
== '\n')
1402 newlinetypes
|= NEWLINE_LF
;
1404 if (c
== '\n') break;
1406 if ( c
== EOF
&& skipnextlf
)
1407 newlinetypes
|= NEWLINE_CR
;
1408 } else /* If not universal newlines use the normal loop */
1409 while ((c
= GETC(fp
)) != EOF
&&
1410 (*buf
++ = c
) != '\n' &&
1414 FILE_END_ALLOW_THREADS(f
)
1415 f
->f_newlinetypes
= newlinetypes
;
1416 f
->f_skipnextlf
= skipnextlf
;
1421 PyErr_SetFromErrno(PyExc_IOError
);
1427 if (PyErr_CheckSignals()) {
1433 /* Must be because buf == end */
1436 used_v_size
= total_v_size
;
1437 increment
= total_v_size
>> 2; /* mild exponential growth */
1438 total_v_size
+= increment
;
1439 if (total_v_size
> PY_SSIZE_T_MAX
) {
1440 PyErr_SetString(PyExc_OverflowError
,
1441 "line is longer than a Python string can hold");
1445 if (_PyString_Resize(&v
, total_v_size
) < 0)
1447 buf
= BUF(v
) + used_v_size
;
1448 end
= BUF(v
) + total_v_size
;
1451 used_v_size
= buf
- BUF(v
);
1452 if (used_v_size
!= total_v_size
)
1453 _PyString_Resize(&v
, used_v_size
);
1457 /* External C interface */
1460 PyFile_GetLine(PyObject
*f
, int n
)
1465 PyErr_BadInternalCall();
1469 if (PyFile_Check(f
)) {
1470 PyFileObject
*fo
= (PyFileObject
*)f
;
1471 if (fo
->f_fp
== NULL
)
1472 return err_closed();
1473 /* refuse to mix with f.next() */
1474 if (fo
->f_buf
!= NULL
&&
1475 (fo
->f_bufend
- fo
->f_bufptr
) > 0 &&
1476 fo
->f_buf
[0] != '\0')
1477 return err_iterbuffered();
1478 result
= get_line(fo
, n
);
1484 reader
= PyObject_GetAttrString(f
, "readline");
1488 args
= PyTuple_New(0);
1490 args
= Py_BuildValue("(i)", n
);
1495 result
= PyEval_CallObject(reader
, args
);
1498 if (result
!= NULL
&& !PyString_Check(result
) &&
1499 !PyUnicode_Check(result
)) {
1502 PyErr_SetString(PyExc_TypeError
,
1503 "object.readline() returned non-string");
1507 if (n
< 0 && result
!= NULL
&& PyString_Check(result
)) {
1508 char *s
= PyString_AS_STRING(result
);
1509 Py_ssize_t len
= PyString_GET_SIZE(result
);
1513 PyErr_SetString(PyExc_EOFError
,
1514 "EOF when reading a line");
1516 else if (s
[len
-1] == '\n') {
1517 if (result
->ob_refcnt
== 1)
1518 _PyString_Resize(&result
, len
-1);
1521 v
= PyString_FromStringAndSize(s
, len
-1);
1527 #ifdef Py_USING_UNICODE
1528 if (n
< 0 && result
!= NULL
&& PyUnicode_Check(result
)) {
1529 Py_UNICODE
*s
= PyUnicode_AS_UNICODE(result
);
1530 Py_ssize_t len
= PyUnicode_GET_SIZE(result
);
1534 PyErr_SetString(PyExc_EOFError
,
1535 "EOF when reading a line");
1537 else if (s
[len
-1] == '\n') {
1538 if (result
->ob_refcnt
== 1)
1539 PyUnicode_Resize(&result
, len
-1);
1542 v
= PyUnicode_FromUnicode(s
, len
-1);
1555 file_readline(PyFileObject
*f
, PyObject
*args
)
1559 if (f
->f_fp
== NULL
)
1560 return err_closed();
1561 /* refuse to mix with f.next() */
1562 if (f
->f_buf
!= NULL
&&
1563 (f
->f_bufend
- f
->f_bufptr
) > 0 &&
1564 f
->f_buf
[0] != '\0')
1565 return err_iterbuffered();
1566 if (!PyArg_ParseTuple(args
, "|i:readline", &n
))
1569 return PyString_FromString("");
1572 return get_line(f
, n
);
1576 file_readlines(PyFileObject
*f
, PyObject
*args
)
1579 PyObject
*list
= NULL
;
1581 char small_buffer
[SMALLCHUNK
];
1582 char *buffer
= small_buffer
;
1583 size_t buffersize
= SMALLCHUNK
;
1584 PyObject
*big_buffer
= NULL
;
1587 size_t totalread
= 0;
1592 if (f
->f_fp
== NULL
)
1593 return err_closed();
1594 /* refuse to mix with f.next() */
1595 if (f
->f_buf
!= NULL
&&
1596 (f
->f_bufend
- f
->f_bufptr
) > 0 &&
1597 f
->f_buf
[0] != '\0')
1598 return err_iterbuffered();
1599 if (!PyArg_ParseTuple(args
, "|l:readlines", &sizehint
))
1601 if ((list
= PyList_New(0)) == NULL
)
1607 FILE_BEGIN_ALLOW_THREADS(f
)
1609 nread
= Py_UniversalNewlineFread(buffer
+nfilled
,
1610 buffersize
-nfilled
, f
->f_fp
, (PyObject
*)f
);
1611 FILE_END_ALLOW_THREADS(f
)
1612 shortread
= (nread
< buffersize
-nfilled
);
1616 if (!ferror(f
->f_fp
))
1618 PyErr_SetFromErrno(PyExc_IOError
);
1623 p
= (char *)memchr(buffer
+nfilled
, '\n', nread
);
1625 /* Need a larger buffer to fit this line */
1628 if (buffersize
> PY_SSIZE_T_MAX
) {
1629 PyErr_SetString(PyExc_OverflowError
,
1630 "line is longer than a Python string can hold");
1633 if (big_buffer
== NULL
) {
1634 /* Create the big buffer */
1635 big_buffer
= PyString_FromStringAndSize(
1637 if (big_buffer
== NULL
)
1639 buffer
= PyString_AS_STRING(big_buffer
);
1640 memcpy(buffer
, small_buffer
, nfilled
);
1643 /* Grow the big buffer */
1644 if ( _PyString_Resize(&big_buffer
, buffersize
) < 0 )
1646 buffer
= PyString_AS_STRING(big_buffer
);
1650 end
= buffer
+nfilled
+nread
;
1653 /* Process complete lines */
1655 line
= PyString_FromStringAndSize(q
, p
-q
);
1658 err
= PyList_Append(list
, line
);
1663 p
= (char *)memchr(q
, '\n', end
-q
);
1664 } while (p
!= NULL
);
1665 /* Move the remaining incomplete line to the start */
1667 memmove(buffer
, q
, nfilled
);
1669 if (totalread
>= (size_t)sizehint
)
1673 /* Partial last line */
1674 line
= PyString_FromStringAndSize(buffer
, nfilled
);
1678 /* Need to complete the last line */
1679 PyObject
*rest
= get_line(f
, 0);
1684 PyString_Concat(&line
, rest
);
1689 err
= PyList_Append(list
, line
);
1696 Py_XDECREF(big_buffer
);
1705 file_write(PyFileObject
*f
, PyObject
*args
)
1710 if (f
->f_fp
== NULL
)
1711 return err_closed();
1713 if (!PyArg_ParseTuple(args
, "s*", &pbuf
))
1718 if (!PyArg_ParseTuple(args
, "t#", &s
, &n
))
1721 FILE_BEGIN_ALLOW_THREADS(f
)
1723 n2
= fwrite(s
, 1, n
, f
->f_fp
);
1724 FILE_END_ALLOW_THREADS(f
)
1726 PyBuffer_Release(&pbuf
);
1728 PyErr_SetFromErrno(PyExc_IOError
);
1737 file_writelines(PyFileObject
*f
, PyObject
*seq
)
1739 #define CHUNKSIZE 1000
1740 PyObject
*list
, *line
;
1741 PyObject
*it
; /* iter(seq) */
1744 Py_ssize_t i
, j
, nwritten
, len
;
1746 assert(seq
!= NULL
);
1747 if (f
->f_fp
== NULL
)
1748 return err_closed();
1752 islist
= PyList_Check(seq
);
1756 it
= PyObject_GetIter(seq
);
1758 PyErr_SetString(PyExc_TypeError
,
1759 "writelines() requires an iterable argument");
1762 /* From here on, fail by going to error, to reclaim "it". */
1763 list
= PyList_New(CHUNKSIZE
);
1768 /* Strategy: slurp CHUNKSIZE lines into a private list,
1769 checking that they are all strings, then write that list
1770 without holding the interpreter lock, then come back for more. */
1771 for (index
= 0; ; index
+= CHUNKSIZE
) {
1774 list
= PyList_GetSlice(seq
, index
, index
+CHUNKSIZE
);
1777 j
= PyList_GET_SIZE(list
);
1780 for (j
= 0; j
< CHUNKSIZE
; j
++) {
1781 line
= PyIter_Next(it
);
1783 if (PyErr_Occurred())
1787 PyList_SetItem(list
, j
, line
);
1793 /* Check that all entries are indeed strings. If not,
1794 apply the same rules as for file.write() and
1795 convert the results to strings. This is slow, but
1796 seems to be the only way since all conversion APIs
1797 could potentially execute Python code. */
1798 for (i
= 0; i
< j
; i
++) {
1799 PyObject
*v
= PyList_GET_ITEM(list
, i
);
1800 if (!PyString_Check(v
)) {
1802 if (((f
->f_binary
&&
1803 PyObject_AsReadBuffer(v
,
1804 (const void**)&buffer
,
1806 PyObject_AsCharBuffer(v
,
1809 PyErr_SetString(PyExc_TypeError
,
1810 "writelines() argument must be a sequence of strings");
1813 line
= PyString_FromStringAndSize(buffer
,
1818 PyList_SET_ITEM(list
, i
, line
);
1822 /* Since we are releasing the global lock, the
1823 following code may *not* execute Python code. */
1825 FILE_BEGIN_ALLOW_THREADS(f
)
1827 for (i
= 0; i
< j
; i
++) {
1828 line
= PyList_GET_ITEM(list
, i
);
1829 len
= PyString_GET_SIZE(line
);
1830 nwritten
= fwrite(PyString_AS_STRING(line
),
1832 if (nwritten
!= len
) {
1833 FILE_ABORT_ALLOW_THREADS(f
)
1834 PyErr_SetFromErrno(PyExc_IOError
);
1839 FILE_END_ALLOW_THREADS(f
)
1855 file_self(PyFileObject
*f
)
1857 if (f
->f_fp
== NULL
)
1858 return err_closed();
1860 return (PyObject
*)f
;
1864 file_xreadlines(PyFileObject
*f
)
1866 if (PyErr_WarnPy3k("f.xreadlines() not supported in 3.x, "
1867 "try 'for line in f' instead", 1) < 0)
1869 return file_self(f
);
1873 file_exit(PyObject
*f
, PyObject
*args
)
1875 PyObject
*ret
= PyObject_CallMethod(f
, "close", NULL
);
1877 /* If error occurred, pass through */
1880 /* We cannot return the result of close since a true
1881 * value will be interpreted as "yes, swallow the
1882 * exception if one was raised inside the with block". */
1886 PyDoc_STRVAR(readline_doc
,
1887 "readline([size]) -> next line from the file, as a string.\n"
1889 "Retain newline. A non-negative size argument limits the maximum\n"
1890 "number of bytes to return (an incomplete line may be returned then).\n"
1891 "Return an empty string at EOF.");
1893 PyDoc_STRVAR(read_doc
,
1894 "read([size]) -> read at most size bytes, returned as a string.\n"
1896 "If the size argument is negative or omitted, read until EOF is reached.\n"
1897 "Notice that when in non-blocking mode, less data than what was requested\n"
1898 "may be returned, even if no size parameter was given.");
1900 PyDoc_STRVAR(write_doc
,
1901 "write(str) -> None. Write string str to file.\n"
1903 "Note that due to buffering, flush() or close() may be needed before\n"
1904 "the file on disk reflects the data written.");
1906 PyDoc_STRVAR(fileno_doc
,
1907 "fileno() -> integer \"file descriptor\".\n"
1909 "This is needed for lower-level file interfaces, such os.read().");
1911 PyDoc_STRVAR(seek_doc
,
1912 "seek(offset[, whence]) -> None. Move to new file position.\n"
1914 "Argument offset is a byte count. Optional argument whence defaults to\n"
1915 "0 (offset from start of file, offset should be >= 0); other values are 1\n"
1916 "(move relative to current position, positive or negative), and 2 (move\n"
1917 "relative to end of file, usually negative, although many platforms allow\n"
1918 "seeking beyond the end of a file). If the file is opened in text mode,\n"
1919 "only offsets returned by tell() are legal. Use of other offsets causes\n"
1920 "undefined behavior."
1922 "Note that not all file objects are seekable.");
1924 #ifdef HAVE_FTRUNCATE
1925 PyDoc_STRVAR(truncate_doc
,
1926 "truncate([size]) -> None. Truncate the file to at most size bytes.\n"
1928 "Size defaults to the current file position, as returned by tell().");
1931 PyDoc_STRVAR(tell_doc
,
1932 "tell() -> current file position, an integer (may be a long integer).");
1934 PyDoc_STRVAR(readinto_doc
,
1935 "readinto() -> Undocumented. Don't use this; it may go away.");
1937 PyDoc_STRVAR(readlines_doc
,
1938 "readlines([size]) -> list of strings, each a line from the file.\n"
1940 "Call readline() repeatedly and return a list of the lines so read.\n"
1941 "The optional size argument, if given, is an approximate bound on the\n"
1942 "total number of bytes in the lines returned.");
1944 PyDoc_STRVAR(xreadlines_doc
,
1945 "xreadlines() -> returns self.\n"
1947 "For backward compatibility. File objects now include the performance\n"
1948 "optimizations previously implemented in the xreadlines module.");
1950 PyDoc_STRVAR(writelines_doc
,
1951 "writelines(sequence_of_strings) -> None. Write the strings to the file.\n"
1953 "Note that newlines are not added. The sequence can be any iterable object\n"
1954 "producing strings. This is equivalent to calling write() for each string.");
1956 PyDoc_STRVAR(flush_doc
,
1957 "flush() -> None. Flush the internal I/O buffer.");
1959 PyDoc_STRVAR(close_doc
,
1960 "close() -> None or (perhaps) an integer. Close the file.\n"
1962 "Sets data attribute .closed to True. A closed file cannot be used for\n"
1963 "further I/O operations. close() may be called more than once without\n"
1964 "error. Some kinds of file objects (for example, opened by popen())\n"
1965 "may return an exit status upon closing.");
1967 PyDoc_STRVAR(isatty_doc
,
1968 "isatty() -> true or false. True if the file is connected to a tty device.");
1970 PyDoc_STRVAR(enter_doc
,
1971 "__enter__() -> self.");
1973 PyDoc_STRVAR(exit_doc
,
1974 "__exit__(*excinfo) -> None. Closes the file.");
1976 static PyMethodDef file_methods
[] = {
1977 {"readline", (PyCFunction
)file_readline
, METH_VARARGS
, readline_doc
},
1978 {"read", (PyCFunction
)file_read
, METH_VARARGS
, read_doc
},
1979 {"write", (PyCFunction
)file_write
, METH_VARARGS
, write_doc
},
1980 {"fileno", (PyCFunction
)file_fileno
, METH_NOARGS
, fileno_doc
},
1981 {"seek", (PyCFunction
)file_seek
, METH_VARARGS
, seek_doc
},
1982 #ifdef HAVE_FTRUNCATE
1983 {"truncate", (PyCFunction
)file_truncate
, METH_VARARGS
, truncate_doc
},
1985 {"tell", (PyCFunction
)file_tell
, METH_NOARGS
, tell_doc
},
1986 {"readinto", (PyCFunction
)file_readinto
, METH_VARARGS
, readinto_doc
},
1987 {"readlines", (PyCFunction
)file_readlines
, METH_VARARGS
, readlines_doc
},
1988 {"xreadlines",(PyCFunction
)file_xreadlines
, METH_NOARGS
, xreadlines_doc
},
1989 {"writelines",(PyCFunction
)file_writelines
, METH_O
, writelines_doc
},
1990 {"flush", (PyCFunction
)file_flush
, METH_NOARGS
, flush_doc
},
1991 {"close", (PyCFunction
)file_close
, METH_NOARGS
, close_doc
},
1992 {"isatty", (PyCFunction
)file_isatty
, METH_NOARGS
, isatty_doc
},
1993 {"__enter__", (PyCFunction
)file_self
, METH_NOARGS
, enter_doc
},
1994 {"__exit__", (PyCFunction
)file_exit
, METH_VARARGS
, exit_doc
},
1995 {NULL
, NULL
} /* sentinel */
1998 #define OFF(x) offsetof(PyFileObject, x)
2000 static PyMemberDef file_memberlist
[] = {
2001 {"mode", T_OBJECT
, OFF(f_mode
), RO
,
2002 "file mode ('r', 'U', 'w', 'a', possibly with 'b' or '+' added)"},
2003 {"name", T_OBJECT
, OFF(f_name
), RO
,
2005 {"encoding", T_OBJECT
, OFF(f_encoding
), RO
,
2007 {"errors", T_OBJECT
, OFF(f_errors
), RO
,
2008 "Unicode error handler"},
2009 /* getattr(f, "closed") is implemented without this table */
2010 {NULL
} /* Sentinel */
2014 get_closed(PyFileObject
*f
, void *closure
)
2016 return PyBool_FromLong((long)(f
->f_fp
== 0));
2019 get_newlines(PyFileObject
*f
, void *closure
)
2021 switch (f
->f_newlinetypes
) {
2022 case NEWLINE_UNKNOWN
:
2026 return PyString_FromString("\r");
2028 return PyString_FromString("\n");
2029 case NEWLINE_CR
|NEWLINE_LF
:
2030 return Py_BuildValue("(ss)", "\r", "\n");
2032 return PyString_FromString("\r\n");
2033 case NEWLINE_CR
|NEWLINE_CRLF
:
2034 return Py_BuildValue("(ss)", "\r", "\r\n");
2035 case NEWLINE_LF
|NEWLINE_CRLF
:
2036 return Py_BuildValue("(ss)", "\n", "\r\n");
2037 case NEWLINE_CR
|NEWLINE_LF
|NEWLINE_CRLF
:
2038 return Py_BuildValue("(sss)", "\r", "\n", "\r\n");
2040 PyErr_Format(PyExc_SystemError
,
2041 "Unknown newlines value 0x%x\n",
2048 get_softspace(PyFileObject
*f
, void *closure
)
2050 if (PyErr_WarnPy3k("file.softspace not supported in 3.x", 1) < 0)
2052 return PyInt_FromLong(f
->f_softspace
);
2056 set_softspace(PyFileObject
*f
, PyObject
*value
)
2059 if (PyErr_WarnPy3k("file.softspace not supported in 3.x", 1) < 0)
2062 if (value
== NULL
) {
2063 PyErr_SetString(PyExc_TypeError
,
2064 "can't delete softspace attribute");
2068 new = PyInt_AsLong(value
);
2069 if (new == -1 && PyErr_Occurred())
2071 f
->f_softspace
= new;
2075 static PyGetSetDef file_getsetlist
[] = {
2076 {"closed", (getter
)get_closed
, NULL
, "True if the file is closed"},
2077 {"newlines", (getter
)get_newlines
, NULL
,
2078 "end-of-line convention used in this file"},
2079 {"softspace", (getter
)get_softspace
, (setter
)set_softspace
,
2080 "flag indicating that a space needs to be printed; used by print"},
2085 drop_readahead(PyFileObject
*f
)
2087 if (f
->f_buf
!= NULL
) {
2088 PyMem_Free(f
->f_buf
);
2093 /* Make sure that file has a readahead buffer with at least one byte
2094 (unless at EOF) and no more than bufsize. Returns negative value on
2095 error, will set MemoryError if bufsize bytes cannot be allocated. */
2097 readahead(PyFileObject
*f
, int bufsize
)
2099 Py_ssize_t chunksize
;
2101 if (f
->f_buf
!= NULL
) {
2102 if( (f
->f_bufend
- f
->f_bufptr
) >= 1)
2107 if ((f
->f_buf
= (char *)PyMem_Malloc(bufsize
)) == NULL
) {
2111 FILE_BEGIN_ALLOW_THREADS(f
)
2113 chunksize
= Py_UniversalNewlineFread(
2114 f
->f_buf
, bufsize
, f
->f_fp
, (PyObject
*)f
);
2115 FILE_END_ALLOW_THREADS(f
)
2116 if (chunksize
== 0) {
2117 if (ferror(f
->f_fp
)) {
2118 PyErr_SetFromErrno(PyExc_IOError
);
2124 f
->f_bufptr
= f
->f_buf
;
2125 f
->f_bufend
= f
->f_buf
+ chunksize
;
2129 /* Used by file_iternext. The returned string will start with 'skip'
2130 uninitialized bytes followed by the remainder of the line. Don't be
2131 horrified by the recursive call: maximum recursion depth is limited by
2132 logarithmic buffer growth to about 50 even when reading a 1gb line. */
2134 static PyStringObject
*
2135 readahead_get_line_skip(PyFileObject
*f
, int skip
, int bufsize
)
2142 if (f
->f_buf
== NULL
)
2143 if (readahead(f
, bufsize
) < 0)
2146 len
= f
->f_bufend
- f
->f_bufptr
;
2148 return (PyStringObject
*)
2149 PyString_FromStringAndSize(NULL
, skip
);
2150 bufptr
= (char *)memchr(f
->f_bufptr
, '\n', len
);
2151 if (bufptr
!= NULL
) {
2152 bufptr
++; /* Count the '\n' */
2153 len
= bufptr
- f
->f_bufptr
;
2154 s
= (PyStringObject
*)
2155 PyString_FromStringAndSize(NULL
, skip
+len
);
2158 memcpy(PyString_AS_STRING(s
)+skip
, f
->f_bufptr
, len
);
2159 f
->f_bufptr
= bufptr
;
2160 if (bufptr
== f
->f_bufend
)
2163 bufptr
= f
->f_bufptr
;
2165 f
->f_buf
= NULL
; /* Force new readahead buffer */
2166 assert(skip
+len
< INT_MAX
);
2167 s
= readahead_get_line_skip(
2168 f
, (int)(skip
+len
), bufsize
+ (bufsize
>>2) );
2173 memcpy(PyString_AS_STRING(s
)+skip
, bufptr
, len
);
2179 /* A larger buffer size may actually decrease performance. */
2180 #define READAHEAD_BUFSIZE 8192
2183 file_iternext(PyFileObject
*f
)
2187 if (f
->f_fp
== NULL
)
2188 return err_closed();
2190 l
= readahead_get_line_skip(f
, 0, READAHEAD_BUFSIZE
);
2191 if (l
== NULL
|| PyString_GET_SIZE(l
) == 0) {
2195 return (PyObject
*)l
;
2200 file_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
2203 static PyObject
*not_yet_string
;
2205 assert(type
!= NULL
&& type
->tp_alloc
!= NULL
);
2207 if (not_yet_string
== NULL
) {
2208 not_yet_string
= PyString_InternFromString("<uninitialized file>");
2209 if (not_yet_string
== NULL
)
2213 self
= type
->tp_alloc(type
, 0);
2215 /* Always fill in the name and mode, so that nobody else
2216 needs to special-case NULLs there. */
2217 Py_INCREF(not_yet_string
);
2218 ((PyFileObject
*)self
)->f_name
= not_yet_string
;
2219 Py_INCREF(not_yet_string
);
2220 ((PyFileObject
*)self
)->f_mode
= not_yet_string
;
2222 ((PyFileObject
*)self
)->f_encoding
= Py_None
;
2224 ((PyFileObject
*)self
)->f_errors
= Py_None
;
2225 ((PyFileObject
*)self
)->weakreflist
= NULL
;
2226 ((PyFileObject
*)self
)->unlocked_count
= 0;
2232 file_init(PyObject
*self
, PyObject
*args
, PyObject
*kwds
)
2234 PyFileObject
*foself
= (PyFileObject
*)self
;
2236 static char *kwlist
[] = {"name", "mode", "buffering", 0};
2240 int wideargument
= 0;
2245 assert(PyFile_Check(self
));
2246 if (foself
->f_fp
!= NULL
) {
2247 /* Have to close the existing file first. */
2248 PyObject
*closeresult
= file_close(foself
);
2249 if (closeresult
== NULL
)
2251 Py_DECREF(closeresult
);
2255 if (PyArg_ParseTupleAndKeywords(args
, kwds
, "U|si:file",
2256 kwlist
, &po
, &mode
, &bufsize
)) {
2258 if (fill_file_fields(foself
, NULL
, po
, mode
,
2262 /* Drop the argument parsing error as narrow
2263 strings are also valid. */
2268 if (!wideargument
) {
2271 if (!PyArg_ParseTupleAndKeywords(args
, kwds
, "et|si:file", kwlist
,
2272 Py_FileSystemDefaultEncoding
,
2277 /* We parse again to get the name as a PyObject */
2278 if (!PyArg_ParseTupleAndKeywords(args
, kwds
, "O|si:file",
2279 kwlist
, &o_name
, &mode
,
2283 if (fill_file_fields(foself
, NULL
, o_name
, mode
,
2287 if (open_the_file(foself
, name
, mode
) == NULL
)
2289 foself
->f_setbuf
= NULL
;
2290 PyFile_SetBufSize(self
, bufsize
);
2297 PyMem_Free(name
); /* free the encoded string */
2301 PyDoc_VAR(file_doc
) =
2303 "file(name[, mode[, buffering]]) -> file object\n"
2305 "Open a file. The mode can be 'r', 'w' or 'a' for reading (default),\n"
2306 "writing or appending. The file will be created if it doesn't exist\n"
2307 "when opened for writing or appending; it will be truncated when\n"
2308 "opened for writing. Add a 'b' to the mode for binary files.\n"
2309 "Add a '+' to the mode to allow simultaneous reading and writing.\n"
2310 "If the buffering argument is given, 0 means unbuffered, 1 means line\n"
2311 "buffered, and larger numbers specify the buffer size. The preferred way\n"
2312 "to open a file is with the builtin open() function.\n"
2315 "Add a 'U' to mode to open the file for input with universal newline\n"
2316 "support. Any line ending in the input file will be seen as a '\\n'\n"
2317 "in Python. Also, a file so opened gains the attribute 'newlines';\n"
2318 "the value for this attribute is one of None (no newline read yet),\n"
2319 "'\\r', '\\n', '\\r\\n' or a tuple containing all the newline types seen.\n"
2321 "'U' cannot be combined with 'w' or '+' mode.\n"
2324 PyTypeObject PyFile_Type
= {
2325 PyVarObject_HEAD_INIT(&PyType_Type
, 0)
2327 sizeof(PyFileObject
),
2329 (destructor
)file_dealloc
, /* tp_dealloc */
2334 (reprfunc
)file_repr
, /* tp_repr */
2335 0, /* tp_as_number */
2336 0, /* tp_as_sequence */
2337 0, /* tp_as_mapping */
2341 PyObject_GenericGetAttr
, /* tp_getattro */
2342 /* softspace is writable: we must supply tp_setattro */
2343 PyObject_GenericSetAttr
, /* tp_setattro */
2344 0, /* tp_as_buffer */
2345 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
| Py_TPFLAGS_HAVE_WEAKREFS
, /* tp_flags */
2346 file_doc
, /* tp_doc */
2347 0, /* tp_traverse */
2349 0, /* tp_richcompare */
2350 offsetof(PyFileObject
, weakreflist
), /* tp_weaklistoffset */
2351 (getiterfunc
)file_self
, /* tp_iter */
2352 (iternextfunc
)file_iternext
, /* tp_iternext */
2353 file_methods
, /* tp_methods */
2354 file_memberlist
, /* tp_members */
2355 file_getsetlist
, /* tp_getset */
2358 0, /* tp_descr_get */
2359 0, /* tp_descr_set */
2360 0, /* tp_dictoffset */
2361 file_init
, /* tp_init */
2362 PyType_GenericAlloc
, /* tp_alloc */
2363 file_new
, /* tp_new */
2364 PyObject_Del
, /* tp_free */
2367 /* Interface for the 'soft space' between print items. */
2370 PyFile_SoftSpace(PyObject
*f
, int newflag
)
2376 else if (PyFile_Check(f
)) {
2377 oldflag
= ((PyFileObject
*)f
)->f_softspace
;
2378 ((PyFileObject
*)f
)->f_softspace
= newflag
;
2382 v
= PyObject_GetAttrString(f
, "softspace");
2387 oldflag
= PyInt_AsLong(v
);
2388 assert(oldflag
< INT_MAX
);
2391 v
= PyInt_FromLong((long)newflag
);
2395 if (PyObject_SetAttrString(f
, "softspace", v
) != 0)
2400 return (int)oldflag
;
2403 /* Interfaces to write objects/strings to file-like objects */
2406 PyFile_WriteObject(PyObject
*v
, PyObject
*f
, int flags
)
2408 PyObject
*writer
, *value
, *args
, *result
;
2410 PyErr_SetString(PyExc_TypeError
, "writeobject with NULL file");
2413 else if (PyFile_Check(f
)) {
2414 PyFileObject
*fobj
= (PyFileObject
*) f
;
2415 #ifdef Py_USING_UNICODE
2416 PyObject
*enc
= fobj
->f_encoding
;
2419 if (fobj
->f_fp
== NULL
) {
2423 #ifdef Py_USING_UNICODE
2424 if ((flags
& Py_PRINT_RAW
) &&
2425 PyUnicode_Check(v
) && enc
!= Py_None
) {
2426 char *cenc
= PyString_AS_STRING(enc
);
2427 char *errors
= fobj
->f_errors
== Py_None
?
2428 "strict" : PyString_AS_STRING(fobj
->f_errors
);
2429 value
= PyUnicode_AsEncodedString(v
, cenc
, errors
);
2436 result
= file_PyObject_Print(value
, fobj
, flags
);
2440 return file_PyObject_Print(v
, fobj
, flags
);
2443 writer
= PyObject_GetAttrString(f
, "write");
2446 if (flags
& Py_PRINT_RAW
) {
2447 if (PyUnicode_Check(v
)) {
2451 value
= PyObject_Str(v
);
2454 value
= PyObject_Repr(v
);
2455 if (value
== NULL
) {
2459 args
= PyTuple_Pack(1, value
);
2465 result
= PyEval_CallObject(writer
, args
);
2476 PyFile_WriteString(const char *s
, PyObject
*f
)
2480 /* Should be caused by a pre-existing error */
2481 if (!PyErr_Occurred())
2482 PyErr_SetString(PyExc_SystemError
,
2483 "null file for PyFile_WriteString");
2486 else if (PyFile_Check(f
)) {
2487 PyFileObject
*fobj
= (PyFileObject
*) f
;
2488 FILE *fp
= PyFile_AsFile(f
);
2493 FILE_BEGIN_ALLOW_THREADS(fobj
)
2495 FILE_END_ALLOW_THREADS(fobj
)
2498 else if (!PyErr_Occurred()) {
2499 PyObject
*v
= PyString_FromString(s
);
2503 err
= PyFile_WriteObject(v
, f
, Py_PRINT_RAW
);
2511 /* Try to get a file-descriptor from a Python object. If the object
2512 is an integer or long integer, its value is returned. If not, the
2513 object's fileno() method is called if it exists; the method must return
2514 an integer or long integer, which is returned as the file descriptor value.
2515 -1 is returned on failure.
2518 int PyObject_AsFileDescriptor(PyObject
*o
)
2523 if (PyInt_Check(o
)) {
2524 fd
= PyInt_AsLong(o
);
2526 else if (PyLong_Check(o
)) {
2527 fd
= PyLong_AsLong(o
);
2529 else if ((meth
= PyObject_GetAttrString(o
, "fileno")) != NULL
)
2531 PyObject
*fno
= PyEval_CallObject(meth
, NULL
);
2536 if (PyInt_Check(fno
)) {
2537 fd
= PyInt_AsLong(fno
);
2540 else if (PyLong_Check(fno
)) {
2541 fd
= PyLong_AsLong(fno
);
2545 PyErr_SetString(PyExc_TypeError
,
2546 "fileno() returned a non-integer");
2552 PyErr_SetString(PyExc_TypeError
,
2553 "argument must be an int, or have a fileno() method.");
2558 PyErr_Format(PyExc_ValueError
,
2559 "file descriptor cannot be a negative integer (%i)",
2566 /* From here on we need access to the real fgets and fread */
2571 ** Py_UniversalNewlineFgets is an fgets variation that understands
2572 ** all of \r, \n and \r\n conventions.
2573 ** The stream should be opened in binary mode.
2574 ** If fobj is NULL the routine always does newline conversion, and
2575 ** it may peek one char ahead to gobble the second char in \r\n.
2576 ** If fobj is non-NULL it must be a PyFileObject. In this case there
2577 ** is no readahead but in stead a flag is used to skip a following
2578 ** \n on the next read. Also, if the file is open in binary mode
2579 ** the whole conversion is skipped. Finally, the routine keeps track of
2580 ** the different types of newlines seen.
2581 ** Note that we need no error handling: fgets() treats error and eof
2585 Py_UniversalNewlineFgets(char *buf
, int n
, FILE *stream
, PyObject
*fobj
)
2589 int newlinetypes
= 0;
2591 int univ_newline
= 1;
2594 if (!PyFile_Check(fobj
)) {
2595 errno
= ENXIO
; /* What can you do... */
2598 univ_newline
= ((PyFileObject
*)fobj
)->f_univ_newline
;
2599 if ( !univ_newline
)
2600 return fgets(buf
, n
, stream
);
2601 newlinetypes
= ((PyFileObject
*)fobj
)->f_newlinetypes
;
2602 skipnextlf
= ((PyFileObject
*)fobj
)->f_skipnextlf
;
2605 c
= 'x'; /* Shut up gcc warning */
2606 while (--n
> 0 && (c
= GETC(stream
)) != EOF
) {
2610 /* Seeing a \n here with skipnextlf true
2611 ** means we saw a \r before.
2613 newlinetypes
|= NEWLINE_CRLF
;
2615 if (c
== EOF
) break;
2618 ** Note that c == EOF also brings us here,
2619 ** so we're okay if the last char in the file
2622 newlinetypes
|= NEWLINE_CR
;
2626 /* A \r is translated into a \n, and we skip
2627 ** an adjacent \n, if any. We don't set the
2628 ** newlinetypes flag until we've seen the next char.
2632 } else if ( c
== '\n') {
2633 newlinetypes
|= NEWLINE_LF
;
2636 if (c
== '\n') break;
2638 if ( c
== EOF
&& skipnextlf
)
2639 newlinetypes
|= NEWLINE_CR
;
2640 FUNLOCKFILE(stream
);
2643 ((PyFileObject
*)fobj
)->f_newlinetypes
= newlinetypes
;
2644 ((PyFileObject
*)fobj
)->f_skipnextlf
= skipnextlf
;
2645 } else if ( skipnextlf
) {
2646 /* If we have no file object we cannot save the
2647 ** skipnextlf flag. We have to readahead, which
2648 ** will cause a pause if we're reading from an
2649 ** interactive stream, but that is very unlikely
2650 ** unless we're doing something silly like
2651 ** execfile("/dev/tty").
2663 ** Py_UniversalNewlineFread is an fread variation that understands
2664 ** all of \r, \n and \r\n conventions.
2665 ** The stream should be opened in binary mode.
2666 ** fobj must be a PyFileObject. In this case there
2667 ** is no readahead but in stead a flag is used to skip a following
2668 ** \n on the next read. Also, if the file is open in binary mode
2669 ** the whole conversion is skipped. Finally, the routine keeps track of
2670 ** the different types of newlines seen.
2673 Py_UniversalNewlineFread(char *buf
, size_t n
,
2674 FILE *stream
, PyObject
*fobj
)
2677 PyFileObject
*f
= (PyFileObject
*)fobj
;
2678 int newlinetypes
, skipnextlf
;
2680 assert(buf
!= NULL
);
2681 assert(stream
!= NULL
);
2683 if (!fobj
|| !PyFile_Check(fobj
)) {
2684 errno
= ENXIO
; /* What can you do... */
2687 if (!f
->f_univ_newline
)
2688 return fread(buf
, 1, n
, stream
);
2689 newlinetypes
= f
->f_newlinetypes
;
2690 skipnextlf
= f
->f_skipnextlf
;
2691 /* Invariant: n is the number of bytes remaining to be filled
2699 nread
= fread(dst
, 1, n
, stream
);
2704 n
-= nread
; /* assuming 1 byte out for each in; will adjust */
2705 shortread
= n
!= 0; /* true iff EOF or error */
2709 /* Save as LF and set flag to skip next LF. */
2713 else if (skipnextlf
&& c
== '\n') {
2714 /* Skip LF, and remember we saw CR LF. */
2716 newlinetypes
|= NEWLINE_CRLF
;
2720 /* Normal char to be stored in buffer. Also
2721 * update the newlinetypes flag if either this
2722 * is an LF or the previous char was a CR.
2725 newlinetypes
|= NEWLINE_LF
;
2726 else if (skipnextlf
)
2727 newlinetypes
|= NEWLINE_CR
;
2733 /* If this is EOF, update type flags. */
2734 if (skipnextlf
&& feof(stream
))
2735 newlinetypes
|= NEWLINE_CR
;
2739 f
->f_newlinetypes
= newlinetypes
;
2740 f
->f_skipnextlf
= skipnextlf
;