Skip to content
Merged
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
6 changes: 3 additions & 3 deletions Lib/_pyio.py
Original file line number Diff line number Diff line change
Expand Up @@ -2386,18 +2386,18 @@ def _reset_encoder(position):
raise ValueError("tell on closed file")
if not self._seekable:
raise UnsupportedOperation("underlying stream is not seekable")
if whence == 1: # seek relative to current position
if whence == SEEK_CUR:
if cookie != 0:
raise UnsupportedOperation("can't do nonzero cur-relative seeks")
# Seeking to the current position should attempt to
# sync the underlying buffer with the current position.
whence = 0
cookie = self.tell()
if whence == 2: # seek relative to end of file
elif whence == SEEK_END:
if cookie != 0:
raise UnsupportedOperation("can't do nonzero end-relative seeks")
self.flush()
position = self.buffer.seek(0, 2)
position = self.buffer.seek(0, whence)
self._set_decoded_chars('')
self._snapshot = None
if self._decoder:
Expand Down
14 changes: 8 additions & 6 deletions Modules/_io/textio.c
Original file line number Diff line number Diff line change
Expand Up @@ -2344,7 +2344,8 @@ _io_TextIOWrapper_seek_impl(textio *self, PyObject *cookieObj, int whence)
goto fail;
}

if (whence == 1) {
switch (whence) {
case SEEK_CUR:
/* seek relative to current position */
cmp = PyObject_RichCompareBool(cookieObj, _PyLong_Zero, Py_EQ);
if (cmp < 0)
Expand All @@ -2362,8 +2363,7 @@ _io_TextIOWrapper_seek_impl(textio *self, PyObject *cookieObj, int whence)
cookieObj = _PyObject_CallMethodId((PyObject *)self, &PyId_tell, NULL);
if (cookieObj == NULL)
goto fail;
}
else if (whence == 2) {
case SEEK_END:
/* seek relative to end of file */
cmp = PyObject_RichCompareBool(cookieObj, _PyLong_Zero, Py_EQ);
if (cmp < 0)
Expand Down Expand Up @@ -2401,10 +2401,12 @@ _io_TextIOWrapper_seek_impl(textio *self, PyObject *cookieObj, int whence)
}
}
return res;
}
else if (whence != 0) {
case SEEK_SET:
break;
default:
PyErr_Format(PyExc_ValueError,
"invalid whence (%d, should be 0, 1 or 2)", whence);
"invalid whence (%d, should be %d, %d or %d)", whence,
SEEK_SET, SEEK_CUR, SEEK_END);
goto fail;
}

Expand Down