【Python -- dict 类】教程文章相关的互联网学习教程文章

python – 包装C库【代码】

我有一个private.h,public.h和file.c,我需要将它包装到Cython中.如何包装函数Person_ptr Person_create(const char * name);? private.h:#ifndef __PERSON_PRIVATE_H__ #define __PERSON_PRIVATE_H__#include "Person.h"typedef struct Person_TAG {char* name;int age; } Person;void person_init(Person_ptr self, const char* name);# endif /* __PERSON_PRIVATE_H__ */public.h#ifndef __PERSON_H__ #define __PERSON_H__#in...

python对象到本机c指针【代码】

我正在考虑使用python作为一个嵌入式脚本语言的想法,我正在努力的项目,并使大多数工作.但是我似乎无法将python扩展对象转换回本机c指针. 所以这是我的班级:class CGEGameModeBase { public:virtual void FunctionCall()=0;virtual const char* StringReturn()=0; };class CGEPYGameMode : public CGEGameModeBase, public boost::python::wrapper<CGEPYGameMode> { public:virtual void FunctionCall(){if (override f = this->ge...

c – Qt调用外部Python脚本【代码】

我正在尝试为我用Python编写的命令行工具编写一个GUI包装器.有人建议我应该使用Qt. 下面是我的项目的.cpp文件:#include "v_1.h" #include "ui_v_1.h" #include<QtCore/QFile> #include<QtCore/QTextStream> #include <QProcess> #include <QPushButton> v_1::v_1(QWidget *parent) :QMainWindow(parent),ui(new Ui::v_1){ui->setupUi(this);}v_1::~v_1(){delete ui;}void v_1::on_pushButton_clicked() {QProcess p;p.start("pyth...

Python:转换字符串以用于ctypes.c_void_p()【代码】

给一个字符串:msg="hello world"如何将其定义为ctypes.c_void_p()数据类型? 以下代码产生“无法转换为指针”异常:data=ctypes.c_void_p(msg)数据必须是C中的void *类型,因为它被传递给DLL. 我假设有一种方法可以使用struct包打包/解压缩字符串,但不幸的是我对这个过程非常不熟悉.解决方法:像这样的东西?使用ctypes.cast?>>> import ctypes >>> p1= ctypes.c_char_p("hi mom") >>> ctypes.cast( p1, ctypes.c_void_p ) c_void_...

python -c malloc数组指针在cython中返回【代码】

如何有效地将cython中的malloc数组指针(或numpy数组指针)返回到python3. 只要我不返回数组指针,cython代码就可以正常工作 我想要:def double complex* randn_zig(int n):...r = malloc(n*n*sizeof(double complex))...return rc11(gcc 11)等价物是:double complex* randn_zig(int n){r = malloc(n*n*sizeof(double complex))return r }我努力了< double complex *> randn_zig(int n): 和randn_zig(< double complex *> r,int n)...

我应该在Python C Extension中将Py_INCREF和Py_DECREF放在哪个块上?【代码】

每当我调用我的函数时,每个调用的内存使用量增加大约10M,所以我认为这里有一些内存泄漏..... PyObject *pair = PyTuple_New(2), *item = PyList_New(0);PyTuple_SetItem(pair, 0, PyInt_FromLong(v[j]));if(v[j] != DISTANCE_MAX && (p[j] || d[0][j])){jp=j;while(jp!=istart) {PyList_Append(item, PyInt_FromLong(jp));jp=p[jp];}PyList_Append(item, PyInt_FromLong(jp));PyList_Reverse(item); }PyTuple_SetItem(pair, 1, ite...

Python C扩展:文档的方法签名?【代码】

我正在编写C扩展,我想让我的方法的签名可见为内省.static PyObject* foo(PyObject *self, PyObject *args) {/* blabla [...] */}PyDoc_STRVAR(foo_doc,"Great example function\n""Arguments: (timeout, flags=None)\n""Doc blahblah doc doc doc.");static PyMethodDef methods[] = {{"foo", foo, METH_VARARGS, foo_doc},{NULL}, };PyMODINIT_FUNC init_myexample(void) {(void) Py_InitModule3("_myexample", methods, "a simpl...

python – 手工编写C名称

我正在使用idapython插件在Python中为IDA Pro反汇编程序编写脚本.使用此功能,我可以填补IDA自动分析不足的空白. 令我难以理解的一个领域是命名位置/功能(为了更好的术语)“漂亮的名字”.我的意思的一个例子如下图所示: idapython和IDA Pro本身只允许我输入基本的C-ish函数名称.如果我输入不允许的符号(例如范围解析运算符),它们将被替换为下划线.但是,如果我手动输入一个受损的名字(例如__ZN9IOService15powerChangeDoneEm),IDA P...

python – Cython&C:通过引用传递【代码】

我是Cython和C的菜鸟,所以我对论证传递有疑问.我想避免在以下场景中传递参数的副本:# somefile.pyx #distutils: language = c++ from libcpp.vector cimport vectordef add_one(vector[int] vect):cdef int in = vect.size()for i in range(n):vect[i] += 1cdef vector[int] v for i in range(100000):v.push_back(i) add_one(v) # <-- ??我希望方法add_one只是修改v“就地”.我相信C,你可以通过使用&预先挂起参数来实现这一点,这...

C名称Mangling Library for Python

我想在Python程序中修改和解码C函数名称.有没有这样的东西?我现在搜索了几个小时,也许我很幸运…解决方法:你很可能不想在Python中这样做.另外,您可能不应该从DLL中导出损坏的名称,因为它使得很难用于具有不同编译器的任何人. 如果必须使用损坏的名称,那么只需在Python代码中对它们进行硬编码即可.如果您要在Python代码中进行修改,那么您必须: >了解相关编译器的实现特定规则.>在Python中指定每个函数的C函数签名. 对我来说,用Pyt...

如何在Python中处理来自C的PyObject *【代码】

我在C中创建了DLL,导出函数返回PyObject *.然后我使用ctypes在Python中导入DLL.现在,我怎样才能得到真正的PyObject? 这是c代码的一部分:PyObject* _stdcall getList(){PyObject * PList = NULL;PyObject * PItem = NULL;PList = PyList_New(10);vector <int> intVector;int i;for(int i=0;i<10;i++){intVector.push_back(i);}for(vector<int>::const_iterator it=intVector.begin();it<intVector.end();it++){PItem = Py_BuildVa...

C相当于Python String Slice?【代码】

在python中,我能够切割部分字符串;换句话说,只需在某个位置后打印字符.在C中有相同的吗? Python代码:text= "Apple Pear Orange" print text[6:]会打印:梨橙解决方法:是的,它是substr方法:basic_string substr( size_type pos = 0,size_type count = npos ) const;Returns a substring [pos, pos+count). If the requested substring extends past the end of the string, or if count == npos, the returned substring is [pos...

当从C应用程序中的嵌入式Python调用时,Numpy导入在多数组扩展库上失败

我正在运行一个C应用程序,它尝试使用https://docs.python.org/3.5/extending/embedding.html函数调用来运行python.这是应用程序错误消息管道给我的错误.class ‘ImportError’:Importing the multiarray numpy extension module failed. Mostlikely you are trying to import a failed build of numpy.If you’re working with a numpy git repo, try git clean -xdf (removes allfiles not under version control). Otherwise r...

使用python解析C代码

我有一个巨大的C文件(~100k行),我需要能够解析.主要是我需要能够从其定义中获取有关每个结构的各个字段的详细信息(如结构中每个字段的字段名称和类型).是否有一个好的(开源,我可以在我的代码中使用)方式来做到这一点?或者我应该为此编写自己的解析器.如果我必须自己写,有人可以提出一个好的起点吗?我之前从未使用过python. 谢谢解决方法:查看this link以获取可用于Python的广泛解析工具列表.具体来说,要解析c代码,请尝试pycparse...

使用ctypes将python对象作为参数传递给C/C++函数【代码】

我有一个带有PyObject作为参数的函数的DLL就像是void MyFunction(PyObject* obj) {PyObject *func, *res, *test;//function getAddress of python objectfunc = PyObject_GetAttrString(obj, "getAddress");res = PyObject_CallFunction(func, NULL);cout << "Address: " << PyString_AsString( PyObject_Str(res) ) << endl; }我想使用ctypes从python中调用dll中的这个函数 我的python代码看起来像import ctypes as cpath = "h:\l...