Commit ce965c08 authored by AraHaan's avatar AraHaan

Add CYTHON_NO_PYINIT_EXPORT macro. (#1944)

When building the generated code in an embedded interpreter the module
init function will no longer be exported if CYTHON_NO_PYINIT_EXPORT is defined.

See https://github.com/cython/cython/issues/1944 for details.
parent fa5432f2
...@@ -2261,8 +2261,28 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode): ...@@ -2261,8 +2261,28 @@ 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.enter_cfunc_scope(self.scope) code.enter_cfunc_scope(self.scope)
code.putln("") code.putln("")
header2 = "PyMODINIT_FUNC init%s(void)" % env.module_name code.putln("#if PY_MAJOR_VERSION < 3")
header3 = "PyMODINIT_FUNC %s(void)" % self.mod_init_func_cname('PyInit', env) code.putln("#ifdef CYTHON_NO_PYINIT_EXPORT")
code.putln("/* define this to void manually because PyMODINIT_FUNC")
code.putln("* adds __declspec(dllexport) to it's definition.")
code.putln("*/")
code.putln("#define __Pyx_PyMODINIT_FUNC void")
code.putln("#else")
code.putln("#define __Pyx_PyMODINIT_FUNC PyMODINIT_FUNC")
code.putln("#endif")
code.putln("#else")
code.putln("#ifdef CYTHON_NO_PYINIT_EXPORT")
code.putln("/* define this to PyObject * manually because PyMODINIT_FUNC")
code.putln("* adds __declspec(dllexport) to it's definition.")
code.putln("*/")
code.putln("#define __Pyx_PyMODINIT_FUNC PyObject *")
code.putln("#else")
code.putln("#define __Pyx_PyMODINIT_FUNC PyMODINIT_FUNC")
code.putln("#endif")
code.putln("#endif")
code.putln("")
header2 = "__Pyx_PyMODINIT_FUNC init%s(void)" % env.module_name
header3 = "__Pyx_PyMODINIT_FUNC %s(void)" % self.mod_init_func_cname('PyInit', env)
code.putln("#if PY_MAJOR_VERSION < 3") code.putln("#if PY_MAJOR_VERSION < 3")
code.putln("%s; /*proto*/" % header2) code.putln("%s; /*proto*/" % header2)
code.putln(header2) code.putln(header2)
......
...@@ -43,6 +43,44 @@ paths to libraries you need to link with] ...@@ -43,6 +43,44 @@ paths to libraries you need to link with]
A ``yourmod.so`` file is now in the same directory and your module, A ``yourmod.so`` file is now in the same directory and your module,
``yourmod``, is available for you to import as you normally would. ``yourmod``, is available for you to import as you normally would.
Compiling in a embedded interpreter
===================================
Sometimes you might want to use cython to compile a python module you
made into your hand made embedded python. Here is how to do that.
On the top of your C file above the main function you created
type this without the quotes::
PyMODINIT_FUNC PyInit__test(void);
for python 3.x and::
PyMODINIT_FUNC init_test(void);
for python 2.
Where Module Name is the name of the module you cythonized. If you
are not sure on the name of the module init function refer to your
generated module source file.
You need to use ``PyImport_AppendInittab`` but right
before you use ``Py_SetProgramName`` and ``Py_Initialize`` in your
main as well::
PyImport_AppendInittab("_test", PyInit__test);
for python 3.x and::
PyImport_AppendInittab("_test", init_test)
After that is done go in and for all the sources you use define the
following in the compiler's command line::
CYTHON_NO_PYINIT_EXPORT
This macro is to ensure that your module initialization function is
not exported.
Compiling with ``distutils`` Compiling with ``distutils``
============================ ============================
......
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