[Python-checkins] r67320 - in python/trunk: Lib/test/test_unicodedata.py Misc/NEWS Python/ast.c

benjamin.peterson python-checkins at python.org
Fri Nov 21 23:27:24 CET 2008


Author: benjamin.peterson
Date: Fri Nov 21 23:27:24 2008
New Revision: 67320

Log:
don't segfault when \N escapes are used and unicodedata fails to load

Fixes #4367


Modified:
   python/trunk/Lib/test/test_unicodedata.py
   python/trunk/Misc/NEWS
   python/trunk/Python/ast.c

Modified: python/trunk/Lib/test/test_unicodedata.py
==============================================================================
--- python/trunk/Lib/test/test_unicodedata.py	(original)
+++ python/trunk/Lib/test/test_unicodedata.py	Fri Nov 21 23:27:24 2008
@@ -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 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,

Modified: python/trunk/Misc/NEWS
==============================================================================
--- python/trunk/Misc/NEWS	(original)
+++ python/trunk/Misc/NEWS	Fri Nov 21 23:27:24 2008
@@ -12,6 +12,9 @@
 Core and Builtins
 -----------------
 
+- Issue #4367: Python would segfault during compiling when the unicodedata
+  module couldn't be imported and \N escapes were present.
+
 - Issue #4233: Changed semantic of ``_fileio.FileIO``'s ``close()`` 
   method on file objects with closefd=False. The file descriptor is still
   kept open but the file object behaves like a closed file. The ``FileIO``

Modified: python/trunk/Python/ast.c
==============================================================================
--- python/trunk/Python/ast.c	(original)
+++ python/trunk/Python/ast.c	Fri Nov 21 23:27:24 2008
@@ -1294,13 +1294,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 = 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");
                 }


More information about the Python-checkins mailing list