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

Skip to content

Commit 6f7fad1

Browse files
committed
Merged revisions 67320 via svnmerge from
svn+ssh://[email protected]/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 ........
1 parent 4a98a2a commit 6f7fad1

2 files changed

Lines changed: 27 additions & 3 deletions

File tree

Lib/test/test_unicodedata.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@
44
55
(c) Copyright CNRI, All Rights Reserved. NO WARRANTY.
66
7-
"""#"
8-
import unittest, test.support
7+
"""
8+
9+
import sys
10+
import unittest
911
import hashlib
12+
import subprocess
13+
import test.support
1014

1115
encoding = 'utf-8'
1216

@@ -197,6 +201,25 @@ def test_east_asian_width(self):
197201

198202
class UnicodeMiscTest(UnicodeDatabaseTest):
199203

204+
def test_failed_import_during_compiling(self):
205+
# Issue 4367
206+
# Decoding \N escapes requires the unicodedata module. If it can't be
207+
# imported, we shouldn't segfault.
208+
209+
# This program should raise a SyntaxError in the eval.
210+
code = "import sys;" \
211+
"sys.modules['unicodedata'] = None;" \
212+
"""eval("'\\\\N{SOFT HYPHEN}'")"""
213+
args = [sys.executable, "-c", code]
214+
# We use a subprocess because the unicodedata module may already have
215+
# been loaded in this process.
216+
popen = subprocess.Popen(args, stderr=subprocess.PIPE)
217+
popen.wait()
218+
self.assertEqual(popen.returncode, 1)
219+
error = "SyntaxError: (unicode error) \\N escapes not supported " \
220+
"(can't load unicodedata module)"
221+
self.assertTrue(error in popen.stderr.read().decode("ascii"))
222+
200223
def test_decimal_numeric_consistent(self):
201224
# Test that decimal and numeric are consistent,
202225
# i.e. if a character has a decimal value,

Python/ast.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1321,13 +1321,14 @@ ast_for_atom(struct compiling *c, const node *n)
13211321
if (PyErr_ExceptionMatches(PyExc_UnicodeError)) {
13221322
PyObject *type, *value, *tback, *errstr;
13231323
PyErr_Fetch(&type, &value, &tback);
1324-
errstr = ((PyUnicodeErrorObject *)value)->reason;
1324+
errstr = PyObject_Str(value);
13251325
if (errstr) {
13261326
char *s = "";
13271327
char buf[128];
13281328
s = _PyUnicode_AsString(errstr);
13291329
PyOS_snprintf(buf, sizeof(buf), "(unicode error) %s", s);
13301330
ast_error(n, buf);
1331+
Py_DECREF(errstr);
13311332
} else {
13321333
ast_error(n, "(unicode error) unknown error");
13331334
}

0 commit comments

Comments
 (0)