python-c-extension

In Python how can one tell if a module comes from a C extension?

不羁岁月 提交于 2019-11-30 13:57:52
What is the correct or most robust way to tell from Python if an imported module comes from a C extension as opposed to a pure Python module? This is useful, for example, if a Python package has a module with both a pure Python implementation and a C implementation, and you want to be able to tell at runtime which one is being used. One idea is to examine the file extension of module.__file__ , but I'm not sure all the file extensions one should check for and if this approach is necessarily the most reliable. First, I don't think this is at all useful. It's very common for modules to be pure

Difference between PyMODINIT_FUNC and PyModule_Create

馋奶兔 提交于 2019-11-30 01:24:13
问题 If I'm understanding correctly, PyMODINIT_FUNC in Python 2.X has been replaced by PyModule_Create in Python3.X Both return PyObject* , however, in Python 3.X, the module's initialization function MUST return the PyObject* to the module - i.e. PyMODINIT_FUNC PyInit_spam(void) { return PyModule_Create(&spammodule); } whereas in Python2.X, this is not necessary - i.e. PyMODINIT_FUNC initspam(void) { (void) Py_InitModule("spam", SpamMethods); } So, my sanity checking questions are: Is my

How to introspect a function defined in a Cython C extension module

坚强是说给别人听的谎言 提交于 2019-11-29 02:32:42
Python's inspect module doesn't seem to be able to inspect the signatures of "built-in" functions, which include functions defined in C extension modules, like those defined by Cython. Is there any way to get the signature of a Python function you have defined in such a module, and specifically in Cython? I am looking to be able to find the available keyword arguments. MWE: # mwe.pyx def example(a, b=None): pass and import pyximport; pyximport.install() import mwe import inspect inspect.signature(mwe.example) yields: Traceback (most recent call last): File "mwe_py.py", line 5, in <module>

Python C extension: Use extension PYD or DLL?

狂风中的少年 提交于 2019-11-28 18:44:35
I have a Python extension written in C and I wonder if I should use the file extension DLL or PYD under Windows. (And what would I use in Linux?) Are there any differences (besides the filename)? I found an unofficial article . Is this the secret of pyc? Why can't I find any official article on this topic? pyd files are just dll files ready for python importing. To distinguish them from normal dlls, I recommend .pyd not .dll in windows. Here is the official doc about this issue: http://docs.python.org/faq/windows.html#is-a-pyd-file-the-same-as-a-dll According to the Creating Your Own Project

Python extension module with variable number of arguments

旧巷老猫 提交于 2019-11-28 09:54:16
I am trying to figure out how in C extension modules to have a variable (and maybe) quite large number of arguments to a function. Reading about PyArg_ParseTuple it seems you have to know how many to accept, some mandatory and some optional but all with their own variable. I was hoping PyArg_UnpackTuple would be able to handle this but it seems to just give me bus errors when I try and use it in what appears to be the wrong way. As an example take the following python code that one might want to make into an extension module (in C). def hypot(*vals): if len(vals) !=1 : return math.sqrt(sum((v

Cython package with __init__.pyx: Possible?

倾然丶 夕夏残阳落幕 提交于 2019-11-28 07:19:22
Is it possible to create a Python 2.7 package using __init__.pyx (compiled to __init__.so )? If so how? I haven't had any luck getting it to work. Here is what I have tried: setup.py : #!/usr/bin/env python from distutils.core import setup from distutils.extension import Extension from Cython.Distutils import build_ext foo = Extension(name='foo.__init__', sources=['foo/__init__.pyx']) bar = Extension(name='foo.bar', sources=['foo/bar.pyx']) setup(name='foo', packages = ['foo'], cmdclass={'build_ext':build_ext}, ext_modules = [foo, bar]) foo/__init__.pyx : import foo.bar cpdef hello_world():

How to make a copy of a python module at runtime?

风格不统一 提交于 2019-11-28 07:15:12
I need to make a copy of a socket module to be able to use it and to have one more socket module monkey-patched and use it differently. Is this possible? I mean to really copy a module, namely to get the same result at runtime as if I've copied socketmodule.c , changed the initsocket() function to initmy_socket() , and installed it as my_socket extension. hasanyasin You can always do tricks like importing a module then deleting it from sys.modules or trying to copy a module. However, Python already provides what you want in its Standard Library. import imp # Standard module to do such things

ImportError: dynamic module does not define init function (initfizzbuzz)

痴心易碎 提交于 2019-11-28 04:54:25
I tried to compile fizzbuzz.c to import from python. For building fizzbuzz.c ,I used python setup.py build_ext -i . After building it, I tried to import fizzbuzz.c but the error below occurred. How can I solve this problem ? Error >>> import fizzbuzz Traceback (most recent call last): File "<stdin>", line 1, in <module> ImportError: dynamic module does not define init function (initfizzbuzz) fizzbuzz.c #include <stdio.h> void fizzbuzz(int n){ for (int i=1; i <= n; i++){ if (i % 3 == 0 && i % 5 ==0){ printf("fizzbuzz %d \n", i); } else if (i % 3 == 0){ printf("fizz %d \n", i); } else if(i % 5 =

How to introspect a function defined in a Cython C extension module

倖福魔咒の 提交于 2019-11-27 21:58:36
问题 Python's inspect module doesn't seem to be able to inspect the signatures of "built-in" functions, which include functions defined in C extension modules, like those defined by Cython. Is there any way to get the signature of a Python function you have defined in such a module, and specifically in Cython? I am looking to be able to find the available keyword arguments. MWE: # mwe.pyx def example(a, b=None): pass and import pyximport; pyximport.install() import mwe import inspect inspect

Python C extension: Use extension PYD or DLL?

白昼怎懂夜的黑 提交于 2019-11-27 20:17:37
问题 I have a Python extension written in C and I wonder if I should use the file extension DLL or PYD under Windows. (And what would I use in Linux?) Are there any differences (besides the filename)? I found an unofficial article. Is this the secret of pyc? Why can't I find any official article on this topic? 回答1: pyd files are just dll files ready for python importing. To distinguish them from normal dlls, I recommend .pyd not .dll in windows. Here is the official doc about this issue: http:/