ctypes 为 c_ulong 重新实现 rshift

mag*_*gie 5 python ctypes operator-overloading bit-shift

我正在通过 ctypes 访问 C 库,但遇到以下问题:

我正在使用 ctypeslib 生成“包装器”(使用 ctypes 访问库的​​ ctypes 命令)。C 库包含在此步骤中转换为 python 函数的宏。(为了尽可能独立于库内部,我想在 python 中使用其中一些宏。)

这些宏之一如下所示:

# using the ctypes types
myuint16_t = c_ushort
myuint32_t = c_ulong

def mymacro(x): return (myuint16_t)((myuint32_t)(x) >> 16) # macro
Run Code Online (Sandbox Code Playgroud)

我想通过以下方式(在函数内部)在单独的模块中使用生成的函数:

return wrapper.mymacro(valueToBeConverted) # valueToBeConverted is an int
Run Code Online (Sandbox Code Playgroud)

但是使用这一行我得到了以下错误:

....   
def mymacro(x): return (myuint16_t)((myuint32_t)(x) >> 16) # macro
TypeError: unsupported operand type(s) for >>: 'c_ulong' and 'int'
Run Code Online (Sandbox Code Playgroud)

(我知道移动 c_ulong 的常用方法是,c_ulongvar.value >> x但每次 C 库中的某些内容发生变化时,我都必须修补生成的包装器。所以我尽量避免这种情况)。

这里好像__rshift__不能用c_ulong的实现。

print c_ulong.__rshift__
# throws AttributeError: type object 'c_ulong' has no attribute '__rshift__'
Run Code Online (Sandbox Code Playgroud)

嗯,看起来很奇怪......所以我决定重新实现__rshift__c_ulong的方法来让它工作:

from ctypes import *
from types import MethodType

def rshift(self, val):
    print self.value >> val

# create an unbound method which applies to all (even existing) instances
c_ulong.__rshift__ = MethodType(rshift, None, c_ulong)

a = c_ulong(1)
a >> 16
Run Code Online (Sandbox Code Playgroud)

但它并不能解决问题。我仍然收到错误:

a >> 16
TypeError: unsupported operand type(s) for >>: 'c_ulong' and 'int'
Run Code Online (Sandbox Code Playgroud)

__rshift__方法是否可能只能用于同一类的两个实例?我尝试了以下方法:

def rshift(self, val):
    print self.value >> int(val.value)

a = c_ulong(1)
a >> c_ulong(16) 
Run Code Online (Sandbox Code Playgroud)

它有效。但这也意味着我仍然需要修补生成的包装器。

所以:有人知道这里有什么诀窍吗?

更新

@eryksun 的解决方案奏效了。我在用:

from ctypes import *
# from types import MethodType

def _rshift(self, other):
    if hasattr(other, 'value'):
        other = other.value
    return c_ulong(self.value >> other)

def _lshift(self, other):
    if hasattr(other, 'value'):
        other = other.value
    return c_ulong(self.value << other)

def _coerce(self, other):
    try:
        return self, self.__class__(other)
    except TypeError:
        return NotImplemented

# Add the functions to the type. A method is created when
# accessed as an attribute of an instance.
c_ulong.__lshift__ = _lshift
c_ulong.__rshift__ = _rshift
c_ulong.__coerce__ = _coerce
Run Code Online (Sandbox Code Playgroud)

Ery*_*Sun 4

由于该_ctypes._SimpleCData类型没有标志Py_TPFLAGS_CHECKTYPES,因此 2.x 子类被视为在二进制运算中使用的旧式数字__coerce__。函数中的调用方案和实现参见Objects/abstract.cbinary_op1

void *出于演示目的,可以在类型对象上切换此标志,您只需在字段中定义(模糊地使用大量)tp_flags即可。

黑客入侵PyTypeObject

from ctypes import *
import _ctypes

Py_TPFLAGS_CHECKTYPES = 1 << 4

class PyTypeObject(Structure):
    _fields_ = (('ob_refcnt', c_ssize_t),
                ('ob_type', c_void_p),
                ('ob_size', c_ssize_t),
                ('tp_name', c_char_p),
                ('tp_basicsize', c_ssize_t),
                ('tp_itemsize', c_ssize_t),
                ('tp_dealloc', c_void_p),
                ('tp_print', c_void_p),
                ('tp_getattr', c_void_p),
                ('tp_setattr', c_void_p),
                ('tp_compare', c_void_p),
                ('tp_repr', c_void_p),
                ('tp_as_number', c_void_p),
                ('tp_as_sequence', c_void_p),
                ('tp_as_mapping', c_void_p),
                ('tp_hash', c_void_p),
                ('tp_call', c_void_p),
                ('tp_str', c_void_p),
                ('tp_getattro', c_void_p),
                ('tp_setattro', c_void_p),
                ('tp_as_buffer', c_void_p),
                ('tp_flags', c_long))
Run Code Online (Sandbox Code Playgroud)

接下来,创建一个unsigned long子类,并使用from_address工厂为其创建一个子类PyTypeObject。使用built-in获取地址id,这是特定于CPython的实现细节:

class c_ulong(_ctypes._SimpleCData):
    _type_ = "L"

    def __rshift__(self, other):
        print '__rshift__', self, other
        if hasattr(other, 'value'):
            other = other.value
        return c_ulong(self.value >> other)

c_ulong_type = PyTypeObject.from_address(id(c_ulong))
Run Code Online (Sandbox Code Playgroud)

演示

>>> a = c_ulong(16)
>>> b = c_ulong(2)

>>> a >> b
__rshift__ c_ulong(16L) c_ulong(2L)
c_ulong(4L)

>>> a >> 2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for >>: 'c_ulong' and 'int'
Run Code Online (Sandbox Code Playgroud)

正如预期的那样,最后一步失败了。现在设置标志:

>>> c_ulong_type.tp_flags |= Py_TPFLAGS_CHECKTYPES

>>> a >> 2
__rshift__ c_ulong(16L) 2
c_ulong(4L)
Run Code Online (Sandbox Code Playgroud)

问题解决了?但这是一个黑客行为。重试并__coerce__实施。


实施__coerce__

class c_ulong(_ctypes._SimpleCData):
    _type_ = "L"

    def __rshift__(self, other):
        print '__rshift__', self, other
        if hasattr(other, 'value'):
            other = other.value
        return c_ulong(self.value >> other)

    def __coerce__(self, other):
        print '__coerce__', self, other
        try:
            return self, self.__class__(other)
        except TypeError:
            return NotImplemented
Run Code Online (Sandbox Code Playgroud)

演示

>>> a = c_ulong(16)
>>> b = c_ulong(2)

>>> a >> 2
__coerce__ c_ulong(16L) 2
__rshift__ c_ulong(16L) c_ulong(2L)
c_ulong(4L)

>>> 16 >> b
__coerce__ c_ulong(2L) 16
__rshift__ c_ulong(16L) c_ulong(2L)
c_ulong(4L)
Run Code Online (Sandbox Code Playgroud)

当然,如果c_ulong无法创建 a ,则会失败,例如 a float

>>> a >> 2.0
__coerce__ c_ulong(16L) 2.0
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for >>: 'c_ulong' and 'float'
Run Code Online (Sandbox Code Playgroud)