标签: python-c-api

从python绑定到pgcrypto

我想从python中调用一些pgcrypto函数.即px_crypt.我似乎无法弄清楚正确的目标文件链接似乎.

这是我的代码:

#include <Python.h>

#include "postgres.h"

#include "pgcrypto/px-crypt.h"


static PyObject*
pgcrypt(PyObject* self, PyObject* args)
{
    const char* key;
    const char* setting;

    if (!PyArg_ParseTuple(args, "ss", &key, &setting))
        return NULL;

    return Py_BuildValue("s", px_crypt(key, setting, "", 0));
}

static PyMethodDef PgCryptMethods[] =
{
     {"pgcrypt", pgcrypt, METH_VARARGS, "Call pgcrypto's crypt"},
     {NULL, NULL, 0, NULL}
};

PyMODINIT_FUNC
initpypgcrypto(void)
{
     (void) Py_InitModule("pypgcrypto", PgCryptMethods);
}
Run Code Online (Sandbox Code Playgroud)

和gcc命令和输出:

x86_64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fno-strict-aliasing -D_FORTIFY_SOURCE=2 -g -fstack-protector-strong -Wformat -Werror=format-security -fPIC -I/home/ionut/github/postgres/contrib/ -I/usr/include/postgresql/9.4/server/ -I/usr/include/python2.7 -c …
Run Code Online (Sandbox Code Playgroud)

c python postgresql python-c-api pgcrypto

7
推荐指数
1
解决办法
507
查看次数

Python C-API 使 len(...) 与扩展类一起使用

在 Python 中创建类时,我可以简单地创建一个def __len__(self):方法来完成len(InstanceOfMyClass)工作,但是我不知道如何通过 C-API 使用扩展类来执行此操作。

我尝试添加一个__len__方法,但这似乎不起作用

{"__len__",(PyCFunction)&TestClass_GetLen,METH_NOARGS,""},
Run Code Online (Sandbox Code Playgroud)

Python测试代码:

def test(my_instance):
    x = len(my_instance)#exception
    return x

TypeError: object of type 'test_module.test_class' has no len()
Run Code Online (Sandbox Code Playgroud)

测试类代码

struct TestClass;
static int TestClass_Init(TestClass *self, PyObject *args, PyObject* kwds);
static void TestClass_Dealloc(TestClass *self);
static PyObject* TestClass_GetLen(TestClass *self);

