Tag: python

C,Python,ctypes退出代码-1073741819(0xC0000005)

C代码(DLL) #include #include #include struct Doc { wchar_t path[512]; int r; int g; int b; }; struct Docs { struct Doc docs[1000000]; }; struct Color { int r; int g; int b; }; float distance(int a, int b, int c, int d, int e, int f) { return sqrt((ab)*(ab)+(cd)*(cd)+(ef)*(ef)); } struct Doc main(long len, struct […]

错误:’PyObject’(又名’_object’)类型的值不能在上下文中转换为’bool’

我将一个python模块传递给C作为PyObject 。 我想使用以下表单检查我的C代码中的值是否为NONE: int func(PyObject tmp) { if(tmp) { // etc 我收到以下错误。 我如何从PyObject转换为布尔值,simillar转换为Python函数的行为方式。 值得注意的是,当tmp是boost::python::object变量时,此命令按预期工作。 ex_program.cpp:72:7: error: value of type ‘PyObject’ (aka ‘_object’) is not contextually convertible to ‘bool’ if (tmp) ^~~

AttributeError:’module’对象在尝试将c代码导入python时没有属性

我有一个导入来导入在c中创建的共享对象库。 它看起来像这样: import Cal 然后我尝试使用共享对象库在python中进行调用,如下所示: status = Cal.Cal_readFile(filename, result) 但是,当我在运行我的python文件时遇到此代码时,我收到以下错误: Exception in Tkinter callback Traceback (most recent call last): File “/usr/lib/python3.4/tkinter/__init__.py”, line 1541, in __call__ return self.func(*args) File “xcal.py”, line 72, in openDialog status = Cal.Cal_readFile(filename, result) AttributeError: ‘module’ object has no attribute ‘Cal_readFile’ 我的共享对象库的makefile如下所示: CC = gcc CFLAGS = -Wall -std=c11 -g -DNDEBUG `pkg-config […]

指向SWIG中C结构的python指针 – 访问struct成员

我正在尝试使用一个非常基本的Python-to-C接口与SWIG一起工作,我可以将一个指向结构的指针传递给一个C函数并且成员被填充,然后我可以访问python中的成员。 在下面的例子中,除了我尝试打印结构成员时,一切都有效: print swig_test.diags_mem0_get(var) 结果是: $ ./runpython.py Traceback (most recent call last): File “./runpython.py”, line 11, in print swig_test.diags_mem0_get(var) AttributeError: ‘module’ object has no attribute ‘diags_mem0_get’ 鉴于此: print var.mem0 结果是: $ ./runpython.py swig/python detected a memory leak of type ‘uint16_t *’, no destructor found. 我正在关注SWIG 3.0文档,特别是“5.5结构和联合”部分: http : //swig.org/Doc3.0/SWIGDocumentation.html#SWIG_nn31 我究竟做错了什么? 我把这个例子简化为裸骨: swig_test.h typedef struct diags_t […]

将C模块与Python 3.7链接。 在Windows中使用CMake

我正在尝试创建一个可以从Python调用的C库,到目前为止我已经创建了暴露模块信息和方法表的代理C文件(为简单起见,只添加了一个方法get_cycle_count ,其实现无关紧要): static PyMethodDef NESulator_methods[] = { {“NESulator”, get_cycle_count, METH_VARARGS, “Retrieves the current cycle count”}, {NULL, NULL, 0, NULL} /* Sentinel */ }; static struct PyModuleDef NESulator_module = { PyModuleDef_HEAD_INIT, “NESulator”, /* name of module */ NULL, /* module documentation, may be NULL */ -1, /* size of per-interpreter state of the module, or -1 […]

Python C扩展:从引擎中提取参数

我想用Python对我的函数进行数学测试。 可以访问Python的典型程序是: #include #include #include int RunTests() { Py_Initialize(); PyRun_SimpleString(“a=5”); PyRun_SimpleString(“b=’Hello'”); PyRun_SimpleString(“c=1+2j”); PyRun_SimpleString(“d=[1,3,5,7,9]”); //question here Py_Finalize(); return 0; } 我的问题是:如何将参数a,b,c,d提取到PyObject ?

基于C-exit代码捕获ctypes中的exception

我正在使用ctypes调用Python/numpy中用C编写的共享库。 这很有效,但是,当在C使用exit函数时, iPython会出现一些意外结果。 请考虑以下示例,其中数组“A”的第一项在C修改。 如果值为负,则应引发exception。 C代码: #include #include #include extern void cfun(double* A) { // raise exception if A[0]<0.0 if ( A[0]<0.0 ) { printf("Negative value of A[0] encountered\n"); exit(1); } // change "A[0]" to it's square A[0] = pow(A[0],2); } 哪个是使用编译的 gcc -c -fPIC fun.c gcc -shared -o test.so fun.o 包装Python -code: import numpy […]

在f2py中设置fortran编译器

我试图运行f2py 示例来创建编译的扩展模块¶: # import os # os.environ[“CC”] = “gcc” # os.environ[“CXX”] = “g++” # Using post-0.2.2 scipy_distutils to display fortran compilers from scipy_distutils.fcompiler import new_fcompiler compiler = new_fcompiler() # or new_fcompiler(compiler=’intel’) compiler.dump_properties() #Generate add.f wrapper from numpy import f2py with open(“add.f”) as sourcefile: sourcecode = sourcefile.read() print ‘Fortran code’ print sourcecode # f2py.compile(sourcecode, modulename=’add’, extra_args […]

嵌入C语言时的Python语法错误

我有一个python文件,当我对我的python解释器执行它时运行正常。 我正在尝试使用python C API从C程序调用相同的文件: #include #include int main(int argc, char* argv[]){ FILE* fp; Py_SetProgramName(argv[0]); Py_Initialize(); PySys_SetArgv(argc, argv); fp = fopen(“floatcli.py”, “r”); PyRun_SimpleFile(fp, “floatcli.py”); Py_Finalize(); } 但是,当我运行这个时,我得到一个python语法错误: File “floatcli.py”, line 1 üBa ^ SyntaxError: invalid syntax (在这里没有显示的BBa中还有一个装在盒子里的?)。 floatcli.py的第一行只是一个导入语句……任何想法是怎么回事?

shout-python分段错误我该怎么解决这个问题?

我正在尝试为icecast2 / shoutcast创建一个源代码。 但在编译完所有内容后,我遇到了分段错误。 在使用gdb进一步调试之后,我得到了更详细的错误。 我不知道任何类型的c,所以我不知道该怎么做这个错误 Program received signal SIGSEGV, Segmentation fault. send_mp3 (self=0x988eb0, buff = 0xa5c154 “” at mp3.c:175175 mp3.c: No such file or directory. 我想也许是循环使用了很多资源。 但无论我设置多少time.sleep(),我仍然得到相同的结果。 import random import shout from pyModules import db from pyModules import error import ID3 import time import sys import glob class Audio(object): def __init__(self): self.count = 0 […]