Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 78f1e4c

Browse files
committed
Merge with 3.3
2 parents 78c330d + 815b41b commit 78f1e4c

5 files changed

Lines changed: 30 additions & 2 deletions

File tree

.hgeol

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

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

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

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_source_encoding.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import importlib
66
import os
77
import sys
8+
import subprocess
89

910
class SourceEncodingTest(unittest.TestCase):
1011

@@ -58,6 +59,14 @@ def test_issue7820(self):
5859
# two bytes in common with the UTF-8 BOM
5960
self.assertRaises(SyntaxError, eval, b'\xef\xbb\x20')
6061

62+
def test_20731(self):
63+
sub = subprocess.Popen([sys.executable,
64+
os.path.join(os.path.dirname(__file__),
65+
'coding20731.py')],
66+
stderr=subprocess.PIPE)
67+
err = sub.communicate()[1]
68+
self.assertEquals(err, b'')
69+
6170
def test_error_message(self):
6271
compile(b'# -*- coding: iso-8859-15 -*-\n', 'dummy', 'exec')
6372
compile(b'\xef\xbb\xbf\n', 'dummy', 'exec')

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ What's New in Python 3.4.1?
88
Core and Builtins
99
-----------------
1010

11+
- Issue #20731: Properly position in source code files even if they
12+
are opened in text mode. Patch by Serhiy Storchaka.
13+
1114
- Issue #20637: Key-sharing now also works for instance dictionaries of
1215
subclasses. Patch by Peter Ingebretson.
1316

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)