python-c-extension

Implementing nb_inplace_add results in returning a read-only buffer object

隐身守侯 提交于 2020-01-05 12:13:50
问题 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 relevant code is: static PyObject * ModPoly_InPlaceAdd(PyObject *self, PyObject *other) { if (!ModPoly_Check(self)) { //Since it's in-place addition the control flow should never // enter here(I suppose) if (!ModPoly_Check(other)) { PyErr_SetString(PyExc_TypeError, "Neither argument is a ModPolynomial.

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

僤鯓⒐⒋嵵緔 提交于 2020-01-05 03:52:09
问题 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 C API can be called with C++ code? More to the point, I notice that the official python documentation bundles C and C++ extensions together on the same page. Nowhere am I able to find a C++ API. This leads me to believe that the same API is safe to use in both languages. Can someone confirm or

Creating a numpy array of custom-class objects with C API

一曲冷凌霜 提交于 2020-01-03 14:15:19
问题 Using the C API, I would like to create a numpy array containing objects of type Quaternion , which is a class I've written in C++. I already have an array of these (actually a std::vector ), and I want to make a copy -- or use the same memory if possible. Since this isn't a basic type, I need to use Py_Object types, and can't use PyArray_SimpleNew or anything easy like that. I'm guessing I might want to use PyArray_NewFromDescr or even PyArray_SimpleNewFromDescr , but I am completely and

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

吃可爱长大的小学妹 提交于 2020-01-01 09:20:49
问题 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 <Python.h> int main(int argc, char *argv[]) { Py_SetProgramName(argv[0]); Py_Initialize(); PyRun_SimpleString("from time import time, ctime\n" "print 'Today is', ctime(time())\n"); Py_Finalize(); return 0; } I use the following command to compile it which works fine for me and gives me the desired object file: gcc -c $(python2.7

Returning objects to Python from C

╄→гoц情女王★ 提交于 2020-01-01 05:09:07
问题 I've read the documentation for the Python C-API, and even written a few extension modules. However, I'm still a bit unclear on the exact semantics when it comes to returning Python objects from a C function. The limited examples in the Python docs usually show a C function which returns the result of Py_BuildValue . Now, Py_BuildValue returns a New Reference , and transfers ownership of this reference over to the interpreter. So, can I extrapolate from this that it is a general rule that any

Docstrings in C extensions to Python?

大兔子大兔子 提交于 2019-12-31 12:23:12
问题 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? 回答1: Docstrings for types can be included as the tp_doc member in the PyTypeObject structure, see an example in the docs. Docstrings for functions can be included in the ml_doc field of the module's method table. If you want your docstrings to be "physically close" to the actual functions, you could include string constants above your function

Compiler can't find Python.h

ぐ巨炮叔叔 提交于 2019-12-29 01:05:15
问题 I'm kinda new to C, and I can't seem to do what I want. I need to make some Python bindings for C functions, and I think I can figure this out. But there's one little line that WILL NOT WORK. #include <Python.h> I get this: fatal error #1035: Can't find include file <Python.h>. I've tried everything. I just stuck Python.h in the same folder as my project first. Then I put the file name in Library and Object files under linker. Then I did the same with python31.lib, then I put the directory

Cython package with __init__.pyx: Possible?

我怕爱的太早我们不能终老 提交于 2019-12-28 12:18:15
问题 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 distutils.extension import Extension from Cython.Distutils import build_ext foo = Extension(name='foo.__init__', sources=['foo/__init__.pyx']) bar = Extension(name='foo.bar', sources=['foo/bar.pyx']) setup(name='foo', packages = ['foo'], cmdclass={

Cython package with __init__.pyx: Possible?

我怕爱的太早我们不能终老 提交于 2019-12-28 12:18:09
问题 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 distutils.extension import Extension from Cython.Distutils import build_ext foo = Extension(name='foo.__init__', sources=['foo/__init__.pyx']) bar = Extension(name='foo.bar', sources=['foo/bar.pyx']) setup(name='foo', packages = ['foo'], cmdclass={

Python C extension: Extract parameter from the engine

空扰寡人 提交于 2019-12-25 08:01:26
问题 I would like to use Python to perform Mathematics tests on my functions. A typical program that can gain access to Python is this: #include <iostream> #include <string> #include <Python.h> int RunTests() { Py_Initialize(); PyRun_SimpleString("a=5"); PyRun_SimpleString("b='Hello'"); PyRun_SimpleString("c=1+2j"); PyRun_SimpleString("d=[1,3,5,7,9]"); //question here Py_Finalize(); return 0; } My question is: How can I extract the parameters a,b,c,d to PyObject s? 回答1: PyRun_SimpleString()