栏目分类:
子分类:
返回
文库吧用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
文库吧 > IT > 面试经验 > 面试问答

如何使用纯Python扩展API(python3)包装C ++对象?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何使用纯Python扩展API(python3)包装C ++对象?

似乎答案实际上在这里: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"namespace transformation{     Voice::Voice(int fftSize) {        printf("c++ constructor of voicen");        this->fftSize=fftSize;        mem=new double[fftSize];        }    Voice::~Voice() { delete [] mem; }    int Voice::filter(int freq) {        printf("c++ voice filter methodn");        return (doubleIt(3));    }    int Voice::doubleIt(int i) { return 2*i; }}

原始h文件:

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包装器文件: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}  };static PyTypeObject PyVoiceType = { PyVarObject_HEAD_INIT(NULL, 0)   "voice.Voice"             };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文件:

setup.py

from distutils.core import setup, Extensionsetup(name='voicePkg', version='1.0',        ext_modules=[Extension('voice', ['voiceWrapper.cpp','Voice.cpp'])])

python测试文件:

test.py

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

和魔术:

sudo python3 setup.py installpython3 test.py

输出为:

c++ constructor of voicec++ voice filter methodresult=6


转载请注明:文章转载自 www.wk8.com.cn
本文地址:https://www.wk8.com.cn/it/640112.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 wk8.com.cn

ICP备案号:晋ICP备2021003244-6号