benjamin.peterson
2008-11-21 22:58:57 UTC
Author: benjamin.peterson
Date: Fri Nov 21 23:58:57 2008
New Revision: 67322
Log:
Merged revisions 67320 via svnmerge from
svn+ssh://pythondev at svn.python.org/python/trunk
........
r67320 | benjamin.peterson | 2008-11-21 16:27:24 -0600 (Fri, 21 Nov 2008) | 4 lines
don't segfault when \N escapes are used and unicodedata fails to load
Fixes #4367
........
Modified:
python/branches/py3k/ (props changed)
python/branches/py3k/Lib/test/test_unicodedata.py
python/branches/py3k/Python/ast.c
Modified: python/branches/py3k/Lib/test/test_unicodedata.py
==============================================================================
--- python/branches/py3k/Lib/test/test_unicodedata.py (original)
+++ python/branches/py3k/Lib/test/test_unicodedata.py Fri Nov 21 23:58:57 2008
@@ -4,9 +4,13 @@
(c) Copyright CNRI, All Rights Reserved. NO WARRANTY.
-"""#"
-import unittest, test.support
+"""
+
+import sys
+import unittest
import hashlib
+import subprocess
+import test.support
encoding = 'utf-8'
@@ -197,6 +201,25 @@
class UnicodeMiscTest(UnicodeDatabaseTest):
+ def test_failed_import_during_compiling(self):
+ # Issue 4367
+ # Decoding \N escapes requires the unicodedata module. If it can't be
+ # imported, we shouldn't segfault.
+
+ # This program should raise a SyntaxError in the eval.
+ code = "import sys;" \
+ "sys.modules['unicodedata'] = None;" \
+ """eval("'\\\\N{SOFT HYPHEN}'")"""
+ args = [sys.executable, "-c", code]
+ # We use a subprocess because the unicodedata module may already have
+ # been loaded in this process.
+ popen = subprocess.Popen(args, stderr=subprocess.PIPE)
+ popen.wait()
+ self.assertEqual(popen.returncode, 1)
+ error = "SyntaxError: (unicode error) \\N escapes not supported " \
+ "(can't load unicodedata module)"
+ self.assertTrue(error in popen.stderr.read().decode("ascii"))
+
def test_decimal_numeric_consistent(self):
# Test that decimal and numeric are consistent,
# i.e. if a character has a decimal value,
Modified: python/branches/py3k/Python/ast.c
==============================================================================
--- python/branches/py3k/Python/ast.c (original)
+++ python/branches/py3k/Python/ast.c Fri Nov 21 23:58:57 2008
@@ -1321,13 +1321,14 @@
if (PyErr_ExceptionMatches(PyExc_UnicodeError)) {
PyObject *type, *value, *tback, *errstr;
PyErr_Fetch(&type, &value, &tback);
- errstr = ((PyUnicodeErrorObject *)value)->reason;
+ errstr = PyObject_Str(value);
if (errstr) {
char *s = "";
char buf[128];
s = _PyUnicode_AsString(errstr);
PyOS_snprintf(buf, sizeof(buf), "(unicode error) %s", s);
ast_error(n, buf);
+ Py_DECREF(errstr);
} else {
ast_error(n, "(unicode error) unknown error");
}
Date: Fri Nov 21 23:58:57 2008
New Revision: 67322
Log:
Merged revisions 67320 via svnmerge from
svn+ssh://pythondev at svn.python.org/python/trunk
........
r67320 | benjamin.peterson | 2008-11-21 16:27:24 -0600 (Fri, 21 Nov 2008) | 4 lines
don't segfault when \N escapes are used and unicodedata fails to load
Fixes #4367
........
Modified:
python/branches/py3k/ (props changed)
python/branches/py3k/Lib/test/test_unicodedata.py
python/branches/py3k/Python/ast.c
Modified: python/branches/py3k/Lib/test/test_unicodedata.py
==============================================================================
--- python/branches/py3k/Lib/test/test_unicodedata.py (original)
+++ python/branches/py3k/Lib/test/test_unicodedata.py Fri Nov 21 23:58:57 2008
@@ -4,9 +4,13 @@
(c) Copyright CNRI, All Rights Reserved. NO WARRANTY.
-"""#"
-import unittest, test.support
+"""
+
+import sys
+import unittest
import hashlib
+import subprocess
+import test.support
encoding = 'utf-8'
@@ -197,6 +201,25 @@
class UnicodeMiscTest(UnicodeDatabaseTest):
+ def test_failed_import_during_compiling(self):
+ # Issue 4367
+ # Decoding \N escapes requires the unicodedata module. If it can't be
+ # imported, we shouldn't segfault.
+
+ # This program should raise a SyntaxError in the eval.
+ code = "import sys;" \
+ "sys.modules['unicodedata'] = None;" \
+ """eval("'\\\\N{SOFT HYPHEN}'")"""
+ args = [sys.executable, "-c", code]
+ # We use a subprocess because the unicodedata module may already have
+ # been loaded in this process.
+ popen = subprocess.Popen(args, stderr=subprocess.PIPE)
+ popen.wait()
+ self.assertEqual(popen.returncode, 1)
+ error = "SyntaxError: (unicode error) \\N escapes not supported " \
+ "(can't load unicodedata module)"
+ self.assertTrue(error in popen.stderr.read().decode("ascii"))
+
def test_decimal_numeric_consistent(self):
# Test that decimal and numeric are consistent,
# i.e. if a character has a decimal value,
Modified: python/branches/py3k/Python/ast.c
==============================================================================
--- python/branches/py3k/Python/ast.c (original)
+++ python/branches/py3k/Python/ast.c Fri Nov 21 23:58:57 2008
@@ -1321,13 +1321,14 @@
if (PyErr_ExceptionMatches(PyExc_UnicodeError)) {
PyObject *type, *value, *tback, *errstr;
PyErr_Fetch(&type, &value, &tback);
- errstr = ((PyUnicodeErrorObject *)value)->reason;
+ errstr = PyObject_Str(value);
if (errstr) {
char *s = "";
char buf[128];
s = _PyUnicode_AsString(errstr);
PyOS_snprintf(buf, sizeof(buf), "(unicode error) %s", s);
ast_error(n, buf);
+ Py_DECREF(errstr);
} else {
ast_error(n, "(unicode error) unknown error");
}