我正在使用ioctl调用进行内核模块编程,以便在用户空间和内核空间之间进行通信。我计划用python编写用户空间api。
为了在内核和用户空间之间传递数据,ioctl调用使用地址,并使用copy_to_user或copy_from_user复制数据。这里的地址是由unsigned long arg提供的。
int ioctl(struct inode *inode,struct file *filp,unsigned int cmd,unsigned long arg)
在C用户空间编程中,传递地址是微不足道的。我想做的是,使用python的创建一个与我在内核模块中定义的结构兼容的结构,并使用
我正在为C库编写一个带有ctype的python包装器,并且我有一个返回结构的C函数。但是该结构是在另一个C文件中定义的,该文件是由swig包装的。
我简化了结构和代码。
这个结构是由swig包装的。
struct point {
int x;
int y;
};
此函数由ctype包装。
struct point add_points(struct point a, struct point b) {
struct point c;
c.x = a.x + b.x;
c.y = a.y + b.y;
return c;
}
Python包装器。
import swig
我有一个C/C++结构,它包含一个对象(例如,一个STL容器)。我想使用ctype从Python中访问它。例如,
C/C++:
struct Foo {
int bar;
vector<int> baz;
double quxx;
}
Python:
from ctypes import *
class Foo(Structure):
_fields_ = [
("bar", c_int),
("baz", ???), <--- what do i put here?
("quxx",
我有一个大约500 K uint_32s的大长的ctype数组。我希望在ctype结构中引用从arr0x4000到arr0x8000的一个节。
基本上:
class my_struct(Structure):
_fields_ = [
(important_thing, c_uint32*128),
(other_thing, c_uint32*16) #etc. etc.
]
#arr is a huge ctypes array of length 500K made up of uint_32
section1 = cast(addres
我有一个Python扩展,它封装了专有产品的库。我们公司拥有大量使用专有产品的C代码。与用我的C扩展重写Python不同,我认为我可以简单地将一个胶囊返回到Python,并允许我的库的用户用ctype包装一些C函数。
这是一个有效的方法吗?还有更好的吗?
这里有一些代码来说明我的方法。
我的Python C扩展:
typedef struct {
PyObject_HEAD
Foo *foo; /* The proprietary data structure we are wrapping */
} PyFoo;
/*
* Expose a pointer to Foo
我正在使用Python3.6中的ctype与C库进行交互。其中一个函数启用库,并将类型定义返回给需要传递给后续函数调用的内部对象。下面是一个例子: // Type definition of the internal object.
typedef struct lib_internal_obj * lib_internal_obj_t;
/**
* Struct containing the available options for the library
*/
struct lib_options {
char ip[255];
我需要使用共享库(.so)处理从C程序到python实例(Django)的大量数据。是否可以通过ctype返回它们?
例如:
import ctypes
a = ctypes.CDLL('foo.so')
b = a.collect()
latitude = b.latitude
longitude = b.longitude
和一个C:
main()
{
/* steps to get the data */
return <kind of struct or smth else>;
}
我是新手,所以有没有一种方式来传递这样的数据呢?
在ctype结构中,可以指定默认值吗?
例如,使用常规的python函数,您可以执行以下操作:
def func(a, b=2):
print a + b
这将允许这种行为:
func(1) # prints 3
func(1, 20) # prints 21
func(1, b=50) # prints 51
在ctype结构中可以做到这一点吗?
例如:
class Struct(Structure):
_fields_ = [("a", c_int), ("b", c_int)] # b default should be 2
当C函数用ctype包装时,当方法返回一个空指针到映射到从ctypes.Structure派生的python类的结构时会发生什么?另外,当传递给方法的有效指针设置为空值时会发生什么情况?以下测试将帮助我们找到答案:
//python_ctypes_test.c
#include <stdlib.h>
struct test_struct {
int a;
int b;
int c;
int d;
} typedef test_struct;
test_struct* get_test_struct(const int valid) {
t
我有一个在c++和python进程之间共享的ctype对象。python进程从该对象获取输入值,在Tensorflow中运行它们,我将得到一个numpy数组作为输出。由于这些数组非常大,我想知道是否有更好的方法将数据从tensorflow的输出复制回共享的ctype对象,以便c++进程可以对它们进行操作。(速度是问题所在,是的。)
现在,我正在逐个复制每个值:
output = np.array([12, 13, 11, 10]) # in reality this is fairly large (the Tensorflow result)
for i, value in enumerat
我用C语言编写了一个结构,并将数据加载到其中。
struct service { char a[150]; char b[150]; };
void Food1(struct service ***p, int* pSize) {
int i;
*pSize = 2;
struct service **pReturn = (struct service **)malloc(2 *sizeof(struct service *));
for (i = 0; i < 2; i++) {
pReturn[i] = (struct servi
我通过ctype把一个dll集成到python中,这个dll需要设置一个回调函数。但是dll回调函数声明如下
void setCallBack(void(*)(Data &a))
Data is C struct, declare as below
typedef struct {
int index,
....
}Data
我不清楚如何通过ctype设置它,我尝试如下,但返回"WindowsError: error -1073741795 Windows Error 0xC000001D“错误,您能帮我举一个如何设置回调的例子吗?
mydll = cdll.
我刚刚开始使用python,我想知道我应该如何定义python的联合(使用ctype)?希望我是对的,联合是通过ctype来支持的。例如,下面的c代码在python中是如何实现的。
struct test
{
char something[10];
int status;
};
struct test2
{
char else[10];
int status;
int alive;
};
union tests
{
struct test a;
struct test2 b;
};
struct tester
{
char more_chars[20];
int magic;
union
使用python的ctype,可以指定接受类型的指针:
class METADATA(Structure):
_fields_ = [("classes", c_int),
("names", POINTER(c_char_p))]
对于JNR,它看起来如下:
public static class Metadata extends Struct{
public Metadata(jnr.ffi.Runtime rt) {
super(rt);
}
public final Struct.Unsig
我正在为一个使用SWIG公开其C++应用程序接口的程序编写一个python脚本。SWIG公开的函数有一个类似如下的接口:
void writePixelsRect(JoxColor* colors, int left, int top, int width, int height);
JoxColor是一个POD结构,如下所示:
struct JoxColor {
float r, g, b, a;
};
我可以很容易地在Python语言中创建一个JoxColor并调用writePixelsRect,如下所示:
c = JoxApi.JoxColor()
c.r = r
c.g = g
上下文:我的Python代码将2D顶点数组传递给OpenGL。
我测试了两种方法,一种使用ctype,另一种使用struct,后者的速度要快两倍以上。
from random import random
points = [(random(), random()) for _ in xrange(1000)]
from ctypes import c_float
def array_ctypes(points):
n = len(points)
return n, (c_float*(2*n))(*[u for point in points for u in point])