struct TestClass
{
    PyObject_HEAD;
};
static PyMethodDef TestClass_methods[] =
{
    {"__len__",(PyCFunction)&TestClass_GetLen,METH_O,""},
    {NULL}
};
static PyTypeObject TestClass_type = {PyObject_HEAD_INIT(NULL)};
bool InitTestClass(PyObject *module)
{
    TestClass_type.tp_basicsize = sizeof(TestClass);
    TestClass_type.tp_name      = PY_MODULE_NAME".TestClass";
    TestClass_type.tp_doc       = ""; …
Run Code Online (Sandbox Code Playgroud)

c python python-c-api

6
推荐指数
1
解决办法
1588
查看次数

Python C-API 模块退出处理程序 - 相当于 atexit?

我正在使用 Python 版本 2.6.4

当我的扩展模块退出/卸载时,我必须从 C 库调用一个函数。对于 C 扩展模块来说,atexit 的等效项是什么?

python python-c-api python-c-extension

6
推荐指数
1
解决办法
1586
查看次数

使用 PyArg_ParseTuple 解析用户定义的类型

如何使用 解析用户定义的类型(或来自现有非标准库的类型)PyArg_ParseTuple?

python python-c-api python-2.7 python-bindings

6
推荐指数
2
解决办法
1422
查看次数

Extending and Embedding Python in C++: Is PyImport_AddModule required to import extension module before importing python script?

I am embedding Python in a C++ application and I also need to call back in to the C++ code from Python. I have been able to do this with the following simple example but I'm having a weird problem in my C++ application.

Module definitions...

static PyObject*
say_hello(PyObject* self, PyObject* args)
{
    const char* name;

    if (!PyArg_ParseTuple(args, "s", &name))
        return NULL;

    printf("Hello %s!\n", name);

    Py_RETURN_NONE;
}

static  PyMethodDef HelloMethods[] =
{
    { "say_hello", say_hello, METH_VARARGS, "Greet somebody." },
    { …
Run Code Online (Sandbox Code Playgroud)

c++ python python-embedding python-c-api

6
推荐指数
0
解决办法
1265
查看次数

嵌入 CPython:如何构造 Python 可调用对象来包装 C 回调指针?

假设我将 CPython 解释器嵌入到一个用 C 编写的较大程序中。该程序的 C 组件偶尔需要调用用 Python 编写的函数,并将回调函数作为参数提供给它们。

使用 CPython扩展和嵌入API,如何构造一个包装 C 函数指针的 Python“可调用”对象,以便我可以将该对象传递给 Python 代码并让 Python 代码成功回调到 C 代码中?

注意:这是最初由用户dhanasubbu发布的问题的修订版本,我回答了该问题,但随后被删除。我认为这实际上是一个好问题,所以我将我所写的内容转换为我自己对问题的陈述的自我回答。欢迎其他答案。

c python cpython python-c-api

6
推荐指数
1
解决办法
664
查看次数

如何在 C API 中创建 Python 字节对象

我有一个 s 的 Numpy 向量,我正在尝试使用 C API尽快从中bool获取对象。bytes(理想情况下,我想将向量的二进制值映射到字节对象。)

我可以成功读入向量并且我有数据bool_vec_arr。我想创建一个int并以这种方式设置它的位:

PyBytesObject * pbo; 
int byte = 0;
int i = 0;
while ( i < vec->dimensions[0] )  
{
    if ( bool_vec_arr[i] )
    {
        byte |= 1UL << i % 8;
    }
    i++;
    if (i % 8 == 0)
    {
        /* do something here? */
        byte = 0;
    }
}
return PyBuildValue("S", pbo); 
Run Code Online (Sandbox Code Playgroud)

但我不确定如何使用 中的 byte 值pbo。有没有人有什么建议?

python python-c-api python-3.x

6
推荐指数
1
解决办法
1916
查看次数

PyThreadState_SetAsyncExc 没有效果

我正在使用具有多个线程的多个解释器,并且我尝试使用它PyThreadState_SetAsyncExc来停止脚本,但它没有效果:

void stop()
{
    PyThreadState *ts = PyThreadState_Swap(this->tstate);
    PyObject *exc = PyObject_CallObject(PyExc_Exception, Py_BuildValue("(s)", "stopped"));
    int n = PyThreadState_SetAsyncExc(this->tstate->thread_id, exc);
    if(n < 1)
        std::cerr << "Script::stop: thread not found!" << std::endl;
    PyThreadState_Swap(ts);
}
Run Code Online (Sandbox Code Playgroud)

返回值为1,但线程继续运行。为什么?

完整的测试用例:

#include <Python.h>
#include <iostream>
#include <thread>

using namespace std::chrono_literals;

struct Script
{
    Script()
    {
        PyThreadState *ts = PyThreadState_Get();
        this->tstate = Py_NewInterpreter();
        PyThreadState_Swap(ts);
    }

    virtual ~Script()
    {
        if(!this->tstate) return;
        PyThreadState *ts = PyThreadState_Swap(this->tstate);
        Py_EndInterpreter(this->tstate);
        PyThreadState_Swap(ts);
    }

    void run(const char *code)
    {
        PyThreadState *ts = PyThreadState_Swap(this->tstate); …
Run Code Online (Sandbox Code Playgroud)

python python-c-api python-multithreading

6
推荐指数
1
解决办法
827
查看次数

GCOV 版本不匹配 - 预期 700e 得到 408R

在一台装有 GCC 4.4.7/GCOV 4.4.7 的服务器上,我能够成功运行测试。然而,在具有 GCC 4.8.5/GCOV 4.8.5 的不同服务器上,运行测试会导致以下错误:

profiling:/path/to/foo.gcda:Version mismatch - expected 700e got 408R
Run Code Online (Sandbox Code Playgroud)

以下是版本的输出:

$ gcc --version
gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-36)

$ gcov --version
gcov (GCC) 4.8.5 20150623 (Red Hat 4.8.5-36)
Run Code Online (Sandbox Code Playgroud)

经过搜索,这个错误似乎经常是由 gcc 和 gcov 版本不匹配引起的,但我的版本是相同的。

我们最近将此服务器上的 gcc 从 4.4.7 升级到 4.8.5。这个问题似乎是由升级引起的。

我应该提到,我正在测试 Python C 扩展,我认为这与测试普通 C 应用程序有点不同。

我执行以下操作:

export CFLAGS="--coverage"
python setup.py build_ext --inplace
python tests.py
Run Code Online (Sandbox Code Playgroud)

在两台服务器上,第二个命令都会.gcno相应地创建文件。

在 4.4.7 的服务器上,第三个命令将成功创建 .gcda 文件。但是 4.8.5 的服务器打印该错误消息

c python gcc gcov python-c-api

6
推荐指数
1
解决办法
5732
查看次数

PyObject 与 PyTypeObject

我正在尝试构建一个Python-C接口,但是当我查看源代码时,我对PyObject和感到困惑PyTypeObject。有人可以解释一下它们之间的区别吗?如果有人能提供一个例子那就太好了。

c python python-c-api

6
推荐指数
1
解决办法
771
查看次数