如何使用纯Python扩展API(python3)包装C ++对象? [英] How to wrap a C++ object using pure Python Extension API (python3)?

查看:119
本文介绍了如何使用纯Python扩展API(python3)包装C ++对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何使用 Python扩展API 包装C ++对象(和distutils),而无需外部工具(例如Cython,Boost,SWIG等)。



请注意,我的C ++对象具有内存分配,因此必须调用析构函数以避免内存泄漏。

  #include Voice.h 

命名空间转换
{
Voice :: Voice(int fftSize){mem = new double [fftSize]; }

Voice ::〜Voice(){delete [] mem; }

int Voice :: method1(){/ *做东西* / return(1); }
}

我只想在Python中做类似的事情:

 导入声音

v = voice.Voice(512)
result = v.method1()


解决方案

似乎答案实际上在这里: https://docs.python.org/3.6/extending/newtypes.html



通过示例,但并非一帆风顺。



编辑1:



<实际上,并不是将C ++对象包装在Python对象中,而是用C代码创建一个Python对象。 (edit2:所以您可以包装C ++对象!)



编辑2:



以下是使用 Python新类型





原始C ++文件: Voice.cpp

  #include< cstdio> 

#include Voice.h

命名空间转换
{
Voice :: Voice(int fftSize){
printf( c ++ voice\n的构造函数);
this-> fftSize = fftSize;
mem = new double [fftSize];
}

Voice ::〜Voice(){delete [] mem; }

int语音:: filter(int freq){
printf( c ++语音过滤方法\n);
return(doubleIt(3));
}
int语音:: doubleIt(int i){return 2 * i; }
}



原始h文件: Voice.h

 命名空间转换{

class Voice {
public:
double * mem;
int fftSize;

Voice(int fftSize);
〜Voice();

int过滤器(int freq);
int doubleIt(int i);
};

}



C ++ Python包装文件:voiceWrapper.cpp

  #include< Python.h> 

#include< cstdio>
//〜#include structmember.h

#include Voice.h

使用transform :: Voice;

typedef struct {
PyObject_HEAD
Voice * ptrObj;
} PyVoice;




静态PyModuleDef语音模块= {
PyModuleDef_HEAD_INIT,
voice,
包装了C ++的示例模块对象,
-1,
NULL,NULL,NULL,NULL,NULL
};

static int PyVoice_init(PyVoice * self,PyObject * args,PyObject * kwds)
//初始化PyVoice对象
{
int fftSize;

if(!PyArg_ParseTuple(args, i,& fftSize))
返回-1;

self-> ptrObj =新语音(fftSize);

返回0;
}

static void PyVoice_dealloc(PyVoice * self)
//破坏对象
{
delete self-> ptrObj;
Py_TYPE(self)-> tp_free(self);
}


静态PyObject * PyVoice_filter(PyVoice * self,PyObject * args)
{
int freq;
int retval;

if(!PyArg_ParseTuple(args, i,& freq))
返回Py_False;

retval =(self-> ptrObj)-> filter(freq);

返回Py_BuildValue( i,retval);
}


静态PyMethodDef PyVoice_methods [] = {
{ filter,(PyCFunction)PyVoice_filter,METH_VARARGS, filter the mem voice},
{NULL} / *前哨* /
};

静态PyTypeObject PyVoiceType = {PyVarObject_HEAD_INIT(NULL,0)
voice.Voice / * tp_name * /
};


PyMODINIT_FUNC PyInit_voice(void)
//创建模块
{
PyObject * m;

PyVoiceType.tp_new = PyType_GenericNew;
PyVoiceType.tp_basicsize = sizeof(PyVoice);
PyVoiceType.tp_dealloc =(析构函数)PyVoice_dealloc;
PyVoiceType.tp_flags = Py_TPFLAGS_DEFAULT;
PyVoiceType.tp_doc =语音对象;
PyVoiceType.tp_methods = PyVoice_methods;
//〜PyVoiceType.tp_members = Noddy_members;
PyVoiceType.tp_init =(initproc)PyVoice_init;

如果(PyType_Ready(& PyVoiceType)< 0)
返回NULL;

m = PyModule_Create(& voicemodule);
if(m == NULL)
返回NULL;

Py_INCREF(& PyVoiceType);
PyModule_AddObject(m, Voice,(PyObject *)& PyVoiceType); //将语音对象添加到模块
return m;中。
}



distutils文件:distutils.core导入设置中的 setup.py

 ,扩展

setup(name ='voicePkg',version ='1.0',\
ext_modules = [Extension('voice',['voiceWrapper.cpp','Voice.cpp' ])])



python测试文件: test.py

 导入声音

v = voice.Voice(512)
result = v.filter(5)
print('result ='+ str(result))



和魔术:

  sudo python3 setup.py install 
python3 test.py

输出为:

  c ++语音构造函数
c ++语音过滤器方法
result = 6

享受!



Doom


