python-extensions

manually building a python extension in cygwin with boost.python

巧了我就是萌 提交于 2019-12-07 07:46:16
问题 Sorry for such a general title, but i'm not quite sure what exactly i'm missing or what i'm doing wrong. My aim is to build a python extension using boost.python under cygwin and avoiding boost.build tools, that is using make instead of bjam. The latter way was working for me quite good, but now i want to do it this way. I solved many issues by googling and looking for similar topics and that helped me to figure out some tricks and to move forward. Yet at the last step there seems to be some

Passing numpy integer array to c code

ε祈祈猫儿з 提交于 2019-12-06 09:00:44
I'm trying to write Cython code to dump a dense feature matrix, target vector pair to libsvm format faster than sklearn's built in code. I get a compilation error complaining about a type issue with passing the target vector (a numpy array of ints) to the relevant c function. Here's the code: import numpy as np cimport numpy as np cimport cython cdef extern from "cdump.h": int filedump( double features[], int numexemplars, int numfeats, int target[], char* outfname) @cython.boundscheck(False) @cython.wraparound(False) def fastdumpdense_libsvmformat(np.ndarray[np.double_t,ndim=2] X, y, outfname

Import and use standard Python module from inside Python C extension

大兔子大兔子 提交于 2019-12-06 02:53:37
问题 I have Python extension module written in C. I want to use in this C code one of the standard Python modules, for example os or shutil . How is best to do this? 回答1: PyObject* os = PyImport_ImportModuleNoBlock("os"); if (os == NULL) return NULL; someattr = PyObject_GetAttrString(os, "someattr"); Py_DECREF(os); If you import the module only once e.g., in init_yourmodule() function then use PyImport_ImportModule("os") . 回答2: Don't. Instead, change your extension module so that it provides a

Data corruption: Where's the bug‽

匆匆过客 提交于 2019-12-05 04:50:28
Last edit: I've figured out what the problem was (see my own answer below) but I cannot mark the question as answered, it would seem. If someone can answer the questions I have in my answer below, namely, is this a bug in Cython or is this Cython's intended behavior, I will mark that answer as accepted, because that would be the most useful lesson to gain from this, IMHO. Firstly, I have to start by saying that I have been trying to figure this out for three days, and I am just banging my head against the wall. As best as I can tell from the documentation, I am doing things correctly.

Import and use standard Python module from inside Python C extension

独自空忆成欢 提交于 2019-12-04 07:31:17
I have Python extension module written in C. I want to use in this C code one of the standard Python modules, for example os or shutil . How is best to do this? PyObject* os = PyImport_ImportModuleNoBlock("os"); if (os == NULL) return NULL; someattr = PyObject_GetAttrString(os, "someattr"); Py_DECREF(os); If you import the module only once e.g., in init_yourmodule() function then use PyImport_ImportModule("os") . Don't. Instead, change your extension module so that it provides a service to Python, and then write Python code which calls os , shutil and your module. In fact, for a lot of the

Tutorials on optimizing non-trivial Python applications with C extensions or Cython

半世苍凉 提交于 2019-12-02 17:33:29
The Python community has published helpful reference material showing how to profile Python code, and the technical details of Python extensions in C or in Cython . I am still searching for tutorials which show, however, for non-trivial Python programs, the following: How to identify the hotspots which will benefit from optimization by conversion to a C extension Just as importantly, how to identify the hotspots which will not benefit from conversion to a C extension Finally, how to make the appropriate conversion from Python to C, either using the Python C-API or (perhaps even preferably)

Compiler can't find Py_InitModule() .. is it deprecated and if so what should I use?

隐身守侯 提交于 2019-11-30 11:12:57
I am attempting to write a C extension for python. With the code (below) I get the compiler warning: implicit declaration of function ‘Py_InitModule’ And it fails at run time with this error: undefined symbol: Py_InitModule I have spent literally hours searching for a solution with no joy. I have tried multiple minor changes to syntax, I even found a post suggesting the method has been deprecated. However I find no replacement. Here is the code: #include <Python.h> //a func to calc fib numbers int cFib(int n) { if (n<2) return n; return cFib(n-1) + cFib(n-2); } static PyObject* fib(PyObject*

Aspect oriented programming (AOP) in Python [duplicate]

风流意气都作罢 提交于 2019-11-29 23:32:20
Possible Duplicate: Any AOP support library for Python? I am familiar with the AspectJ extension for the Java language. I want to know if there is such a thing for Python. Don't get me wrong, I do not mean a library but a language extension like AspectJ is to Java. jsbueno Python does not need something like a "language extension" for being able to work in an Aspect Oriented way. That is simply due to the dynamic mechanisms in Python itself. A Google search will yield a couple projects - but despite looking merely like libraries, it is all that is needed in Python. I am not making this up - it

Cython compiled C extension: ImportError: dynamic module does not define init function

亡梦爱人 提交于 2019-11-29 21:22:29
I have just compiled part of my C library as an extension using Cython, as a "proof of concept". I managed to hack the code (const correctnes problems etc aside), to finally get an extension built. However, when I attempted to import the newly created extension, I got the following error: ImportError: dynamic module does not define init function What am I doing wrong and how do I fix this? I am using Cythn 0.11.2 and Python 2.6.5 on Ubuntu 10.0.4 I've found that a frequent cause of this problem is, when using a distutils setup file to compile the code, that the .pyx base name does not match

Cython compiled C extension: ImportError: dynamic module does not define init function

穿精又带淫゛_ 提交于 2019-11-28 16:39:35
问题 I have just compiled part of my C library as an extension using Cython, as a "proof of concept". I managed to hack the code (const correctnes problems etc aside), to finally get an extension built. However, when I attempted to import the newly created extension, I got the following error: ImportError: dynamic module does not define init function What am I doing wrong and how do I fix this? I am using Cythn 0.11.2 and Python 2.6.5 on Ubuntu 10.0.4 回答1: I've found that a frequent cause of this