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

learn more… | top users | synonyms

3
votes
0answers
99 views

Optimising boost::python::object instances

I recently started using Boost's Python library to wrap parts of a rather large C++ library. Quite by chance, I discovered that every Python object created by Boost Python, is at least bigger than ...
0
votes
1answer
56 views

Calling numpy function from C-code

I'm trying to move some MatLab code with Mex extensions into Python with numpy and scipy libraries. Using this wonderful tutorial http://www.scipy.org/Cookbook/C_Extensions/NumPy_arrays, I quite ...
0
votes
0answers
32 views

Namespaces/packages names with Python's C extensions

I'm learning/experimenting with Python extensions in C. I'm trying to wrap a C library such that it can be used in a more Pythonic way. For example: import mylib widget = ...
0
votes
1answer
33 views

What's the proper way to clean up static python object references in a CPython extension module?

The CPython headers define a macro to declare a method that is run to initialize your module on import: PyMODINIT_FUNC My initializer creates references to other python objects, what is the best way ...
4
votes
3answers
197 views

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 ...
-1
votes
1answer
57 views

How do I install libsandbox? I have some problems during installation [closed]

I have some problems installing libsandbox and pysandbox. I've tried with binary and source packages but no. It seems to do OK but, when I run: from sandbox import * it displays Traceback (most ...
2
votes
2answers
156 views

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

So, 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 ...
3
votes
2answers
102 views

Using function factories in pyx/pxd files to generate cython function wrappers for C-library

I am re-evaluating different ways to wrap external C libraries into Python. I had chosen long ago to use the plain Python C API, which was fast, simple, standalone and, as I thought, future-proof. ...
1
vote
1answer
79 views

Python C-ext namespace mixed with regular python submodules?

This question has some resemblance with: Nested Python C Extensions/Modules? Only with a slight twist. Here I'm not trying to mix two C-exts, but one C-ext and a regular python submodule instead. ...
0
votes
1answer
33 views

can not reference Py_Initialize using c invoke python

I want to use a c program invoke a python program, os:ubuntu 12.10 x64 python2.7.3 C code: #include <stdio.h> #include <stdlib.h> #include <python2.7/Python.h> int main(int argc, ...
2
votes
1answer
71 views

C-extension in Python: Conver wchar string to Python Value

In C-extension of Python, I can use Py_BuildValue() to Conver char string to Python Value like this: Py_BuildValue("s", str). But, if the string is wchar array, the Py_BuildValue("s", str) can not be ...
1
vote
0answers
74 views

setting stdin to wide character orientation in Python extension module

In a C Python extension module I'm using a library which does fwide(stdin,1). This is causing an EOFError when i call input() in Python code. When i avoid fwide(stdin,1) in module's C code, the python ...
3
votes
1answer
127 views

When is PyEval_InitThreads meant to be called? [duplicate]

I'm a bit confused about when I'm supposed to call PyEval_InitThreads. In general, I understand that PyEval_InitThreads must be called whenever a non-Python thread (i.e. a thread that is spawned ...
1
vote
1answer
59 views

What is the proper way to get binary data from python 2.7 into a C module

I am reading binary data from a file in python and trying to send that data to a c module. In python the data is read like so file = open("data", "rb") data = file.read() I want the data as a ...
0
votes
0answers
18 views

Will recompilation affect the running program?

If I recompile a program when it is still running, will the program still be executed as the original before recompilation? I just find some answers from Is it safe to recompile an executable while ...
1
vote
1answer
148 views

Python C Extension and Xcode 4.5

I would like to create a python C extension using XCode 4.5.2 so that I can use the xcode debugger. The extensions require the C file to be linked as a .so file. I have not been able to make .so ...
2
votes
2answers
132 views

Python C extension - maintaining state

I need to write a Python extension in C that I will use to: perform CPU-intensive initialization on a file; make multiple function calls that rely on the initialized data to return results to me; ...
0
votes
1answer
168 views

Python C extension - Receiving a dict as argument

I'm writing a C extension and I'm quite lost on how to receive a dict as an argument. As the docs don't have any specifics on how to achieve this I tried to parse the argument as a Python Object and ...
0
votes
0answers
19 views

How to return a gsl_vector in a python C-extension?

I generated a gsl_vector in my C function, and want to return as an PyObject. Thanks in advance for any suggestions.
1
vote
1answer
174 views

Python C extension: return PyFloat_FromDouble(double) segfault

I recently converted some slow python code into a C extension. It works beautifully, except that it generates a segfault at the 162nd call into it, right at the return statement. Here's how it works. ...
0
votes
1answer
125 views

Compile file .c with embedded Python/C functions

I'm starting the study of Python/C API and I make the first code to test some functions, I write this: file: test.c #include "Python.h" int main() { PyObject* none = Py_BuildValue(""); } I ...
1
vote
0answers
153 views

Register non-static method with PyMethodDef

I am looking for a way to register non static methods of my MyWidget class as python methods. MyWidget class is a QWidget class and it's on the main window. So I want to initialize python when the ...
3
votes
2answers
564 views

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 ...
2
votes
3answers
150 views

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 ...
0
votes
1answer
158 views

calling third party c functions from python

I have a requirement of calling third party c functions from inside python. To do this I created a c api which has all the python specific c code ( using METH_VARARGS) to call the third party ...
0
votes
1answer
75 views

calling legacy c functions from python

I want to call legacy c third party functions from python. I created a C api to make the function calls simpler. In my python file I tried to import the *.so for the api which links with the legacy ...
0
votes
1answer
45 views

Implementing nb_inplace_add results in returning a read-only buffer object

I'm writing an implementation of the in-place add operation. But, for some reason, I sometimes get a read-only buffer as result(while I'm adding a custom extension class and an integer...). The ...
0
votes
1answer
60 views

