Commit f6c590d3 authored by Stefan Behnel's avatar Stefan Behnel

reduce code redundancy in Py2/3 cleanup code setup

parent 1bb2ba41
...@@ -1975,15 +1975,8 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode): ...@@ -1975,15 +1975,8 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode):
if not Options.generate_cleanup_code: if not Options.generate_cleanup_code:
return return
code.putln("#if PY_MAJOR_VERSION >= 3") code.putln('static void %s(CYTHON_UNUSED PyObject *self) {' %
code.putln('static void %s(CYTHON_UNUSED PyObject *self)' %
Naming.cleanup_cname)
code.putln("#else")
code.putln('static PyObject *%s(CYTHON_UNUSED PyObject *self, CYTHON_UNUSED PyObject *unused)' %
Naming.cleanup_cname) Naming.cleanup_cname)
code.putln("#endif")
code.putln("{")
if Options.generate_cleanup_code >= 2: if Options.generate_cleanup_code >= 2:
code.putln("/*--- Global cleanup code ---*/") code.putln("/*--- Global cleanup code ---*/")
rev_entries = list(env.var_entries) rev_entries = list(env.var_entries)
...@@ -2026,10 +2019,6 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode): ...@@ -2026,10 +2019,6 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode):
code.putln('Py_CLEAR(%s);' % Naming.builtins_cname) code.putln('Py_CLEAR(%s);' % Naming.builtins_cname)
code.putln('#endif') code.putln('#endif')
code.putln("#if PY_MAJOR_VERSION < 3")
code.putln("Py_INCREF(Py_None); return Py_None;")
code.putln('#endif')
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('.', '__'))
if Options.embed == "main": if Options.embed == "main":
...@@ -2048,13 +2037,13 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode): ...@@ -2048,13 +2037,13 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode):
doc = "__Pyx_DOCSTR(%s)" % code.get_string_const(env.doc) doc = "__Pyx_DOCSTR(%s)" % code.get_string_const(env.doc)
else: else:
doc = "0" doc = "0"
code.putln("")
code.putln("#if PY_MAJOR_VERSION >= 3")
if Options.generate_cleanup_code: if Options.generate_cleanup_code:
cleanup_func = "(freefunc)%s" % Naming.cleanup_cname cleanup_func = "(freefunc)%s" % Naming.cleanup_cname
code.putln("static void %s(PyObject *self); /*proto*/" % Naming.cleanup_cname)
else: else:
cleanup_func = 'NULL' cleanup_func = 'NULL'
code.putln("")
code.putln("#if PY_MAJOR_VERSION >= 3")
code.putln("static struct PyModuleDef %s = {" % Naming.pymoduledef_cname) code.putln("static struct PyModuleDef %s = {" % Naming.pymoduledef_cname)
code.putln(" PyModuleDef_HEAD_INIT,") code.putln(" PyModuleDef_HEAD_INIT,")
code.putln(' __Pyx_NAMESTR("%s"),' % env.module_name) code.putln(' __Pyx_NAMESTR("%s"),' % env.module_name)
......
...@@ -478,16 +478,19 @@ end: ...@@ -478,16 +478,19 @@ end:
/////////////// RegisterModuleCleanup.proto /////////////// /////////////// RegisterModuleCleanup.proto ///////////////
//@substitute: naming //@substitute: naming
static void ${cleanup_cname}(PyObject *self); /*proto*/
static int __Pyx_RegisterCleanup(void); /*proto*/ static int __Pyx_RegisterCleanup(void); /*proto*/
#if PY_MAJOR_VERSION < 3
static PyObject* ${cleanup_cname}(PyObject *self, PyObject *unused); /*proto*/
#endif
/////////////// RegisterModuleCleanup /////////////// /////////////// RegisterModuleCleanup ///////////////
//@substitute: naming //@substitute: naming
//@requires: ImportExport.c::ModuleImport //@requires: ImportExport.c::ModuleImport
#if PY_MAJOR_VERSION < 3 #if PY_MAJOR_VERSION < 3
static PyObject* ${cleanup_cname}_atexit(PyObject *module, CYTHON_UNUSED PyObject *unused) {
${cleanup_cname}(module);
Py_INCREF(Py_None); return Py_None;
}
static int __Pyx_RegisterCleanup(void) { static int __Pyx_RegisterCleanup(void) {
// Don't use Py_AtExit because that has a 32-call limit and is called // Don't use Py_AtExit because that has a 32-call limit and is called
// after python finalization. // after python finalization.
...@@ -496,7 +499,8 @@ static int __Pyx_RegisterCleanup(void) { ...@@ -496,7 +499,8 @@ static int __Pyx_RegisterCleanup(void) {
// user exit code to run before us that may depend on the globals // user exit code to run before us that may depend on the globals
// and cached objects that we are about to clean up. // and cached objects that we are about to clean up.
static PyMethodDef cleanup_def = {__Pyx_NAMESTR("__cleanup"), (PyCFunction)${cleanup_cname}, METH_NOARGS, 0}; static PyMethodDef cleanup_def = {
__Pyx_NAMESTR("__cleanup"), (PyCFunction)${cleanup_cname}_atexit, METH_NOARGS, 0};
PyObject *cleanup_func = 0; PyObject *cleanup_func = 0;
PyObject *atexit = 0; PyObject *atexit = 0;
...@@ -528,8 +532,7 @@ static int __Pyx_RegisterCleanup(void) { ...@@ -528,8 +532,7 @@ static int __Pyx_RegisterCleanup(void) {
if (!args) if (!args)
goto bad; goto bad;
ret = PyList_Insert(reg, 0, args); ret = PyList_Insert(reg, 0, args);
} else } else {
{
if (!reg) if (!reg)
PyErr_Clear(); PyErr_Clear();
Py_XDECREF(reg); Py_XDECREF(reg);
......
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