• Python ctypes中cast/py_object用法


    class ctypes.py_object

    Represents the C PyObject * datatype. Calling this without an argument creates a NULL PyObject * pointer.

    示例:

    >>> dc = {'a':'aaa','b':'bbb'}

    >>> c = py_object(dc)
    >>> c
    py_object({'b': 'bbb', 'a': 'aaa'})
    >>> c.value
    {'b': 'bbb', 'a': 'aaa'}
    >>> dc
    {'b': 'bbb', 'a': 'aaa'}

    Type conversions

    Usually, ctypes does strict type checking. This means, if you have POINTER(c_int) in the argtypes list of a function or as the type of a member field in a structure definition, only instances of exactly the same type are accepted. There are some exceptions to this rule, where ctypes accepts other objects. For example, you can pass compatible array instances instead of pointer types. So, for POINTER(c_int), ctypes accepts an array of c_int:

    >>>
    >>> class Bar(Structure):
    ...     _fields_ = [("count", c_int), ("values", POINTER(c_int))]
    ...
    >>> bar = Bar()
    >>> bar.values = (c_int * 3)(1, 2, 3)
    >>> bar.count = 3
    >>> for i in range(bar.count):
    ...     print(bar.values[i])
    ...
    1
    2
    3
    >>>
    

    In addition, if a function argument is explicitly declared to be a pointer type (such as POINTER(c_int)) in argtypes, an object of the pointed type (c_int in this case) can be passed to the function. ctypes will apply the required byref() conversion in this case automatically.

    To set a POINTER type field to NULL, you can assign None:

    >>>
    >>> bar.values = None
    >>>
    

    Sometimes you have instances of incompatible types. In C, you can cast one type into another type. ctypes provides a cast() function which can be used in the same way. The Bar structure defined above accepts POINTER(c_int) pointers or c_int arrays for its values field, but not instances of other types:

    >>>
    >>> bar.values = (c_byte * 4)()
    Traceback (most recent call last):
      File "<stdin>", line 1, in ?
    TypeError: incompatible types, c_byte_Array_4 instance instead of LP_c_long instance
    >>>
    

    For these cases, the cast() function is handy.

    The cast() function can be used to cast a ctypes instance into a pointer to a different ctypes data type. cast() takes two parameters, a ctypes object that is or can be converted to a pointer of some kind, and a ctypes pointer type. It returns an instance of the second argument, which references the same memory block as the first argument:

    >>>
    >>> a = (c_byte * 4)()
    >>> cast(a, POINTER(c_int))
    <ctypes.LP_c_long object at ...>
    >>>
    

    So, cast() can be used to assign to the values field of Bar the structure:

    >>>
    >>> bar = Bar()
    >>> bar.values = cast((c_byte * 4)(), POINTER(c_int))
    >>> print(bar.values[0])
    0
    >>>
    
  • 相关阅读:
    深度学习100问之深度学习的本质
    Docker在Windows下的安装以及Hello World
    杂谈——如何在CSDN上上传图片,并添加到自定义栏目中
    打造livecd的注意事项
    打造livecd的注意事项
    磁盘管理基础
    磁盘管理基础
    磁盘管理基础
    磁盘管理基础
    LFS资料和SSH远程登录全过程
  • 原文地址:https://www.cnblogs.com/fendou-999/p/3858080.html
Copyright © 2020-2023  润新知