summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAraHaan <seandhunt_7@yahoo.com>2017-10-21 10:13:27 -0400
committerAraHaan <seandhunt_7@yahoo.com>2017-10-21 11:36:13 -0400
commitce965c0873e7246b185cec450100541ddca60486 (patch)
treeb6e2c79a7c17a7c80701930c6c959f1d2a625a10
parentfa5432f22b9030a59acf6ed54656ff76b0149146 (diff)
downloadcython-ce965c0873e7246b185cec450100541ddca60486.tar.gz
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.
-rw-r--r--Cython/Compiler/ModuleNode.py24
-rw-r--r--docs/src/reference/compilation.rst38
2 files changed, 60 insertions, 2 deletions
diff --git a/Cython/Compiler/ModuleNode.py b/Cython/Compiler/ModuleNode.py
index eb03230ac..e82e4c40c 100644
--- a/Cython/Compiler/ModuleNode.py
+++ b/Cython/Compiler/ModuleNode.py
@@ -2261,8 +2261,28 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode):
def generate_module_init_func(self, imported_modules, env, code):
code.enter_cfunc_scope(self.scope)
code.putln("")
- header2 = "PyMODINIT_FUNC init%s(void)" % env.module_name
- header3 = "PyMODINIT_FUNC %s(void)" % self.mod_init_func_cname('PyInit', env)
+ code.putln("#if PY_MAJOR_VERSION < 3")
+ 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("%s; /*proto*/" % header2)
code.putln(header2)
diff --git a/docs/src/reference/compilation.rst b/docs/src/reference/compilation.rst
index f520873ef..3f1334796 100644
--- a/docs/src/reference/compilation.rst
+++ b/docs/src/reference/compilation.rst
@@ -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,
``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``
============================