zoukankan      html  css  js  c++  java
  • 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
    >>>
    
  • 相关阅读:
    linux 虚拟机web服务接入互联网
    golang操作数据库
    开启提示:press esc in 5 seconds to skip 如何操作
    如何将qcow2转为vhd
    统信UOS如何分卷压缩
    统信UOS欧拉版本如何制作启动盘
    UOS输错密码导致长时间锁定怎么办?
    在UOS中使用WPS编辑文件,忘记保存关闭了文件,怎么找回?
    uos server版一开始没有安装桌面,后面客户需要加装DDE桌面如何实现
    uos系统升级时,我不想升级相关软件包,应该如何去做
  • 原文地址:https://www.cnblogs.com/fendou-999/p/3858080.html
Copyright © 2011-2022 走看看