Skip to content

Commit 40d3781

Browse files
committed
- Fix segfault with invalid coding.
- SF Bug #772896, unknown encoding results in MemoryError, which is not helpful I will only backport the segfault fix. I'll let Anthony decide if he wants the other changes backported. I will do the backport if asked.
1 parent d45014b commit 40d3781

7 files changed

Lines changed: 39 additions & 3 deletions

File tree

Lib/test/bad_coding.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# -*- coding: uft-8 -*-

Lib/test/test_coding.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
import test.test_support, unittest
3+
import os
4+
5+
class CodingTest(unittest.TestCase):
6+
def test_bad_coding(self):
7+
module_name = 'bad_coding'
8+
self.assertRaises(SyntaxError, __import__, 'test.' + module_name)
9+
10+
path = os.path.dirname(__file__)
11+
filename = os.path.join(path, module_name + '.py')
12+
fp = open(filename)
13+
text = fp.read()
14+
fp.close()
15+
self.assertRaises(SyntaxError, compile, text, filename, 'exec')
16+
17+
def test_main():
18+
test.test_support.run_unittest(CodingTest)
19+
20+
if __name__ == "__main__":
21+
test_main()

Misc/NEWS

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

15+
- Fix segfault with invalid coding.
16+
17+
- SF bug #772896: unknown encoding results in MemoryError.
18+
1519
- All iterators now have a Boolean value of true. Formerly, some iterators
1620
supported a __len__() method which evaluated to False when the iterator
1721
was empty.

Parser/parsetok.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ PyParser_ParseStringFlagsFilename(const char *s, const char *filename,
4242
initerr(err_ret, filename);
4343

4444
if ((tok = PyTokenizer_FromString(s)) == NULL) {
45-
err_ret->error = E_NOMEM;
45+
err_ret->error = PyErr_Occurred() ? E_DECODE : E_NOMEM;
4646
return NULL;
4747
}
4848

Parser/pgenmain.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,13 @@ getgrammar(char *filename)
116116
return g;
117117
}
118118

119+
/* Can't happen in pgen */
120+
PyObject*
121+
PyErr_Occurred()
122+
{
123+
return 0;
124+
}
125+
119126
void
120127
Py_FatalError(const char *msg)
121128
{

Parser/tokenizer.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -603,8 +603,11 @@ decode_str(const char *str, struct tok_state *tok)
603603
if (tok->enc != NULL) {
604604
assert(utf8 == NULL);
605605
utf8 = translate_into_utf8(str, tok->enc);
606-
if (utf8 == NULL)
606+
if (utf8 == NULL) {
607+
PyErr_Format(PyExc_SyntaxError,
608+
"unknown encoding: %s", tok->enc);
607609
return NULL;
610+
}
608611
str = PyString_AsString(utf8);
609612
}
610613
#endif

Python/pythonrun.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1487,7 +1487,7 @@ err_input(perrdetail *err)
14871487
msg = "unknown decode error";
14881488
Py_DECREF(type);
14891489
Py_DECREF(value);
1490-
Py_DECREF(tb);
1490+
Py_XDECREF(tb);
14911491
break;
14921492
}
14931493
case E_LINECONT:

0 commit comments

Comments
 (0)