Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Lib/test/test_io/test_fileio.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ def testWeakRefs(self):

def testSeekTell(self):
self.f.write(bytes(range(20)))
# An invalid argument must not mark the file as non-seekable.
self.assertRaises(OSError, self.f.seek, -1)
self.assertTrue(self.f.seekable())
self.assertEqual(self.f.tell(), 20)
self.f.seek(0)
self.assertEqual(self.f.tell(), 0)
Expand All @@ -61,6 +64,7 @@ def testSeekTell(self):
self.assertEqual(self.f.tell(), 10)
self.f.seek(-5, 2)
self.assertEqual(self.f.tell(), 15)
self.assertTrue(self.f.seekable())

def testAttributes(self):
# verify expected attributes exist
Expand Down Expand Up @@ -553,6 +557,13 @@ def testAbles(self):
f.close()

if sys.platform != "win32":
# On Windows, lseek somehow succeeds on pipes.
r, w = os.pipe()
os.close(w)
with self.FileIO(r, "r") as f:
self.assertRaises(OSError, f.seek, 0)
self.assertEqual(f.seekable(), False)

try:
f = self.FileIO("/dev/tty", "a")
except OSError:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix :class:`io.FileIO` ``seekable`` being cached as false because of an invalid
argument to :meth:`!io.FileIO.seek`
8 changes: 7 additions & 1 deletion Modules/_io/fileio.c
Original file line number Diff line number Diff line change
Expand Up @@ -1013,7 +1013,13 @@ portable_lseek(fileio *self, PyObject *posobj, int whence, bool suppress_pipe_er
_Py_END_SUPPRESS_IPH
Py_END_ALLOW_THREADS

if (self->seekable < 0) {
if (self->seekable < 0
&& !(res < 0 && (errno == EINVAL
#ifdef EOVERFLOW
|| errno == EOVERFLOW
#endif
|| errno == ENXIO)))
{
self->seekable = (res >= 0);
}

Expand Down
Loading