Added LoggerAdapter class, changed copyright dates, made check for extra parameter...
[python.git] / Objects / fileobject.c
bloba5a42fcccc378598df0a5dcf22f2b3fbe119b84a
1 /* File object implementation */
3 #define PY_SSIZE_T_CLEAN
4 #include "Python.h"
5 #include "structmember.h"
7 #ifdef HAVE_SYS_TYPES_H
8 #include <sys/types.h>
9 #endif /* HAVE_SYS_TYPES_H */
11 #ifdef MS_WINDOWS
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
16 #include <windows.h>
17 #endif
19 #ifdef _MSC_VER
20 /* Need GetVersion to see if on NT so safe to use _wfopen */
21 #define WIN32_LEAN_AND_MEAN
22 #include <windows.h>
23 #endif /* _MSC_VER */
25 #if defined(PYOS_OS2) && defined(PYCC_GCC)
26 #include <io.h>
27 #endif
29 #define BUF(v) PyString_AS_STRING((PyStringObject *)v)
31 #ifndef DONT_HAVE_ERRNO_H
32 #include <errno.h>
33 #endif
35 #ifdef HAVE_GETC_UNLOCKED
36 #define GETC(f) getc_unlocked(f)
37 #define FLOCKFILE(f) flockfile(f)
38 #define FUNLOCKFILE(f) funlockfile(f)
39 #else
40 #define GETC(f) getc(f)
41 #define FLOCKFILE(f)
42 #define FUNLOCKFILE(f)
43 #endif
45 /* Bits in f_newlinetypes */
46 #define NEWLINE_UNKNOWN 0 /* No newline seen, yet */
47 #define NEWLINE_CR 1 /* \r newline seen */
48 #define NEWLINE_LF 2 /* \n newline seen */
49 #define NEWLINE_CRLF 4 /* \r\n newline seen */
51 #ifdef __cplusplus
52 extern "C" {
53 #endif
55 FILE *
56 PyFile_AsFile(PyObject *f)
58 if (f == NULL || !PyFile_Check(f))
59 return NULL;
60 else
61 return ((PyFileObject *)f)->f_fp;
64 PyObject *
65 PyFile_Name(PyObject *f)
67 if (f == NULL || !PyFile_Check(f))
68 return NULL;
69 else
70 return ((PyFileObject *)f)->f_name;
73 /* On Unix, fopen will succeed for directories.
74 In Python, there should be no file objects referring to
75 directories, so we need a check. */
77 static PyFileObject*
78 dircheck(PyFileObject* f)
80 #if defined(HAVE_FSTAT) && defined(S_IFDIR) && defined(EISDIR)
81 struct stat buf;
82 if (f->f_fp == NULL)
83 return f;
84 if (fstat(fileno(f->f_fp), &buf) == 0 &&
85 S_ISDIR(buf.st_mode)) {
86 #ifdef HAVE_STRERROR
87 char *msg = strerror(EISDIR);
88 #else
89 char *msg = "Is a directory";
90 #endif
91 PyObject *exc = PyObject_CallFunction(PyExc_IOError, "(is)",
92 EISDIR, msg);
93 PyErr_SetObject(PyExc_IOError, exc);
94 Py_XDECREF(exc);
95 return NULL;
97 #endif
98 return f;
102 static PyObject *
103 fill_file_fields(PyFileObject *f, FILE *fp, PyObject *name, char *mode,
104 int (*close)(FILE *))
106 assert(name != NULL);
107 assert(f != NULL);
108 assert(PyFile_Check(f));
109 assert(f->f_fp == NULL);
111 Py_DECREF(f->f_name);
112 Py_DECREF(f->f_mode);
113 Py_DECREF(f->f_encoding);
115 Py_INCREF(name);
116 f->f_name = name;
118 f->f_mode = PyString_FromString(mode);
120 f->f_close = close;
121 f->f_softspace = 0;
122 f->f_binary = strchr(mode,'b') != NULL;
123 f->f_buf = NULL;
124 f->f_univ_newline = (strchr(mode, 'U') != NULL);
125 f->f_newlinetypes = NEWLINE_UNKNOWN;
126 f->f_skipnextlf = 0;
127 Py_INCREF(Py_None);
128 f->f_encoding = Py_None;
130 if (f->f_mode == NULL)
131 return NULL;
132 f->f_fp = fp;
133 f = dircheck(f);
134 return (PyObject *) f;
137 /* check for known incorrect mode strings - problem is, platforms are
138 free to accept any mode characters they like and are supposed to
139 ignore stuff they don't understand... write or append mode with
140 universal newline support is expressly forbidden by PEP 278.
141 Additionally, remove the 'U' from the mode string as platforms
142 won't know what it is. Non-zero return signals an exception */
144 _PyFile_SanitizeMode(char *mode)
146 char *upos;
147 size_t len = strlen(mode);
149 if (!len) {
150 PyErr_SetString(PyExc_ValueError, "empty mode string");
151 return -1;
154 upos = strchr(mode, 'U');
155 if (upos) {
156 memmove(upos, upos+1, len-(upos-mode)); /* incl null char */
158 if (mode[0] == 'w' || mode[0] == 'a') {
159 PyErr_Format(PyExc_ValueError, "universal newline "
160 "mode can only be used with modes "
161 "starting with 'r'");
162 return -1;
165 if (mode[0] != 'r') {
166 memmove(mode+1, mode, strlen(mode)+1);
167 mode[0] = 'r';
170 if (!strchr(mode, 'b')) {
171 memmove(mode+2, mode+1, strlen(mode));
172 mode[1] = 'b';
174 } else if (mode[0] != 'r' && mode[0] != 'w' && mode[0] != 'a') {
175 PyErr_Format(PyExc_ValueError, "mode string must begin with "
176 "one of 'r', 'w', 'a' or 'U', not '%.200s'", mode);
177 return -1;
180 return 0;
183 static PyObject *
184 open_the_file(PyFileObject *f, char *name, char *mode)
186 char *newmode;
187 assert(f != NULL);
188 assert(PyFile_Check(f));
189 #ifdef MS_WINDOWS
190 /* windows ignores the passed name in order to support Unicode */
191 assert(f->f_name != NULL);
192 #else
193 assert(name != NULL);
194 #endif
195 assert(mode != NULL);
196 assert(f->f_fp == NULL);
198 /* probably need to replace 'U' by 'rb' */
199 newmode = PyMem_MALLOC(strlen(mode) + 3);
200 if (!newmode) {
201 PyErr_NoMemory();
202 return NULL;
204 strcpy(newmode, mode);
206 if (_PyFile_SanitizeMode(newmode)) {
207 f = NULL;
208 goto cleanup;
211 /* rexec.py can't stop a user from getting the file() constructor --
212 all they have to do is get *any* file object f, and then do
213 type(f). Here we prevent them from doing damage with it. */
214 if (PyEval_GetRestricted()) {
215 PyErr_SetString(PyExc_IOError,
216 "file() constructor not accessible in restricted mode");
217 f = NULL;
218 goto cleanup;
220 errno = 0;
222 #ifdef MS_WINDOWS
223 if (PyUnicode_Check(f->f_name)) {
224 PyObject *wmode;
225 wmode = PyUnicode_DecodeASCII(newmode, strlen(newmode), NULL);
226 if (f->f_name && wmode) {
227 Py_BEGIN_ALLOW_THREADS
228 /* PyUnicode_AS_UNICODE OK without thread
229 lock as it is a simple dereference. */
230 f->f_fp = _wfopen(PyUnicode_AS_UNICODE(f->f_name),
231 PyUnicode_AS_UNICODE(wmode));
232 Py_END_ALLOW_THREADS
234 Py_XDECREF(wmode);
236 #endif
237 if (NULL == f->f_fp && NULL != name) {
238 Py_BEGIN_ALLOW_THREADS
239 f->f_fp = fopen(name, newmode);
240 Py_END_ALLOW_THREADS
243 if (f->f_fp == NULL) {
244 #if defined _MSC_VER && (_MSC_VER < 1400 || !defined(__STDC_SECURE_LIB__))
245 /* MSVC 6 (Microsoft) leaves errno at 0 for bad mode strings,
246 * across all Windows flavors. When it sets EINVAL varies
247 * across Windows flavors, the exact conditions aren't
248 * documented, and the answer lies in the OS's implementation
249 * of Win32's CreateFile function (whose source is secret).
250 * Seems the best we can do is map EINVAL to ENOENT.
251 * Starting with Visual Studio .NET 2005, EINVAL is correctly
252 * set by our CRT error handler (set in exceptions.c.)
254 if (errno == 0) /* bad mode string */
255 errno = EINVAL;
256 else if (errno == EINVAL) /* unknown, but not a mode string */
257 errno = ENOENT;
258 #endif
259 if (errno == EINVAL)
260 PyErr_Format(PyExc_IOError, "invalid mode: %s",
261 mode);
262 else
263 PyErr_SetFromErrnoWithFilenameObject(PyExc_IOError, f->f_name);
264 f = NULL;
266 if (f != NULL)
267 f = dircheck(f);
269 cleanup:
270 PyMem_FREE(newmode);
272 return (PyObject *)f;
275 PyObject *
276 PyFile_FromFile(FILE *fp, char *name, char *mode, int (*close)(FILE *))
278 PyFileObject *f = (PyFileObject *)PyFile_Type.tp_new(&PyFile_Type,
279 NULL, NULL);
280 if (f != NULL) {
281 PyObject *o_name = PyString_FromString(name);
282 if (o_name == NULL)
283 return NULL;
284 if (fill_file_fields(f, fp, o_name, mode, close) == NULL) {
285 Py_DECREF(f);
286 f = NULL;
288 Py_DECREF(o_name);
290 return (PyObject *) f;
293 PyObject *
294 PyFile_FromString(char *name, char *mode)
296 extern int fclose(FILE *);
297 PyFileObject *f;
299 f = (PyFileObject *)PyFile_FromFile((FILE *)NULL, name, mode, fclose);
300 if (f != NULL) {
301 if (open_the_file(f, name, mode) == NULL) {
302 Py_DECREF(f);
303 f = NULL;
306 return (PyObject *)f;
309 void
310 PyFile_SetBufSize(PyObject *f, int bufsize)
312 PyFileObject *file = (PyFileObject *)f;
313 if (bufsize >= 0) {
314 int type;
315 switch (bufsize) {
316 case 0:
317 type = _IONBF;
318 break;
319 #ifdef HAVE_SETVBUF
320 case 1:
321 type = _IOLBF;
322 bufsize = BUFSIZ;
323 break;
324 #endif
325 default:
326 type = _IOFBF;
327 #ifndef HAVE_SETVBUF
328 bufsize = BUFSIZ;
329 #endif
330 break;
332 fflush(file->f_fp);
333 if (type == _IONBF) {
334 PyMem_Free(file->f_setbuf);
335 file->f_setbuf = NULL;
336 } else {
337 file->f_setbuf = (char *)PyMem_Realloc(file->f_setbuf,
338 bufsize);
340 #ifdef HAVE_SETVBUF
341 setvbuf(file->f_fp, file->f_setbuf, type, bufsize);
342 #else /* !HAVE_SETVBUF */
343 setbuf(file->f_fp, file->f_setbuf);
344 #endif /* !HAVE_SETVBUF */
348 /* Set the encoding used to output Unicode strings.
349 Returh 1 on success, 0 on failure. */
352 PyFile_SetEncoding(PyObject *f, const char *enc)
354 PyFileObject *file = (PyFileObject*)f;
355 PyObject *str = PyString_FromString(enc);
357 assert(PyFile_Check(f));
358 if (!str)
359 return 0;
360 Py_DECREF(file->f_encoding);
361 file->f_encoding = str;
362 return 1;
365 static PyObject *
366 err_closed(void)
368 PyErr_SetString(PyExc_ValueError, "I/O operation on closed file");
369 return NULL;
372 /* Refuse regular file I/O if there's data in the iteration-buffer.
373 * Mixing them would cause data to arrive out of order, as the read*
374 * methods don't use the iteration buffer. */
375 static PyObject *
376 err_iterbuffered(void)
378 PyErr_SetString(PyExc_ValueError,
379 "Mixing iteration and read methods would lose data");
380 return NULL;
383 static void drop_readahead(PyFileObject *);
385 /* Methods */
387 static void
388 file_dealloc(PyFileObject *f)
390 int sts = 0;
391 if (f->weakreflist != NULL)
392 PyObject_ClearWeakRefs((PyObject *) f);
393 if (f->f_fp != NULL && f->f_close != NULL) {
394 Py_BEGIN_ALLOW_THREADS
395 sts = (*f->f_close)(f->f_fp);
396 Py_END_ALLOW_THREADS
397 if (sts == EOF)
398 #ifdef HAVE_STRERROR
399 PySys_WriteStderr("close failed: [Errno %d] %s\n", errno, strerror(errno));
400 #else
401 PySys_WriteStderr("close failed: [Errno %d]\n", errno);
402 #endif
404 PyMem_Free(f->f_setbuf);
405 Py_XDECREF(f->f_name);
406 Py_XDECREF(f->f_mode);
407 Py_XDECREF(f->f_encoding);
408 drop_readahead(f);
409 Py_TYPE(f)->tp_free((PyObject *)f);
412 static PyObject *
413 file_repr(PyFileObject *f)
415 if (PyUnicode_Check(f->f_name)) {
416 #ifdef Py_USING_UNICODE
417 PyObject *ret = NULL;
418 PyObject *name = PyUnicode_AsUnicodeEscapeString(f->f_name);
419 const char *name_str = name ? PyString_AsString(name) : "?";
420 ret = PyString_FromFormat("<%s file u'%s', mode '%s' at %p>",
421 f->f_fp == NULL ? "closed" : "open",
422 name_str,
423 PyString_AsString(f->f_mode),
425 Py_XDECREF(name);
426 return ret;
427 #endif
428 } else {
429 return PyString_FromFormat("<%s file '%s', mode '%s' at %p>",
430 f->f_fp == NULL ? "closed" : "open",
431 PyString_AsString(f->f_name),
432 PyString_AsString(f->f_mode),
437 static PyObject *
438 file_close(PyFileObject *f)
440 int sts = 0;
441 if (f->f_fp != NULL) {
442 if (f->f_close != NULL) {
443 Py_BEGIN_ALLOW_THREADS
444 errno = 0;
445 sts = (*f->f_close)(f->f_fp);
446 Py_END_ALLOW_THREADS
448 f->f_fp = NULL;
450 PyMem_Free(f->f_setbuf);
451 f->f_setbuf = NULL;
452 if (sts == EOF)
453 return PyErr_SetFromErrno(PyExc_IOError);
454 if (sts != 0)
455 return PyInt_FromLong((long)sts);
456 Py_INCREF(Py_None);
457 return Py_None;
461 /* Our very own off_t-like type, 64-bit if possible */
462 #if !defined(HAVE_LARGEFILE_SUPPORT)
463 typedef off_t Py_off_t;
464 #elif SIZEOF_OFF_T >= 8
465 typedef off_t Py_off_t;
466 #elif SIZEOF_FPOS_T >= 8
467 typedef fpos_t Py_off_t;
468 #else
469 #error "Large file support, but neither off_t nor fpos_t is large enough."
470 #endif
473 /* a portable fseek() function
474 return 0 on success, non-zero on failure (with errno set) */
475 static int
476 _portable_fseek(FILE *fp, Py_off_t offset, int whence)
478 #if !defined(HAVE_LARGEFILE_SUPPORT)
479 return fseek(fp, offset, whence);
480 #elif defined(HAVE_FSEEKO) && SIZEOF_OFF_T >= 8
481 return fseeko(fp, offset, whence);
482 #elif defined(HAVE_FSEEK64)
483 return fseek64(fp, offset, whence);
484 #elif defined(__BEOS__)
485 return _fseek(fp, offset, whence);
486 #elif SIZEOF_FPOS_T >= 8
487 /* lacking a 64-bit capable fseek(), use a 64-bit capable fsetpos()
488 and fgetpos() to implement fseek()*/
489 fpos_t pos;
490 switch (whence) {
491 case SEEK_END:
492 #ifdef MS_WINDOWS
493 fflush(fp);
494 if (_lseeki64(fileno(fp), 0, 2) == -1)
495 return -1;
496 #else
497 if (fseek(fp, 0, SEEK_END) != 0)
498 return -1;
499 #endif
500 /* fall through */
501 case SEEK_CUR:
502 if (fgetpos(fp, &pos) != 0)
503 return -1;
504 offset += pos;
505 break;
506 /* case SEEK_SET: break; */
508 return fsetpos(fp, &offset);
509 #else
510 #error "Large file support, but no way to fseek."
511 #endif
515 /* a portable ftell() function
516 Return -1 on failure with errno set appropriately, current file
517 position on success */
518 static Py_off_t
519 _portable_ftell(FILE* fp)
521 #if !defined(HAVE_LARGEFILE_SUPPORT)
522 return ftell(fp);
523 #elif defined(HAVE_FTELLO) && SIZEOF_OFF_T >= 8
524 return ftello(fp);
525 #elif defined(HAVE_FTELL64)
526 return ftell64(fp);
527 #elif SIZEOF_FPOS_T >= 8
528 fpos_t pos;
529 if (fgetpos(fp, &pos) != 0)
530 return -1;
531 return pos;
532 #else
533 #error "Large file support, but no way to ftell."
534 #endif
538 static PyObject *
539 file_seek(PyFileObject *f, PyObject *args)
541 int whence;
542 int ret;
543 Py_off_t offset;
544 PyObject *offobj, *off_index;
546 if (f->f_fp == NULL)
547 return err_closed();
548 drop_readahead(f);
549 whence = 0;
550 if (!PyArg_ParseTuple(args, "O|i:seek", &offobj, &whence))
551 return NULL;
552 off_index = PyNumber_Index(offobj);
553 if (!off_index) {
554 if (!PyFloat_Check(offobj))
555 return NULL;
556 /* Deprecated in 2.6 */
557 PyErr_Clear();
558 if (PyErr_Warn(PyExc_DeprecationWarning,
559 "integer argument expected, got float"))
560 return NULL;
561 off_index = offobj;
562 Py_INCREF(offobj);
564 #if !defined(HAVE_LARGEFILE_SUPPORT)
565 offset = PyInt_AsLong(off_index);
566 #else
567 offset = PyLong_Check(off_index) ?
568 PyLong_AsLongLong(off_index) : PyInt_AsLong(off_index);
569 #endif
570 Py_DECREF(off_index);
571 if (PyErr_Occurred())
572 return NULL;
574 Py_BEGIN_ALLOW_THREADS
575 errno = 0;
576 ret = _portable_fseek(f->f_fp, offset, whence);
577 Py_END_ALLOW_THREADS
579 if (ret != 0) {
580 PyErr_SetFromErrno(PyExc_IOError);
581 clearerr(f->f_fp);
582 return NULL;
584 f->f_skipnextlf = 0;
585 Py_INCREF(Py_None);
586 return Py_None;
590 #ifdef HAVE_FTRUNCATE
591 static PyObject *
592 file_truncate(PyFileObject *f, PyObject *args)
594 Py_off_t newsize;
595 PyObject *newsizeobj = NULL;
596 Py_off_t initialpos;
597 int ret;
599 if (f->f_fp == NULL)
600 return err_closed();
601 if (!PyArg_UnpackTuple(args, "truncate", 0, 1, &newsizeobj))
602 return NULL;
604 /* Get current file position. If the file happens to be open for
605 * update and the last operation was an input operation, C doesn't
606 * define what the later fflush() will do, but we promise truncate()
607 * won't change the current position (and fflush() *does* change it
608 * then at least on Windows). The easiest thing is to capture
609 * current pos now and seek back to it at the end.
611 Py_BEGIN_ALLOW_THREADS
612 errno = 0;
613 initialpos = _portable_ftell(f->f_fp);
614 Py_END_ALLOW_THREADS
615 if (initialpos == -1)
616 goto onioerror;
618 /* Set newsize to current postion if newsizeobj NULL, else to the
619 * specified value.
621 if (newsizeobj != NULL) {
622 #if !defined(HAVE_LARGEFILE_SUPPORT)
623 newsize = PyInt_AsLong(newsizeobj);
624 #else
625 newsize = PyLong_Check(newsizeobj) ?
626 PyLong_AsLongLong(newsizeobj) :
627 PyInt_AsLong(newsizeobj);
628 #endif
629 if (PyErr_Occurred())
630 return NULL;
632 else /* default to current position */
633 newsize = initialpos;
635 /* Flush the stream. We're mixing stream-level I/O with lower-level
636 * I/O, and a flush may be necessary to synch both platform views
637 * of the current file state.
639 Py_BEGIN_ALLOW_THREADS
640 errno = 0;
641 ret = fflush(f->f_fp);
642 Py_END_ALLOW_THREADS
643 if (ret != 0)
644 goto onioerror;
646 #ifdef MS_WINDOWS
647 /* MS _chsize doesn't work if newsize doesn't fit in 32 bits,
648 so don't even try using it. */
650 HANDLE hFile;
652 /* Have to move current pos to desired endpoint on Windows. */
653 Py_BEGIN_ALLOW_THREADS
654 errno = 0;
655 ret = _portable_fseek(f->f_fp, newsize, SEEK_SET) != 0;
656 Py_END_ALLOW_THREADS
657 if (ret)
658 goto onioerror;
660 /* Truncate. Note that this may grow the file! */
661 Py_BEGIN_ALLOW_THREADS
662 errno = 0;
663 hFile = (HANDLE)_get_osfhandle(fileno(f->f_fp));
664 ret = hFile == (HANDLE)-1;
665 if (ret == 0) {
666 ret = SetEndOfFile(hFile) == 0;
667 if (ret)
668 errno = EACCES;
670 Py_END_ALLOW_THREADS
671 if (ret)
672 goto onioerror;
674 #else
675 Py_BEGIN_ALLOW_THREADS
676 errno = 0;
677 ret = ftruncate(fileno(f->f_fp), newsize);
678 Py_END_ALLOW_THREADS
679 if (ret != 0)
680 goto onioerror;
681 #endif /* !MS_WINDOWS */
683 /* Restore original file position. */
684 Py_BEGIN_ALLOW_THREADS
685 errno = 0;
686 ret = _portable_fseek(f->f_fp, initialpos, SEEK_SET) != 0;
687 Py_END_ALLOW_THREADS
688 if (ret)
689 goto onioerror;
691 Py_INCREF(Py_None);
692 return Py_None;
694 onioerror:
695 PyErr_SetFromErrno(PyExc_IOError);
696 clearerr(f->f_fp);
697 return NULL;
699 #endif /* HAVE_FTRUNCATE */
701 static PyObject *
702 file_tell(PyFileObject *f)
704 Py_off_t pos;
706 if (f->f_fp == NULL)
707 return err_closed();
708 Py_BEGIN_ALLOW_THREADS
709 errno = 0;
710 pos = _portable_ftell(f->f_fp);
711 Py_END_ALLOW_THREADS
712 if (pos == -1) {
713 PyErr_SetFromErrno(PyExc_IOError);
714 clearerr(f->f_fp);
715 return NULL;
717 if (f->f_skipnextlf) {
718 int c;
719 c = GETC(f->f_fp);
720 if (c == '\n') {
721 f->f_newlinetypes |= NEWLINE_CRLF;
722 pos++;
723 f->f_skipnextlf = 0;
724 } else if (c != EOF) ungetc(c, f->f_fp);
726 #if !defined(HAVE_LARGEFILE_SUPPORT)
727 return PyInt_FromLong(pos);
728 #else
729 return PyLong_FromLongLong(pos);
730 #endif
733 static PyObject *
734 file_fileno(PyFileObject *f)
736 if (f->f_fp == NULL)
737 return err_closed();
738 return PyInt_FromLong((long) fileno(f->f_fp));
741 static PyObject *
742 file_flush(PyFileObject *f)
744 int res;
746 if (f->f_fp == NULL)
747 return err_closed();
748 Py_BEGIN_ALLOW_THREADS
749 errno = 0;
750 res = fflush(f->f_fp);
751 Py_END_ALLOW_THREADS
752 if (res != 0) {
753 PyErr_SetFromErrno(PyExc_IOError);
754 clearerr(f->f_fp);
755 return NULL;
757 Py_INCREF(Py_None);
758 return Py_None;
761 static PyObject *
762 file_isatty(PyFileObject *f)
764 long res;
765 if (f->f_fp == NULL)
766 return err_closed();
767 Py_BEGIN_ALLOW_THREADS
768 res = isatty((int)fileno(f->f_fp));
769 Py_END_ALLOW_THREADS
770 return PyBool_FromLong(res);
774 #if BUFSIZ < 8192
775 #define SMALLCHUNK 8192
776 #else
777 #define SMALLCHUNK BUFSIZ
778 #endif
780 #if SIZEOF_INT < 4
781 #define BIGCHUNK (512 * 32)
782 #else
783 #define BIGCHUNK (512 * 1024)
784 #endif
786 static size_t
787 new_buffersize(PyFileObject *f, size_t currentsize)
789 #ifdef HAVE_FSTAT
790 off_t pos, end;
791 struct stat st;
792 if (fstat(fileno(f->f_fp), &st) == 0) {
793 end = st.st_size;
794 /* The following is not a bug: we really need to call lseek()
795 *and* ftell(). The reason is that some stdio libraries
796 mistakenly flush their buffer when ftell() is called and
797 the lseek() call it makes fails, thereby throwing away
798 data that cannot be recovered in any way. To avoid this,
799 we first test lseek(), and only call ftell() if lseek()
800 works. We can't use the lseek() value either, because we
801 need to take the amount of buffered data into account.
802 (Yet another reason why stdio stinks. :-) */
803 pos = lseek(fileno(f->f_fp), 0L, SEEK_CUR);
804 if (pos >= 0) {
805 pos = ftell(f->f_fp);
807 if (pos < 0)
808 clearerr(f->f_fp);
809 if (end > pos && pos >= 0)
810 return currentsize + end - pos + 1;
811 /* Add 1 so if the file were to grow we'd notice. */
813 #endif
814 if (currentsize > SMALLCHUNK) {
815 /* Keep doubling until we reach BIGCHUNK;
816 then keep adding BIGCHUNK. */
817 if (currentsize <= BIGCHUNK)
818 return currentsize + currentsize;
819 else
820 return currentsize + BIGCHUNK;
822 return currentsize + SMALLCHUNK;
825 #if defined(EWOULDBLOCK) && defined(EAGAIN) && EWOULDBLOCK != EAGAIN
826 #define BLOCKED_ERRNO(x) ((x) == EWOULDBLOCK || (x) == EAGAIN)
827 #else
828 #ifdef EWOULDBLOCK
829 #define BLOCKED_ERRNO(x) ((x) == EWOULDBLOCK)
830 #else
831 #ifdef EAGAIN
832 #define BLOCKED_ERRNO(x) ((x) == EAGAIN)
833 #else
834 #define BLOCKED_ERRNO(x) 0
835 #endif
836 #endif
837 #endif
839 static PyObject *
840 file_read(PyFileObject *f, PyObject *args)
842 long bytesrequested = -1;
843 size_t bytesread, buffersize, chunksize;
844 PyObject *v;
846 if (f->f_fp == NULL)
847 return err_closed();
848 /* refuse to mix with f.next() */
849 if (f->f_buf != NULL &&
850 (f->f_bufend - f->f_bufptr) > 0 &&
851 f->f_buf[0] != '\0')
852 return err_iterbuffered();
853 if (!PyArg_ParseTuple(args, "|l:read", &bytesrequested))
854 return NULL;
855 if (bytesrequested < 0)
856 buffersize = new_buffersize(f, (size_t)0);
857 else
858 buffersize = bytesrequested;
859 if (buffersize > PY_SSIZE_T_MAX) {
860 PyErr_SetString(PyExc_OverflowError,
861 "requested number of bytes is more than a Python string can hold");
862 return NULL;
864 v = PyString_FromStringAndSize((char *)NULL, buffersize);
865 if (v == NULL)
866 return NULL;
867 bytesread = 0;
868 for (;;) {
869 Py_BEGIN_ALLOW_THREADS
870 errno = 0;
871 chunksize = Py_UniversalNewlineFread(BUF(v) + bytesread,
872 buffersize - bytesread, f->f_fp, (PyObject *)f);
873 Py_END_ALLOW_THREADS
874 if (chunksize == 0) {
875 if (!ferror(f->f_fp))
876 break;
877 clearerr(f->f_fp);
878 /* When in non-blocking mode, data shouldn't
879 * be discarded if a blocking signal was
880 * received. That will also happen if
881 * chunksize != 0, but bytesread < buffersize. */
882 if (bytesread > 0 && BLOCKED_ERRNO(errno))
883 break;
884 PyErr_SetFromErrno(PyExc_IOError);
885 Py_DECREF(v);
886 return NULL;
888 bytesread += chunksize;
889 if (bytesread < buffersize) {
890 clearerr(f->f_fp);
891 break;
893 if (bytesrequested < 0) {
894 buffersize = new_buffersize(f, buffersize);
895 if (_PyString_Resize(&v, buffersize) < 0)
896 return NULL;
897 } else {
898 /* Got what was requested. */
899 break;
902 if (bytesread != buffersize)
903 _PyString_Resize(&v, bytesread);
904 return v;
907 static PyObject *
908 file_readinto(PyFileObject *f, PyObject *args)
910 char *ptr;
911 Py_ssize_t ntodo;
912 Py_ssize_t ndone, nnow;
914 if (f->f_fp == NULL)
915 return err_closed();
916 /* refuse to mix with f.next() */
917 if (f->f_buf != NULL &&
918 (f->f_bufend - f->f_bufptr) > 0 &&
919 f->f_buf[0] != '\0')
920 return err_iterbuffered();
921 if (!PyArg_ParseTuple(args, "w#", &ptr, &ntodo))
922 return NULL;
923 ndone = 0;
924 while (ntodo > 0) {
925 Py_BEGIN_ALLOW_THREADS
926 errno = 0;
927 nnow = Py_UniversalNewlineFread(ptr+ndone, ntodo, f->f_fp,
928 (PyObject *)f);
929 Py_END_ALLOW_THREADS
930 if (nnow == 0) {
931 if (!ferror(f->f_fp))
932 break;
933 PyErr_SetFromErrno(PyExc_IOError);
934 clearerr(f->f_fp);
935 return NULL;
937 ndone += nnow;
938 ntodo -= nnow;
940 return PyInt_FromSsize_t(ndone);
943 /**************************************************************************
944 Routine to get next line using platform fgets().
946 Under MSVC 6:
948 + MS threadsafe getc is very slow (multiple layers of function calls before+
949 after each character, to lock+unlock the stream).
950 + The stream-locking functions are MS-internal -- can't access them from user
951 code.
952 + There's nothing Tim could find in the MS C or platform SDK libraries that
953 can worm around this.
954 + MS fgets locks/unlocks only once per line; it's the only hook we have.
956 So we use fgets for speed(!), despite that it's painful.
958 MS realloc is also slow.
960 Reports from other platforms on this method vs getc_unlocked (which MS doesn't
961 have):
962 Linux a wash
963 Solaris a wash
964 Tru64 Unix getline_via_fgets significantly faster
966 CAUTION: The C std isn't clear about this: in those cases where fgets
967 writes something into the buffer, can it write into any position beyond the
968 required trailing null byte? MSVC 6 fgets does not, and no platform is (yet)
969 known on which it does; and it would be a strange way to code fgets. Still,
970 getline_via_fgets may not work correctly if it does. The std test
971 test_bufio.py should fail if platform fgets() routinely writes beyond the
972 trailing null byte. #define DONT_USE_FGETS_IN_GETLINE to disable this code.
973 **************************************************************************/
975 /* Use this routine if told to, or by default on non-get_unlocked()
976 * platforms unless told not to. Yikes! Let's spell that out:
977 * On a platform with getc_unlocked():
978 * By default, use getc_unlocked().
979 * If you want to use fgets() instead, #define USE_FGETS_IN_GETLINE.
980 * On a platform without getc_unlocked():
981 * By default, use fgets().
982 * If you don't want to use fgets(), #define DONT_USE_FGETS_IN_GETLINE.
984 #if !defined(USE_FGETS_IN_GETLINE) && !defined(HAVE_GETC_UNLOCKED)
985 #define USE_FGETS_IN_GETLINE
986 #endif
988 #if defined(DONT_USE_FGETS_IN_GETLINE) && defined(USE_FGETS_IN_GETLINE)
989 #undef USE_FGETS_IN_GETLINE
990 #endif
992 #ifdef USE_FGETS_IN_GETLINE
993 static PyObject*
994 getline_via_fgets(FILE *fp)
996 /* INITBUFSIZE is the maximum line length that lets us get away with the fast
997 * no-realloc, one-fgets()-call path. Boosting it isn't free, because we have
998 * to fill this much of the buffer with a known value in order to figure out
999 * how much of the buffer fgets() overwrites. So if INITBUFSIZE is larger
1000 * than "most" lines, we waste time filling unused buffer slots. 100 is
1001 * surely adequate for most peoples' email archives, chewing over source code,
1002 * etc -- "regular old text files".
1003 * MAXBUFSIZE is the maximum line length that lets us get away with the less
1004 * fast (but still zippy) no-realloc, two-fgets()-call path. See above for
1005 * cautions about boosting that. 300 was chosen because the worst real-life
1006 * text-crunching job reported on Python-Dev was a mail-log crawler where over
1007 * half the lines were 254 chars.
1009 #define INITBUFSIZE 100
1010 #define MAXBUFSIZE 300
1011 char* p; /* temp */
1012 char buf[MAXBUFSIZE];
1013 PyObject* v; /* the string object result */
1014 char* pvfree; /* address of next free slot */
1015 char* pvend; /* address one beyond last free slot */
1016 size_t nfree; /* # of free buffer slots; pvend-pvfree */
1017 size_t total_v_size; /* total # of slots in buffer */
1018 size_t increment; /* amount to increment the buffer */
1019 size_t prev_v_size;
1021 /* Optimize for normal case: avoid _PyString_Resize if at all
1022 * possible via first reading into stack buffer "buf".
1024 total_v_size = INITBUFSIZE; /* start small and pray */
1025 pvfree = buf;
1026 for (;;) {
1027 Py_BEGIN_ALLOW_THREADS
1028 pvend = buf + total_v_size;
1029 nfree = pvend - pvfree;
1030 memset(pvfree, '\n', nfree);
1031 assert(nfree < INT_MAX); /* Should be atmost MAXBUFSIZE */
1032 p = fgets(pvfree, (int)nfree, fp);
1033 Py_END_ALLOW_THREADS
1035 if (p == NULL) {
1036 clearerr(fp);
1037 if (PyErr_CheckSignals())
1038 return NULL;
1039 v = PyString_FromStringAndSize(buf, pvfree - buf);
1040 return v;
1042 /* fgets read *something* */
1043 p = memchr(pvfree, '\n', nfree);
1044 if (p != NULL) {
1045 /* Did the \n come from fgets or from us?
1046 * Since fgets stops at the first \n, and then writes
1047 * \0, if it's from fgets a \0 must be next. But if
1048 * that's so, it could not have come from us, since
1049 * the \n's we filled the buffer with have only more
1050 * \n's to the right.
1052 if (p+1 < pvend && *(p+1) == '\0') {
1053 /* It's from fgets: we win! In particular,
1054 * we haven't done any mallocs yet, and can
1055 * build the final result on the first try.
1057 ++p; /* include \n from fgets */
1059 else {
1060 /* Must be from us: fgets didn't fill the
1061 * buffer and didn't find a newline, so it
1062 * must be the last and newline-free line of
1063 * the file.
1065 assert(p > pvfree && *(p-1) == '\0');
1066 --p; /* don't include \0 from fgets */
1068 v = PyString_FromStringAndSize(buf, p - buf);
1069 return v;
1071 /* yuck: fgets overwrote all the newlines, i.e. the entire
1072 * buffer. So this line isn't over yet, or maybe it is but
1073 * we're exactly at EOF. If we haven't already, try using the
1074 * rest of the stack buffer.
1076 assert(*(pvend-1) == '\0');
1077 if (pvfree == buf) {
1078 pvfree = pvend - 1; /* overwrite trailing null */
1079 total_v_size = MAXBUFSIZE;
1081 else
1082 break;
1085 /* The stack buffer isn't big enough; malloc a string object and read
1086 * into its buffer.
1088 total_v_size = MAXBUFSIZE << 1;
1089 v = PyString_FromStringAndSize((char*)NULL, (int)total_v_size);
1090 if (v == NULL)
1091 return v;
1092 /* copy over everything except the last null byte */
1093 memcpy(BUF(v), buf, MAXBUFSIZE-1);
1094 pvfree = BUF(v) + MAXBUFSIZE - 1;
1096 /* Keep reading stuff into v; if it ever ends successfully, break
1097 * after setting p one beyond the end of the line. The code here is
1098 * very much like the code above, except reads into v's buffer; see
1099 * the code above for detailed comments about the logic.
1101 for (;;) {
1102 Py_BEGIN_ALLOW_THREADS
1103 pvend = BUF(v) + total_v_size;
1104 nfree = pvend - pvfree;
1105 memset(pvfree, '\n', nfree);
1106 assert(nfree < INT_MAX);
1107 p = fgets(pvfree, (int)nfree, fp);
1108 Py_END_ALLOW_THREADS
1110 if (p == NULL) {
1111 clearerr(fp);
1112 if (PyErr_CheckSignals()) {
1113 Py_DECREF(v);
1114 return NULL;
1116 p = pvfree;
1117 break;
1119 p = memchr(pvfree, '\n', nfree);
1120 if (p != NULL) {
1121 if (p+1 < pvend && *(p+1) == '\0') {
1122 /* \n came from fgets */
1123 ++p;
1124 break;
1126 /* \n came from us; last line of file, no newline */
1127 assert(p > pvfree && *(p-1) == '\0');
1128 --p;
1129 break;
1131 /* expand buffer and try again */
1132 assert(*(pvend-1) == '\0');
1133 increment = total_v_size >> 2; /* mild exponential growth */
1134 prev_v_size = total_v_size;
1135 total_v_size += increment;
1136 /* check for overflow */
1137 if (total_v_size <= prev_v_size ||
1138 total_v_size > PY_SSIZE_T_MAX) {
1139 PyErr_SetString(PyExc_OverflowError,
1140 "line is longer than a Python string can hold");
1141 Py_DECREF(v);
1142 return NULL;
1144 if (_PyString_Resize(&v, (int)total_v_size) < 0)
1145 return NULL;
1146 /* overwrite the trailing null byte */
1147 pvfree = BUF(v) + (prev_v_size - 1);
1149 if (BUF(v) + total_v_size != p)
1150 _PyString_Resize(&v, p - BUF(v));
1151 return v;
1152 #undef INITBUFSIZE
1153 #undef MAXBUFSIZE
1155 #endif /* ifdef USE_FGETS_IN_GETLINE */
1157 /* Internal routine to get a line.
1158 Size argument interpretation:
1159 > 0: max length;
1160 <= 0: read arbitrary line
1163 static PyObject *
1164 get_line(PyFileObject *f, int n)
1166 FILE *fp = f->f_fp;
1167 int c;
1168 char *buf, *end;
1169 size_t total_v_size; /* total # of slots in buffer */
1170 size_t used_v_size; /* # used slots in buffer */
1171 size_t increment; /* amount to increment the buffer */
1172 PyObject *v;
1173 int newlinetypes = f->f_newlinetypes;
1174 int skipnextlf = f->f_skipnextlf;
1175 int univ_newline = f->f_univ_newline;
1177 #if defined(USE_FGETS_IN_GETLINE)
1178 if (n <= 0 && !univ_newline )
1179 return getline_via_fgets(fp);
1180 #endif
1181 total_v_size = n > 0 ? n : 100;
1182 v = PyString_FromStringAndSize((char *)NULL, total_v_size);
1183 if (v == NULL)
1184 return NULL;
1185 buf = BUF(v);
1186 end = buf + total_v_size;
1188 for (;;) {
1189 Py_BEGIN_ALLOW_THREADS
1190 FLOCKFILE(fp);
1191 if (univ_newline) {
1192 c = 'x'; /* Shut up gcc warning */
1193 while ( buf != end && (c = GETC(fp)) != EOF ) {
1194 if (skipnextlf ) {
1195 skipnextlf = 0;
1196 if (c == '\n') {
1197 /* Seeing a \n here with
1198 * skipnextlf true means we
1199 * saw a \r before.
1201 newlinetypes |= NEWLINE_CRLF;
1202 c = GETC(fp);
1203 if (c == EOF) break;
1204 } else {
1205 newlinetypes |= NEWLINE_CR;
1208 if (c == '\r') {
1209 skipnextlf = 1;
1210 c = '\n';
1211 } else if ( c == '\n')
1212 newlinetypes |= NEWLINE_LF;
1213 *buf++ = c;
1214 if (c == '\n') break;
1216 if ( c == EOF && skipnextlf )
1217 newlinetypes |= NEWLINE_CR;
1218 } else /* If not universal newlines use the normal loop */
1219 while ((c = GETC(fp)) != EOF &&
1220 (*buf++ = c) != '\n' &&
1221 buf != end)
1223 FUNLOCKFILE(fp);
1224 Py_END_ALLOW_THREADS
1225 f->f_newlinetypes = newlinetypes;
1226 f->f_skipnextlf = skipnextlf;
1227 if (c == '\n')
1228 break;
1229 if (c == EOF) {
1230 if (ferror(fp)) {
1231 PyErr_SetFromErrno(PyExc_IOError);
1232 clearerr(fp);
1233 Py_DECREF(v);
1234 return NULL;
1236 clearerr(fp);
1237 if (PyErr_CheckSignals()) {
1238 Py_DECREF(v);
1239 return NULL;
1241 break;
1243 /* Must be because buf == end */
1244 if (n > 0)
1245 break;
1246 used_v_size = total_v_size;
1247 increment = total_v_size >> 2; /* mild exponential growth */
1248 total_v_size += increment;
1249 if (total_v_size > PY_SSIZE_T_MAX) {
1250 PyErr_SetString(PyExc_OverflowError,
1251 "line is longer than a Python string can hold");
1252 Py_DECREF(v);
1253 return NULL;
1255 if (_PyString_Resize(&v, total_v_size) < 0)
1256 return NULL;
1257 buf = BUF(v) + used_v_size;
1258 end = BUF(v) + total_v_size;
1261 used_v_size = buf - BUF(v);
1262 if (used_v_size != total_v_size)
1263 _PyString_Resize(&v, used_v_size);
1264 return v;
1267 /* External C interface */
1269 PyObject *
1270 PyFile_GetLine(PyObject *f, int n)
1272 PyObject *result;
1274 if (f == NULL) {
1275 PyErr_BadInternalCall();
1276 return NULL;
1279 if (PyFile_Check(f)) {
1280 PyFileObject *fo = (PyFileObject *)f;
1281 if (fo->f_fp == NULL)
1282 return err_closed();
1283 /* refuse to mix with f.next() */
1284 if (fo->f_buf != NULL &&
1285 (fo->f_bufend - fo->f_bufptr) > 0 &&
1286 fo->f_buf[0] != '\0')
1287 return err_iterbuffered();
1288 result = get_line(fo, n);
1290 else {
1291 PyObject *reader;
1292 PyObject *args;
1294 reader = PyObject_GetAttrString(f, "readline");
1295 if (reader == NULL)
1296 return NULL;
1297 if (n <= 0)
1298 args = PyTuple_New(0);
1299 else
1300 args = Py_BuildValue("(i)", n);
1301 if (args == NULL) {
1302 Py_DECREF(reader);
1303 return NULL;
1305 result = PyEval_CallObject(reader, args);
1306 Py_DECREF(reader);
1307 Py_DECREF(args);
1308 if (result != NULL && !PyString_Check(result) &&
1309 !PyUnicode_Check(result)) {
1310 Py_DECREF(result);
1311 result = NULL;
1312 PyErr_SetString(PyExc_TypeError,
1313 "object.readline() returned non-string");
1317 if (n < 0 && result != NULL && PyString_Check(result)) {
1318 char *s = PyString_AS_STRING(result);
1319 Py_ssize_t len = PyString_GET_SIZE(result);
1320 if (len == 0) {
1321 Py_DECREF(result);
1322 result = NULL;
1323 PyErr_SetString(PyExc_EOFError,
1324 "EOF when reading a line");
1326 else if (s[len-1] == '\n') {
1327 if (result->ob_refcnt == 1)
1328 _PyString_Resize(&result, len-1);
1329 else {
1330 PyObject *v;
1331 v = PyString_FromStringAndSize(s, len-1);
1332 Py_DECREF(result);
1333 result = v;
1337 #ifdef Py_USING_UNICODE
1338 if (n < 0 && result != NULL && PyUnicode_Check(result)) {
1339 Py_UNICODE *s = PyUnicode_AS_UNICODE(result);
1340 Py_ssize_t len = PyUnicode_GET_SIZE(result);
1341 if (len == 0) {
1342 Py_DECREF(result);
1343 result = NULL;
1344 PyErr_SetString(PyExc_EOFError,
1345 "EOF when reading a line");
1347 else if (s[len-1] == '\n') {
1348 if (result->ob_refcnt == 1)
1349 PyUnicode_Resize(&result, len-1);
1350 else {
1351 PyObject *v;
1352 v = PyUnicode_FromUnicode(s, len-1);
1353 Py_DECREF(result);
1354 result = v;
1358 #endif
1359 return result;
1362 /* Python method */
1364 static PyObject *
1365 file_readline(PyFileObject *f, PyObject *args)
1367 int n = -1;
1369 if (f->f_fp == NULL)
1370 return err_closed();
1371 /* refuse to mix with f.next() */
1372 if (f->f_buf != NULL &&
1373 (f->f_bufend - f->f_bufptr) > 0 &&
1374 f->f_buf[0] != '\0')
1375 return err_iterbuffered();
1376 if (!PyArg_ParseTuple(args, "|i:readline", &n))
1377 return NULL;
1378 if (n == 0)
1379 return PyString_FromString("");
1380 if (n < 0)
1381 n = 0;
1382 return get_line(f, n);
1385 static PyObject *
1386 file_readlines(PyFileObject *f, PyObject *args)
1388 long sizehint = 0;
1389 PyObject *list;
1390 PyObject *line;
1391 char small_buffer[SMALLCHUNK];
1392 char *buffer = small_buffer;
1393 size_t buffersize = SMALLCHUNK;
1394 PyObject *big_buffer = NULL;
1395 size_t nfilled = 0;
1396 size_t nread;
1397 size_t totalread = 0;
1398 char *p, *q, *end;
1399 int err;
1400 int shortread = 0;
1402 if (f->f_fp == NULL)
1403 return err_closed();
1404 /* refuse to mix with f.next() */
1405 if (f->f_buf != NULL &&
1406 (f->f_bufend - f->f_bufptr) > 0 &&
1407 f->f_buf[0] != '\0')
1408 return err_iterbuffered();
1409 if (!PyArg_ParseTuple(args, "|l:readlines", &sizehint))
1410 return NULL;
1411 if ((list = PyList_New(0)) == NULL)
1412 return NULL;
1413 for (;;) {
1414 if (shortread)
1415 nread = 0;
1416 else {
1417 Py_BEGIN_ALLOW_THREADS
1418 errno = 0;
1419 nread = Py_UniversalNewlineFread(buffer+nfilled,
1420 buffersize-nfilled, f->f_fp, (PyObject *)f);
1421 Py_END_ALLOW_THREADS
1422 shortread = (nread < buffersize-nfilled);
1424 if (nread == 0) {
1425 sizehint = 0;
1426 if (!ferror(f->f_fp))
1427 break;
1428 PyErr_SetFromErrno(PyExc_IOError);
1429 clearerr(f->f_fp);
1430 error:
1431 Py_DECREF(list);
1432 list = NULL;
1433 goto cleanup;
1435 totalread += nread;
1436 p = (char *)memchr(buffer+nfilled, '\n', nread);
1437 if (p == NULL) {
1438 /* Need a larger buffer to fit this line */
1439 nfilled += nread;
1440 buffersize *= 2;
1441 if (buffersize > PY_SSIZE_T_MAX) {
1442 PyErr_SetString(PyExc_OverflowError,
1443 "line is longer than a Python string can hold");
1444 goto error;
1446 if (big_buffer == NULL) {
1447 /* Create the big buffer */
1448 big_buffer = PyString_FromStringAndSize(
1449 NULL, buffersize);
1450 if (big_buffer == NULL)
1451 goto error;
1452 buffer = PyString_AS_STRING(big_buffer);
1453 memcpy(buffer, small_buffer, nfilled);
1455 else {
1456 /* Grow the big buffer */
1457 if ( _PyString_Resize(&big_buffer, buffersize) < 0 )
1458 goto error;
1459 buffer = PyString_AS_STRING(big_buffer);
1461 continue;
1463 end = buffer+nfilled+nread;
1464 q = buffer;
1465 do {
1466 /* Process complete lines */
1467 p++;
1468 line = PyString_FromStringAndSize(q, p-q);
1469 if (line == NULL)
1470 goto error;
1471 err = PyList_Append(list, line);
1472 Py_DECREF(line);
1473 if (err != 0)
1474 goto error;
1475 q = p;
1476 p = (char *)memchr(q, '\n', end-q);
1477 } while (p != NULL);
1478 /* Move the remaining incomplete line to the start */
1479 nfilled = end-q;
1480 memmove(buffer, q, nfilled);
1481 if (sizehint > 0)
1482 if (totalread >= (size_t)sizehint)
1483 break;
1485 if (nfilled != 0) {
1486 /* Partial last line */
1487 line = PyString_FromStringAndSize(buffer, nfilled);
1488 if (line == NULL)
1489 goto error;
1490 if (sizehint > 0) {
1491 /* Need to complete the last line */
1492 PyObject *rest = get_line(f, 0);
1493 if (rest == NULL) {
1494 Py_DECREF(line);
1495 goto error;
1497 PyString_Concat(&line, rest);
1498 Py_DECREF(rest);
1499 if (line == NULL)
1500 goto error;
1502 err = PyList_Append(list, line);
1503 Py_DECREF(line);
1504 if (err != 0)
1505 goto error;
1507 cleanup:
1508 Py_XDECREF(big_buffer);
1509 return list;
1512 static PyObject *
1513 file_write(PyFileObject *f, PyObject *args)
1515 char *s;
1516 Py_ssize_t n, n2;
1517 if (f->f_fp == NULL)
1518 return err_closed();
1519 if (!PyArg_ParseTuple(args, f->f_binary ? "s#" : "t#", &s, &n))
1520 return NULL;
1521 f->f_softspace = 0;
1522 Py_BEGIN_ALLOW_THREADS
1523 errno = 0;
1524 n2 = fwrite(s, 1, n, f->f_fp);
1525 Py_END_ALLOW_THREADS
1526 if (n2 != n) {
1527 PyErr_SetFromErrno(PyExc_IOError);
1528 clearerr(f->f_fp);
1529 return NULL;
1531 Py_INCREF(Py_None);
1532 return Py_None;
1535 static PyObject *
1536 file_writelines(PyFileObject *f, PyObject *seq)
1538 #define CHUNKSIZE 1000
1539 PyObject *list, *line;
1540 PyObject *it; /* iter(seq) */
1541 PyObject *result;
1542 int index, islist;
1543 Py_ssize_t i, j, nwritten, len;
1545 assert(seq != NULL);
1546 if (f->f_fp == NULL)
1547 return err_closed();
1549 result = NULL;
1550 list = NULL;
1551 islist = PyList_Check(seq);
1552 if (islist)
1553 it = NULL;
1554 else {
1555 it = PyObject_GetIter(seq);
1556 if (it == NULL) {
1557 PyErr_SetString(PyExc_TypeError,
1558 "writelines() requires an iterable argument");
1559 return NULL;
1561 /* From here on, fail by going to error, to reclaim "it". */
1562 list = PyList_New(CHUNKSIZE);
1563 if (list == NULL)
1564 goto error;
1567 /* Strategy: slurp CHUNKSIZE lines into a private list,
1568 checking that they are all strings, then write that list
1569 without holding the interpreter lock, then come back for more. */
1570 for (index = 0; ; index += CHUNKSIZE) {
1571 if (islist) {
1572 Py_XDECREF(list);
1573 list = PyList_GetSlice(seq, index, index+CHUNKSIZE);
1574 if (list == NULL)
1575 goto error;
1576 j = PyList_GET_SIZE(list);
1578 else {
1579 for (j = 0; j < CHUNKSIZE; j++) {
1580 line = PyIter_Next(it);
1581 if (line == NULL) {
1582 if (PyErr_Occurred())
1583 goto error;
1584 break;
1586 PyList_SetItem(list, j, line);
1589 if (j == 0)
1590 break;
1592 /* Check that all entries are indeed strings. If not,
1593 apply the same rules as for file.write() and
1594 convert the results to strings. This is slow, but
1595 seems to be the only way since all conversion APIs
1596 could potentially execute Python code. */
1597 for (i = 0; i < j; i++) {
1598 PyObject *v = PyList_GET_ITEM(list, i);
1599 if (!PyString_Check(v)) {
1600 const char *buffer;
1601 if (((f->f_binary &&
1602 PyObject_AsReadBuffer(v,
1603 (const void**)&buffer,
1604 &len)) ||
1605 PyObject_AsCharBuffer(v,
1606 &buffer,
1607 &len))) {
1608 PyErr_SetString(PyExc_TypeError,
1609 "writelines() argument must be a sequence of strings");
1610 goto error;
1612 line = PyString_FromStringAndSize(buffer,
1613 len);
1614 if (line == NULL)
1615 goto error;
1616 Py_DECREF(v);
1617 PyList_SET_ITEM(list, i, line);
1621 /* Since we are releasing the global lock, the
1622 following code may *not* execute Python code. */
1623 Py_BEGIN_ALLOW_THREADS
1624 f->f_softspace = 0;
1625 errno = 0;
1626 for (i = 0; i < j; i++) {
1627 line = PyList_GET_ITEM(list, i);
1628 len = PyString_GET_SIZE(line);
1629 nwritten = fwrite(PyString_AS_STRING(line),
1630 1, len, f->f_fp);
1631 if (nwritten != len) {
1632 Py_BLOCK_THREADS
1633 PyErr_SetFromErrno(PyExc_IOError);
1634 clearerr(f->f_fp);
1635 goto error;
1638 Py_END_ALLOW_THREADS
1640 if (j < CHUNKSIZE)
1641 break;
1644 Py_INCREF(Py_None);
1645 result = Py_None;
1646 error:
1647 Py_XDECREF(list);
1648 Py_XDECREF(it);
1649 return result;
1650 #undef CHUNKSIZE
1653 static PyObject *
1654 file_self(PyFileObject *f)
1656 if (f->f_fp == NULL)
1657 return err_closed();
1658 Py_INCREF(f);
1659 return (PyObject *)f;
1662 static PyObject *
1663 file_exit(PyFileObject *f, PyObject *args)
1665 PyObject *ret = file_close(f);
1666 if (!ret)
1667 /* If error occurred, pass through */
1668 return NULL;
1669 Py_DECREF(ret);
1670 /* We cannot return the result of close since a true
1671 * value will be interpreted as "yes, swallow the
1672 * exception if one was raised inside the with block". */
1673 Py_RETURN_NONE;
1676 PyDoc_STRVAR(readline_doc,
1677 "readline([size]) -> next line from the file, as a string.\n"
1678 "\n"
1679 "Retain newline. A non-negative size argument limits the maximum\n"
1680 "number of bytes to return (an incomplete line may be returned then).\n"
1681 "Return an empty string at EOF.");
1683 PyDoc_STRVAR(read_doc,
1684 "read([size]) -> read at most size bytes, returned as a string.\n"
1685 "\n"
1686 "If the size argument is negative or omitted, read until EOF is reached.\n"
1687 "Notice that when in non-blocking mode, less data than what was requested\n"
1688 "may be returned, even if no size parameter was given.");
1690 PyDoc_STRVAR(write_doc,
1691 "write(str) -> None. Write string str to file.\n"
1692 "\n"
1693 "Note that due to buffering, flush() or close() may be needed before\n"
1694 "the file on disk reflects the data written.");
1696 PyDoc_STRVAR(fileno_doc,
1697 "fileno() -> integer \"file descriptor\".\n"
1698 "\n"
1699 "This is needed for lower-level file interfaces, such os.read().");
1701 PyDoc_STRVAR(seek_doc,
1702 "seek(offset[, whence]) -> None. Move to new file position.\n"
1703 "\n"
1704 "Argument offset is a byte count. Optional argument whence defaults to\n"
1705 "0 (offset from start of file, offset should be >= 0); other values are 1\n"
1706 "(move relative to current position, positive or negative), and 2 (move\n"
1707 "relative to end of file, usually negative, although many platforms allow\n"
1708 "seeking beyond the end of a file). If the file is opened in text mode,\n"
1709 "only offsets returned by tell() are legal. Use of other offsets causes\n"
1710 "undefined behavior."
1711 "\n"
1712 "Note that not all file objects are seekable.");
1714 #ifdef HAVE_FTRUNCATE
1715 PyDoc_STRVAR(truncate_doc,
1716 "truncate([size]) -> None. Truncate the file to at most size bytes.\n"
1717 "\n"
1718 "Size defaults to the current file position, as returned by tell().");
1719 #endif
1721 PyDoc_STRVAR(tell_doc,
1722 "tell() -> current file position, an integer (may be a long integer).");
1724 PyDoc_STRVAR(readinto_doc,
1725 "readinto() -> Undocumented. Don't use this; it may go away.");
1727 PyDoc_STRVAR(readlines_doc,
1728 "readlines([size]) -> list of strings, each a line from the file.\n"
1729 "\n"
1730 "Call readline() repeatedly and return a list of the lines so read.\n"
1731 "The optional size argument, if given, is an approximate bound on the\n"
1732 "total number of bytes in the lines returned.");
1734 PyDoc_STRVAR(xreadlines_doc,
1735 "xreadlines() -> returns self.\n"
1736 "\n"
1737 "For backward compatibility. File objects now include the performance\n"
1738 "optimizations previously implemented in the xreadlines module.");
1740 PyDoc_STRVAR(writelines_doc,
1741 "writelines(sequence_of_strings) -> None. Write the strings to the file.\n"
1742 "\n"
1743 "Note that newlines are not added. The sequence can be any iterable object\n"
1744 "producing strings. This is equivalent to calling write() for each string.");
1746 PyDoc_STRVAR(flush_doc,
1747 "flush() -> None. Flush the internal I/O buffer.");
1749 PyDoc_STRVAR(close_doc,
1750 "close() -> None or (perhaps) an integer. Close the file.\n"
1751 "\n"
1752 "Sets data attribute .closed to True. A closed file cannot be used for\n"
1753 "further I/O operations. close() may be called more than once without\n"
1754 "error. Some kinds of file objects (for example, opened by popen())\n"
1755 "may return an exit status upon closing.");
1757 PyDoc_STRVAR(isatty_doc,
1758 "isatty() -> true or false. True if the file is connected to a tty device.");
1760 PyDoc_STRVAR(enter_doc,
1761 "__enter__() -> self.");
1763 PyDoc_STRVAR(exit_doc,
1764 "__exit__(*excinfo) -> None. Closes the file.");
1766 static PyMethodDef file_methods[] = {
1767 {"readline", (PyCFunction)file_readline, METH_VARARGS, readline_doc},
1768 {"read", (PyCFunction)file_read, METH_VARARGS, read_doc},
1769 {"write", (PyCFunction)file_write, METH_VARARGS, write_doc},
1770 {"fileno", (PyCFunction)file_fileno, METH_NOARGS, fileno_doc},
1771 {"seek", (PyCFunction)file_seek, METH_VARARGS, seek_doc},
1772 #ifdef HAVE_FTRUNCATE
1773 {"truncate", (PyCFunction)file_truncate, METH_VARARGS, truncate_doc},
1774 #endif
1775 {"tell", (PyCFunction)file_tell, METH_NOARGS, tell_doc},
1776 {"readinto", (PyCFunction)file_readinto, METH_VARARGS, readinto_doc},
1777 {"readlines", (PyCFunction)file_readlines,METH_VARARGS, readlines_doc},
1778 {"xreadlines",(PyCFunction)file_self, METH_NOARGS, xreadlines_doc},
1779 {"writelines",(PyCFunction)file_writelines, METH_O, writelines_doc},
1780 {"flush", (PyCFunction)file_flush, METH_NOARGS, flush_doc},
1781 {"close", (PyCFunction)file_close, METH_NOARGS, close_doc},
1782 {"isatty", (PyCFunction)file_isatty, METH_NOARGS, isatty_doc},
1783 {"__enter__", (PyCFunction)file_self, METH_NOARGS, enter_doc},
1784 {"__exit__", (PyCFunction)file_exit, METH_VARARGS, exit_doc},
1785 {NULL, NULL} /* sentinel */
1788 #define OFF(x) offsetof(PyFileObject, x)
1790 static PyMemberDef file_memberlist[] = {
1791 {"softspace", T_INT, OFF(f_softspace), 0,
1792 "flag indicating that a space needs to be printed; used by print"},
1793 {"mode", T_OBJECT, OFF(f_mode), RO,
1794 "file mode ('r', 'U', 'w', 'a', possibly with 'b' or '+' added)"},
1795 {"name", T_OBJECT, OFF(f_name), RO,
1796 "file name"},
1797 {"encoding", T_OBJECT, OFF(f_encoding), RO,
1798 "file encoding"},
1799 /* getattr(f, "closed") is implemented without this table */
1800 {NULL} /* Sentinel */
1803 static PyObject *
1804 get_closed(PyFileObject *f, void *closure)
1806 return PyBool_FromLong((long)(f->f_fp == 0));
1808 static PyObject *
1809 get_newlines(PyFileObject *f, void *closure)
1811 switch (f->f_newlinetypes) {
1812 case NEWLINE_UNKNOWN:
1813 Py_INCREF(Py_None);
1814 return Py_None;
1815 case NEWLINE_CR:
1816 return PyString_FromString("\r");
1817 case NEWLINE_LF:
1818 return PyString_FromString("\n");
1819 case NEWLINE_CR|NEWLINE_LF:
1820 return Py_BuildValue("(ss)", "\r", "\n");
1821 case NEWLINE_CRLF:
1822 return PyString_FromString("\r\n");
1823 case NEWLINE_CR|NEWLINE_CRLF:
1824 return Py_BuildValue("(ss)", "\r", "\r\n");
1825 case NEWLINE_LF|NEWLINE_CRLF:
1826 return Py_BuildValue("(ss)", "\n", "\r\n");
1827 case NEWLINE_CR|NEWLINE_LF|NEWLINE_CRLF:
1828 return Py_BuildValue("(sss)", "\r", "\n", "\r\n");
1829 default:
1830 PyErr_Format(PyExc_SystemError,
1831 "Unknown newlines value 0x%x\n",
1832 f->f_newlinetypes);
1833 return NULL;
1837 static PyGetSetDef file_getsetlist[] = {
1838 {"closed", (getter)get_closed, NULL, "True if the file is closed"},
1839 {"newlines", (getter)get_newlines, NULL,
1840 "end-of-line convention used in this file"},
1841 {0},
1844 static void
1845 drop_readahead(PyFileObject *f)
1847 if (f->f_buf != NULL) {
1848 PyMem_Free(f->f_buf);
1849 f->f_buf = NULL;
1853 /* Make sure that file has a readahead buffer with at least one byte
1854 (unless at EOF) and no more than bufsize. Returns negative value on
1855 error, will set MemoryError if bufsize bytes cannot be allocated. */
1856 static int
1857 readahead(PyFileObject *f, int bufsize)
1859 Py_ssize_t chunksize;
1861 if (f->f_buf != NULL) {
1862 if( (f->f_bufend - f->f_bufptr) >= 1)
1863 return 0;
1864 else
1865 drop_readahead(f);
1867 if ((f->f_buf = (char *)PyMem_Malloc(bufsize)) == NULL) {
1868 PyErr_NoMemory();
1869 return -1;
1871 Py_BEGIN_ALLOW_THREADS
1872 errno = 0;
1873 chunksize = Py_UniversalNewlineFread(
1874 f->f_buf, bufsize, f->f_fp, (PyObject *)f);
1875 Py_END_ALLOW_THREADS
1876 if (chunksize == 0) {
1877 if (ferror(f->f_fp)) {
1878 PyErr_SetFromErrno(PyExc_IOError);
1879 clearerr(f->f_fp);
1880 drop_readahead(f);
1881 return -1;
1884 f->f_bufptr = f->f_buf;
1885 f->f_bufend = f->f_buf + chunksize;
1886 return 0;
1889 /* Used by file_iternext. The returned string will start with 'skip'
1890 uninitialized bytes followed by the remainder of the line. Don't be
1891 horrified by the recursive call: maximum recursion depth is limited by
1892 logarithmic buffer growth to about 50 even when reading a 1gb line. */
1894 static PyStringObject *
1895 readahead_get_line_skip(PyFileObject *f, int skip, int bufsize)
1897 PyStringObject* s;
1898 char *bufptr;
1899 char *buf;
1900 Py_ssize_t len;
1902 if (f->f_buf == NULL)
1903 if (readahead(f, bufsize) < 0)
1904 return NULL;
1906 len = f->f_bufend - f->f_bufptr;
1907 if (len == 0)
1908 return (PyStringObject *)
1909 PyString_FromStringAndSize(NULL, skip);
1910 bufptr = (char *)memchr(f->f_bufptr, '\n', len);
1911 if (bufptr != NULL) {
1912 bufptr++; /* Count the '\n' */
1913 len = bufptr - f->f_bufptr;
1914 s = (PyStringObject *)
1915 PyString_FromStringAndSize(NULL, skip+len);
1916 if (s == NULL)
1917 return NULL;
1918 memcpy(PyString_AS_STRING(s)+skip, f->f_bufptr, len);
1919 f->f_bufptr = bufptr;
1920 if (bufptr == f->f_bufend)
1921 drop_readahead(f);
1922 } else {
1923 bufptr = f->f_bufptr;
1924 buf = f->f_buf;
1925 f->f_buf = NULL; /* Force new readahead buffer */
1926 assert(skip+len < INT_MAX);
1927 s = readahead_get_line_skip(
1928 f, (int)(skip+len), bufsize + (bufsize>>2) );
1929 if (s == NULL) {
1930 PyMem_Free(buf);
1931 return NULL;
1933 memcpy(PyString_AS_STRING(s)+skip, bufptr, len);
1934 PyMem_Free(buf);
1936 return s;
1939 /* A larger buffer size may actually decrease performance. */
1940 #define READAHEAD_BUFSIZE 8192
1942 static PyObject *
1943 file_iternext(PyFileObject *f)
1945 PyStringObject* l;
1947 if (f->f_fp == NULL)
1948 return err_closed();
1950 l = readahead_get_line_skip(f, 0, READAHEAD_BUFSIZE);
1951 if (l == NULL || PyString_GET_SIZE(l) == 0) {
1952 Py_XDECREF(l);
1953 return NULL;
1955 return (PyObject *)l;
1959 static PyObject *
1960 file_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1962 PyObject *self;
1963 static PyObject *not_yet_string;
1965 assert(type != NULL && type->tp_alloc != NULL);
1967 if (not_yet_string == NULL) {
1968 not_yet_string = PyString_FromString("<uninitialized file>");
1969 if (not_yet_string == NULL)
1970 return NULL;
1973 self = type->tp_alloc(type, 0);
1974 if (self != NULL) {
1975 /* Always fill in the name and mode, so that nobody else
1976 needs to special-case NULLs there. */
1977 Py_INCREF(not_yet_string);
1978 ((PyFileObject *)self)->f_name = not_yet_string;
1979 Py_INCREF(not_yet_string);
1980 ((PyFileObject *)self)->f_mode = not_yet_string;
1981 Py_INCREF(Py_None);
1982 ((PyFileObject *)self)->f_encoding = Py_None;
1983 ((PyFileObject *)self)->weakreflist = NULL;
1985 return self;
1988 static int
1989 file_init(PyObject *self, PyObject *args, PyObject *kwds)
1991 PyFileObject *foself = (PyFileObject *)self;
1992 int ret = 0;
1993 static char *kwlist[] = {"name", "mode", "buffering", 0};
1994 char *name = NULL;
1995 char *mode = "r";
1996 int bufsize = -1;
1997 int wideargument = 0;
1999 assert(PyFile_Check(self));
2000 if (foself->f_fp != NULL) {
2001 /* Have to close the existing file first. */
2002 PyObject *closeresult = file_close(foself);
2003 if (closeresult == NULL)
2004 return -1;
2005 Py_DECREF(closeresult);
2008 #ifdef Py_WIN_WIDE_FILENAMES
2009 if (GetVersion() < 0x80000000) { /* On NT, so wide API available */
2010 PyObject *po;
2011 if (PyArg_ParseTupleAndKeywords(args, kwds, "U|si:file",
2012 kwlist, &po, &mode, &bufsize)) {
2013 wideargument = 1;
2014 if (fill_file_fields(foself, NULL, po, mode,
2015 fclose) == NULL)
2016 goto Error;
2017 } else {
2018 /* Drop the argument parsing error as narrow
2019 strings are also valid. */
2020 PyErr_Clear();
2023 #endif
2025 if (!wideargument) {
2026 PyObject *o_name;
2028 if (!PyArg_ParseTupleAndKeywords(args, kwds, "et|si:file", kwlist,
2029 Py_FileSystemDefaultEncoding,
2030 &name,
2031 &mode, &bufsize))
2032 return -1;
2034 /* We parse again to get the name as a PyObject */
2035 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|si:file",
2036 kwlist, &o_name, &mode,
2037 &bufsize))
2038 goto Error;
2040 if (fill_file_fields(foself, NULL, o_name, mode,
2041 fclose) == NULL)
2042 goto Error;
2044 if (open_the_file(foself, name, mode) == NULL)
2045 goto Error;
2046 foself->f_setbuf = NULL;
2047 PyFile_SetBufSize(self, bufsize);
2048 goto Done;
2050 Error:
2051 ret = -1;
2052 /* fall through */
2053 Done:
2054 PyMem_Free(name); /* free the encoded string */
2055 return ret;
2058 PyDoc_VAR(file_doc) =
2059 PyDoc_STR(
2060 "file(name[, mode[, buffering]]) -> file object\n"
2061 "\n"
2062 "Open a file. The mode can be 'r', 'w' or 'a' for reading (default),\n"
2063 "writing or appending. The file will be created if it doesn't exist\n"
2064 "when opened for writing or appending; it will be truncated when\n"
2065 "opened for writing. Add a 'b' to the mode for binary files.\n"
2066 "Add a '+' to the mode to allow simultaneous reading and writing.\n"
2067 "If the buffering argument is given, 0 means unbuffered, 1 means line\n"
2068 "buffered, and larger numbers specify the buffer size. The preferred way\n"
2069 "to open a file is with the builtin open() function.\n"
2071 PyDoc_STR(
2072 "Add a 'U' to mode to open the file for input with universal newline\n"
2073 "support. Any line ending in the input file will be seen as a '\\n'\n"
2074 "in Python. Also, a file so opened gains the attribute 'newlines';\n"
2075 "the value for this attribute is one of None (no newline read yet),\n"
2076 "'\\r', '\\n', '\\r\\n' or a tuple containing all the newline types seen.\n"
2077 "\n"
2078 "'U' cannot be combined with 'w' or '+' mode.\n"
2081 PyTypeObject PyFile_Type = {
2082 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2083 "file",
2084 sizeof(PyFileObject),
2086 (destructor)file_dealloc, /* tp_dealloc */
2087 0, /* tp_print */
2088 0, /* tp_getattr */
2089 0, /* tp_setattr */
2090 0, /* tp_compare */
2091 (reprfunc)file_repr, /* tp_repr */
2092 0, /* tp_as_number */
2093 0, /* tp_as_sequence */
2094 0, /* tp_as_mapping */
2095 0, /* tp_hash */
2096 0, /* tp_call */
2097 0, /* tp_str */
2098 PyObject_GenericGetAttr, /* tp_getattro */
2099 /* softspace is writable: we must supply tp_setattro */
2100 PyObject_GenericSetAttr, /* tp_setattro */
2101 0, /* tp_as_buffer */
2102 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_WEAKREFS, /* tp_flags */
2103 file_doc, /* tp_doc */
2104 0, /* tp_traverse */
2105 0, /* tp_clear */
2106 0, /* tp_richcompare */
2107 offsetof(PyFileObject, weakreflist), /* tp_weaklistoffset */
2108 (getiterfunc)file_self, /* tp_iter */
2109 (iternextfunc)file_iternext, /* tp_iternext */
2110 file_methods, /* tp_methods */
2111 file_memberlist, /* tp_members */
2112 file_getsetlist, /* tp_getset */
2113 0, /* tp_base */
2114 0, /* tp_dict */
2115 0, /* tp_descr_get */
2116 0, /* tp_descr_set */
2117 0, /* tp_dictoffset */
2118 file_init, /* tp_init */
2119 PyType_GenericAlloc, /* tp_alloc */
2120 file_new, /* tp_new */
2121 PyObject_Del, /* tp_free */
2124 /* Interface for the 'soft space' between print items. */
2127 PyFile_SoftSpace(PyObject *f, int newflag)
2129 long oldflag = 0;
2130 if (f == NULL) {
2131 /* Do nothing */
2133 else if (PyFile_Check(f)) {
2134 oldflag = ((PyFileObject *)f)->f_softspace;
2135 ((PyFileObject *)f)->f_softspace = newflag;
2137 else {
2138 PyObject *v;
2139 v = PyObject_GetAttrString(f, "softspace");
2140 if (v == NULL)
2141 PyErr_Clear();
2142 else {
2143 if (PyInt_Check(v))
2144 oldflag = PyInt_AsLong(v);
2145 assert(oldflag < INT_MAX);
2146 Py_DECREF(v);
2148 v = PyInt_FromLong((long)newflag);
2149 if (v == NULL)
2150 PyErr_Clear();
2151 else {
2152 if (PyObject_SetAttrString(f, "softspace", v) != 0)
2153 PyErr_Clear();
2154 Py_DECREF(v);
2157 return (int)oldflag;
2160 /* Interfaces to write objects/strings to file-like objects */
2163 PyFile_WriteObject(PyObject *v, PyObject *f, int flags)
2165 PyObject *writer, *value, *args, *result;
2166 if (f == NULL) {
2167 PyErr_SetString(PyExc_TypeError, "writeobject with NULL file");
2168 return -1;
2170 else if (PyFile_Check(f)) {
2171 FILE *fp = PyFile_AsFile(f);
2172 #ifdef Py_USING_UNICODE
2173 PyObject *enc = ((PyFileObject*)f)->f_encoding;
2174 int result;
2175 #endif
2176 if (fp == NULL) {
2177 err_closed();
2178 return -1;
2180 #ifdef Py_USING_UNICODE
2181 if ((flags & Py_PRINT_RAW) &&
2182 PyUnicode_Check(v) && enc != Py_None) {
2183 char *cenc = PyString_AS_STRING(enc);
2184 value = PyUnicode_AsEncodedString(v, cenc, "strict");
2185 if (value == NULL)
2186 return -1;
2187 } else {
2188 value = v;
2189 Py_INCREF(value);
2191 result = PyObject_Print(value, fp, flags);
2192 Py_DECREF(value);
2193 return result;
2194 #else
2195 return PyObject_Print(v, fp, flags);
2196 #endif
2198 writer = PyObject_GetAttrString(f, "write");
2199 if (writer == NULL)
2200 return -1;
2201 if (flags & Py_PRINT_RAW) {
2202 if (PyUnicode_Check(v)) {
2203 value = v;
2204 Py_INCREF(value);
2205 } else
2206 value = PyObject_Str(v);
2208 else
2209 value = PyObject_Repr(v);
2210 if (value == NULL) {
2211 Py_DECREF(writer);
2212 return -1;
2214 args = PyTuple_Pack(1, value);
2215 if (args == NULL) {
2216 Py_DECREF(value);
2217 Py_DECREF(writer);
2218 return -1;
2220 result = PyEval_CallObject(writer, args);
2221 Py_DECREF(args);
2222 Py_DECREF(value);
2223 Py_DECREF(writer);
2224 if (result == NULL)
2225 return -1;
2226 Py_DECREF(result);
2227 return 0;
2231 PyFile_WriteString(const char *s, PyObject *f)
2233 if (f == NULL) {
2234 /* Should be caused by a pre-existing error */
2235 if (!PyErr_Occurred())
2236 PyErr_SetString(PyExc_SystemError,
2237 "null file for PyFile_WriteString");
2238 return -1;
2240 else if (PyFile_Check(f)) {
2241 FILE *fp = PyFile_AsFile(f);
2242 if (fp == NULL) {
2243 err_closed();
2244 return -1;
2246 Py_BEGIN_ALLOW_THREADS
2247 fputs(s, fp);
2248 Py_END_ALLOW_THREADS
2249 return 0;
2251 else if (!PyErr_Occurred()) {
2252 PyObject *v = PyString_FromString(s);
2253 int err;
2254 if (v == NULL)
2255 return -1;
2256 err = PyFile_WriteObject(v, f, Py_PRINT_RAW);
2257 Py_DECREF(v);
2258 return err;
2260 else
2261 return -1;
2264 /* Try to get a file-descriptor from a Python object. If the object
2265 is an integer or long integer, its value is returned. If not, the
2266 object's fileno() method is called if it exists; the method must return
2267 an integer or long integer, which is returned as the file descriptor value.
2268 -1 is returned on failure.
2271 int PyObject_AsFileDescriptor(PyObject *o)
2273 int fd;
2274 PyObject *meth;
2276 if (PyInt_Check(o)) {
2277 fd = PyInt_AsLong(o);
2279 else if (PyLong_Check(o)) {
2280 fd = PyLong_AsLong(o);
2282 else if ((meth = PyObject_GetAttrString(o, "fileno")) != NULL)
2284 PyObject *fno = PyEval_CallObject(meth, NULL);
2285 Py_DECREF(meth);
2286 if (fno == NULL)
2287 return -1;
2289 if (PyInt_Check(fno)) {
2290 fd = PyInt_AsLong(fno);
2291 Py_DECREF(fno);
2293 else if (PyLong_Check(fno)) {
2294 fd = PyLong_AsLong(fno);
2295 Py_DECREF(fno);
2297 else {
2298 PyErr_SetString(PyExc_TypeError,
2299 "fileno() returned a non-integer");
2300 Py_DECREF(fno);
2301 return -1;
2304 else {
2305 PyErr_SetString(PyExc_TypeError,
2306 "argument must be an int, or have a fileno() method.");
2307 return -1;
2310 if (fd < 0) {
2311 PyErr_Format(PyExc_ValueError,
2312 "file descriptor cannot be a negative integer (%i)",
2313 fd);
2314 return -1;
2316 return fd;
2319 /* From here on we need access to the real fgets and fread */
2320 #undef fgets
2321 #undef fread
2324 ** Py_UniversalNewlineFgets is an fgets variation that understands
2325 ** all of \r, \n and \r\n conventions.
2326 ** The stream should be opened in binary mode.
2327 ** If fobj is NULL the routine always does newline conversion, and
2328 ** it may peek one char ahead to gobble the second char in \r\n.
2329 ** If fobj is non-NULL it must be a PyFileObject. In this case there
2330 ** is no readahead but in stead a flag is used to skip a following
2331 ** \n on the next read. Also, if the file is open in binary mode
2332 ** the whole conversion is skipped. Finally, the routine keeps track of
2333 ** the different types of newlines seen.
2334 ** Note that we need no error handling: fgets() treats error and eof
2335 ** identically.
2337 char *
2338 Py_UniversalNewlineFgets(char *buf, int n, FILE *stream, PyObject *fobj)
2340 char *p = buf;
2341 int c;
2342 int newlinetypes = 0;
2343 int skipnextlf = 0;
2344 int univ_newline = 1;
2346 if (fobj) {
2347 if (!PyFile_Check(fobj)) {
2348 errno = ENXIO; /* What can you do... */
2349 return NULL;
2351 univ_newline = ((PyFileObject *)fobj)->f_univ_newline;
2352 if ( !univ_newline )
2353 return fgets(buf, n, stream);
2354 newlinetypes = ((PyFileObject *)fobj)->f_newlinetypes;
2355 skipnextlf = ((PyFileObject *)fobj)->f_skipnextlf;
2357 FLOCKFILE(stream);
2358 c = 'x'; /* Shut up gcc warning */
2359 while (--n > 0 && (c = GETC(stream)) != EOF ) {
2360 if (skipnextlf ) {
2361 skipnextlf = 0;
2362 if (c == '\n') {
2363 /* Seeing a \n here with skipnextlf true
2364 ** means we saw a \r before.
2366 newlinetypes |= NEWLINE_CRLF;
2367 c = GETC(stream);
2368 if (c == EOF) break;
2369 } else {
2371 ** Note that c == EOF also brings us here,
2372 ** so we're okay if the last char in the file
2373 ** is a CR.
2375 newlinetypes |= NEWLINE_CR;
2378 if (c == '\r') {
2379 /* A \r is translated into a \n, and we skip
2380 ** an adjacent \n, if any. We don't set the
2381 ** newlinetypes flag until we've seen the next char.
2383 skipnextlf = 1;
2384 c = '\n';
2385 } else if ( c == '\n') {
2386 newlinetypes |= NEWLINE_LF;
2388 *p++ = c;
2389 if (c == '\n') break;
2391 if ( c == EOF && skipnextlf )
2392 newlinetypes |= NEWLINE_CR;
2393 FUNLOCKFILE(stream);
2394 *p = '\0';
2395 if (fobj) {
2396 ((PyFileObject *)fobj)->f_newlinetypes = newlinetypes;
2397 ((PyFileObject *)fobj)->f_skipnextlf = skipnextlf;
2398 } else if ( skipnextlf ) {
2399 /* If we have no file object we cannot save the
2400 ** skipnextlf flag. We have to readahead, which
2401 ** will cause a pause if we're reading from an
2402 ** interactive stream, but that is very unlikely
2403 ** unless we're doing something silly like
2404 ** execfile("/dev/tty").
2406 c = GETC(stream);
2407 if ( c != '\n' )
2408 ungetc(c, stream);
2410 if (p == buf)
2411 return NULL;
2412 return buf;
2416 ** Py_UniversalNewlineFread is an fread variation that understands
2417 ** all of \r, \n and \r\n conventions.
2418 ** The stream should be opened in binary mode.
2419 ** fobj must be a PyFileObject. In this case there
2420 ** is no readahead but in stead a flag is used to skip a following
2421 ** \n on the next read. Also, if the file is open in binary mode
2422 ** the whole conversion is skipped. Finally, the routine keeps track of
2423 ** the different types of newlines seen.
2425 size_t
2426 Py_UniversalNewlineFread(char *buf, size_t n,
2427 FILE *stream, PyObject *fobj)
2429 char *dst = buf;
2430 PyFileObject *f = (PyFileObject *)fobj;
2431 int newlinetypes, skipnextlf;
2433 assert(buf != NULL);
2434 assert(stream != NULL);
2436 if (!fobj || !PyFile_Check(fobj)) {
2437 errno = ENXIO; /* What can you do... */
2438 return 0;
2440 if (!f->f_univ_newline)
2441 return fread(buf, 1, n, stream);
2442 newlinetypes = f->f_newlinetypes;
2443 skipnextlf = f->f_skipnextlf;
2444 /* Invariant: n is the number of bytes remaining to be filled
2445 * in the buffer.
2447 while (n) {
2448 size_t nread;
2449 int shortread;
2450 char *src = dst;
2452 nread = fread(dst, 1, n, stream);
2453 assert(nread <= n);
2454 if (nread == 0)
2455 break;
2457 n -= nread; /* assuming 1 byte out for each in; will adjust */
2458 shortread = n != 0; /* true iff EOF or error */
2459 while (nread--) {
2460 char c = *src++;
2461 if (c == '\r') {
2462 /* Save as LF and set flag to skip next LF. */
2463 *dst++ = '\n';
2464 skipnextlf = 1;
2466 else if (skipnextlf && c == '\n') {
2467 /* Skip LF, and remember we saw CR LF. */
2468 skipnextlf = 0;
2469 newlinetypes |= NEWLINE_CRLF;
2470 ++n;
2472 else {
2473 /* Normal char to be stored in buffer. Also
2474 * update the newlinetypes flag if either this
2475 * is an LF or the previous char was a CR.
2477 if (c == '\n')
2478 newlinetypes |= NEWLINE_LF;
2479 else if (skipnextlf)
2480 newlinetypes |= NEWLINE_CR;
2481 *dst++ = c;
2482 skipnextlf = 0;
2485 if (shortread) {
2486 /* If this is EOF, update type flags. */
2487 if (skipnextlf && feof(stream))
2488 newlinetypes |= NEWLINE_CR;
2489 break;
2492 f->f_newlinetypes = newlinetypes;
2493 f->f_skipnextlf = skipnextlf;
2494 return dst - buf;
2497 #ifdef __cplusplus
2499 #endif