summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMartin v. Löwis <martin@v.loewis.de>2014-02-28 14:27:29 (GMT)
committerMartin v. Löwis <martin@v.loewis.de>2014-02-28 14:27:29 (GMT)
commit815b41b1cdb98686fc3f9cdf995b6983c12c04b3 (patch)
tree58c273e0da20ff65f83301041971ec3e723bd1d3
parent9db1ab82508cd4be4195e520da8cb8d86cd1f7e4 (diff)
downloadcpython-815b41b1cdb98686fc3f9cdf995b6983c12c04b3.zip
cpython-815b41b1cdb98686fc3f9cdf995b6983c12c04b3.tar.gz
cpython-815b41b1cdb98686fc3f9cdf995b6983c12c04b3.tar.bz2
Issue #20731: Properly position in source code files even if they
are opened in text mode. Patch by Serhiy Storchaka.
-rw-r--r--.hgeol2
-rw-r--r--Lib/test/coding20731.py4
-rw-r--r--Lib/test/test_coding.py9
-rw-r--r--Misc/NEWS3
-rw-r--r--Parser/tokenizer.c14
5 files changed, 29 insertions, 3 deletions
diff --git a/.hgeol b/.hgeol
index 4e03690..73a4770 100644
--- a/.hgeol
+++ b/.hgeol
@@ -37,6 +37,8 @@ Lib/test/xmltestdata/* = BIN
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!
diff --git a/Lib/test/coding20731.py b/Lib/test/coding20731.py
new file mode 100644
index 0000000..ca4962e
--- /dev/null
+++ b/Lib/test/coding20731.py
@@ -0,0 +1,4 @@
+#coding:latin1
+
+
+
diff --git a/Lib/test/test_coding.py b/Lib/test/test_coding.py
index 989c7a8..7606767 100644
--- a/Lib/test/test_coding.py
+++ b/Lib/test/test_coding.py
@@ -1,6 +1,6 @@
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):
@@ -58,6 +58,13 @@ class CodingTest(unittest.TestCase):
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()
diff --git a/Misc/NEWS b/Misc/NEWS
index 4c99dd3..3b70783 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@ What's New in Python 3.3.5 release candidate 1?
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
diff --git a/Parser/tokenizer.c b/Parser/tokenizer.c
index 8530723..660c0f0 100644
--- a/Parser/tokenizer.c
+++ b/Parser/tokenizer.c
@@ -498,9 +498,13 @@ fp_setreadl(struct tok_state *tok, const char* enc)
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;
}
@@ -513,6 +517,12 @@ fp_setreadl(struct tok_state *tok, const char* enc)
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);