summaryrefslogtreecommitdiffstats
path: root/Python/ast.c
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2017-09-30 19:51:37 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2017-09-30 19:51:37 (GMT)
commita4dfe1c9eaca6faf206f63a178233ffea208c906 (patch)
tree65a56aaf6c606b02231600d21ffde9bcc07609ab /Python/ast.c
parent9ef28b6ad3d5aff767e3d852499def8b5ae5ff5d (diff)
downloadcpython-a4dfe1c9eaca6faf206f63a178233ffea208c906.zip
cpython-a4dfe1c9eaca6faf206f63a178233ffea208c906.tar.gz
cpython-a4dfe1c9eaca6faf206f63a178233ffea208c906.tar.bz2
[3.6] bpo-31592: Fix an assertion failure in Python parser in case of a bad unicodedata.normalize(). (GH-3767) (#3836)
(cherry picked from commit 7dc46d8cf5854d9f4ce3271b29c21aea4872e8ad)
Diffstat (limited to 'Python/ast.c')
-rw-r--r--Python/ast.c28
1 files changed, 16 insertions, 12 deletions
diff --git a/Python/ast.c b/Python/ast.c
index aa4acc9..2973b9f 100644
--- a/Python/ast.c
+++ b/Python/ast.c
@@ -589,7 +589,6 @@ struct compiling {
PyArena *c_arena; /* Arena for allocating memory. */
PyObject *c_filename; /* filename */
PyObject *c_normalize; /* Normalization function from unicodedata. */
- PyObject *c_normalize_args; /* Normalization argument tuple. */
};
static asdl_seq *seq_for_testlist(struct compiling *, const node *);
@@ -624,12 +623,6 @@ init_normalization(struct compiling *c)
Py_DECREF(m);
if (!c->c_normalize)
return 0;
- c->c_normalize_args = Py_BuildValue("(sN)", "NFKC", Py_None);
- if (!c->c_normalize_args) {
- Py_CLEAR(c->c_normalize);
- return 0;
- }
- PyTuple_SET_ITEM(c->c_normalize_args, 1, NULL);
return 1;
}
@@ -645,15 +638,29 @@ new_identifier(const char *n, struct compiling *c)
identifier; if so, normalize to NFKC. */
if (!PyUnicode_IS_ASCII(id)) {
PyObject *id2;
+ _Py_IDENTIFIER(NFKC);
if (!c->c_normalize && !init_normalization(c)) {
Py_DECREF(id);
return NULL;
}
- PyTuple_SET_ITEM(c->c_normalize_args, 1, id);
- id2 = PyObject_Call(c->c_normalize, c->c_normalize_args, NULL);
+ PyObject *form = _PyUnicode_FromId(&PyId_NFKC);
+ if (form == NULL) {
+ Py_DECREF(id);
+ return NULL;
+ }
+ PyObject *args[2] = {form, id};
+ id2 = _PyObject_FastCall(c->c_normalize, args, 2);
Py_DECREF(id);
if (!id2)
return NULL;
+ if (!PyUnicode_Check(id2)) {
+ PyErr_Format(PyExc_TypeError,
+ "unicodedata.normalize() must return a string, not "
+ "%.200s",
+ Py_TYPE(id2)->tp_name);
+ Py_DECREF(id2);
+ return NULL;
+ }
id = id2;
}
PyUnicode_InternInPlace(&id);
@@ -773,7 +780,6 @@ PyAST_FromNodeObject(const node *n, PyCompilerFlags *flags,
/* borrowed reference */
c.c_filename = filename;
c.c_normalize = NULL;
- c.c_normalize_args = NULL;
if (TYPE(n) == encoding_decl)
n = CHILD(n, 0);
@@ -866,8 +872,6 @@ PyAST_FromNodeObject(const node *n, PyCompilerFlags *flags,
out:
if (c.c_normalize) {
Py_DECREF(c.c_normalize);
- PyTuple_SET_ITEM(c.c_normalize_args, 1, NULL);
- Py_DECREF(c.c_normalize_args);
}
return res;
}