self.open(support.TESTFN, mode)
self.assertIn('invalid mode', str(cm.exception))
+ def test_open_pipe_with_append(self):
+ # bpo-27805: Ignore ESPIPE from lseek() in open().
+ r, w = os.pipe()
+ self.addCleanup(os.close, r)
+ f = self.open(w, 'a')
+ self.addCleanup(f.close)
+ # Check that the file is marked non-seekable. On Windows, however, lseek
+ # somehow succeeds on pipes.
+ if sys.platform != 'win32':
+ self.assertFalse(f.seekable())
+
def test_io_after_close(self):
for kwargs in [
{"mode": "w"},
#include "Python.h"
#include "pycore_object.h"
#include "structmember.h"
+#include <stdbool.h>
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#define PyFileIO_Check(op) (PyObject_TypeCheck((op), &PyFileIO_Type))
/* Forward declarations */
-static PyObject* portable_lseek(fileio *self, PyObject *posobj, int whence);
+static PyObject* portable_lseek(fileio *self, PyObject *posobj, int whence, bool suppress_pipe_error);
int
_PyFileIO_closed(PyObject *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, NULL, 2);
+ PyObject *pos = portable_lseek(self, NULL, 2, true);
if (pos == NULL)
goto error;
Py_DECREF(pos);
return err_closed();
if (self->seekable < 0) {
/* portable_lseek() sets the seekable attribute */
- PyObject *pos = portable_lseek(self, NULL, SEEK_CUR);
+ PyObject *pos = portable_lseek(self, NULL, SEEK_CUR, false);
assert(self->seekable >= 0);
if (pos == NULL) {
PyErr_Clear();
/* Cribbed from posix_lseek() */
static PyObject *
-portable_lseek(fileio *self, PyObject *posobj, int whence)
+portable_lseek(fileio *self, PyObject *posobj, int whence, bool suppress_pipe_error)
{
Py_off_t pos, res;
int fd = self->fd;
self->seekable = (res >= 0);
}
- if (res < 0)
- return PyErr_SetFromErrno(PyExc_OSError);
+ if (res < 0) {
+ if (suppress_pipe_error && errno == ESPIPE) {
+ res = 0;
+ } else {
+ return PyErr_SetFromErrno(PyExc_OSError);
+ }
+ }
#if defined(HAVE_LARGEFILE_SUPPORT)
return PyLong_FromLongLong(res);
if (self->fd < 0)
return err_closed();
- return portable_lseek(self, pos, whence);
+ return portable_lseek(self, pos, whence, false);
}
/*[clinic input]
if (self->fd < 0)
return err_closed();
- return portable_lseek(self, NULL, 1);
+ return portable_lseek(self, NULL, 1, false);
}
#ifdef HAVE_FTRUNCATE
if (posobj == Py_None) {
/* Get the current position. */
- posobj = portable_lseek(self, NULL, 1);
+ posobj = portable_lseek(self, NULL, 1, false);
if (posobj == NULL)
return NULL;
}