I want to know how to wrap a C++ object with Python Extension API (and distutils) without external tools (like Cython, Boost, SWIG, ...). Just in pure Python way without creating a dll.

Note that my C++ object has memory allocations so destructor has to be called to avoid memory leaks.

#include "Voice.h"

namespace transformation
{ 
  Voice::Voice(int fftSize) { mem=new double[fftSize]; } 

  Voice::~Voice() { delete [] mem; } 

  int Voice::method1() { /*do stuff*/ return (1); } 
}

I just want to do somethings like that in Python :

import voice

v=voice.Voice(512)
result=v.method1()

解决方案

Seems that the answer was in fact here : https://docs.python.org/3.6/extending/newtypes.html

With examples, but not really easy.

EDIT 1 :

In fact, it is not really for wrapping a C++ object in a Python object, but rather to create a Python object with C code. (edit2 : and so you can wrap C++ object!)

EDIT 2 :

Here is a solution using the Python newtypes

.

Original C++ file : Voice.cpp

#include <cstdio>

#include "Voice.h"

namespace transformation
{ 
    Voice::Voice(int fftSize) {
        printf("c++ constructor of voice\n");
        this->fftSize=fftSize;
        mem=new double[fftSize];
        } 

    Voice::~Voice() { delete [] mem; } 

    int Voice::filter(int freq) {
        printf("c++ voice filter method\n");
        return (doubleIt(3));
    }
    int Voice::doubleIt(int i) { return 2*i; }
}

.

Original h file : Voice.h

namespace transformation {

    class Voice {
    public:
        double *mem;
        int fftSize;

        Voice(int fftSize);
        ~Voice();

        int filter(int freq);
        int doubleIt(int i);
    };

}

.

C++ Python wrapper file : voiceWrapper.cpp

#include <Python.h>

#include <cstdio>
//~ #include "structmember.h"

#include "Voice.h"

using transformation::Voice;

typedef struct {
    PyObject_HEAD
    Voice * ptrObj;
} PyVoice;




static PyModuleDef voicemodule = {
    PyModuleDef_HEAD_INIT,
    "voice",
    "Example module that wrapped a C++ object",
    -1,
    NULL, NULL, NULL, NULL, NULL
};

static int PyVoice_init(PyVoice *self, PyObject *args, PyObject *kwds)
// initialize PyVoice Object
{
    int fftSize;

    if (! PyArg_ParseTuple(args, "i", &fftSize))
        return -1;

    self->ptrObj=new Voice(fftSize);

    return 0;
}

static void PyVoice_dealloc(PyVoice * self)
// destruct the object
{
    delete self->ptrObj;
    Py_TYPE(self)->tp_free(self);
}


static PyObject * PyVoice_filter(PyVoice* self, PyObject* args)
{
    int freq;
    int retval;

    if (! PyArg_ParseTuple(args, "i", &freq))
        return Py_False;

    retval = (self->ptrObj)->filter(freq);

    return Py_BuildValue("i",retval);
}


static PyMethodDef PyVoice_methods[] = {
    { "filter", (PyCFunction)PyVoice_filter,    METH_VARARGS,       "filter the mem voice" },
    {NULL}  /* Sentinel */
};

static PyTypeObject PyVoiceType = { PyVarObject_HEAD_INIT(NULL, 0)
                                    "voice.Voice"   /* tp_name */
                                };


PyMODINIT_FUNC PyInit_voice(void)
// create the module
{
    PyObject* m;

    PyVoiceType.tp_new = PyType_GenericNew;
    PyVoiceType.tp_basicsize=sizeof(PyVoice);
    PyVoiceType.tp_dealloc=(destructor) PyVoice_dealloc;
    PyVoiceType.tp_flags=Py_TPFLAGS_DEFAULT;
    PyVoiceType.tp_doc="Voice objects";
    PyVoiceType.tp_methods=PyVoice_methods;
    //~ PyVoiceType.tp_members=Noddy_members;
    PyVoiceType.tp_init=(initproc)PyVoice_init;

    if (PyType_Ready(&PyVoiceType) < 0)
        return NULL;

    m = PyModule_Create(&voicemodule);
    if (m == NULL)
        return NULL;

    Py_INCREF(&PyVoiceType);
    PyModule_AddObject(m, "Voice", (PyObject *)&PyVoiceType); // Add Voice object to the module
    return m;
}

.

distutils file : setup.py

from distutils.core import setup, Extension

setup(name='voicePkg', version='1.0',  \
      ext_modules=[Extension('voice', ['voiceWrapper.cpp','Voice.cpp'])])

.

python test file : test.py

import voice

v=voice.Voice(512)
result=v.filter(5)
print('result='+str(result))

.

and magic :

sudo python3 setup.py install
python3 test.py

Output is :

c++ constructor of voice
c++ voice filter method
result=6

Enjoy !

Doom

这篇关于如何使用纯Python扩展API(python3)包装C ++对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