are opened in text mode. Patch by Serhiy Storchaka.
Lib/venv/scripts/nt/* = BIN
+Lib/test/coding20731.py = BIN
+
# All other files (which presumably are human-editable) are "native".
# This must be the last rule!
--- /dev/null
+#coding:latin1
+
+
+
import unittest
from test.support import TESTFN, unlink, unload
-import importlib, os, sys
+import importlib, os, sys, subprocess
class CodingTest(unittest.TestCase):
def test_bad_coding(self):
self.assertTrue(c.exception.args[0].startswith(expected),
msg=c.exception.args[0])
+ def test_20731(self):
+ sub = subprocess.Popen([sys.executable,
+ os.path.join(os.path.dirname(__file__),
+ 'coding20731.py')],
+ stderr=subprocess.PIPE)
+ err = sub.communicate()[1]
+ self.assertEquals(err, b'')
if __name__ == "__main__":
unittest.main()
Core and Builtins
-----------------
+- Issue #20731: Properly position in source code files even if they
+ are opened in text mode. Patch by Serhiy Storchaka.
+
- Issue #19619: str.encode, bytes.decode and bytearray.decode now use an
internal API to throw LookupError for known non-text encodings, rather
than attempting the encoding or decoding operation and then throwing a
fd = fileno(tok->fp);
/* Due to buffering the file offset for fd can be different from the file
- * position of tok->fp. */
+ * position of tok->fp. If tok->fp was opened in text mode on Windows,
+ * its file position counts CRLF as one char and can't be directly mapped
+ * to the file offset for fd. Instead we step back one byte and read to
+ * the end of line.*/
pos = ftell(tok->fp);
- if (pos == -1 || lseek(fd, (off_t)pos, SEEK_SET) == (off_t)-1) {
+ if (pos == -1 ||
+ lseek(fd, (off_t)(pos > 0 ? pos - 1 : pos), SEEK_SET) == (off_t)-1) {
PyErr_SetFromErrnoWithFilename(PyExc_OSError, NULL);
goto cleanup;
}
Py_XDECREF(tok->decoding_readline);
readline = _PyObject_GetAttrId(stream, &PyId_readline);
tok->decoding_readline = readline;
+ if (pos > 0) {
+ if (PyObject_CallObject(readline, NULL) == NULL) {
+ readline = NULL;
+ goto cleanup;
+ }
+ }
cleanup:
Py_XDECREF(stream);