Tag: python extensions

Python C扩展:PyEval_GetLocals()返回NULL

我需要在C / C ++中从Python读取局部变量。 当我尝试PyEval_GetLocals ,我得到一个NULL。 尽管Python已初始化,但会发生这种情况 以下是一个最小的例子。 #include #include Py_Initialize(); PyRun_SimpleString(“a=5”); PyObject *locals = PyEval_GetLocals(); std::cout<<locals<<std::endl; //prints NULL (prints 0) Py_Finalize(); 在手册中 ,它表示如果没有帧正在运行它会返回NULL,但是……正在运行一个帧! 我究竟做错了什么? 我在Debian Jessie中运行它。

C:staticforward的Python扩展

所以我需要使用子进程模块的代码来添加我需要的一些function。 当我尝试编译_subprocess.c文件时,它会给出以下错误消息: Error 1 error C2086: ‘PyTypeObject sp_handle_type’ : redefinition 这是与_subprocess.c文件相关的代码部分: typedef struct { PyObject_HEAD HANDLE handle; } sp_handle_object; staticforward PyTypeObject sp_handle_type; static PyObject* sp_handle_new(HANDLE handle) { sp_handle_object* self; self = PyObject_NEW(sp_handle_object, &sp_handle_type); if (self == NULL) return NULL; self->handle = handle; return (PyObject*)self; } #if defined(MS_WIN32) && !defined(MS_WIN64) #define HANDLE_TO_PYNUM(handle) PyInt_FromLong((long) handle) #define PY_HANDLE_PARAM […]

集成C和Python:ValueError:模块函数不能设置METH_CLASS或METH_STATIC

我正在第一次尝试集成C和Python 2.7.3。 首先,我只是想为Python编写一个可以进行基本添加的C模块。 (它被称为npfind,因为一旦我弄明白了,我想为numpy编写一个find方法) npfind.h: #include extern int add(int a, int b); npfind.c: #include “npfind.h” int add(int a, int b) { return a + b; } pynpfind.c: #include “Python.h” #include “npfind.h” static char* py_add_doc = “Adds two numbers.”; static PyObject* py_add(PyObject* self, PyObject* args) { int a, b, r; if (!PyArg_ParseTuple(args, “ii”, &a, &b)) { […]

从Python C扩展中导入和使用标准Python模块

我有用C编写的Python扩展模块。我想在这个C代码中使用标准Python模块之一,例如os或shutil 。 怎么做到这一点最好?

编译器找不到Py_InitModule()..它是否已被弃用,如果是这样,我应该使用什么?

我正在尝试为python编写C扩展。 使用代码(如下),我收​​到编译器警告: implicit declaration of function ‘Py_InitModule’ 它在运行时失败并出现此错误: undefined symbol: Py_InitModule 我花了几个小时寻找一个没有快乐的解决方案。 我尝试过多次语法修改,我甚至发现一篇post暗示该方法已被弃用。 但是我发现没有替代品。 这是代码: #include //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* self,PyObject* args) { int n; if (!PyArg_ParseTuple(args,"i",&n)) return NULL; return Py_BuildValue("i",cFib(n)); } static PyMethodDef module_methods[] = { […]