Use names SEEK_SET, etc instead of magic number (GH-12057)
The previous code hardcoded `SEEK_SET`, etc. While it's very unlikely that these values will change, it's best to use the definitions to avoid there being mismatches in behavior with the code in the future. Signed-off-by: Enji Cooper <yaneurabeya@gmail.com>
This commit is contained in:
parent
8589f14bbe
commit
848037c147
|
@ -2386,18 +2386,18 @@ class TextIOWrapper(TextIOBase):
|
||||||
raise ValueError("tell on closed file")
|
raise ValueError("tell on closed file")
|
||||||
if not self._seekable:
|
if not self._seekable:
|
||||||
raise UnsupportedOperation("underlying stream is not seekable")
|
raise UnsupportedOperation("underlying stream is not seekable")
|
||||||
if whence == 1: # seek relative to current position
|
if whence == SEEK_CUR:
|
||||||
if cookie != 0:
|
if cookie != 0:
|
||||||
raise UnsupportedOperation("can't do nonzero cur-relative seeks")
|
raise UnsupportedOperation("can't do nonzero cur-relative seeks")
|
||||||
# Seeking to the current position should attempt to
|
# Seeking to the current position should attempt to
|
||||||
# sync the underlying buffer with the current position.
|
# sync the underlying buffer with the current position.
|
||||||
whence = 0
|
whence = 0
|
||||||
cookie = self.tell()
|
cookie = self.tell()
|
||||||
if whence == 2: # seek relative to end of file
|
elif whence == SEEK_END:
|
||||||
if cookie != 0:
|
if cookie != 0:
|
||||||
raise UnsupportedOperation("can't do nonzero end-relative seeks")
|
raise UnsupportedOperation("can't do nonzero end-relative seeks")
|
||||||
self.flush()
|
self.flush()
|
||||||
position = self.buffer.seek(0, 2)
|
position = self.buffer.seek(0, whence)
|
||||||
self._set_decoded_chars('')
|
self._set_decoded_chars('')
|
||||||
self._snapshot = None
|
self._snapshot = None
|
||||||
if self._decoder:
|
if self._decoder:
|
||||||
|
|
|
@ -2344,7 +2344,8 @@ _io_TextIOWrapper_seek_impl(textio *self, PyObject *cookieObj, int whence)
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (whence == 1) {
|
switch (whence) {
|
||||||
|
case SEEK_CUR:
|
||||||
/* seek relative to current position */
|
/* seek relative to current position */
|
||||||
cmp = PyObject_RichCompareBool(cookieObj, _PyLong_Zero, Py_EQ);
|
cmp = PyObject_RichCompareBool(cookieObj, _PyLong_Zero, Py_EQ);
|
||||||
if (cmp < 0)
|
if (cmp < 0)
|
||||||
|
@ -2362,8 +2363,7 @@ _io_TextIOWrapper_seek_impl(textio *self, PyObject *cookieObj, int whence)
|
||||||
cookieObj = _PyObject_CallMethodId((PyObject *)self, &PyId_tell, NULL);
|
cookieObj = _PyObject_CallMethodId((PyObject *)self, &PyId_tell, NULL);
|
||||||
if (cookieObj == NULL)
|
if (cookieObj == NULL)
|
||||||
goto fail;
|
goto fail;
|
||||||
}
|
case SEEK_END:
|
||||||
else if (whence == 2) {
|
|
||||||
/* seek relative to end of file */
|
/* seek relative to end of file */
|
||||||
cmp = PyObject_RichCompareBool(cookieObj, _PyLong_Zero, Py_EQ);
|
cmp = PyObject_RichCompareBool(cookieObj, _PyLong_Zero, Py_EQ);
|
||||||
if (cmp < 0)
|
if (cmp < 0)
|
||||||
|
@ -2401,10 +2401,12 @@ _io_TextIOWrapper_seek_impl(textio *self, PyObject *cookieObj, int whence)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return res;
|
return res;
|
||||||
}
|
case SEEK_SET:
|
||||||
else if (whence != 0) {
|
break;
|
||||||
|
default:
|
||||||
PyErr_Format(PyExc_ValueError,
|
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;
|
goto fail;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue