self.pystring_table = rootwriter.new_writer()
self.init_cached_builtins_writer = rootwriter.new_writer()
self.initwriter = rootwriter.new_writer()
+ self.cleanupwriter = rootwriter.new_writer()
if Options.cache_builtins:
self.init_cached_builtins_writer.enter_cfunc_scope()
self.initwriter.putln("")
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("static __Pyx_StringTabEntry %s[] = {" %
Naming.stringtab_cname)
w.putln("return -1;")
w.putln("}")
w.exit_cfunc_scope()
+
+ w = self.cleanupwriter
+ w.putln("}")
+ w.exit_cfunc_scope()
def insert_initcode_into(self, code):
if self.pystring_table_needed:
code.insert(self.init_cached_builtins_writer)
code.insert(self.initwriter)
+ def insert_cleanupcode_into(self, code):
+ code.insert(self.cleanupwriter)
+
def put_pyobject_decl(self, entry):
self.decls_writer.putln("static PyObject *%s;" % entry.cname)
if utility_code.impl:
self.utildefwriter.put(utility_code.impl)
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):
return name in self.used_utility_code
code.exit_cfunc_scope() # done with labels
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.enter_cfunc_scope()
return
env.use_utility_code(import_module_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('static PyObject* %s(PyObject *self, PyObject *unused) {' % Naming.cleanup_cname)
if Options.generate_cleanup_code >= 2:
if entry.visibility != 'extern':
if entry.type.is_pyobject and entry.used:
code.put_var_decref_clear(entry)
+ code.putln("__Pyx_CleanupGlobals();")
if Options.generate_cleanup_code >= 3:
code.putln("/*--- Type import cleanup code ---*/")
for type, _ in env.types_imported.items():
self.init(writer, pos)
else:
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)