#define PyFileIO_Check(op) (PyObject_TypeCheck((op), &PyFileIO_Type))
+/* Forward declarations */
+static PyObject* portable_lseek(fileio *self, PyObject *posobj, int whence);
+
int
_PyFileIO_closed(PyObject *self)
{
Py_RETURN_NONE;
}
-static PyObject *
-portable_lseek(int fd, PyObject *posobj, int whence);
-
-static PyObject *portable_lseek(int fd, PyObject *posobj, int whence);
-
/* Returns 0 on success, -1 with exception set on failure. */
static int
internal_close(fileio *self)
/* For consistent behaviour, we explicitly seek to the
end of file (otherwise, it might be done only on the
first write()). */
- PyObject *pos = portable_lseek(self->fd, NULL, 2);
+ PyObject *pos = portable_lseek(self, NULL, 2);
if (pos == NULL)
goto error;
Py_DECREF(pos);
if (self->fd < 0)
return err_closed();
if (self->seekable < 0) {
- PyObject *pos = portable_lseek(self->fd, NULL, SEEK_CUR);
+ /* portable_lseek() sets the seekable attribute */
+ PyObject *pos = portable_lseek(self, NULL, SEEK_CUR);
+ assert(self->seekable >= 0);
if (pos == NULL) {
PyErr_Clear();
- self->seekable = 0;
- } else {
+ }
+ else {
Py_DECREF(pos);
- self->seekable = 1;
}
}
return PyBool_FromLong((long) self->seekable);
/* Cribbed from posix_lseek() */
static PyObject *
-portable_lseek(int fd, PyObject *posobj, int whence)
+portable_lseek(fileio *self, PyObject *posobj, int whence)
{
Py_off_t pos, res;
+ int fd = self->fd;
#ifdef SEEK_SET
/* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
}
#endif /* SEEK_SET */
- if (posobj == NULL)
+ if (posobj == NULL) {
pos = 0;
+ }
else {
if(PyFloat_Check(posobj)) {
PyErr_SetString(PyExc_TypeError, "an integer is required");
#endif
_Py_END_SUPPRESS_IPH
Py_END_ALLOW_THREADS
+
+ if (self->seekable < 0) {
+ self->seekable = (res >= 0);
+ }
+
if (res < 0)
return PyErr_SetFromErrno(PyExc_OSError);
if (self->fd < 0)
return err_closed();
- return portable_lseek(self->fd, pos, whence);
+ return portable_lseek(self, pos, whence);
}
/*[clinic input]
if (self->fd < 0)
return err_closed();
- return portable_lseek(self->fd, NULL, 1);
+ return portable_lseek(self, NULL, 1);
}
#ifdef HAVE_FTRUNCATE
if (posobj == Py_None || posobj == NULL) {
/* Get the current position. */
- posobj = portable_lseek(fd, NULL, 1);
+ posobj = portable_lseek(self, NULL, 1);
if (posobj == NULL)
return NULL;
}