Commit 566fc5c0 authored by Stefan Behnel's avatar Stefan Behnel

Keep owned references to builtins module, "cython_runtime" module and...

Keep owned references to builtins module, "cython_runtime" module and preimport module to prevent crashes when they get removed from sys.modules. Generate cleanup code for them in the end.
Closes #2885.
parent 7941b93a
...@@ -2704,11 +2704,11 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode): ...@@ -2704,11 +2704,11 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode):
# if entry.type.is_pyobject and entry.used: # if entry.type.is_pyobject and entry.used:
# code.putln("Py_DECREF(%s); %s = 0;" % ( # code.putln("Py_DECREF(%s); %s = 0;" % (
# code.entry_as_pyobject(entry), entry.cname)) # code.entry_as_pyobject(entry), entry.cname))
code.putln('#if CYTHON_COMPILING_IN_PYPY') if Options.pre_import is not None:
code.putln('Py_CLEAR(%s);' % Naming.builtins_cname) code.put_decref_clear(Naming.preimport_cname, py_object_type,
code.putln('#endif') nanny=False, clear_before_decref=True)
code.put_decref_clear(env.module_dict_cname, py_object_type, for cname in [Naming.cython_runtime_cname, env.module_dict_cname, Naming.builtins_cname]:
nanny=False, clear_before_decref=True) code.put_decref_clear(cname, py_object_type, nanny=False, clear_before_decref=True)
def generate_main_method(self, env, code): def generate_main_method(self, env, code):
module_is_main = "%s%s" % (Naming.module_is_main, self.full_module_name.replace('.', '__')) module_is_main = "%s%s" % (Naming.module_is_main, self.full_module_name.replace('.', '__'))
...@@ -2815,13 +2815,12 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode): ...@@ -2815,13 +2815,12 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode):
'%s = PyImport_AddModule(__Pyx_BUILTIN_MODULE_NAME); %s' % ( '%s = PyImport_AddModule(__Pyx_BUILTIN_MODULE_NAME); %s' % (
Naming.builtins_cname, Naming.builtins_cname,
code.error_goto_if_null(Naming.builtins_cname, self.pos))) code.error_goto_if_null(Naming.builtins_cname, self.pos)))
code.put_incref(Naming.builtins_cname, py_object_type, nanny=False)
code.putln( code.putln(
'%s = PyImport_AddModule((char *) "cython_runtime"); %s' % ( '%s = PyImport_AddModule((char *) "cython_runtime"); %s' % (
Naming.cython_runtime_cname, Naming.cython_runtime_cname,
code.error_goto_if_null(Naming.cython_runtime_cname, self.pos))) code.error_goto_if_null(Naming.cython_runtime_cname, self.pos)))
code.putln('#if CYTHON_COMPILING_IN_PYPY') code.put_incref(Naming.cython_runtime_cname, py_object_type, nanny=False)
code.putln('Py_INCREF(%s);' % Naming.builtins_cname)
code.putln('#endif')
code.putln( code.putln(
'if (PyObject_SetAttrString(%s, "__builtins__", %s) < 0) %s;' % ( 'if (PyObject_SetAttrString(%s, "__builtins__", %s) < 0) %s;' % (
env.module_cname, env.module_cname,
...@@ -2833,6 +2832,7 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode): ...@@ -2833,6 +2832,7 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode):
Naming.preimport_cname, Naming.preimport_cname,
Options.pre_import, Options.pre_import,
code.error_goto_if_null(Naming.preimport_cname, self.pos))) code.error_goto_if_null(Naming.preimport_cname, self.pos)))
code.put_incref(Naming.preimport_cname, py_object_type, nanny=False)
def generate_global_init_code(self, env, code): def generate_global_init_code(self, env, code):
# Generate code to initialise global PyObject * # Generate code to initialise global PyObject *
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment