
一.c,ctypes和python的数据类型的对应关系
- ctypes type ctype Python type
- c_char char 1-character string
- c_wchar wchar_t 1-character unicode string
- c_byte char int/long
- c_ubyte unsigned char int/long
- c_short short int/long
- c_ushort unsigned short int/long
- c_int int int/long
- c_uint unsigned int int/long
- c_long long int/long
- c_ulong unsigned long int/long
- c_longlong __int64 or long long int/long
- c_ulonglong unsigned __int64 or unsigned long long int/long
- c_float float float
- c_double double float
- c_char_p char * (NUL terminated) string or None
- c_wchar_p wchar_t * (NUL terminated) unicode or None
- c_void_p void * int/long or None
sum_test.py:
2.操作int
1.创建一个c文件
3.操作字符串
- >>> p=create_string_buffer(10)
- >>> p.raw
- '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
- >>> p.value=http://developer.51cto.com/art/201707/'fefefe'
- >>> p.raw
- 'fefefe\x00\x00\x00\x00'
- >>> p.value=http://developer.51cto.com/art/201707/'fefeeeeeeeeeeeeeeeeeeeeeee' #字符串太长,报错
- Traceback (most recent call last):
- File "<stdin>", line 1, in <module>
- ValueError: string too long
4.操作指针
- >>> i=c_int(999)
- >>> pi=pointer(i)
- >>> pi
- <__main__.LP_c_int object at 0x7f7be1983b00>
- >>> pi.value
- Traceback (most recent call last):
- File "<stdin>", line 1, in <module>
- AttributeError: 'LP_c_int' object has no attribute 'value'
- >>> pi.contents
- c_int(999)
- >>> pi.contents=c_long(34343)
- >>> pi.contents
- c_int(34343)
- 经由过程pointer获取一个值的指针
- 经由过程contents获取一个指针的值
5.c的构造体
- #定义一个c的structure,包含两个成员变量x和y
- >>> class POINT(Structure):
推荐阅读
平顺家当涵盖的范畴异常广泛,固然每个细分市场都有“头牌”,但并没有某一家安然巨擘有底气传播鼓>>>详细阅读
本文标题:Python调用C模块以及性能分析
地址:http://www.17bianji.com/lsqh/36195.html
1/2 1

网友点评
精彩导读
科技快报
品牌展示