python-extensions

creating numpy array in c extension segfaults

假如想象 提交于 2020-03-22 03:51:04
问题 I'm just trying to start off by creating a numpy array before I even start to write my extension. Here is a super simple program: #include <stdio.h> #include <iostream> #include "Python.h" #include "numpy/npy_common.h" #include "numpy/ndarrayobject.h" #include "numpy/arrayobject.h" int main(int argc, char * argv[]) { int n = 2; int nd = 1; npy_intp size = {1}; PyObject* alpha = PyArray_SimpleNew(nd, &size, NPY_DOUBLE); return 0; } This program segfaults on the PyArray_SimpleNew call and I don

creating numpy array in c extension segfaults

梦想的初衷 提交于 2020-03-22 03:50:37
问题 I'm just trying to start off by creating a numpy array before I even start to write my extension. Here is a super simple program: #include <stdio.h> #include <iostream> #include "Python.h" #include "numpy/npy_common.h" #include "numpy/ndarrayobject.h" #include "numpy/arrayobject.h" int main(int argc, char * argv[]) { int n = 2; int nd = 1; npy_intp size = {1}; PyObject* alpha = PyArray_SimpleNew(nd, &size, NPY_DOUBLE); return 0; } This program segfaults on the PyArray_SimpleNew call and I don

Cython C-level interface of package: *.pxd files are not found

依然范特西╮ 提交于 2020-02-04 01:46:38
问题 In a nutshell I try to compile a cython extension called extension2 that cimports a file extension from a self-created package. When building extension2 , I get the error that extension.pxd is not found though this file is exactly at the sepcified path. Details I am building two packages involving cython, a package A and a package B that depends on A . A is a subpacke of a namespace package nsp . That is, the folder structure looks as follows: ├── nsp │ └── A | ├── extension.pxd | ├──

Is the import order of extensions in module filenames guaranteed in Python?

寵の児 提交于 2020-01-04 05:43:05
问题 Experimentally, I verified that when a compiled extension.pyd (or .so ) and plain extension.py both exist in the same directory, the .pyd file gets imported first ; the .py is only imported if the .pyd file is not found: In [1]: import extension In [2]: extension.__file__ Out[2]: 'extension.pyd' In [3]: import glob; glob.glob("extension.py*") Out[3]: ['extension.py', 'extension.pyd'] Is that guaranteed to be the same for all versions of Python, and can I rely on this to add logic to the .py

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

微笑、不失礼 提交于 2019-12-31 08:59:47
问题 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

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

最后都变了- 提交于 2019-12-30 03:47:07
问题 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

How to build and distribute a Python/Cython package that depends on third party libFoo.so

守給你的承諾、 提交于 2019-12-29 05:18:28
问题 I've written a Python module that depends on some C extensions. Those C extensions depend in turn on several compiled C libraries. I'd like to be able to distribute this module bundled with all the dependencies. I've put together a minimal example (it can be found on GitHub in its entirety). The directory structure is: $ tree . . ├── README.md ├── poc │ ├── __init__.py │ ├── cython_extensions │ │ ├── __init__.py │ │ ├── cvRoberts_dns.c │ │ ├── cvRoberts_dns.h │ │ ├── helloworld.c │ │ ├──

Python C Extension: PyEval_GetLocals() returns NULL

落爺英雄遲暮 提交于 2019-12-24 07:38:10
问题 I need to read local variables from Python in C/C++. When I try to PyEval_GetLocals , I get a NULL. This happens although Python is initialized. The following is a minimal example. #include <iostream> #include <Python.h> Py_Initialize(); PyRun_SimpleString("a=5"); PyObject *locals = PyEval_GetLocals(); std::cout<<locals<<std::endl; //prints NULL (prints 0) Py_Finalize(); In the manual, it says that it returns NULL if no frame is running, but... there's a frame running! What am I doing wrong?

How can I get python.h into my python virtualenv on Mac OSX?

我是研究僧i 提交于 2019-12-23 16:16:07
问题 I'm writing a C extension for a python application, and need to test python-specific C code. To do so I need to import Python.h into my C files, but for the life of me I haven't been able to do that. Most tutorials suggest something along the lines of sudo apt-get python-dev, but my system doesn't have apt-get, and even if it did I think it'd be better to have the dev files in my virtualenv. Any idea how to get Python.h into my virtualenv? 回答1: Assuming you are using MacOSX and Python

What does Cython do with imports?

混江龙づ霸主 提交于 2019-12-23 07:47:11
问题 I want to create a Python extension and I really like the idea of using Cython. Mainly to gain more knowledge about it and to take advantage of speed gains, if any. I have read quite a bit of Cython documentation but I am not a computer scientist (yet) and do not have an in depth knowledge to understand the low-level basics, hence the reason for my following questions: I was just wondering, what happens if I use an externally imported (for example, an ORM or SQL library or any other 3rd party