Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Gwenaël Samain
cython
Commits
0d3ea9af
Commit
0d3ea9af
authored
Oct 29, 2017
by
Stefan Behnel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Move docs section on embedding multiple modules further down as it's not a very common use case.
parent
ad0a820a
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
49 additions
and
48 deletions
+49
-48
docs/src/reference/compilation.rst
docs/src/reference/compilation.rst
+49
-48
No files found.
docs/src/reference/compilation.rst
View file @
0d3ea9af
...
...
@@ -45,54 +45,6 @@ A ``yourmod.so`` file is now in the same directory and your module,
``yourmod``, is available for you to import as you normally would.
Integrating multiple modules
============================
It some scenarios, it can be useful to link multiple Cython modules
(or other extension modules) into a single binary, e.g. when embedding
Python in another application. This can be done through the inittab
import mechanism of CPython.
Create a new C file to integrate the extension modules and add this
macro to it::
#if PY_MAJOR_VERSION < 3
# define MODINIT(name) init ## name
#else
# define MODINIT(name) PyInit_ ## name
#endif
If you are only targeting Python 3.x, just use ``PyInit_`` as prefix.
Then, for each or the modules, declare its module init function
as follows, replacing ``...`` by the name of the module::
PyMODINIT_FUNC MODINIT(...) (void);
In C++, declare them as ``extern C``.
If you are not sure of the name of the module init function, refer
to your generated module source file and look for a function name
starting with ``PyInit_``.
Next, before you start the Python runtime from your application code
with ``Py_Initialize()``, you need to initialise the modules at runtime
using the ``PyImport_AppendInittab()`` C-API function, again inserting
the name of each of the modules::
PyImport_AppendInittab("...", MODINIT(...));
This enables normal imports for the embedded extension modules.
In order to prevent the joined binary from exporting all of the module
init functions as public symbols, Cython 0.28 and later can hide these
symbols if the macro ``CYTHON_NO_PYINIT_EXPORT`` is defined while
C-compiling the module C files.
Also take a look at the `cython_freeze
<https://github.com/cython/cython/blob/master/bin/cython_freeze>` tool.
Compiling with ``distutils``
============================
...
...
@@ -357,6 +309,55 @@ e.g.::
These ``.pxd`` files need not have corresponding ``.pyx``
modules if they contain purely declarations of external libraries.
Integrating multiple modules
============================
In some scenarios, it can be useful to link multiple Cython modules
(or other extension modules) into a single binary, e.g. when embedding
Python in another application. This can be done through the inittab
import mechanism of CPython.
Create a new C file to integrate the extension modules and add this
macro to it::
#if PY_MAJOR_VERSION < 3
# define MODINIT(name) init ## name
#else
# define MODINIT(name) PyInit_ ## name
#endif
If you are only targeting Python 3.x, just use ``PyInit_`` as prefix.
Then, for each or the modules, declare its module init function
as follows, replacing ``...`` by the name of the module::
PyMODINIT_FUNC MODINIT(...) (void);
In C++, declare them as ``extern C``.
If you are not sure of the name of the module init function, refer
to your generated module source file and look for a function name
starting with ``PyInit_``.
Next, before you start the Python runtime from your application code
with ``Py_Initialize()``, you need to initialise the modules at runtime
using the ``PyImport_AppendInittab()`` C-API function, again inserting
the name of each of the modules::
PyImport_AppendInittab("...", MODINIT(...));
This enables normal imports for the embedded extension modules.
In order to prevent the joined binary from exporting all of the module
init functions as public symbols, Cython 0.28 and later can hide these
symbols if the macro ``CYTHON_NO_PYINIT_EXPORT`` is defined while
C-compiling the module C files.
Also take a look at the `cython_freeze
<https://github.com/cython/cython/blob/master/bin/cython_freeze>` tool.
Compiling with :mod:`pyximport`
===============================
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment