From d2fd48faf93872ca7dc9ac96b7e21a27d68ac479 Mon Sep 17 00:00:00 2001 From: Hai Shi Date: Wed, 5 Feb 2020 20:45:26 +0800 Subject: [PATCH 1/2] Calling Py_DECREF() if PyModule_AddObject() fails to run --- Python/Python-ast.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Python/Python-ast.c b/Python/Python-ast.c index d5465d795cfa4d..7c981cfa7fb127 100644 --- a/Python/Python-ast.c +++ b/Python/Python-ast.c @@ -9888,8 +9888,10 @@ PyInit__ast(void) m = PyState_FindModule(&_astmodule); if (!m) return NULL; Py_INCREF(astmodulestate(m)->AST_type); - if (PyModule_AddObject(m, "AST", astmodulestate_global->AST_type) < 0) + if (PyModule_AddObject(m, "AST", astmodulestate_global->AST_type) < 0) { + Py_DECREF(astmodulestate(m)->AST_type); return NULL; + } if (PyModule_AddIntMacro(m, PyCF_ALLOW_TOP_LEVEL_AWAIT) < 0) return NULL; if (PyModule_AddIntMacro(m, PyCF_ONLY_AST) < 0) From c00cfa73a7dc4d5903278c1e614da17b40a341fb Mon Sep 17 00:00:00 2001 From: Hai Shi Date: Wed, 5 Feb 2020 21:00:02 +0800 Subject: [PATCH 2/2] update asdl_c.py --- Parser/asdl_c.py | 2 +- Python/Python-ast.c | 6 ++---- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/Parser/asdl_c.py b/Parser/asdl_c.py index daac0966f564a4..e899df93dbde89 100755 --- a/Parser/asdl_c.py +++ b/Parser/asdl_c.py @@ -998,8 +998,8 @@ def visitModule(self, mod): self.emit("if (!init_types()) return NULL;", 1) self.emit('m = PyState_FindModule(&_astmodule);', 1) self.emit("if (!m) return NULL;", 1) - self.emit('Py_INCREF(astmodulestate(m)->AST_type);', 1) self.emit('if (PyModule_AddObject(m, "AST", astmodulestate_global->AST_type) < 0) return NULL;', 1) + self.emit('Py_INCREF(astmodulestate(m)->AST_type);', 1) self.emit('if (PyModule_AddIntMacro(m, PyCF_ALLOW_TOP_LEVEL_AWAIT) < 0)', 1) self.emit("return NULL;", 2) self.emit('if (PyModule_AddIntMacro(m, PyCF_ONLY_AST) < 0)', 1) diff --git a/Python/Python-ast.c b/Python/Python-ast.c index 7c981cfa7fb127..048a5440de00cc 100644 --- a/Python/Python-ast.c +++ b/Python/Python-ast.c @@ -9887,11 +9887,9 @@ PyInit__ast(void) if (!init_types()) return NULL; m = PyState_FindModule(&_astmodule); if (!m) return NULL; - Py_INCREF(astmodulestate(m)->AST_type); - if (PyModule_AddObject(m, "AST", astmodulestate_global->AST_type) < 0) { - Py_DECREF(astmodulestate(m)->AST_type); + if (PyModule_AddObject(m, "AST", astmodulestate_global->AST_type) < 0) return NULL; - } + Py_INCREF(astmodulestate(m)->AST_type); if (PyModule_AddIntMacro(m, PyCF_ALLOW_TOP_LEVEL_AWAIT) < 0) return NULL; if (PyModule_AddIntMacro(m, PyCF_ONLY_AST) < 0)