Commit 1edfca7f authored by Stefan Behnel's avatar Stefan Behnel

quick work-around for PEP 3121 implementation in Py3 beta

parent 59cbd0ad
...@@ -247,6 +247,7 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode): ...@@ -247,6 +247,7 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode):
self.generate_filename_init_prototype(code) self.generate_filename_init_prototype(code)
if env.has_import_star: if env.has_import_star:
self.generate_import_star(env, code) self.generate_import_star(env, code)
self.generate_pymoduledef_struct(env, code)
self.generate_module_init_func(modules[:-1], env, code) self.generate_module_init_func(modules[:-1], env, code)
code.mark_pos(None) code.mark_pos(None)
self.generate_module_cleanup_func(env, code) self.generate_module_cleanup_func(env, code)
...@@ -1516,9 +1517,17 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode): ...@@ -1516,9 +1517,17 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode):
def generate_module_init_func(self, imported_modules, env, code): def generate_module_init_func(self, imported_modules, env, code):
code.putln("") code.putln("")
header = "PyMODINIT_FUNC init%s(void)" % env.module_name header2 = "PyMODINIT_FUNC init%s(void)" % env.module_name
code.putln("%s; /*proto*/" % header) header3 = "PyMODINIT_FUNC PyInit_%s(void)" % env.module_name
code.putln("%s {" % header) code.putln("#if PY_MAJOR_VERSION < 3")
code.putln("%s; /*proto*/" % header2)
code.putln(header2)
code.putln("#else")
code.putln("%s; /*proto*/" % header3)
code.putln(header3)
code.putln("#endif")
code.putln("{")
code.put_var_declarations(env.temp_entries) code.put_var_declarations(env.temp_entries)
code.putln("%s = PyTuple_New(0); %s" % (Naming.empty_tuple, code.error_goto_if_null(Naming.empty_tuple, self.pos))); code.putln("%s = PyTuple_New(0); %s" % (Naming.empty_tuple, code.error_goto_if_null(Naming.empty_tuple, self.pos)));
...@@ -1563,13 +1572,21 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode): ...@@ -1563,13 +1572,21 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode):
self.body.generate_execution_code(code) self.body.generate_execution_code(code)
if Options.generate_cleanup_code: if Options.generate_cleanup_code:
# this should be replaced by the module's tp_clear in Py3
code.putln("if (__Pyx_RegisterCleanup()) %s;" % code.error_goto(self.pos)) code.putln("if (__Pyx_RegisterCleanup()) %s;" % code.error_goto(self.pos))
code.putln("#if PY_MAJOR_VERSION < 3")
code.putln("return;") code.putln("return;")
code.putln("#else")
code.putln("return %s;" % env.module_cname)
code.putln("#endif")
code.put_label(code.error_label) code.put_label(code.error_label)
code.put_var_xdecrefs(env.temp_entries) code.put_var_xdecrefs(env.temp_entries)
code.putln('__Pyx_AddTraceback("%s");' % env.qualified_name) code.putln('__Pyx_AddTraceback("%s");' % env.qualified_name)
env.use_utility_code(Nodes.traceback_utility_code) env.use_utility_code(Nodes.traceback_utility_code)
code.putln("#if PY_MAJOR_VERSION >= 3")
code.putln("return NULL;")
code.putln("#endif")
code.putln('}') code.putln('}')
def generate_module_cleanup_func(self, env, code): def generate_module_cleanup_func(self, env, code):
...@@ -1610,6 +1627,26 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode): ...@@ -1610,6 +1627,26 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode):
def generate_filename_init_call(self, code): def generate_filename_init_call(self, code):
code.putln("%s();" % Naming.fileinit_cname) code.putln("%s();" % Naming.fileinit_cname)
def generate_pymoduledef_struct(self, env, code):
if env.doc:
doc = env.doc_cname
else:
doc = "0"
code.putln("")
code.putln("#if PY_MAJOR_VERSION >= 3")
code.putln("static struct PyModuleDef %s = {" % Naming.pymoduledef_cname)
code.putln(" PyModuleDef_HEAD_INIT,")
code.putln(' "%s",' % env.module_name)
code.putln(" %s, /* m_doc */" % doc)
code.putln(" -1, /* m_size */")
code.putln(" %s /* m_methods */," % env.method_table_cname)
code.putln(" NULL, /* m_reload */")
code.putln(" NULL, /* m_traverse */")
code.putln(" NULL, /* m_clear */")
code.putln(" NULL /* m_free */")
code.putln("};")
code.putln("#endif")
def generate_module_creation_code(self, env, code): def generate_module_creation_code(self, env, code):
# Generate code to create the module object and # Generate code to create the module object and
# install the builtins. # install the builtins.
...@@ -1617,19 +1654,28 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode): ...@@ -1617,19 +1654,28 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode):
doc = env.doc_cname doc = env.doc_cname
else: else:
doc = "0" doc = "0"
code.putln("#if PY_MAJOR_VERSION < 3")
code.putln( code.putln(
'%s = Py_InitModule4("%s", %s, %s, 0, PYTHON_API_VERSION);' % ( '%s = Py_InitModule4("%s", %s, %s, 0, PYTHON_API_VERSION);' % (
env.module_cname, env.module_cname,
env.module_name, env.module_name,
env.method_table_cname, env.method_table_cname,
doc)) doc))
code.putln("#else")
code.putln(
"%s = PyModule_Create(&%s);" % (
env.module_cname,
Naming.pymoduledef_cname))
code.putln("#endif")
code.putln( code.putln(
"if (!%s) %s;" % ( "if (!%s) %s;" % (
env.module_cname, env.module_cname,
code.error_goto(self.pos))); code.error_goto(self.pos)));
code.putln("#if PY_MAJOR_VERSION < 3")
code.putln( code.putln(
"Py_INCREF(%s);" % "Py_INCREF(%s);" %
env.module_cname) env.module_cname)
code.putln("#endif")
code.putln( code.putln(
'%s = PyImport_AddModule(__Pyx_BUILTIN_MODULE_NAME);' % '%s = PyImport_AddModule(__Pyx_BUILTIN_MODULE_NAME);' %
Naming.builtins_cname) Naming.builtins_cname)
......
...@@ -65,6 +65,7 @@ empty_tuple = pyrex_prefix + "empty_tuple" ...@@ -65,6 +65,7 @@ empty_tuple = pyrex_prefix + "empty_tuple"
print_function = pyrex_prefix + "print" print_function = pyrex_prefix + "print"
print_function_kwargs = pyrex_prefix + "print_kwargs" print_function_kwargs = pyrex_prefix + "print_kwargs"
cleanup_cname = pyrex_prefix + "module_cleanup" cleanup_cname = pyrex_prefix + "module_cleanup"
pymoduledef_cname = pyrex_prefix + "moduledef"
optional_args_cname = pyrex_prefix + "optional_args" optional_args_cname = pyrex_prefix + "optional_args"
no_opt_args = pyrex_prefix + "no_opt_args" no_opt_args = pyrex_prefix + "no_opt_args"
import_star = pyrex_prefix + "import_star" import_star = pyrex_prefix + "import_star"
......
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