Skip to content

Commit 815b41b

Browse files
committed
Issue #20731: Properly position in source code files even if they
are opened in text mode. Patch by Serhiy Storchaka.
1 parent 9db1ab8 commit 815b41b

5 files changed

Lines changed: 29 additions & 3 deletions

File tree

.hgeol

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ Lib/test/xmltestdata/* = BIN
3737

3838
Lib/venv/scripts/nt/* = BIN
3939

40+
Lib/test/coding20731.py = BIN
41+
4042
# All other files (which presumably are human-editable) are "native".
4143
# This must be the last rule!
4244

Lib/test/coding20731.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#coding:latin1
2+
3+
4+

Lib/test/test_coding.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import unittest
22
from test.support import TESTFN, unlink, unload
3-
import importlib, os, sys
3+
import importlib, os, sys, subprocess
44

55
class CodingTest(unittest.TestCase):
66
def test_bad_coding(self):
@@ -58,6 +58,13 @@ def test_error_from_string(self):
5858
self.assertTrue(c.exception.args[0].startswith(expected),
5959
msg=c.exception.args[0])
6060

61+
def test_20731(self):
62+
sub = subprocess.Popen([sys.executable,
63+
os.path.join(os.path.dirname(__file__),
64+
'coding20731.py')],
65+
stderr=subprocess.PIPE)
66+
err = sub.communicate()[1]
67+
self.assertEquals(err, b'')
6168

6269
if __name__ == "__main__":
6370
unittest.main()

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ What's New in Python 3.3.5 release candidate 1?
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #20731: Properly position in source code files even if they
14+
are opened in text mode. Patch by Serhiy Storchaka.
15+
1316
- Issue #19619: str.encode, bytes.decode and bytearray.decode now use an
1417
internal API to throw LookupError for known non-text encodings, rather
1518
than attempting the encoding or decoding operation and then throwing a

Parser/tokenizer.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -498,9 +498,13 @@ fp_setreadl(struct tok_state *tok, const char* enc)
498498

499499
fd = fileno(tok->fp);
500500
/* Due to buffering the file offset for fd can be different from the file
501-
* position of tok->fp. */
501+
* position of tok->fp. If tok->fp was opened in text mode on Windows,
502+
* its file position counts CRLF as one char and can't be directly mapped
503+
* to the file offset for fd. Instead we step back one byte and read to
504+
* the end of line.*/
502505
pos = ftell(tok->fp);
503-
if (pos == -1 || lseek(fd, (off_t)pos, SEEK_SET) == (off_t)-1) {
506+
if (pos == -1 ||
507+
lseek(fd, (off_t)(pos > 0 ? pos - 1 : pos), SEEK_SET) == (off_t)-1) {
504508
PyErr_SetFromErrnoWithFilename(PyExc_OSError, NULL);
505509
goto cleanup;
506510
}
@@ -513,6 +517,12 @@ fp_setreadl(struct tok_state *tok, const char* enc)
513517
Py_XDECREF(tok->decoding_readline);
514518
readline = _PyObject_GetAttrId(stream, &PyId_readline);
515519
tok->decoding_readline = readline;
520+
if (pos > 0) {
521+
if (PyObject_CallObject(readline, NULL) == NULL) {
522+
readline = NULL;
523+
goto cleanup;
524+
}
525+
}
516526

517527
cleanup:
518528
Py_XDECREF(stream);

0 commit comments

Comments
 (0)