File Modules/_io/fileio.c changed (mode: 100644) (index 922db3e470..a09c39f76a) |
... |
... |
_Py_IDENTIFIER(name); |
73 |
73 |
|
|
74 |
74 |
#define PyFileIO_Check(op) (PyObject_TypeCheck((op), &PyFileIO_Type)) |
#define PyFileIO_Check(op) (PyObject_TypeCheck((op), &PyFileIO_Type)) |
75 |
75 |
|
|
|
76 |
|
/* Forward declarations */ |
|
77 |
|
static PyObject* portable_lseek(fileio *self, PyObject *posobj, int whence); |
|
78 |
|
|
76 |
79 |
int |
int |
77 |
80 |
_PyFileIO_closed(PyObject *self) |
_PyFileIO_closed(PyObject *self) |
78 |
81 |
{ |
{ |
|
... |
... |
fileio_dealloc_warn(fileio *self, PyObject *source) |
98 |
101 |
Py_RETURN_NONE; |
Py_RETURN_NONE; |
99 |
102 |
} |
} |
100 |
103 |
|
|
101 |
|
static PyObject * |
|
102 |
|
portable_lseek(int fd, PyObject *posobj, int whence); |
|
103 |
|
|
|
104 |
|
static PyObject *portable_lseek(int fd, PyObject *posobj, int whence); |
|
105 |
|
|
|
106 |
104 |
/* Returns 0 on success, -1 with exception set on failure. */ |
/* Returns 0 on success, -1 with exception set on failure. */ |
107 |
105 |
static int |
static int |
108 |
106 |
internal_close(fileio *self) |
internal_close(fileio *self) |
|
... |
... |
_io_FileIO___init___impl(fileio *self, PyObject *nameobj, const char *mode, |
478 |
476 |
/* For consistent behaviour, we explicitly seek to the |
/* For consistent behaviour, we explicitly seek to the |
479 |
477 |
end of file (otherwise, it might be done only on the |
end of file (otherwise, it might be done only on the |
480 |
478 |
first write()). */ |
first write()). */ |
481 |
|
PyObject *pos = portable_lseek(self->fd, NULL, 2); |
|
|
479 |
|
PyObject *pos = portable_lseek(self, NULL, 2); |
482 |
480 |
if (pos == NULL) |
if (pos == NULL) |
483 |
481 |
goto error; |
goto error; |
484 |
482 |
Py_DECREF(pos); |
Py_DECREF(pos); |
|
... |
... |
_io_FileIO_seekable_impl(fileio *self) |
600 |
598 |
if (self->fd < 0) |
if (self->fd < 0) |
601 |
599 |
return err_closed(); |
return err_closed(); |
602 |
600 |
if (self->seekable < 0) { |
if (self->seekable < 0) { |
603 |
|
PyObject *pos = portable_lseek(self->fd, NULL, SEEK_CUR); |
|
|
601 |
|
/* portable_lseek() sets the seekable attribute */ |
|
602 |
|
PyObject *pos = portable_lseek(self, NULL, SEEK_CUR); |
|
603 |
|
assert(self->seekable >= 0); |
604 |
604 |
if (pos == NULL) { |
if (pos == NULL) { |
605 |
605 |
PyErr_Clear(); |
PyErr_Clear(); |
606 |
|
self->seekable = 0; |
|
607 |
|
} else { |
|
|
606 |
|
} |
|
607 |
|
else { |
608 |
608 |
Py_DECREF(pos); |
Py_DECREF(pos); |
609 |
|
self->seekable = 1; |
|
610 |
609 |
} |
} |
611 |
610 |
} |
} |
612 |
611 |
return PyBool_FromLong((long) self->seekable); |
return PyBool_FromLong((long) self->seekable); |
|
... |
... |
_io_FileIO_write_impl(fileio *self, Py_buffer *b) |
865 |
864 |
|
|
866 |
865 |
/* Cribbed from posix_lseek() */ |
/* Cribbed from posix_lseek() */ |
867 |
866 |
static PyObject * |
static PyObject * |
868 |
|
portable_lseek(int fd, PyObject *posobj, int whence) |
|
|
867 |
|
portable_lseek(fileio *self, PyObject *posobj, int whence) |
869 |
868 |
{ |
{ |
870 |
869 |
Py_off_t pos, res; |
Py_off_t pos, res; |
|
870 |
|
int fd = self->fd; |
871 |
871 |
|
|
872 |
872 |
#ifdef SEEK_SET |
#ifdef SEEK_SET |
873 |
873 |
/* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
/* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */ |
|
... |
... |
portable_lseek(int fd, PyObject *posobj, int whence) |
884 |
884 |
} |
} |
885 |
885 |
#endif /* SEEK_SET */ |
#endif /* SEEK_SET */ |
886 |
886 |
|
|
887 |
|
if (posobj == NULL) |
|
|
887 |
|
if (posobj == NULL) { |
888 |
888 |
pos = 0; |
pos = 0; |
|
889 |
|
} |
889 |
890 |
else { |
else { |
890 |
891 |
if(PyFloat_Check(posobj)) { |
if(PyFloat_Check(posobj)) { |
891 |
892 |
PyErr_SetString(PyExc_TypeError, "an integer is required"); |
PyErr_SetString(PyExc_TypeError, "an integer is required"); |
|
... |
... |
portable_lseek(int fd, PyObject *posobj, int whence) |
909 |
910 |
#endif |
#endif |
910 |
911 |
_Py_END_SUPPRESS_IPH |
_Py_END_SUPPRESS_IPH |
911 |
912 |
Py_END_ALLOW_THREADS |
Py_END_ALLOW_THREADS |
|
913 |
|
|
|
914 |
|
if (self->seekable < 0) { |
|
915 |
|
self->seekable = (res >= 0); |
|
916 |
|
} |
|
917 |
|
|
912 |
918 |
if (res < 0) |
if (res < 0) |
913 |
919 |
return PyErr_SetFromErrno(PyExc_OSError); |
return PyErr_SetFromErrno(PyExc_OSError); |
914 |
920 |
|
|
|
... |
... |
_io_FileIO_seek_impl(fileio *self, PyObject *pos, int whence) |
943 |
949 |
if (self->fd < 0) |
if (self->fd < 0) |
944 |
950 |
return err_closed(); |
return err_closed(); |
945 |
951 |
|
|
946 |
|
return portable_lseek(self->fd, pos, whence); |
|
|
952 |
|
return portable_lseek(self, pos, whence); |
947 |
953 |
} |
} |
948 |
954 |
|
|
949 |
955 |
/*[clinic input] |
/*[clinic input] |
|
... |
... |
_io_FileIO_tell_impl(fileio *self) |
961 |
967 |
if (self->fd < 0) |
if (self->fd < 0) |
962 |
968 |
return err_closed(); |
return err_closed(); |
963 |
969 |
|
|
964 |
|
return portable_lseek(self->fd, NULL, 1); |
|
|
970 |
|
return portable_lseek(self, NULL, 1); |
965 |
971 |
} |
} |
966 |
972 |
|
|
967 |
973 |
#ifdef HAVE_FTRUNCATE |
#ifdef HAVE_FTRUNCATE |
|
... |
... |
_io_FileIO_truncate_impl(fileio *self, PyObject *posobj) |
992 |
998 |
|
|
993 |
999 |
if (posobj == Py_None || posobj == NULL) { |
if (posobj == Py_None || posobj == NULL) { |
994 |
1000 |
/* Get the current position. */ |
/* Get the current position. */ |
995 |
|
posobj = portable_lseek(fd, NULL, 1); |
|
|
1001 |
|
posobj = portable_lseek(self, NULL, 1); |
996 |
1002 |
if (posobj == NULL) |
if (posobj == NULL) |
997 |
1003 |
return NULL; |
return NULL; |
998 |
1004 |
} |
} |