Python object extension which gets a list in constructor never passes the creation step (SIGSEV), why?

I've been fighting for a lot of time with an error and I've run short of ideas on what's happening and why it doesn't work. First of all, I'm trying to create a new object type for Python through a ...
2
votes
2answers
188 views

Python Running out of Memory (Using Suffix Trees)

I'm running into a bit of trouble with some code. Please bear in mind that I'm a terrible programmer, so my solution probably isn't very eloquent (and likely the reason why I'm running out of memory - ...
0
votes
0answers
64 views

Parallel python worker graceful failure

While using pp to parallelize a significantly complex machine learning problem I'm finding myself having to rely fairly extensively on third party libraries which are of varying quality. One in ...
1
vote
1answer
20 views

Is it possible to include socketmodule.h in Python C extensions?

I'd like to invoke PySocketModule_ImportModuleAndAPI function defined in socketmodule.h in my Python C-extension.
11
votes
4answers
505 views

How to make a copy of a python module in 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 it possible? I mean to really copy a module, namely to get ...
0
votes
0answers
103 views

python capsules bug with nested packages

I can't create a python capsule for a c extension that is nested in a package ? I.e. an an extension c_extension that live at foo.bar.c_extension PyCapsule_Import expects to find the pointer to the ...
0
votes
0answers
99 views

Extending or replacing Python with C++?

I am working on a JSON HTTP API server that returns records from a MySQL database (after certain memory heavy processing and manipulation). Currently the server is built using the Flask framework. I ...
1
vote
2answers
223 views

lost on Py_DECREF/INCREF when handling PyList_Append in Python C extension

I am lost on Py_DECREF/INCREF when handling PyList_Append. Can anybody have comments on the following codes? PyObject * bugmaybe(PyObject *self, PyObject *args) { PyObject * trio=PyList_New(0); ...
0
votes
1answer
193 views

How to neatly pass char[] to PyObject_CallMethod in Python C extension

I have a python object called "m1", which has a method "coor()". I will pass it to C"++" extension and call "coor()" inside. First I tried: PyArrayObject *coor = (PyArrayObject *) ...
-2
votes
1answer
170 views

How to convert a PyObject in Python C-extension to a string type?

Just can't find the right function. Thanks for advice.
0
votes
1answer
185 views

How to pass a python list to a C extension function and append some values?

Say I have a python list: l=[[1,2],[3,4]] I want to pass it to the following C extension: PyObject *acc(PyObject *self, PyObject *args) { PyObject * l; PyArg_ParseTuple(args, "O", &l); ...
1
vote
1answer
74 views

How to pass a python object to C extension and call its method inside?

say I have a python object "o" with a method "m()", and I want to pass it as: PyObject *f(PyObject *self, PyObject *args) { PyObject *o; PyArg_ParseTuple(args, "O", &o); //o.m(); } ...
0
votes
1answer
195 views

using ctypes to link c++ and python in linux

I am writing a program in python. now i want to use ctypes to use some functions of a class i have in c++. so basically , i have an array of data in python. i have another program in c++ which is ...
2
votes
1answer
471 views

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 ...
1
vote
3answers
71 views

It looks like C code in Python C-API returns ptr to stack variable. What am I missing?

I was reading through the file methodobject.c, because I'm trying to learn about making C extensions for Python, when I saw the following code snippet: PyObject * PyCFunction_Call(PyObject *func, ...
2
votes
1answer
303 views

Difference between PyMODINIT_FUNC and PyModule_Create

If I'm understanding correctly, (a) PyMODINIT_FUNC in Python 2.X has been replaced by PyModule_Create in Python3.X (b) Both return PyObject*, however, in Python 3.X, the module's initialization ...
1
vote
2answers
196 views

Is the python C API entirely compatible with C++?

As I understand the relationship between C and C++, the latter is essentially an extension of the former and retains a certain degree of backwards compatibility. Is it safe to assume that the python ...
1
vote
1answer
166 views

How to change the numpy array elements passed to python C-extension?

This should be a trivial question, but didn't figure out as a beginner. I have the following Python C-extenstion using numpy arrays: #include <cmath> #include <Python.h> #include ...
4
votes
1answer
161 views

PyList_SetItem vs. PyList_SETITEM

From what I can tell, the difference between PyList_SetItem and PyList_SETITEM is that PyList_SetItem will lower the reference count of the list item it overwrites and PyList_SETITEM does not. Is ...
0
votes
1answer
183 views

Python C Extension - Why are methods that use keyword arguments cast to PyCFunction

I've am learning about Python-C extensions and am puzzled as to why methods that use keyword arguments must be cast to PyCFunctions. My understanding of a PyCFunction is that it takes two pointers to ...
0
votes
1answer
358 views

Convert Python long integer to C char array

I am using the Python/C API and would like a C function to take a Python long integer argument, nPyLong, and convert it to a char array, nString, in base 10. Here is a snippet of what I would like to ...
4
votes
3answers
343 views

Python C Extensions - Why must callable C functions take arguments and return PyObject *

I'm just starting to play with Python C extensions and am curious as to why a C function, which is callable from Python must take 2 PyObject* arguments and return a PyObject*. I wrote the following ...
0
votes
1answer
283 views

PyArg_ParseTuple default argument

If I have the following function and the optional argument myobj is not passed, does myobj remain NULL or is it set to Py_None? static PyObject * myfunc(PyObject * self, PyObject * args) { ...

1 2 3