When writing a Cython implementation file (.pyx), is it possible to define functions that are the __attribute__((__constructor__)) or __attribute__((__destructor__)) of the shared library that Cython will create as the extension module?
It wouldn't make sense to place these as prototype declarations in a header file unless you also then defined them in a C file and compiled it such that the function implementations could be cimported... but then the __constructor__-ness of the function would apply to that compiled library, not to the one compiled by Cython.
Note that I am not referring to __cinit__ or __dealloc__ for extension types but instead to C-specific functions that are designed to run upon library load or unload.
It is not clear to me if the requirements of a module built with the CPython API (e.g. Py_InitModule) prevent the module from having any other function built as an entry hook that is automatically invoked when the library is loaded (or unloaded as for the destructor).
Related
I can make as much out as its build system generates a shared object, and also a Python module as a proxy to that shared object. But how does the Python runtime end up doing a dlopen of the generated shared object and bind Python method calls on to corresponding functions in the shared library?
I also found a reference in Python's import system about shared libraries, but nothing beyond that. Does CPython treat .so files in the module path as Python modules?
When doing import module Python will look for files with various extensions that could be python modules. That can be module.py, but also module.so (on Linux) or module.pyd (on Windows).
When loading a shared object, Python will load it like any dynamic library and then it will call the module's init method: It must be named PyInit_{module_name_here} and exported in the shared library.
You can read more about it here.
I've been learning about Cython recently. I'm still a beginner, but I'm excited about the prospect of using it in the future.
In my mind, Cython has two main uses:
Making Cython or C/C++ functions accessible from Python. See: Calling C functions.
You can expose Cython and C/C++ functions to Python by making a Cython module extension using setuptools.
Making Cython or Python functions accessible from C/C++. See: Embedding Cython modules in C/C++ applications.
You can expose Cython and Python functions to C/C++ by embedding the Python interpreter into your C/C++ application using Py_Initialize and company.
What if you want to do both of these things for the same project?
Is there a way to use Cython to create a shared object (.so) file which can both be imported as a Python module, and linked against from C/C++?
In Python ( CPython) we can import module:
import module and module can be just *.py file ( with a python code) or module can be a module written in C/C++ ( be extending python). So, a such module is just compiled object file ( like *.so/*.o on the Unix).
I would like to know how is it executed by the interpreter exactly.
I think that python module is compiled to a bytecode and then it will be interpreted. In the case of C/C++ module functions from a such module are just executed. So, jump to the address and start execution.
Please correct me if I am wrong/ Please say more.
When you import a C extension, python uses the platform's shared library loader to load the library and then, as you say, jumps to a function in the library. But you can't load just any library or jump to any function this way. It only works for libs specifically implemented to support python and to functions that are exported by the library as a python object. The lib must understand python objects and use those objects to communicate.
Alternately, instead of importing, you can use a foreign-function library like ctypes to load the library and convert data to the C view of data to make calls.
I am wrapping C++ code for use in Python using SWIG. The C++ module I am wrapping has C++ dependencies of other modules located within a different package. However, rather than directly importing/including these files, I would like to import a previously created Python library/dynamic shared library to deal with the dependencies. I want to use this method because I do not want to hard-code files in this package for them to work. I will simply have access to the shared library.
Currently, without importing the library, compiling the new module with the wrapper file results in the error:
"fatal error: string: No such file or directory compilation terminated."
as a header file the new module depends on is not available within this package. I do not want to copy the required headers the new module has a dependency on into this package.
I would like to know if this is possible within either the SWIG interface file or CMake.
Thanks for your help.
I have legacy test scripts written in C for use with Loadrunner. I would like to use Grinder instead of Loadrunner. Grinder provides support for anything in Java which can easily be imported into Jython, Grinder's default programming language.
In order to reuse the test scripts written in C could I wrap them in Cython? (http://en.wikipedia.org/wiki/Cython) Cython is a compiled language which will produce extension modules that can be imported into regular Python or CPython code. Once I have this CPython code, will it work in Grinder or does Grinder specifically need Jython?
The question is whether Grinder can support default Python?
Look in the \data subdirectory under your LoadRunner script and you will find the original header and page source that the script was built from.
You are likely better off writing a preprocessor that pulls the original source for the calls and builds a Mock Grinder Script for modification. if you're really cagey you could pull the dynamic parameterization (correlation) tags from the .c files as well as use the script .prm file and parameter tags to modify the grinder prototype as you build it. If you attempt to use the LoadRunner scripts directly you are going to run into all sorts of issues with missing libraries for the support of the scripts.