Debian Codegraph @

Modules/_io/fileio.c

raw
1 /* Author: Daniel Stutzbach */
2
3 #include "Python.h"
4 #include "pycore_fileutils.h" // _Py_BEGIN_SUPPRESS_IPH
5 #include "pycore_object.h" // _PyObject_GC_UNTRACK()
6 #include "pycore_pyerrors.h" // _PyErr_ChainExceptions1()
7 #include "pycore_weakref.h" // FT_CLEAR_WEAKREFS()
8
9 #include <stdbool.h> // bool
10 #ifdef HAVE_UNISTD_H
11 # include <unistd.h> // lseek()
12 #endif
13 #ifdef HAVE_SYS_TYPES_H
14 # include <sys/types.h>
15 #endif
16 #ifdef HAVE_IO_H
17 # include <io.h>
18 #endif
19 #ifdef HAVE_FCNTL_H
20 # include <fcntl.h> // open()
21 #endif
22
23 #include "_iomodule.h"
24
25 /*
26 * Known likely problems:
27 *
28 * - Files larger then 2**32-1
29 * - Files with unicode filenames
30 * - Passing numbers greater than 2**32-1 when an integer is expected
31 * - Making it work on Windows and other oddball platforms
32 *
33 * To Do:
34 *
35 * - autoconfify header file inclusion
36 */
37
38 #ifdef MS_WINDOWS
39 // can simulate truncate with Win32 API functions; see file_truncate
40 # define HAVE_FTRUNCATE
41 # ifndef WIN32_LEAN_AND_MEAN
42 # define WIN32_LEAN_AND_MEAN
43 # endif
44 # include <windows.h>
45 #endif
46
47 #if BUFSIZ < (8*1024)
48 # define SMALLCHUNK (8*1024)
49 #elif (BUFSIZ >= (2 << 25))
50 # error "unreasonable BUFSIZ > 64 MiB defined"
51 #else
52 # define SMALLCHUNK BUFSIZ
53 #endif
54
55 /* Size at which a buffer is considered "large" and behavior should change to
56 avoid excessive memory allocation */
57 #define LARGE_BUFFER_CUTOFF_SIZE 65536
58
59 /*[clinic input]
60 module _io
61 class _io.FileIO "fileio *" "clinic_state()->PyFileIO_Type"
62 [clinic start generated code]*/
63 /*[clinic end generated code: output=da39a3ee5e6b4b0d input=ac25ec278f4d6703]*/
64
65 typedef struct {
66 PyObject_HEAD
67 int fd;
68 unsigned int created : 1;
69 unsigned int readable : 1;
70 unsigned int writable : 1;
71 unsigned int appending : 1;
72 signed int seekable : 2; /* -1 means unknown */
73 unsigned int closefd : 1;
74 char finalizing;
75 /* Stat result which was grabbed at file open, useful for optimizing common
76 File I/O patterns to be more efficient. This is only guidance / an
77 estimate, as it is subject to Time-Of-Check to Time-Of-Use (TOCTOU)
78 issues / bugs. Both the underlying file descriptor and file may be
79 modified outside of the fileio object / Python (ex. gh-90102, GH-121941,
80 gh-109523). */
81 struct _Py_stat_struct *stat_atopen;
82 PyObject *weakreflist;
83 PyObject *dict;
84 } fileio;
85
86 #define PyFileIO_Check(state, op) (PyObject_TypeCheck((op), state->PyFileIO_Type))
87 #define PyFileIO_CAST(op) ((fileio *)(op))
88
89 /* Forward declarations */
90 static PyObject* portable_lseek(fileio *self, PyObject *posobj, int whence, bool suppress_pipe_error);
91
92 int
93 _PyFileIO_closed(PyObject *self)
94 {
95 return (PyFileIO_CAST(self)->fd < 0);
96 }
97
98 /* Because this can call arbitrary code, it shouldn't be called when
99 the refcount is 0 (that is, not directly from tp_dealloc unless
100 the refcount has been temporarily re-incremented). */
101 static PyObject *
102 fileio_dealloc_warn(PyObject *op, PyObject *source)
103 {
104 fileio *self = PyFileIO_CAST(op);
105 if (self->fd >= 0 && self->closefd) {
106 PyObject *exc = PyErr_GetRaisedException();
107 if (PyErr_ResourceWarning(source, 1, "unclosed file %R", source)) {
108 /* Spurious errors can appear at shutdown */
109 if (PyErr_ExceptionMatches(PyExc_Warning)) {
110 PyErr_FormatUnraisable("Exception ignored "
111 "while finalizing file %R", self);
112 }
113 }
114 PyErr_SetRaisedException(exc);
115 }
116 Py_RETURN_NONE;
117 }
118
119 /* Returns 0 on success, -1 with exception set on failure. */
120 static int
121 internal_close(fileio *self)
122 {
123 int err = 0;
124 int save_errno = 0;
125 if (self->fd >= 0) {
126 int fd = self->fd;
127 self->fd = -1;
128 /* fd is accessible and someone else may have closed it */
129 Py_BEGIN_ALLOW_THREADS
130 _Py_BEGIN_SUPPRESS_IPH
131 err = close(fd);
132 if (err < 0)
133 save_errno = errno;
134 _Py_END_SUPPRESS_IPH
135 Py_END_ALLOW_THREADS
136 }
137 PyMem_Free(self->stat_atopen);
138 self->stat_atopen = NULL;
139 if (err < 0) {
140 errno = save_errno;
141 PyErr_SetFromErrno(PyExc_OSError);
142 return -1;
143 }
144 return 0;
145 }
146
147 /*[clinic input]
148 _io.FileIO.close
149
150 cls: defining_class
151 /
152
153 Close the file.
154
155 A closed file cannot be used for further I/O operations. close() may be
156 called more than once without error.
157 [clinic start generated code]*/
158
159 static PyObject *
160 _io_FileIO_close_impl(fileio *self, PyTypeObject *cls)
161 /*[clinic end generated code: output=c30cbe9d1f23ca58 input=70da49e63db7c64d]*/
162 {
163 PyObject *res;
164 int rc;
165 _PyIO_State *state = get_io_state_by_cls(cls);
166 res = PyObject_CallMethodOneArg((PyObject*)state->PyRawIOBase_Type,
167 &_Py_ID(close), (PyObject *)self);
168 if (!self->closefd) {
169 self->fd = -1;
170 return res;
171 }
172
173 PyObject *exc = NULL;
174 if (res == NULL) {
175 exc = PyErr_GetRaisedException();
176 }
177 if (self->finalizing) {
178 PyObject *r = fileio_dealloc_warn((PyObject*)self, (PyObject *) self);
179 if (r) {
180 Py_DECREF(r);
181 }
182 else {
183 PyErr_Clear();
184 }
185 }
186 rc = internal_close(self);
187 if (res == NULL) {
188 _PyErr_ChainExceptions1(exc);
189 }
190 if (rc < 0) {
191 Py_CLEAR(res);
192 }
193 return res;
194 }
195
196 static PyObject *
197 fileio_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
198 {
199 assert(type != NULL && type->tp_alloc != NULL);
200
201 fileio *self = (fileio *) type->tp_alloc(type, 0);
202 if (self == NULL) {
203 return NULL;
204 }
205
206 self->fd = -1;
207 self->created = 0;
208 self->readable = 0;
209 self->writable = 0;
210 self->appending = 0;
211 self->seekable = -1;
212 self->stat_atopen = NULL;
213 self->closefd = 1;
214 self->weakreflist = NULL;
215 return (PyObject *) self;
216 }
217
218 #ifdef O_CLOEXEC
219 extern int _Py_open_cloexec_works;
220 #endif
221
222 /*[clinic input]
223 _io.FileIO.__init__
224 file as nameobj: object
225 mode: str = "r"
226 closefd: bool = True
227 opener: object = None
228
229 Open a file.
230
231 The mode can be 'r' (default), 'w', 'x' or 'a' for reading,
232 writing, exclusive creation or appending. The file will be created if it
233 doesn't exist when opened for writing or appending; it will be truncated
234 when opened for writing. A FileExistsError will be raised if it already
235 exists when opened for creating. Opening a file for creating implies
236 writing so this mode behaves in a similar way to 'w'.Add a '+' to the mode
237 to allow simultaneous reading and writing. A custom opener can be used by
238 passing a callable as *opener*. The underlying file descriptor for the file
239 object is then obtained by calling opener with (*name*, *flags*).
240 *opener* must return an open file descriptor (passing os.open as *opener*
241 results in functionality similar to passing None).
242 [clinic start generated code]*/
243
244 static int
245 _io_FileIO___init___impl(fileio *self, PyObject *nameobj, const char *mode,
246 int closefd, PyObject *opener)
247 /*[clinic end generated code: output=23413f68e6484bbd input=588aac967e0ba74b]*/
248 {
249 #ifdef MS_WINDOWS
250 wchar_t *widename = NULL;
251 #else
252 const char *name = NULL;
253 #endif
254 PyObject *stringobj = NULL;
255 const char *s;
256 int ret = 0;
257 int rwa = 0, plus = 0;
258 int flags = 0;
259 int fd = -1;
260 int fd_is_own = 0;
261 #ifdef O_CLOEXEC
262 int *atomic_flag_works = &_Py_open_cloexec_works;
263 #elif !defined(MS_WINDOWS)
264 int *atomic_flag_works = NULL;
265 #endif
266 int fstat_result;
267 int async_err = 0;
268
269 #ifdef Py_DEBUG
270 _PyIO_State *state = find_io_state_by_def(Py_TYPE(self));
271 assert(PyFileIO_Check(state, self));
272 #endif
273 if (self->fd >= 0) {
274 if (self->closefd) {
275 /* Have to close the existing file first. */
276 if (internal_close(self) < 0) {
277 return -1;
278 }
279 }
280 else
281 self->fd = -1;
282 }
283
284 if (PyBool_Check(nameobj)) {
285 if (PyErr_WarnEx(PyExc_RuntimeWarning,
286 "bool is used as a file descriptor", 1))
287 {
288 return -1;
289 }
290 }
291 fd = PyLong_AsInt(nameobj);
292 if (fd < 0) {
293 if (!PyErr_Occurred()) {
294 PyErr_SetString(PyExc_ValueError,
295 "negative file descriptor");
296 return -1;
297 }
298 PyErr_Clear();
299 }
300
301 if (fd < 0) {
302 #ifdef MS_WINDOWS
303 if (!PyUnicode_FSDecoder(nameobj, &stringobj)) {
304 return -1;
305 }
306 widename = PyUnicode_AsWideCharString(stringobj, NULL);
307 if (widename == NULL)
308 return -1;
309 #else
310 if (!PyUnicode_FSConverter(nameobj, &stringobj)) {
311 return -1;
312 }
313 name = PyBytes_AS_STRING(stringobj);
314 #endif
315 }
316
317 s = mode;
318 while (*s) {
319 switch (*s++) {
320 case 'x':
321 if (rwa) {
322 bad_mode:
323 PyErr_SetString(PyExc_ValueError,
324 "Must have exactly one of create/read/write/append "
325 "mode and at most one plus");
326 goto error;
327 }
328 rwa = 1;
329 self->created = 1;
330 self->writable = 1;
331 flags |= O_EXCL | O_CREAT;
332 break;
333 case 'r':
334 if (rwa)
335 goto bad_mode;
336 rwa = 1;
337 self->readable = 1;
338 break;
339 case 'w':
340 if (rwa)
341 goto bad_mode;
342 rwa = 1;
343 self->writable = 1;
344 flags |= O_CREAT | O_TRUNC;
345 break;
346 case 'a':
347 if (rwa)
348 goto bad_mode;
349 rwa = 1;
350 self->writable = 1;
351 self->appending = 1;
352 flags |= O_APPEND | O_CREAT;
353 break;
354 case 'b':
355 break;
356 case '+':
357 if (plus)
358 goto bad_mode;
359 self->readable = self->writable = 1;
360 plus = 1;
361 break;
362 default:
363 PyErr_Format(PyExc_ValueError,
364 "invalid mode: %.200s", mode);
365 goto error;
366 }
367 }
368
369 if (!rwa)
370 goto bad_mode;
371
372 if (self->readable && self->writable)
373 flags |= O_RDWR;
374 else if (self->readable)
375 flags |= O_RDONLY;
376 else
377 flags |= O_WRONLY;
378
379 #ifdef O_BINARY
380 flags |= O_BINARY;
381 #endif
382
383 #ifdef MS_WINDOWS
384 flags |= O_NOINHERIT;
385 #elif defined(O_CLOEXEC)
386 flags |= O_CLOEXEC;
387 #endif
388
389 if (PySys_Audit("open", "Osi", nameobj, mode, flags) < 0) {
390 goto error;
391 }
392
393 if (fd >= 0) {
394 self->fd = fd;
395 self->closefd = closefd;
396 }
397 else {
398 self->closefd = 1;
399 if (!closefd) {
400 PyErr_SetString(PyExc_ValueError,
401 "Cannot use closefd=False with file name");
402 goto error;
403 }
404
405 errno = 0;
406 if (opener == Py_None) {
407 do {
408 Py_BEGIN_ALLOW_THREADS
409 #ifdef MS_WINDOWS
410 self->fd = _wopen(widename, flags, 0666);
411 #else
412 self->fd = open(name, flags, 0666);
413 #endif
414 Py_END_ALLOW_THREADS
415 } while (self->fd < 0 && errno == EINTR &&
416 !(async_err = PyErr_CheckSignals()));
417
418 if (async_err)
419 goto error;
420
421 if (self->fd < 0) {
422 PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, nameobj);
423 goto error;
424 }
425 }
426 else {
427 PyObject *fdobj;
428
429 #ifndef MS_WINDOWS
430 /* the opener may clear the atomic flag */
431 atomic_flag_works = NULL;
432 #endif
433
434 fdobj = PyObject_CallFunction(opener, "Oi", nameobj, flags);
435 if (fdobj == NULL)
436 goto error;
437 if (!PyLong_Check(fdobj)) {
438 Py_DECREF(fdobj);
439 PyErr_SetString(PyExc_TypeError,
440 "expected integer from opener");
441 goto error;
442 }
443
444 self->fd = PyLong_AsInt(fdobj);
445 Py_DECREF(fdobj);
446 if (self->fd < 0) {
447 if (!PyErr_Occurred()) {
448 /* The opener returned a negative but didn't set an
449 exception. See issue #27066 */
450 PyErr_Format(PyExc_ValueError,
451 "opener returned %d", self->fd);
452 }
453 goto error;
454 }
455 }
456 fd_is_own = 1;
457
458 #ifndef MS_WINDOWS
459 if (_Py_set_inheritable(self->fd, 0, atomic_flag_works) < 0)
460 goto error;
461 #endif
462 }
463
464 PyMem_Free(self->stat_atopen);
465 self->stat_atopen = PyMem_New(struct _Py_stat_struct, 1);
466 if (self->stat_atopen == NULL) {
467 PyErr_NoMemory();
468 goto error;
469 }
470 Py_BEGIN_ALLOW_THREADS
471 fstat_result = _Py_fstat_noraise(self->fd, self->stat_atopen);
472 Py_END_ALLOW_THREADS
473 if (fstat_result < 0) {
474 /* Tolerate fstat() errors other than EBADF. See Issue #25717, where
475 an anonymous file on a Virtual Box shared folder filesystem would
476 raise ENOENT. */
477 #ifdef MS_WINDOWS
478 if (GetLastError() == ERROR_INVALID_HANDLE) {
479 PyErr_SetFromWindowsErr(0);
480 #else
481 if (errno == EBADF) {
482 PyErr_SetFromErrno(PyExc_OSError);
483 #endif
484 goto error;
485 }
486
487 PyMem_Free(self->stat_atopen);
488 self->stat_atopen = NULL;
489 }
490 else {
491 #if defined(S_ISDIR) && defined(EISDIR)
492 /* On Unix, open will succeed for directories.
493 In Python, there should be no file objects referring to
494 directories, so we need a check. */
495 if (S_ISDIR(self->stat_atopen->st_mode)) {
496 errno = EISDIR;
497 PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, nameobj);
498 goto error;
499 }
500 #endif /* defined(S_ISDIR) */
501 }
502
503 #if defined(MS_WINDOWS) || defined(__CYGWIN__)
504 /* don't translate newlines (\r\n <=> \n) */
505 _setmode(self->fd, O_BINARY);
506 #endif
507
508 if (PyObject_SetAttr((PyObject *)self, &_Py_ID(name), nameobj) < 0)
509 goto error;
510
511 if (self->appending) {
512 /* For consistent behaviour, we explicitly seek to the
513 end of file (otherwise, it might be done only on the
514 first write()). */
515 PyObject *pos = portable_lseek(self, NULL, 2, true);
516 if (pos == NULL)
517 goto error;
518 Py_DECREF(pos);
519 }
520
521 goto done;
522
523 error:
524 ret = -1;
525 if (!fd_is_own)
526 self->fd = -1;
527 if (self->fd >= 0) {
528 PyObject *exc = PyErr_GetRaisedException();
529 internal_close(self);
530 _PyErr_ChainExceptions1(exc);
531 }
532 PyMem_Free(self->stat_atopen);
533 self->stat_atopen = NULL;
534
535 done:
536 #ifdef MS_WINDOWS
537 PyMem_Free(widename);
538 #endif
539 Py_CLEAR(stringobj);
540 return ret;
541 }
542
543 static int
544 fileio_traverse(PyObject *op, visitproc visit, void *arg)
545 {
546 fileio *self = PyFileIO_CAST(op);
547 Py_VISIT(Py_TYPE(self));
548 Py_VISIT(self->dict);
549 return 0;
550 }
551
552 static int
553 fileio_clear(PyObject *op)
554 {
555 fileio *self = PyFileIO_CAST(op);
556 Py_CLEAR(self->dict);
557 return 0;
558 }
559
560 static void
561 fileio_dealloc(PyObject *op)
562 {
563 fileio *self = PyFileIO_CAST(op);
564 self->finalizing = 1;
565 if (_PyIOBase_finalize(op) < 0) {
566 return;
567 }
568
569 _PyObject_GC_UNTRACK(self);
570 if (self->stat_atopen != NULL) {
571 PyMem_Free(self->stat_atopen);
572 self->stat_atopen = NULL;
573 }
574 FT_CLEAR_WEAKREFS(op, self->weakreflist);
575 (void)fileio_clear(op);
576
577 PyTypeObject *tp = Py_TYPE(op);
578 tp->tp_free(op);
579 Py_DECREF(tp);
580 }
581
582 static PyObject *
583 err_closed(void)
584 {
585 PyErr_SetString(PyExc_ValueError, "I/O operation on closed file");
586 return NULL;
587 }
588
589 static PyObject *
590 err_mode(_PyIO_State *state, const char *action)
591 {
592 return PyErr_Format(state->unsupported_operation,
593 "File not open for %s", action);
594 }
595
596 /*[clinic input]
597 _io.FileIO.fileno
598
599 Return the underlying file descriptor (an integer).
600 [clinic start generated code]*/
601
602 static PyObject *
603 _io_FileIO_fileno_impl(fileio *self)
604 /*[clinic end generated code: output=a9626ce5398ece90 input=0b9b2de67335ada3]*/
605 {
606 if (self->fd < 0)
607 return err_closed();
608 return PyLong_FromLong((long) self->fd);
609 }
610
611 /*[clinic input]
612 _io.FileIO.readable
613
614 True if file was opened in a read mode.
615 [clinic start generated code]*/
616
617 static PyObject *
618 _io_FileIO_readable_impl(fileio *self)
619 /*[clinic end generated code: output=640744a6150fe9ba input=a3fdfed6eea721c5]*/
620 {
621 if (self->fd < 0)
622 return err_closed();
623 return PyBool_FromLong((long) self->readable);
624 }
625
626 /*[clinic input]
627 _io.FileIO.writable
628
629 True if file was opened in a write mode.
630 [clinic start generated code]*/
631
632 static PyObject *
633 _io_FileIO_writable_impl(fileio *self)
634 /*[clinic end generated code: output=96cefc5446e89977 input=c204a808ca2e1748]*/
635 {
636 if (self->fd < 0)
637 return err_closed();
638 return PyBool_FromLong((long) self->writable);
639 }
640
641 /*[clinic input]
642 _io.FileIO.seekable
643
644 True if file supports random-access.
645 [clinic start generated code]*/
646
647 static PyObject *
648 _io_FileIO_seekable_impl(fileio *self)
649 /*[clinic end generated code: output=47909ca0a42e9287 input=c8e5554d2fd63c7f]*/
650 {
651 if (self->fd < 0)
652 return err_closed();
653 if (self->seekable < 0) {
654 /* portable_lseek() sets the seekable attribute */
655 PyObject *pos = portable_lseek(self, NULL, SEEK_CUR, false);
656 assert(self->seekable >= 0);
657 if (pos == NULL) {
658 PyErr_Clear();
659 }
660 else {
661 Py_DECREF(pos);
662 }
663 }
664 return PyBool_FromLong((long) self->seekable);
665 }
666
667 /*[clinic input]
668 _io.FileIO.readinto
669 cls: defining_class
670 buffer: Py_buffer(accept={rwbuffer})
671 /
672
673 Same as RawIOBase.readinto().
674 [clinic start generated code]*/
675
676 static PyObject *
677 _io_FileIO_readinto_impl(fileio *self, PyTypeObject *cls, Py_buffer *buffer)
678 /*[clinic end generated code: output=97f0f3d69534db34 input=fd20323e18ce1ec8]*/
679 {
680 Py_ssize_t n;
681 int err;
682
683 if (self->fd < 0)
684 return err_closed();
685 if (!self->readable) {
686 _PyIO_State *state = get_io_state_by_cls(cls);
687 return err_mode(state, "reading");
688 }
689
690 n = _Py_read(self->fd, buffer->buf, buffer->len);
691 /* copy errno because PyBuffer_Release() can indirectly modify it */
692 err = errno;
693
694 if (n == -1) {
695 if (err == EAGAIN) {
696 PyErr_Clear();
697 Py_RETURN_NONE;
698 }
699 return NULL;
700 }
701
702 return PyLong_FromSsize_t(n);
703 }
704
705 static size_t
706 new_buffersize(fileio *self, size_t currentsize)
707 {
708 size_t addend;
709
710 /* Expand the buffer by an amount proportional to the current size,
711 giving us amortized linear-time behavior. For bigger sizes, use a
712 less-than-double growth factor to avoid excessive allocation. */
713 assert(currentsize <= PY_SSIZE_T_MAX);
714 if (currentsize > LARGE_BUFFER_CUTOFF_SIZE)
715 addend = currentsize >> 3;
716 else
717 addend = 256 + currentsize;
718 if (addend < SMALLCHUNK)
719 /* Avoid tiny read() calls. */
720 addend = SMALLCHUNK;
721 return addend + currentsize;
722 }
723
724 /*[clinic input]
725 _io.FileIO.readall
726
727 Read all data from the file, returned as bytes.
728
729 Reads until either there is an error or read() returns size 0 (indicates EOF).
730 If the file is already at EOF, returns an empty bytes object.
731
732 In non-blocking mode, returns as much data as could be read before EAGAIN. If no
733 data is available (EAGAIN is returned before bytes are read) returns None.
734 [clinic start generated code]*/
735
736 static PyObject *
737 _io_FileIO_readall_impl(fileio *self)
738 /*[clinic end generated code: output=faa0292b213b4022 input=1e19849857f5d0a1]*/
739 {
740 Py_off_t pos, end;
741 PyObject *result;
742 Py_ssize_t bytes_read = 0;
743 Py_ssize_t n;
744 size_t bufsize;
745
746 if (self->fd < 0) {
747 return err_closed();
748 }
749
750 if (self->stat_atopen != NULL && self->stat_atopen->st_size < _PY_READ_MAX) {
751 end = (Py_off_t)self->stat_atopen->st_size;
752 }
753 else {
754 end = -1;
755 }
756 if (end <= 0) {
757 /* Use a default size and resize as needed. */
758 bufsize = SMALLCHUNK;
759 }
760 else {
761 /* This is probably a real file. */
762 if (end > _PY_READ_MAX - 1) {
763 bufsize = _PY_READ_MAX;
764 }
765 else {
766 /* In order to detect end of file, need a read() of at
767 least 1 byte which returns size 0. Oversize the buffer
768 by 1 byte so the I/O can be completed with two read()
769 calls (one for all data, one for EOF) without needing
770 to resize the buffer. */
771 bufsize = (size_t)end + 1;
772 }
773
774 /* While a lot of code does open().read() to get the whole contents
775 of a file it is possible a caller seeks/reads a ways into the file
776 then calls readall() to get the rest, which would result in allocating
777 more than required. Guard against that for larger files where we expect
778 the I/O time to dominate anyways while keeping small files fast. */
779 if (bufsize > LARGE_BUFFER_CUTOFF_SIZE) {
780 Py_BEGIN_ALLOW_THREADS
781 _Py_BEGIN_SUPPRESS_IPH
782 #ifdef MS_WINDOWS
783 pos = _lseeki64(self->fd, 0L, SEEK_CUR);
784 #else
785 pos = lseek(self->fd, 0L, SEEK_CUR);
786 #endif
787 _Py_END_SUPPRESS_IPH
788 Py_END_ALLOW_THREADS
789
790 if (end >= pos && pos >= 0 && (end - pos) < (_PY_READ_MAX - 1)) {
791 bufsize = (size_t)(end - pos) + 1;
792 }
793 }
794 }
795
796
797 result = PyBytes_FromStringAndSize(NULL, bufsize);
798 if (result == NULL)
799 return NULL;
800
801 while (1) {
802 if (bytes_read >= (Py_ssize_t)bufsize) {
803 bufsize = new_buffersize(self, bytes_read);
804 if (bufsize > PY_SSIZE_T_MAX || bufsize <= 0) {
805 PyErr_SetString(PyExc_OverflowError,
806 "unbounded read returned more bytes "
807 "than a Python bytes object can hold");
808 Py_DECREF(result);
809 return NULL;
810 }
811
812 if (PyBytes_GET_SIZE(result) < (Py_ssize_t)bufsize) {
813 if (_PyBytes_Resize(&result, bufsize) < 0)
814 return NULL;
815 }
816 }
817
818 n = _Py_read(self->fd,
819 PyBytes_AS_STRING(result) + bytes_read,
820 bufsize - bytes_read);
821
822 if (n == 0)
823 break;
824 if (n == -1) {
825 if (errno == EAGAIN) {
826 PyErr_Clear();
827 if (bytes_read > 0)
828 break;
829 Py_DECREF(result);
830 Py_RETURN_NONE;
831 }
832 Py_DECREF(result);
833 return NULL;
834 }
835 bytes_read += n;
836 }
837
838 if (PyBytes_GET_SIZE(result) > bytes_read) {
839 if (_PyBytes_Resize(&result, bytes_read) < 0)
840 return NULL;
841 }
842 return result;
843 }
844
845 /*[clinic input]
846 _io.FileIO.read
847 cls: defining_class
848 size: Py_ssize_t(accept={int, NoneType}) = -1
849 /
850
851 Read at most size bytes, returned as bytes.
852
853 If size is less than 0, read all bytes in the file making multiple read calls.
854 See ``FileIO.readall``.
855
856 Attempts to make only one system call, retrying only per PEP 475 (EINTR). This
857 means less data may be returned than requested.
858
859 In non-blocking mode, returns None if no data is available. Return an empty
860 bytes object at EOF.
861 [clinic start generated code]*/
862
863 static PyObject *
864 _io_FileIO_read_impl(fileio *self, PyTypeObject *cls, Py_ssize_t size)
865 /*[clinic end generated code: output=bbd749c7c224143e input=cf21fddef7d38ab6]*/
866 {
867 char *ptr;
868 Py_ssize_t n;
869 PyObject *bytes;
870
871 if (self->fd < 0)
872 return err_closed();
873 if (!self->readable) {
874 _PyIO_State *state = get_io_state_by_cls(cls);
875 return err_mode(state, "reading");
876 }
877
878 if (size < 0)
879 return _io_FileIO_readall_impl(self);
880
881 if (size > _PY_READ_MAX) {
882 size = _PY_READ_MAX;
883 }
884
885 bytes = PyBytes_FromStringAndSize(NULL, size);
886 if (bytes == NULL)
887 return NULL;
888 ptr = PyBytes_AS_STRING(bytes);
889
890 n = _Py_read(self->fd, ptr, size);
891 if (n == -1) {
892 /* copy errno because Py_DECREF() can indirectly modify it */
893 int err = errno;
894 Py_DECREF(bytes);
895 if (err == EAGAIN) {
896 PyErr_Clear();
897 Py_RETURN_NONE;
898 }
899 return NULL;
900 }
901
902 if (n != size) {
903 if (_PyBytes_Resize(&bytes, n) < 0) {
904 Py_CLEAR(bytes);
905 return NULL;
906 }
907 }
908
909 return (PyObject *) bytes;
910 }
911
912 /*[clinic input]
913 _io.FileIO.write
914 cls: defining_class
915 b: Py_buffer
916 /
917
918 Write buffer b to file, return number of bytes written.
919
920 Only makes one system call, so not all of the data may be written.
921 The number of bytes actually written is returned. In non-blocking mode,
922 returns None if the write would block.
923 [clinic start generated code]*/
924
925 static PyObject *
926 _io_FileIO_write_impl(fileio *self, PyTypeObject *cls, Py_buffer *b)
927 /*[clinic end generated code: output=927e25be80f3b77b input=2776314f043088f5]*/
928 {
929 Py_ssize_t n;
930 int err;
931
932 if (self->fd < 0)
933 return err_closed();
934 if (!self->writable) {
935 _PyIO_State *state = get_io_state_by_cls(cls);
936 return err_mode(state, "writing");
937 }
938
939 n = _Py_write(self->fd, b->buf, b->len);
940 /* copy errno because PyBuffer_Release() can indirectly modify it */
941 err = errno;
942
943 if (n < 0) {
944 if (err == EAGAIN) {
945 PyErr_Clear();
946 Py_RETURN_NONE;
947 }
948 return NULL;
949 }
950
951 return PyLong_FromSsize_t(n);
952 }
953
954 /* XXX Windows support below is likely incomplete */
955
956 /* Cribbed from posix_lseek() */
957 static PyObject *
958 portable_lseek(fileio *self, PyObject *posobj, int whence, bool suppress_pipe_error)
959 {
960 Py_off_t pos, res;
961 int fd = self->fd;
962
963 #ifdef SEEK_SET
964 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
965 switch (whence) {
966 #if SEEK_SET != 0
967 case 0: whence = SEEK_SET; break;
968 #endif
969 #if SEEK_CUR != 1
970 case 1: whence = SEEK_CUR; break;
971 #endif
972 #if SEEK_END != 2
973 case 2: whence = SEEK_END; break;
974 #endif
975 }
976 #endif /* SEEK_SET */
977
978 if (posobj == NULL) {
979 pos = 0;
980 }
981 else {
982 #if defined(HAVE_LARGEFILE_SUPPORT)
983 pos = PyLong_AsLongLong(posobj);
984 #else
985 pos = PyLong_AsLong(posobj);
986 #endif
987 if (PyErr_Occurred())
988 return NULL;
989 }
990
991 Py_BEGIN_ALLOW_THREADS
992 _Py_BEGIN_SUPPRESS_IPH
993 #ifdef MS_WINDOWS
994 res = _lseeki64(fd, pos, whence);
995 #else
996 res = lseek(fd, pos, whence);
997 #endif
998 _Py_END_SUPPRESS_IPH
999 Py_END_ALLOW_THREADS
1000
1001 if (self->seekable < 0) {
1002 self->seekable = (res >= 0);
1003 }
1004
1005 if (res < 0) {
1006 if (suppress_pipe_error && errno == ESPIPE) {
1007 res = 0;
1008 } else {
1009 return PyErr_SetFromErrno(PyExc_OSError);
1010 }
1011 }
1012
1013 #if defined(HAVE_LARGEFILE_SUPPORT)
1014 return PyLong_FromLongLong(res);
1015 #else
1016 return PyLong_FromLong(res);
1017 #endif
1018 }
1019
1020 /*[clinic input]
1021 _io.FileIO.seek
1022 pos: object
1023 whence: int = 0
1024 /
1025
1026 Move to new file position and return the file position.
1027
1028 Argument offset is a byte count. Optional argument whence defaults to
1029 SEEK_SET or 0 (offset from start of file, offset should be >= 0); other values
1030 are SEEK_CUR or 1 (move relative to current position, positive or negative),
1031 and SEEK_END or 2 (move relative to end of file, usually negative, although
1032 many platforms allow seeking beyond the end of a file).
1033
1034 Note that not all file objects are seekable.
1035 [clinic start generated code]*/
1036
1037 static PyObject *
1038 _io_FileIO_seek_impl(fileio *self, PyObject *pos, int whence)
1039 /*[clinic end generated code: output=c976acdf054e6655 input=0439194b0774d454]*/
1040 {
1041 if (self->fd < 0)
1042 return err_closed();
1043
1044 return portable_lseek(self, pos, whence, false);
1045 }
1046
1047 /*[clinic input]
1048 _io.FileIO.tell
1049
1050 Current file position.
1051
1052 Can raise OSError for non seekable files.
1053 [clinic start generated code]*/
1054
1055 static PyObject *
1056 _io_FileIO_tell_impl(fileio *self)
1057 /*[clinic end generated code: output=ffe2147058809d0b input=807e24ead4cec2f9]*/
1058 {
1059 if (self->fd < 0)
1060 return err_closed();
1061
1062 return portable_lseek(self, NULL, 1, false);
1063 }
1064
1065 #ifdef HAVE_FTRUNCATE
1066 /*[clinic input]
1067 _io.FileIO.truncate
1068 cls: defining_class
1069 size as posobj: object = None
1070 /
1071
1072 Truncate the file to at most size bytes and return the truncated size.
1073
1074 Size defaults to the current file position, as returned by tell().
1075 The current file position is changed to the value of size.
1076 [clinic start generated code]*/
1077
1078 static PyObject *
1079 _io_FileIO_truncate_impl(fileio *self, PyTypeObject *cls, PyObject *posobj)
1080 /*[clinic end generated code: output=d936732a49e8d5a2 input=c367fb45d6bb2c18]*/
1081 {
1082 Py_off_t pos;
1083 int ret;
1084 int fd;
1085
1086 fd = self->fd;
1087 if (fd < 0)
1088 return err_closed();
1089 if (!self->writable) {
1090 _PyIO_State *state = get_io_state_by_cls(cls);
1091 return err_mode(state, "writing");
1092 }
1093
1094 if (posobj == Py_None) {
1095 /* Get the current position. */
1096 posobj = portable_lseek(self, NULL, 1, false);
1097 if (posobj == NULL)
1098 return NULL;
1099 }
1100 else {
1101 Py_INCREF(posobj);
1102 }
1103
1104 #if defined(HAVE_LARGEFILE_SUPPORT)
1105 pos = PyLong_AsLongLong(posobj);
1106 #else
1107 pos = PyLong_AsLong(posobj);
1108 #endif
1109 if (PyErr_Occurred()){
1110 Py_DECREF(posobj);
1111 return NULL;
1112 }
1113
1114 Py_BEGIN_ALLOW_THREADS
1115 _Py_BEGIN_SUPPRESS_IPH
1116 errno = 0;
1117 #ifdef MS_WINDOWS
1118 ret = _chsize_s(fd, pos);
1119 #else
1120 ret = ftruncate(fd, pos);
1121 #endif
1122 _Py_END_SUPPRESS_IPH
1123 Py_END_ALLOW_THREADS
1124
1125 if (ret != 0) {
1126 PyErr_SetFromErrno(PyExc_OSError);
1127 Py_DECREF(posobj);
1128 return NULL;
1129 }
1130
1131 /* Since the file was truncated, its size at open is no longer accurate
1132 as an estimate. Clear out the stat result, and rely on dynamic resize
1133 code if a readall is requested. */
1134 if (self->stat_atopen != NULL) {
1135 PyMem_Free(self->stat_atopen);
1136 self->stat_atopen = NULL;
1137 }
1138
1139 return posobj;
1140 }
1141 #endif /* HAVE_FTRUNCATE */
1142
1143 static const char *
1144 mode_string(fileio *self)
1145 {
1146 if (self->created) {
1147 if (self->readable)
1148 return "xb+";
1149 else
1150 return "xb";
1151 }
1152 if (self->appending) {
1153 if (self->readable)
1154 return "ab+";
1155 else
1156 return "ab";
1157 }
1158 else if (self->readable) {
1159 if (self->writable)
1160 return "rb+";
1161 else
1162 return "rb";
1163 }
1164 else
1165 return "wb";
1166 }
1167
1168 static PyObject *
1169 fileio_repr(PyObject *op)
1170 {
1171 fileio *self = PyFileIO_CAST(op);
1172 const char *type_name = Py_TYPE(self)->tp_name;
1173
1174 if (self->fd < 0) {
1175 return PyUnicode_FromFormat("<%.100s [closed]>", type_name);
1176 }
1177
1178 PyObject *nameobj;
1179 if (PyObject_GetOptionalAttr((PyObject *) self, &_Py_ID(name), &nameobj) < 0) {
1180 return NULL;
1181 }
1182 PyObject *res;
1183 if (nameobj == NULL) {
1184 res = PyUnicode_FromFormat(
1185 "<%.100s fd=%d mode='%s' closefd=%s>",
1186 type_name, self->fd, mode_string(self), self->closefd ? "True" : "False");
1187 }
1188 else {
1189 int status = Py_ReprEnter((PyObject *)self);
1190 res = NULL;
1191 if (status == 0) {
1192 res = PyUnicode_FromFormat(
1193 "<%.100s name=%R mode='%s' closefd=%s>",
1194 type_name, nameobj, mode_string(self), self->closefd ? "True" : "False");
1195 Py_ReprLeave((PyObject *)self);
1196 }
1197 else if (status > 0) {
1198 PyErr_Format(PyExc_RuntimeError,
1199 "reentrant call inside %.100s.__repr__", type_name);
1200 }
1201 Py_DECREF(nameobj);
1202 }
1203 return res;
1204 }
1205
1206 /*[clinic input]
1207 _io.FileIO.isatty
1208
1209 True if the file is connected to a TTY device.
1210 [clinic start generated code]*/
1211
1212 static PyObject *
1213 _io_FileIO_isatty_impl(fileio *self)
1214 /*[clinic end generated code: output=932c39924e9a8070 input=cd94ca1f5e95e843]*/
1215 {
1216 long res;
1217
1218 if (self->fd < 0)
1219 return err_closed();
1220 Py_BEGIN_ALLOW_THREADS
1221 _Py_BEGIN_SUPPRESS_IPH
1222 res = isatty(self->fd);
1223 _Py_END_SUPPRESS_IPH
1224 Py_END_ALLOW_THREADS
1225 return PyBool_FromLong(res);
1226 }
1227
1228 /* Checks whether the file is a TTY using an open-only optimization.
1229
1230 TTYs are always character devices. If the interpreter knows a file is
1231 not a character device when it would call ``isatty``, can skip that
1232 call. Inside ``open()`` there is a fresh stat result that contains that
1233 information. Use the stat result to skip a system call. Outside of that
1234 context TOCTOU issues (the fd could be arbitrarily modified by
1235 surrounding code). */
1236 static PyObject *
1237 _io_FileIO_isatty_open_only(PyObject *op, PyObject *Py_UNUSED(dummy))
1238 {
1239 fileio *self = PyFileIO_CAST(op);
1240 if (self->stat_atopen != NULL && !S_ISCHR(self->stat_atopen->st_mode)) {
1241 Py_RETURN_FALSE;
1242 }
1243 return _io_FileIO_isatty_impl(self);
1244 }
1245
1246 #include "clinic/fileio.c.h"
1247
1248 static PyMethodDef fileio_methods[] = {
1249 _IO_FILEIO_READ_METHODDEF
1250 _IO_FILEIO_READALL_METHODDEF
1251 _IO_FILEIO_READINTO_METHODDEF
1252 _IO_FILEIO_WRITE_METHODDEF
1253 _IO_FILEIO_SEEK_METHODDEF
1254 _IO_FILEIO_TELL_METHODDEF
1255 _IO_FILEIO_TRUNCATE_METHODDEF
1256 _IO_FILEIO_CLOSE_METHODDEF
1257 _IO_FILEIO_SEEKABLE_METHODDEF
1258 _IO_FILEIO_READABLE_METHODDEF
1259 _IO_FILEIO_WRITABLE_METHODDEF
1260 _IO_FILEIO_FILENO_METHODDEF
1261 _IO_FILEIO_ISATTY_METHODDEF
1262 {"_isatty_open_only", _io_FileIO_isatty_open_only, METH_NOARGS},
1263 {"_dealloc_warn", fileio_dealloc_warn, METH_O, NULL},
1264 {"__getstate__", _PyIOBase_cannot_pickle, METH_NOARGS},
1265 {NULL, NULL} /* sentinel */
1266 };
1267
1268 /* 'closed' and 'mode' are attributes for backwards compatibility reasons. */
1269
1270 static PyObject *
1271 fileio_get_closed(PyObject *op, void *closure)
1272 {
1273 fileio *self = PyFileIO_CAST(op);
1274 return PyBool_FromLong((long)(self->fd < 0));
1275 }
1276
1277 static PyObject *
1278 fileio_get_closefd(PyObject *op, void *closure)
1279 {
1280 fileio *self = PyFileIO_CAST(op);
1281 return PyBool_FromLong((long)(self->closefd));
1282 }
1283
1284 static PyObject *
1285 fileio_get_mode(PyObject *op, void *closure)
1286 {
1287 fileio *self = PyFileIO_CAST(op);
1288 return PyUnicode_FromString(mode_string(self));
1289 }
1290
1291 static PyObject *
1292 fileio_get_blksize(PyObject *op, void *closure)
1293 {
1294 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
1295 fileio *self = PyFileIO_CAST(op);
1296 if (self->stat_atopen != NULL && self->stat_atopen->st_blksize > 1) {
1297 return PyLong_FromLong(self->stat_atopen->st_blksize);
1298 }
1299 #endif /* HAVE_STRUCT_STAT_ST_BLKSIZE */
1300 return PyLong_FromLong(DEFAULT_BUFFER_SIZE);
1301 }
1302
1303 static PyGetSetDef fileio_getsetlist[] = {
1304 {"closed", fileio_get_closed, NULL, "True if the file is closed"},
1305 {"closefd", fileio_get_closefd, NULL,
1306 "True if the file descriptor will be closed by close()."},
1307 {"mode", fileio_get_mode, NULL, "String giving the file mode"},
1308 {"_blksize", fileio_get_blksize, NULL, "Stat st_blksize if available"},
1309 {NULL},
1310 };
1311
1312 static PyMemberDef fileio_members[] = {
1313 {"_finalizing", Py_T_BOOL, offsetof(fileio, finalizing), 0},
1314 {"__weaklistoffset__", Py_T_PYSSIZET, offsetof(fileio, weakreflist), Py_READONLY},
1315 {"__dictoffset__", Py_T_PYSSIZET, offsetof(fileio, dict), Py_READONLY},
1316 {NULL}
1317 };
1318
1319 static PyType_Slot fileio_slots[] = {
1320 {Py_tp_dealloc, fileio_dealloc},
1321 {Py_tp_repr, fileio_repr},
1322 {Py_tp_doc, (void *)_io_FileIO___init____doc__},
1323 {Py_tp_traverse, fileio_traverse},
1324 {Py_tp_clear, fileio_clear},
1325 {Py_tp_methods, fileio_methods},
1326 {Py_tp_members, fileio_members},
1327 {Py_tp_getset, fileio_getsetlist},
1328 {Py_tp_init, _io_FileIO___init__},
1329 {Py_tp_new, fileio_new},
1330 {0, NULL},
1331 };
1332
1333 PyType_Spec fileio_spec = {
1334 .name = "_io.FileIO",
1335 .basicsize = sizeof(fileio),
1336 .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC |
1337 Py_TPFLAGS_IMMUTABLETYPE),
1338 .slots = fileio_slots,
1339 };