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

Skip to content

Commit db83eb3

Browse files
committed
Fix Bug #1378022, UTF-8 files with a leading BOM crashed the interpreter.
Needs backport.
1 parent e7214a1 commit db83eb3

5 files changed

Lines changed: 19 additions & 2 deletions

File tree

Lib/test/bad_coding2.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#coding: utf8
2+
print '我'

Lib/test/test_coding.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@
55
class CodingTest(unittest.TestCase):
66
def test_bad_coding(self):
77
module_name = 'bad_coding'
8+
self.verify_bad_module(module_name)
9+
10+
def test_bad_coding2(self):
11+
module_name = 'bad_coding2'
12+
self.verify_bad_module(module_name)
13+
14+
def verify_bad_module(self, module_name):
815
self.assertRaises(SyntaxError, __import__, 'test.' + module_name)
916

1017
path = os.path.dirname(__file__)

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ What's New in Python 2.5 alpha 1?
1212
Core and builtins
1313
-----------------
1414

15+
- Bug #1378022, UTF-8 files with a leading BOM crashed the interpreter.
16+
1517
- Support for converting hex strings to floats no longer works.
1618
This was not portable. float('0x3') now raises a ValueError.
1719

Parser/tokenizer.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,12 @@ check_coding_spec(const char* line, int size, struct tok_state *tok,
292292
PyMem_DEL(cs);
293293
}
294294
}
295+
if (!r) {
296+
cs = tok->encoding;
297+
if (!cs)
298+
cs = "with BOM";
299+
PyErr_Format(PyExc_SyntaxError, "encoding problem: %s", cs);
300+
}
295301
return r;
296302
}
297303

Python/pythonrun.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1439,8 +1439,8 @@ err_input(perrdetail *err)
14391439
}
14401440
if (msg == NULL)
14411441
msg = "unknown decode error";
1442-
Py_DECREF(type);
1443-
Py_DECREF(value);
1442+
Py_XDECREF(type);
1443+
Py_XDECREF(value);
14441444
Py_XDECREF(tb);
14451445
break;
14461446
}

0 commit comments

Comments
 (0)