Python CTypes _argtypes 没有 from_params 方法
Python CTypes _argtypes has no from_params method
- 我的任务是在 python 中创建包装器 class 使用
ctypes。我过去一直在编码和做研究
几个星期,并向任何愿意的人提问
帮助。这段 C 代码是我正在使用的代码的最佳复制,所以请记住,我没有按照它呈现的方式构建它!
- 我对 python 和 ctypes 库都比较陌生
- 在 linux
上使用 Python 2.7.2 和 GCC 4.4.2
Question:
When I try to run the following python code, I get this error:
File "pydog.py", line 21, in
srclib.log_dog.argtypes = [pug]
TypeError: item 1 in argtypes has no from_param method
pydog.py:
import ctypes
from ctypes import *
srclib = ctypes.CDLL('/path/DogHouse.so')
class dog(Structure):
_fields_ = [
("name", ctypes.c_char*10),
("color", ctypes.c_char*10)]
pug = dog("doug", "white")
#- Not sure if I need to define arg and res types -#
srclib.log_dog.argtypes = [pug]
#srclib.log_dog.restype =
srclib.log_dog(pug)
DogHouse.h:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* opaque handle */
typedef struct __dog_file dogfile;
/* Input data structure */
typedef struct __dog_input{
const char *name;
const char *color;
}dog_input;
/*Logs the dog_input data into the dog_file struct */
dog_file *log_dog(const dog_input *data);
DogHouse.c:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "DogHouse.h"
struct __dog_file{
const char *name;
const char *color;
};
dog_file *log_dog(const dog_input *data){
dog_file *df;
df->name = data->name;
df->color = data->color;
return df;
}
int main(int argc, char **argv){
}
我试图找到 "from_param" 的语法,但找不到我需要的具体内容(我不确定它是特定于 ctypes 的,还是 python 库的标准。
这是我关于 SO 的第一个问题,所以如果我需要更清楚地更改 anything/be 请告诉我!在此先感谢您的帮助。
argtypes
中的值应该是类型。 pug
是一个实例。给出的代码无法编译,但以下是我想出的:
pydog.py
import ctypes
srclib = ctypes.CDLL('DogHouse')
# An opaque class
class dog_file(ctypes.Structure):
pass
# c_char_p should be used for "const char *"
class dog_input(ctypes.Structure):
_fields_ = [
("name", ctypes.c_char_p),
("color", ctypes.c_char_p)]
srclib.log_dog.argtypes = [ctypes.POINTER(dog_input)] # pointer type
srclib.log_dog.restype = ctypes.POINTER(dog_file) # pointer type to opaque class
# bytes strings used for const char* (Python 3 syntax)
# create instance and pass it to function
pug = dog_input(b'doug',b'white')
print(srclib.log_dog(pug))
DogHouse.h
/* opaque handle */
typedef struct dog_file dog_file;
/* Input data structure */
typedef struct dog_input
{
const char* name;
const char* color;
} dog_input;
/*Logs the dog_input data into the dog_file struct */
__declspec(dllexport)
dog_file* log_dog(const dog_input* data);
DogHouse.c
#include <stdlib.h>
#include "DogHouse.h"
typedef struct dog_file
{
const char* name;
const char* color;
} dog_file;
dog_file* log_dog(const dog_input* data)
{
dog_file* df = malloc(sizeof(dog_file));
df->name = data->name;
df->color = data->color;
return df;
}
注意在函数实现中,原来返回的是一个局部变量。此版本 malloc 内存,但您需要导出另一个函数来释放它或发生内存泄漏。
输出(不透明指针实例)
<__main__.LP_dog_file object at 0x00000000030040C8>
- 我的任务是在 python 中创建包装器 class 使用 ctypes。我过去一直在编码和做研究 几个星期,并向任何愿意的人提问 帮助。这段 C 代码是我正在使用的代码的最佳复制,所以请记住,我没有按照它呈现的方式构建它!
- 我对 python 和 ctypes 库都比较陌生
- 在 linux 上使用 Python 2.7.2 和 GCC 4.4.2
Question: When I try to run the following python code, I get this error:
File "pydog.py", line 21, in
srclib.log_dog.argtypes = [pug]
TypeError: item 1 in argtypes has no from_param method
pydog.py:
import ctypes
from ctypes import *
srclib = ctypes.CDLL('/path/DogHouse.so')
class dog(Structure):
_fields_ = [
("name", ctypes.c_char*10),
("color", ctypes.c_char*10)]
pug = dog("doug", "white")
#- Not sure if I need to define arg and res types -#
srclib.log_dog.argtypes = [pug]
#srclib.log_dog.restype =
srclib.log_dog(pug)
DogHouse.h:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* opaque handle */
typedef struct __dog_file dogfile;
/* Input data structure */
typedef struct __dog_input{
const char *name;
const char *color;
}dog_input;
/*Logs the dog_input data into the dog_file struct */
dog_file *log_dog(const dog_input *data);
DogHouse.c:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "DogHouse.h"
struct __dog_file{
const char *name;
const char *color;
};
dog_file *log_dog(const dog_input *data){
dog_file *df;
df->name = data->name;
df->color = data->color;
return df;
}
int main(int argc, char **argv){
}
我试图找到 "from_param" 的语法,但找不到我需要的具体内容(我不确定它是特定于 ctypes 的,还是 python 库的标准。
这是我关于 SO 的第一个问题,所以如果我需要更清楚地更改 anything/be 请告诉我!在此先感谢您的帮助。
argtypes
中的值应该是类型。 pug
是一个实例。给出的代码无法编译,但以下是我想出的:
pydog.py
import ctypes
srclib = ctypes.CDLL('DogHouse')
# An opaque class
class dog_file(ctypes.Structure):
pass
# c_char_p should be used for "const char *"
class dog_input(ctypes.Structure):
_fields_ = [
("name", ctypes.c_char_p),
("color", ctypes.c_char_p)]
srclib.log_dog.argtypes = [ctypes.POINTER(dog_input)] # pointer type
srclib.log_dog.restype = ctypes.POINTER(dog_file) # pointer type to opaque class
# bytes strings used for const char* (Python 3 syntax)
# create instance and pass it to function
pug = dog_input(b'doug',b'white')
print(srclib.log_dog(pug))
DogHouse.h
/* opaque handle */
typedef struct dog_file dog_file;
/* Input data structure */
typedef struct dog_input
{
const char* name;
const char* color;
} dog_input;
/*Logs the dog_input data into the dog_file struct */
__declspec(dllexport)
dog_file* log_dog(const dog_input* data);
DogHouse.c
#include <stdlib.h>
#include "DogHouse.h"
typedef struct dog_file
{
const char* name;
const char* color;
} dog_file;
dog_file* log_dog(const dog_input* data)
{
dog_file* df = malloc(sizeof(dog_file));
df->name = data->name;
df->color = data->color;
return df;
}
注意在函数实现中,原来返回的是一个局部变量。此版本 malloc 内存,但您需要导出另一个函数来释放它或发生内存泄漏。
输出(不透明指针实例)
<__main__.LP_dog_file object at 0x00000000030040C8>