]> granicus.if.org Git - python/commitdiff
Merged revisions 67320 via svnmerge from
authorBenjamin Peterson <benjamin@python.org>
Fri, 21 Nov 2008 22:52:21 +0000 (22:52 +0000)
committerBenjamin Peterson <benjamin@python.org>
Fri, 21 Nov 2008 22:52:21 +0000 (22:52 +0000)
svn+ssh://pythondev@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
........

Lib/test/test_unicodedata.py
Misc/NEWS
Python/ast.c

index b24e8f7981e40f4bc5d74a93ff1b0d68c82e1206..84999e57b364c4747f32dec3c944e5755fa837ba 100644 (file)
@@ -4,9 +4,13 @@
 
     (c) Copyright CNRI, All Rights Reserved. NO WARRANTY.
 
-"""#"
-import unittest, test.test_support
+"""
+
+import sys
+import unittest
 import hashlib
+import subprocess
+import test.test_support
 
 encoding = 'utf-8'
 
@@ -196,6 +200,25 @@ class UnicodeFunctionsTest(UnicodeDatabaseTest):
 
 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("u'\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())
+
     def test_decimal_numeric_consistent(self):
         # Test that decimal and numeric are consistent,
         # i.e. if a character has a decimal value,
index e7fa503130f7f04f7dfd47c98dad5b5f99e77944..c7312baf15f31121c1426b9fd9dfb9d08960b235 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,9 @@ What's New in Python 2.6.1 alpha 1
 Core and Builtins
 -----------------
 
+- Issue #4367: Python would segfault during compiling when the unicodedata
+  module couldn't be imported and \N escapes were present.
+
 - Issue #4348: Some bytearray methods returned that didn't cause any change to
   the bytearray, returned the same bytearray instead of a copy.
 
index 8414c7448081832f8431f87c78b4b487b635501f..9673cec2dbbc732beafd50151e0e31cd45094169 100644 (file)
@@ -1287,13 +1287,14 @@ ast_for_atom(struct compiling *c, const node *n)
             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 = PyString_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");
                 }