python c extensions are modules written in C/C++ and can be imported and used by python interpreter
Questions tagged [python-c-extension]
257 questions
15
votes
1 answer
How to compile Python extension with debug info using PIP
I have source code for an extension in Python. I want to see debug symbols for that extension when I debug it. How do I tell PIP to compile and link debug symbols for my extension?
(Preferably platform agnostic, else Linux)

user
- 4,920
- 3
- 25
- 38
15
votes
2 answers
Celery: correct way to run lengthy initialization function (per process)
TLDR;
To run an initialization function for each process that is spawned by celery, you can use the worker_process_init signal. As you can read in the docs, handlers for that signal should not be blocking for more than 4 seconds.
But what are the…

basilikum
- 10,378
- 5
- 45
- 58
14
votes
1 answer
Python extension module with variable number of arguments
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…

Brian Larsen
- 1,740
- 16
- 28
14
votes
1 answer
Is there any benefit to using Py_DECREF instead of Py_XDECREF for Python C Extensions?
I'm working through the Python C Extension documentation for defining new types and just finished the Providing finer control over data attributes section.
In this section, they change the example code to ensure that the first and last attributes of…

Matthew Moisen
- 16,701
- 27
- 128
- 231
14
votes
2 answers
How to use Cython typed memoryviews to accept strings from Python?
How can I write a Cython function that takes a byte string object (a normal string, a bytearray, or another object that follows the buffer protocol) as a typed memoryview?
According to the Unicode and Passing Strings Cython tutorial page, the…

Richard Hansen
- 51,690
- 20
- 90
- 97
13
votes
3 answers
What is the best way to deal with "_d" suffix for C extensions when using debug build?
I'm trying to debug my C extension for Python 2.7. I use python2.7 debug build. I build my project with setuptools and my setup.py has such lines:
ext_modules=[Extension("my.extension",
["my/_extension.c"])]
When I invoke…

Gill Bates
- 14,330
- 23
- 70
- 138
13
votes
1 answer
Python-C integration: Ctypes, CFFI or create a Binary Module
Basically I want to make a Python program call functions written in C.
So (as far as I know) my options are:
CTypes/CFFI
Create a DLL/SO/DyLib containing the C functions and access them using CTypes or CFFI. Apparently CFFI is way faster with the…

romulof
- 357
- 2
- 12
12
votes
5 answers
How to use C extensions in python to get around GIL
I want to run a cpu intensive program in Python across multiple cores and am trying to figure out how to write C extensions to do this. Are there any code samples or tutorials on this?

ShahQermez
- 141
- 1
- 5
11
votes
3 answers
"...can't figure out the architecture type of..." problem when compiling Python C-extension with gcc
I just upgraded from Snow Leopard to Lion, and an old python c-extension that I had to update didn't want to compile properly. I don't really know what to do here. Anyone who could help me out so it compiles ok? It compiled just fine back in Snow…

c00kiemonster
- 22,241
- 34
- 95
- 133
11
votes
3 answers
easy_install fails on error "Couldn't find setup script" after binary upload?
After uploading a binary distribution of my Python C extension with python setup.py bdist upload, easy_install [my-package-name] fails on "error: Couldn't find a setup script in /tmp/easy_install/package-name-etc-etc".
What am I doing wrong?

Michael
- 11,612
- 10
- 41
- 43
11
votes
4 answers
Passing 3-dimensional numpy array to C
I'm writing a C extension to my Python program for speed purposes, and running into some very strange behaviour trying to pass in a 3-dimensional numpy array. It works with a 2-dimensional array, but I'm sure I'm screwing something up with the…

DaveTheScientist
- 3,299
- 25
- 19
11
votes
2 answers
C array to PyArray
I'm writing a Python C-Extension without using Cython.
I want to allocate a double array in C, use it in an internal function (that happens to be in Fortran) and return it. I point out that the C-Fortran interface works perfectly in C.
static…

Daniele Bigoni
- 213
- 1
- 3
- 8
11
votes
5 answers
How can I reference #defines in a C file from python?
I have a C file that has a bunch of #defines for bits that I'd like to reference from python. There's enough of them that I'd rather not copy them into my python code, instead is there an accepted method to reference them directly from…

mfisch
- 979
- 9
- 15
10
votes
1 answer
Python C API: Using PyEval_EvalCode
I'm trying to figure out how to use the Python interpreter from C, and I'm having trouble with PyEval_EvalCode. Basically, I'm writing a C function which takes in an arbitrary string of Python code, compiles it, executes it, and then prints out the…

Channel72
- 24,139
- 32
- 108
- 180
10
votes
1 answer
ctypes.ArgumentError: Don't know how to convert parameter
I define a function in C library as follows:
int* Test(char *str1,int id1,char *str2,float val,float *ls)
I want to use it in python so I write the following python code:
str1 = 'a'
str2 = 'b'
id1 = 0
val = 1.0
system('g++ -c -fPIC libtraj.cpp -o…

pfc
- 1,831
- 4
- 27
- 50