Commit 8293ea4c authored by Stefan Behnel's avatar Stefan Behnel

support cleanup code for utility code

parent ab03e674
...@@ -184,6 +184,7 @@ class GlobalState(object): ...@@ -184,6 +184,7 @@ class GlobalState(object):
self.pystring_table = rootwriter.new_writer() self.pystring_table = rootwriter.new_writer()
self.init_cached_builtins_writer = rootwriter.new_writer() self.init_cached_builtins_writer = rootwriter.new_writer()
self.initwriter = rootwriter.new_writer() self.initwriter = rootwriter.new_writer()
self.cleanupwriter = rootwriter.new_writer()
if Options.cache_builtins: if Options.cache_builtins:
self.init_cached_builtins_writer.enter_cfunc_scope() self.init_cached_builtins_writer.enter_cfunc_scope()
...@@ -193,6 +194,10 @@ class GlobalState(object): ...@@ -193,6 +194,10 @@ class GlobalState(object):
self.initwriter.putln("") self.initwriter.putln("")
self.initwriter.putln("static int __Pyx_InitGlobals(void) {") self.initwriter.putln("static int __Pyx_InitGlobals(void) {")
self.cleanupwriter.enter_cfunc_scope()
self.cleanupwriter.putln("")
self.cleanupwriter.putln("static void __Pyx_CleanupGlobals(void) {")
self.pystring_table.putln("") self.pystring_table.putln("")
self.pystring_table.putln("static __Pyx_StringTabEntry %s[] = {" % self.pystring_table.putln("static __Pyx_StringTabEntry %s[] = {" %
Naming.stringtab_cname) Naming.stringtab_cname)
...@@ -230,6 +235,10 @@ class GlobalState(object): ...@@ -230,6 +235,10 @@ class GlobalState(object):
w.putln("return -1;") w.putln("return -1;")
w.putln("}") w.putln("}")
w.exit_cfunc_scope() w.exit_cfunc_scope()
w = self.cleanupwriter
w.putln("}")
w.exit_cfunc_scope()
def insert_initcode_into(self, code): def insert_initcode_into(self, code):
if self.pystring_table_needed: if self.pystring_table_needed:
...@@ -238,6 +247,9 @@ class GlobalState(object): ...@@ -238,6 +247,9 @@ class GlobalState(object):
code.insert(self.init_cached_builtins_writer) code.insert(self.init_cached_builtins_writer)
code.insert(self.initwriter) code.insert(self.initwriter)
def insert_cleanupcode_into(self, code):
code.insert(self.cleanupwriter)
def put_pyobject_decl(self, entry): def put_pyobject_decl(self, entry):
self.decls_writer.putln("static PyObject *%s;" % entry.cname) self.decls_writer.putln("static PyObject *%s;" % entry.cname)
...@@ -351,6 +363,7 @@ class GlobalState(object): ...@@ -351,6 +363,7 @@ class GlobalState(object):
if utility_code.impl: if utility_code.impl:
self.utildefwriter.put(utility_code.impl) self.utildefwriter.put(utility_code.impl)
utility_code.write_init_code(self.initwriter, self.module_pos) utility_code.write_init_code(self.initwriter, self.module_pos)
utility_code.write_cleanup_code(self.cleanupwriter, self.module_pos)
def has_code(self, name): def has_code(self, name):
return name in self.used_utility_code return name in self.used_utility_code
......
...@@ -1542,7 +1542,7 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode): ...@@ -1542,7 +1542,7 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode):
code.exit_cfunc_scope() # done with labels code.exit_cfunc_scope() # done with labels
def generate_module_init_func(self, imported_modules, env, code): def generate_module_init_func(self, imported_modules, env, code):
# Insert code stream of __Pyx_InitGlobals # Insert code stream of __Pyx_InitGlobals()
code.globalstate.insert_initcode_into(code) code.globalstate.insert_initcode_into(code)
code.enter_cfunc_scope() code.enter_cfunc_scope()
...@@ -1627,6 +1627,8 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode): ...@@ -1627,6 +1627,8 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode):
return return
env.use_utility_code(import_module_utility_code) env.use_utility_code(import_module_utility_code)
env.use_utility_code(register_cleanup_utility_code) env.use_utility_code(register_cleanup_utility_code)
# Insert code stream of __Pyx_CleanupGlobals()
code.globalstate.insert_cleanupcode_into(code)
code.putln() code.putln()
code.putln('static PyObject* %s(PyObject *self, PyObject *unused) {' % Naming.cleanup_cname) code.putln('static PyObject* %s(PyObject *self, PyObject *unused) {' % Naming.cleanup_cname)
if Options.generate_cleanup_code >= 2: if Options.generate_cleanup_code >= 2:
...@@ -1637,6 +1639,7 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode): ...@@ -1637,6 +1639,7 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode):
if entry.visibility != 'extern': if entry.visibility != 'extern':
if entry.type.is_pyobject and entry.used: if entry.type.is_pyobject and entry.used:
code.put_var_decref_clear(entry) code.put_var_decref_clear(entry)
code.putln("__Pyx_CleanupGlobals();")
if Options.generate_cleanup_code >= 3: if Options.generate_cleanup_code >= 3:
code.putln("/*--- Type import cleanup code ---*/") code.putln("/*--- Type import cleanup code ---*/")
for type, _ in env.types_imported.items(): for type, _ in env.types_imported.items():
......
...@@ -105,3 +105,11 @@ class UtilityCode(object): ...@@ -105,3 +105,11 @@ class UtilityCode(object):
self.init(writer, pos) self.init(writer, pos)
else: else:
writer.put(self.init) writer.put(self.init)
def write_cleanup_code(self, writer, pos):
if not self.cleanup:
return
if callable(self.cleanup):
self.cleanup(writer, pos)
else:
writer.put(self.cleanup)
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