Questions tagged [python-c-extension]

python c extensions are modules written in C/C++ and can be imported and used by python interpreter

257 questions
40
votes
4 answers

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

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…
Dmitry Trofimov
  • 7,371
  • 1
  • 30
  • 34
35
votes
4 answers

Building lxml for Python 2.7 on Windows

I am trying to build lxml for Python 2.7 on Windows 64 bit machine. I couldn't find lxml egg for Python 2.7 version. So I am compiling it from sources. I am following instructions on this site http://lxml.de/build.html under static linking section.…
Kamal
  • 3,068
  • 5
  • 26
  • 26
32
votes
2 answers

Cython package with __init__.pyx: Possible?

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…
Richard Hansen
  • 51,690
  • 20
  • 90
  • 97
30
votes
4 answers

Python C extension: Use extension PYD or DLL?

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…
gecco
  • 17,969
  • 11
  • 51
  • 68
30
votes
7 answers

PyEval_InitThreads in Python 3: How/when to call it? (the saga continues ad nauseam)

Basically there seems to be massive confusion/ambiguity over when exactly PyEval_InitThreads() is supposed to be called, and what accompanying API calls are needed. The official Python documentation is unfortunately very ambiguous. There are…
Channel72
  • 24,139
  • 32
  • 108
  • 180
29
votes
3 answers

Python and OpenMP C Extensions

I have a C extension in which I'd like to use OpenMP. When I import my module, though, I get an import error: ImportError: /home/.../_entropysplit.so: undefined symbol: GOMP_parallel_end I've compiled the module with -fopenmp and -lgomp. Is this…
ajduff574
  • 2,101
  • 1
  • 21
  • 20
27
votes
3 answers

What is the "correct" way to pass a boolean to a Python C extension?

This is a simple example from the python documentation (http://docs.python.org/extending/extending.html): static PyObject * spam_system(PyObject *self, PyObject *args) { const char *command; int sts; if (!PyArg_ParseTuple(args, "s",…
shygon
  • 273
  • 1
  • 3
  • 6
23
votes
2 answers

How does PyArg_ParseTupleAndKeywords work?

I've been trying to learn how to write C-extensions for Python and want to be sure I understand how PyArg_ParseTupleAndKeywords works. I believe that the first argument is a PyObject pointer that points to an array of the arguments being passed…
user1245262
  • 6,968
  • 8
  • 50
  • 77
22
votes
1 answer

Docstrings in C extensions to Python?

When creating a C extension to Python, is it possible to be able to somehow write comments that are exposed as docstrings to users of the extension?
Tim McNamara
  • 18,019
  • 4
  • 52
  • 83
22
votes
5 answers

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

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…
cjerdonek
  • 5,814
  • 2
  • 32
  • 26
20
votes
2 answers

Extending python with C: Pass a list to PyArg_ParseTuple

I have been trying to get to grips with extending python with C, and so far, based on the documentation, I have had reasonable success in writing small C functions and extending it with Python. However, I am now struck on a rather simple problem -…
JohnJ
  • 6,736
  • 13
  • 49
  • 82
20
votes
7 answers

Is it normal that running python under valgrind shows many errors with memory?

I've tried to debug memory crash in my Python C extension and tried to run script under valgrind. I found there is too much "noise" in the valgrind output, even if I've ran simple command as: valgrind python -c "" Valgrind output full of repeated…
bialix
  • 20,053
  • 8
  • 46
  • 63
19
votes
5 answers

Embedding Python in C, linking fails with undefined reference to `Py_Initialize'

I am trying to compile the example from the docs https://docs.python.org/2.7/extending/embedding.html and my code looks exactly like the one under 5.1: #include int main(int argc, char *argv[]) { Py_SetProgramName(argv[0]); …
Miguellissimo
  • 473
  • 2
  • 4
  • 11
19
votes
1 answer

Difference between PyMODINIT_FUNC and PyModule_Create

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 -…
user1245262
  • 6,968
  • 8
  • 50
  • 77
15
votes
2 answers

What is the easiest way to make an optional C extension for a python package?

I've created a C extension that I'd like to enable in my Python package (using setuptools) only if a command line option is passed in. What is the easiest way to do this? I can't seem to find any straightforward ways of going about this.
Jason Baker
  • 192,085
  • 135
  • 376
  • 510
1
2 3
17 18