diff options
author | da-woods <dw-git@d-woods.co.uk> | 2021-09-27 10:11:12 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-09-27 11:11:12 +0200 |
commit | fa8db66368a9527c8df441a0bbfee714eb795930 (patch) | |
tree | 0f74d5fb9c78a64b836874191a2f1ad43b0a3db3 /Cython/Compiler/ModuleNode.py | |
parent | f94f26a073ee5c9987ccab6acb87ad453c6ec625 (diff) | |
download | cython-fa8db66368a9527c8df441a0bbfee714eb795930.tar.gz |
Avoid AddTraceback() if stringtab isn't set up (GH-4378)
This can happen (rarely) with exceptions that occur very early in the module init process.
Fixes https://github.com/cython/cython/issues/4377
Uses a fake Numpy module for testing to make a version of "import_array" that always fails.
Diffstat (limited to 'Cython/Compiler/ModuleNode.py')
-rw-r--r-- | Cython/Compiler/ModuleNode.py | 11 |
1 files changed, 8 insertions, 3 deletions
diff --git a/Cython/Compiler/ModuleNode.py b/Cython/Compiler/ModuleNode.py index d0687624f..99b2f0028 100644 --- a/Cython/Compiler/ModuleNode.py +++ b/Cython/Compiler/ModuleNode.py @@ -2939,7 +2939,7 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode): # start of module init/exec function (pre/post PEP 489) code.putln("{") - + code.putln('int stringtab_initialized = 0;') tempdecl_code = code.insertion_point() profile = code.globalstate.directives['profile'] @@ -3012,7 +3012,10 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode): code.putln("#endif") code.putln("/*--- Initialize various global constants etc. ---*/") - code.put_error_if_neg(self.pos, "__Pyx_InitGlobals()") + code.put_error_if_neg(self.pos, "__Pyx_InitConstants()") + code.putln("stringtab_initialized = 1;") + code.put_error_if_neg(self.pos, "__Pyx_InitGlobals()") # calls any utility code + code.putln("#if PY_MAJOR_VERSION < 3 && (__PYX_DEFAULT_STRING_ENCODING_IS_ASCII || " "__PYX_DEFAULT_STRING_ENCODING_IS_DEFAULT)") @@ -3095,7 +3098,9 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode): for cname, type in code.funcstate.all_managed_temps(): code.put_xdecref(cname, type) code.putln('if (%s) {' % env.module_cname) - code.putln('if (%s) {' % env.module_dict_cname) + code.putln('if (%s && stringtab_initialized) {' % env.module_dict_cname) + # We can run into errors before the module or stringtab are initialized. + # In this case it is not safe to add a traceback (because it uses the stringtab) code.put_add_traceback(EncodedString("init %s" % env.qualified_name)) code.globalstate.use_utility_code(Nodes.traceback_utility_code) # Module reference and module dict are in global variables which might still be needed |