1 /* Author: Daniel Stutzbach */
3 #define PY_SSIZE_T_CLEAN
8 #include <stddef.h> /* For offsetof */
12 * Known likely problems:
14 * - Files larger then 2**32-1
15 * - Files with unicode filenames
16 * - Passing numbers greater than 2**32-1 when an integer is expected
17 * - Making it work on Windows and other oddball platforms
21 * - autoconfify header file inclusion
25 /* can simulate truncate with Win32 API functions; see file_truncate */
26 #define HAVE_FTRUNCATE
27 #define WIN32_LEAN_AND_MEAN
32 #define SMALLCHUNK (8*1024)
33 #elif (BUFSIZ >= (2 << 25))
34 #error "unreasonable BUFSIZ > 64MB defined"
36 #define SMALLCHUNK BUFSIZ
40 #define BIGCHUNK (512 * 32)
42 #define BIGCHUNK (512 * 1024)
48 unsigned int readable
: 1;
49 unsigned int writable
: 1;
50 signed int seekable
: 2; /* -1 means unknown */
51 unsigned int closefd
: 1;
52 PyObject
*weakreflist
;
56 PyTypeObject PyFileIO_Type
;
58 #define PyFileIO_Check(op) (PyObject_TypeCheck((op), &PyFileIO_Type))
61 _PyFileIO_closed(PyObject
*self
)
63 return ((fileio
*)self
)->fd
< 0;
67 portable_lseek(int fd
, PyObject
*posobj
, int whence
);
69 static PyObject
*portable_lseek(int fd
, PyObject
*posobj
, int whence
);
71 /* Returns 0 on success, -1 with exception set on failure. */
73 internal_close(fileio
*self
)
80 /* fd is accessible and someone else may have closed it */
81 if (_PyVerify_fd(fd
)) {
82 Py_BEGIN_ALLOW_THREADS
94 PyErr_SetFromErrno(PyExc_IOError
);
101 fileio_close(fileio
*self
)
103 if (!self
->closefd
) {
107 errno
= internal_close(self
);
111 return PyObject_CallMethod((PyObject
*)&PyRawIOBase_Type
,
116 fileio_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
120 assert(type
!= NULL
&& type
->tp_alloc
!= NULL
);
122 self
= (fileio
*) type
->tp_alloc(type
, 0);
129 self
->weakreflist
= NULL
;
132 return (PyObject
*) self
;
135 /* On Unix, open will succeed for directories.
136 In Python, there should be no file objects referring to
137 directories, so we need a check. */
140 dircheck(fileio
* self
, const char *name
)
142 #if defined(HAVE_FSTAT) && defined(S_IFDIR) && defined(EISDIR)
146 if (fstat(self
->fd
, &buf
) == 0 && S_ISDIR(buf
.st_mode
)) {
147 char *msg
= strerror(EISDIR
);
149 if (internal_close(self
))
152 exc
= PyObject_CallFunction(PyExc_IOError
, "(iss)",
154 PyErr_SetObject(PyExc_IOError
, exc
);
165 #if defined(HAVE_FSTAT)
167 if (!_PyVerify_fd(fd
) || (fstat(fd
, &buf
) < 0 && errno
== EBADF
)) {
169 char *msg
= strerror(EBADF
);
170 exc
= PyObject_CallFunction(PyExc_OSError
, "(is)",
172 PyErr_SetObject(PyExc_OSError
, exc
);
182 fileio_init(PyObject
*oself
, PyObject
*args
, PyObject
*kwds
)
184 fileio
*self
= (fileio
*) oself
;
185 static char *kwlist
[] = {"file", "mode", "closefd", NULL
};
186 const char *name
= NULL
;
187 PyObject
*nameobj
, *stringobj
= NULL
;
191 Py_UNICODE
*widename
= NULL
;
194 int rwa
= 0, plus
= 0, append
= 0;
199 assert(PyFileIO_Check(oself
));
201 /* Have to close the existing file first. */
202 if (internal_close(self
) < 0)
206 if (!PyArg_ParseTupleAndKeywords(args
, kwds
, "O|si:fileio",
207 kwlist
, &nameobj
, &mode
, &closefd
))
210 if (PyFloat_Check(nameobj
)) {
211 PyErr_SetString(PyExc_TypeError
,
212 "integer argument expected, got float");
216 fd
= PyLong_AsLong(nameobj
);
218 if (!PyErr_Occurred()) {
219 PyErr_SetString(PyExc_ValueError
,
220 "Negative filedescriptor");
227 if (PyUnicode_Check(nameobj
))
228 widename
= PyUnicode_AS_UNICODE(nameobj
);
229 if (widename
== NULL
)
233 if (PyBytes_Check(nameobj
) || PyByteArray_Check(nameobj
)) {
235 if (PyObject_AsCharBuffer(nameobj
, &name
, &namelen
) < 0)
239 PyObject
*u
= PyUnicode_FromObject(nameobj
);
244 stringobj
= PyUnicode_AsEncodedString(
245 u
, Py_FileSystemDefaultEncoding
, "surrogateescape");
247 if (stringobj
== NULL
)
249 if (!PyBytes_Check(stringobj
)) {
250 PyErr_SetString(PyExc_TypeError
,
251 "encoder failed to return bytes");
254 name
= PyBytes_AS_STRING(stringobj
);
264 PyErr_SetString(PyExc_ValueError
,
265 "Must have exactly one of read/write/append mode");
276 flags
|= O_CREAT
| O_TRUNC
;
291 self
->readable
= self
->writable
= 1;
295 PyErr_Format(PyExc_ValueError
,
296 "invalid mode: %.200s", mode
);
304 if (self
->readable
&& self
->writable
)
306 else if (self
->readable
)
324 self
->closefd
= closefd
;
329 PyErr_SetString(PyExc_ValueError
,
330 "Cannot use closefd=False with file name");
334 Py_BEGIN_ALLOW_THREADS
337 if (widename
!= NULL
)
338 self
->fd
= _wopen(widename
, flags
, 0666);
341 self
->fd
= open(name
, flags
, 0666);
345 if (widename
!= NULL
)
346 PyErr_SetFromErrnoWithUnicodeFilename(PyExc_IOError
, widename
);
349 PyErr_SetFromErrnoWithFilename(PyExc_IOError
, name
);
352 if(dircheck(self
, name
) < 0)
356 if (PyObject_SetAttrString((PyObject
*)self
, "name", nameobj
) < 0)
360 /* For consistent behaviour, we explicitly seek to the
361 end of file (otherwise, it might be done only on the
363 PyObject
*pos
= portable_lseek(self
->fd
, NULL
, 2);
380 fileio_traverse(fileio
*self
, visitproc visit
, void *arg
)
382 Py_VISIT(self
->dict
);
387 fileio_clear(fileio
*self
)
389 Py_CLEAR(self
->dict
);
394 fileio_dealloc(fileio
*self
)
396 if (_PyIOBase_finalize((PyObject
*) self
) < 0)
398 _PyObject_GC_UNTRACK(self
);
399 if (self
->weakreflist
!= NULL
)
400 PyObject_ClearWeakRefs((PyObject
*) self
);
401 Py_CLEAR(self
->dict
);
402 Py_TYPE(self
)->tp_free((PyObject
*)self
);
408 PyErr_SetString(PyExc_ValueError
, "I/O operation on closed file");
413 err_mode(char *action
)
415 PyErr_Format(PyExc_ValueError
, "File not open for %s", action
);
420 fileio_fileno(fileio
*self
)
424 return PyInt_FromLong((long) self
->fd
);
428 fileio_readable(fileio
*self
)
432 return PyBool_FromLong((long) self
->readable
);
436 fileio_writable(fileio
*self
)
440 return PyBool_FromLong((long) self
->writable
);
444 fileio_seekable(fileio
*self
)
448 if (self
->seekable
< 0) {
449 PyObject
*pos
= portable_lseek(self
->fd
, NULL
, SEEK_CUR
);
458 return PyBool_FromLong((long) self
->seekable
);
462 fileio_readinto(fileio
*self
, PyObject
*args
)
470 return err_mode("reading");
472 if (!PyArg_ParseTuple(args
, "w*", &pbuf
))
475 if (_PyVerify_fd(self
->fd
)) {
476 Py_BEGIN_ALLOW_THREADS
478 n
= read(self
->fd
, pbuf
.buf
, pbuf
.len
);
482 PyBuffer_Release(&pbuf
);
486 PyErr_SetFromErrno(PyExc_IOError
);
490 return PyLong_FromSsize_t(n
);
494 new_buffersize(fileio
*self
, size_t currentsize
)
499 if (fstat(self
->fd
, &st
) == 0) {
501 pos
= lseek(self
->fd
, 0L, SEEK_CUR
);
502 /* Files claiming a size smaller than SMALLCHUNK may
503 actually be streaming pseudo-files. In this case, we
504 apply the more aggressive algorithm below.
506 if (end
>= SMALLCHUNK
&& end
>= pos
&& pos
>= 0) {
507 /* Add 1 so if the file were to grow we'd notice. */
508 return currentsize
+ end
- pos
+ 1;
512 if (currentsize
> SMALLCHUNK
) {
513 /* Keep doubling until we reach BIGCHUNK;
514 then keep adding BIGCHUNK. */
515 if (currentsize
<= BIGCHUNK
)
516 return currentsize
+ currentsize
;
518 return currentsize
+ BIGCHUNK
;
520 return currentsize
+ SMALLCHUNK
;
524 fileio_readall(fileio
*self
)
527 Py_ssize_t total
= 0;
530 if (!_PyVerify_fd(self
->fd
))
531 return PyErr_SetFromErrno(PyExc_IOError
);
533 result
= PyBytes_FromStringAndSize(NULL
, SMALLCHUNK
);
538 size_t newsize
= new_buffersize(self
, total
);
539 if (newsize
> PY_SSIZE_T_MAX
|| newsize
<= 0) {
540 PyErr_SetString(PyExc_OverflowError
,
541 "unbounded read returned more bytes "
542 "than a Python string can hold ");
547 if (PyBytes_GET_SIZE(result
) < (Py_ssize_t
)newsize
) {
548 if (_PyBytes_Resize(&result
, newsize
) < 0) {
557 Py_BEGIN_ALLOW_THREADS
560 PyBytes_AS_STRING(result
) + total
,
568 if (errno
== EAGAIN
) {
573 PyErr_SetFromErrno(PyExc_IOError
);
579 if (PyBytes_GET_SIZE(result
) > total
) {
580 if (_PyBytes_Resize(&result
, total
) < 0) {
581 /* This should never happen, but just in case */
590 fileio_read(fileio
*self
, PyObject
*args
)
594 Py_ssize_t size
= -1;
600 return err_mode("reading");
602 if (!PyArg_ParseTuple(args
, "|n", &size
))
606 return fileio_readall(self
);
609 bytes
= PyBytes_FromStringAndSize(NULL
, size
);
612 ptr
= PyBytes_AS_STRING(bytes
);
614 if (_PyVerify_fd(self
->fd
)) {
615 Py_BEGIN_ALLOW_THREADS
617 n
= read(self
->fd
, ptr
, size
);
626 PyErr_SetFromErrno(PyExc_IOError
);
631 if (_PyBytes_Resize(&bytes
, n
) < 0) {
637 return (PyObject
*) bytes
;
641 fileio_write(fileio
*self
, PyObject
*args
)
649 return err_mode("writing");
651 if (!PyArg_ParseTuple(args
, "s*", &pbuf
))
654 if (_PyVerify_fd(self
->fd
)) {
655 Py_BEGIN_ALLOW_THREADS
657 n
= write(self
->fd
, pbuf
.buf
, pbuf
.len
);
662 PyBuffer_Release(&pbuf
);
667 PyErr_SetFromErrno(PyExc_IOError
);
671 return PyLong_FromSsize_t(n
);
674 /* XXX Windows support below is likely incomplete */
676 /* Cribbed from posix_lseek() */
678 portable_lseek(int fd
, PyObject
*posobj
, int whence
)
683 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
686 case 0: whence
= SEEK_SET
; break;
689 case 1: whence
= SEEK_CUR
; break;
692 case 2: whence
= SEEK_END
; break;
695 #endif /* SEEK_SET */
700 if(PyFloat_Check(posobj
)) {
701 PyErr_SetString(PyExc_TypeError
, "an integer is required");
704 #if defined(HAVE_LARGEFILE_SUPPORT)
705 pos
= PyLong_AsLongLong(posobj
);
707 pos
= PyLong_AsLong(posobj
);
709 if (PyErr_Occurred())
713 if (_PyVerify_fd(fd
)) {
714 Py_BEGIN_ALLOW_THREADS
715 #if defined(MS_WIN64) || defined(MS_WINDOWS)
716 res
= _lseeki64(fd
, pos
, whence
);
718 res
= lseek(fd
, pos
, whence
);
724 return PyErr_SetFromErrno(PyExc_IOError
);
726 #if defined(HAVE_LARGEFILE_SUPPORT)
727 return PyLong_FromLongLong(res
);
729 return PyLong_FromLong(res
);
734 fileio_seek(fileio
*self
, PyObject
*args
)
742 if (!PyArg_ParseTuple(args
, "O|i", &posobj
, &whence
))
745 return portable_lseek(self
->fd
, posobj
, whence
);
749 fileio_tell(fileio
*self
, PyObject
*args
)
754 return portable_lseek(self
->fd
, NULL
, 1);
757 #ifdef HAVE_FTRUNCATE
759 fileio_truncate(fileio
*self
, PyObject
*args
)
761 PyObject
*posobj
= NULL
;
770 return err_mode("writing");
772 if (!PyArg_ParseTuple(args
, "|O", &posobj
))
775 if (posobj
== Py_None
|| posobj
== NULL
) {
776 /* Get the current position. */
777 posobj
= portable_lseek(fd
, NULL
, 1);
782 /* Move to the position to be truncated. */
783 posobj
= portable_lseek(fd
, posobj
, 0);
788 #if defined(HAVE_LARGEFILE_SUPPORT)
789 pos
= PyLong_AsLongLong(posobj
);
791 pos
= PyLong_AsLong(posobj
);
793 if (pos
== -1 && PyErr_Occurred())
797 /* MS _chsize doesn't work if newsize doesn't fit in 32 bits,
798 so don't even try using it. */
802 /* Truncate. Note that this may grow the file! */
803 Py_BEGIN_ALLOW_THREADS
805 hFile
= (HANDLE
)_get_osfhandle(fd
);
806 ret
= hFile
== (HANDLE
)-1;
808 ret
= SetEndOfFile(hFile
) == 0;
815 Py_BEGIN_ALLOW_THREADS
817 ret
= ftruncate(fd
, pos
);
819 #endif /* !MS_WINDOWS */
822 PyErr_SetFromErrno(PyExc_IOError
);
831 mode_string(fileio
*self
)
833 if (self
->readable
) {
844 fileio_repr(fileio
*self
)
846 PyObject
*nameobj
, *res
;
849 return PyString_FromFormat("<_io.FileIO [closed]>");
851 nameobj
= PyObject_GetAttrString((PyObject
*) self
, "name");
852 if (nameobj
== NULL
) {
853 if (PyErr_ExceptionMatches(PyExc_AttributeError
))
857 res
= PyString_FromFormat("<_io.FileIO fd=%d mode='%s'>",
858 self
->fd
, mode_string(self
));
861 PyObject
*repr
= PyObject_Repr(nameobj
);
865 res
= PyString_FromFormat("<_io.FileIO name=%s mode='%s'>",
866 PyString_AS_STRING(repr
),
874 fileio_isatty(fileio
*self
)
880 Py_BEGIN_ALLOW_THREADS
881 res
= isatty(self
->fd
);
883 return PyBool_FromLong(res
);
887 PyDoc_STRVAR(fileio_doc
,
888 "file(name: str[, mode: str]) -> file IO object\n"
890 "Open a file. The mode can be 'r', 'w' or 'a' for reading (default),\n"
891 "writing or appending. The file will be created if it doesn't exist\n"
892 "when opened for writing or appending; it will be truncated when\n"
893 "opened for writing. Add a '+' to the mode to allow simultaneous\n"
894 "reading and writing.");
896 PyDoc_STRVAR(read_doc
,
897 "read(size: int) -> bytes. read at most size bytes, returned as bytes.\n"
899 "Only makes one system call, so less data may be returned than requested\n"
900 "In non-blocking mode, returns None if no data is available.\n"
901 "On end-of-file, returns ''.");
903 PyDoc_STRVAR(readall_doc
,
904 "readall() -> bytes. read all data from the file, returned as bytes.\n"
906 "In non-blocking mode, returns as much as is immediately available,\n"
907 "or None if no data is available. On end-of-file, returns ''.");
909 PyDoc_STRVAR(write_doc
,
910 "write(b: bytes) -> int. Write bytes b to file, return number written.\n"
912 "Only makes one system call, so not all of the data may be written.\n"
913 "The number of bytes actually written is returned.");
915 PyDoc_STRVAR(fileno_doc
,
916 "fileno() -> int. \"file descriptor\".\n"
918 "This is needed for lower-level file interfaces, such the fcntl module.");
920 PyDoc_STRVAR(seek_doc
,
921 "seek(offset: int[, whence: int]) -> None. Move to new file position.\n"
923 "Argument offset is a byte count. Optional argument whence defaults to\n"
924 "0 (offset from start of file, offset should be >= 0); other values are 1\n"
925 "(move relative to current position, positive or negative), and 2 (move\n"
926 "relative to end of file, usually negative, although many platforms allow\n"
927 "seeking beyond the end of a file)."
929 "Note that not all file objects are seekable.");
931 #ifdef HAVE_FTRUNCATE
932 PyDoc_STRVAR(truncate_doc
,
933 "truncate([size: int]) -> None. Truncate the file to at most size bytes.\n"
935 "Size defaults to the current file position, as returned by tell()."
936 "The current file position is changed to the value of size.");
939 PyDoc_STRVAR(tell_doc
,
940 "tell() -> int. Current file position");
942 PyDoc_STRVAR(readinto_doc
,
943 "readinto() -> Same as RawIOBase.readinto().");
945 PyDoc_STRVAR(close_doc
,
946 "close() -> None. Close the file.\n"
948 "A closed file cannot be used for further I/O operations. close() may be\n"
949 "called more than once without error. Changes the fileno to -1.");
951 PyDoc_STRVAR(isatty_doc
,
952 "isatty() -> bool. True if the file is connected to a tty device.");
954 PyDoc_STRVAR(seekable_doc
,
955 "seekable() -> bool. True if file supports random-access.");
957 PyDoc_STRVAR(readable_doc
,
958 "readable() -> bool. True if file was opened in a read mode.");
960 PyDoc_STRVAR(writable_doc
,
961 "writable() -> bool. True if file was opened in a write mode.");
963 static PyMethodDef fileio_methods
[] = {
964 {"read", (PyCFunction
)fileio_read
, METH_VARARGS
, read_doc
},
965 {"readall", (PyCFunction
)fileio_readall
, METH_NOARGS
, readall_doc
},
966 {"readinto", (PyCFunction
)fileio_readinto
, METH_VARARGS
, readinto_doc
},
967 {"write", (PyCFunction
)fileio_write
, METH_VARARGS
, write_doc
},
968 {"seek", (PyCFunction
)fileio_seek
, METH_VARARGS
, seek_doc
},
969 {"tell", (PyCFunction
)fileio_tell
, METH_VARARGS
, tell_doc
},
970 #ifdef HAVE_FTRUNCATE
971 {"truncate", (PyCFunction
)fileio_truncate
, METH_VARARGS
, truncate_doc
},
973 {"close", (PyCFunction
)fileio_close
, METH_NOARGS
, close_doc
},
974 {"seekable", (PyCFunction
)fileio_seekable
, METH_NOARGS
, seekable_doc
},
975 {"readable", (PyCFunction
)fileio_readable
, METH_NOARGS
, readable_doc
},
976 {"writable", (PyCFunction
)fileio_writable
, METH_NOARGS
, writable_doc
},
977 {"fileno", (PyCFunction
)fileio_fileno
, METH_NOARGS
, fileno_doc
},
978 {"isatty", (PyCFunction
)fileio_isatty
, METH_NOARGS
, isatty_doc
},
979 {NULL
, NULL
} /* sentinel */
982 /* 'closed' and 'mode' are attributes for backwards compatibility reasons. */
985 get_closed(fileio
*self
, void *closure
)
987 return PyBool_FromLong((long)(self
->fd
< 0));
991 get_closefd(fileio
*self
, void *closure
)
993 return PyBool_FromLong((long)(self
->closefd
));
997 get_mode(fileio
*self
, void *closure
)
999 return PyUnicode_FromString(mode_string(self
));
1002 static PyGetSetDef fileio_getsetlist
[] = {
1003 {"closed", (getter
)get_closed
, NULL
, "True if the file is closed"},
1004 {"closefd", (getter
)get_closefd
, NULL
,
1005 "True if the file descriptor will be closed"},
1006 {"mode", (getter
)get_mode
, NULL
, "String giving the file mode"},
1010 PyTypeObject PyFileIO_Type
= {
1011 PyVarObject_HEAD_INIT(NULL
, 0)
1015 (destructor
)fileio_dealloc
, /* tp_dealloc */
1019 0, /* tp_reserved */
1020 (reprfunc
)fileio_repr
, /* tp_repr */
1021 0, /* tp_as_number */
1022 0, /* tp_as_sequence */
1023 0, /* tp_as_mapping */
1027 PyObject_GenericGetAttr
, /* tp_getattro */
1028 0, /* tp_setattro */
1029 0, /* tp_as_buffer */
1030 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
1031 | Py_TPFLAGS_HAVE_GC
, /* tp_flags */
1032 fileio_doc
, /* tp_doc */
1033 (traverseproc
)fileio_traverse
, /* tp_traverse */
1034 (inquiry
)fileio_clear
, /* tp_clear */
1035 0, /* tp_richcompare */
1036 offsetof(fileio
, weakreflist
), /* tp_weaklistoffset */
1038 0, /* tp_iternext */
1039 fileio_methods
, /* tp_methods */
1041 fileio_getsetlist
, /* tp_getset */
1044 0, /* tp_descr_get */
1045 0, /* tp_descr_set */
1046 offsetof(fileio
, dict
), /* tp_dictoffset */
1047 fileio_init
, /* tp_init */
1048 PyType_GenericAlloc
, /* tp_alloc */
1049 fileio_new
, /* tp_new */
1050 PyObject_GC_Del
, /* tp_free */