Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从继承的ctypes.Structure类的构造函数中的\u buffer\u copy调用_Python_Constructor_Ctypes_Init - Fatal编程技术网

Python 从继承的ctypes.Structure类的构造函数中的\u buffer\u copy调用

Python 从继承的ctypes.Structure类的构造函数中的\u buffer\u copy调用,python,constructor,ctypes,init,Python,Constructor,Ctypes,Init,我有以下代码: class MyStruct(ctypes.Structure): _fields_= [('id', ctypes.uint), ('perm', ctypes.uint)] 定义了类之后,我就可以直接在字段上从缓冲区复制数据了。 例如: 一切正常,这里id将是0xAAAAAAAA,perm等于0x11111111 现在,我尝试在实例化过程中使用以下代码执行相同的操作: class MyStruct(ctypes.Structur

我有以下代码:

class MyStruct(ctypes.Structure):
      _fields_= [('id', ctypes.uint),
                 ('perm', ctypes.uint)]
定义了类之后,我就可以直接在字段上从缓冲区复制数据了。 例如:

一切正常,这里id将是0xAAAAAAAA,perm等于0x11111111

现在,我尝试在实例化过程中使用以下代码执行相同的操作:

class MyStruct(ctypes.Structure):
      _fields_= [('id', ctypes.uint),
                 ('perm', ctypes.uint)]
      def __init__(self):
          super(MyStruct, self).__init__()
          self.from_buffer_copy("\xAA\xAA\xAA\xAA\x11\x11\x11\x11")

ms = MyStruct()
print ms.id, ms.perm
但我的代码在以下语句中引发了一个错误:

AttributeError:“MyStruct”对象没有“from\u buffer\u copy”属性

经过一些研究,我发现
from\u buffer\u copy
是一种
ctypes.\u CData
方法。在文档中,我们可以看到
\u CData
类是

这就是我的问题。我想在构造函数中使用
from\u buffer\u copy
,但此时它看起来“不可调用”。你能帮我吗

提前感谢您的回复 问候


PS:我不想使用样式
super(MyStruct,self)。\uuuu init\uuuuu(id=0x4444,perm=0x11111111)
,因为在我的实际代码中,我的fields变量上有很多参数。

问题是,from\u buffer\u copy从给定的缓冲区创建新的结构实例。当您在uuu init_uuuu中使用它时,结构已经创建,因此无法使用from_buffer_copy(这就是为什么它只能作为类方法而不是实例方法使用)

一个简单的解决方案就是继续使用MyStruct.from\u buffer\u copy,或者编写另一个工厂函数来满足您的需要。如果您坚持使用MyStruct(buffer),那么您可以为此使用_new__,因为它是在创建实例之前调用的:

import ctypes
class MyStruct(ctypes.Structure):
    _fields_= [('id', ctypes.c_uint),('perm', ctypes.c_uint)]
    def __new__(cls, buf):
        return cls.from_buffer_copy(buf)

    def __init__(self, data):
        pass  ## data is already present in class

ms = MyStruct("\xAA\xAA\xAA\xAA\x11\x11\x11\x11")
print ms.id, ms.perm

这并不能真正回答您的问题,但是如何将值作为字典传递到结构中:MyStruct(**字段)?这就解决了拥有大量字段的问题。嗨,Luke,MyStruct的输入数据不是参数列表(否则,**字段可能会有用)。在这里,我通过from_buffer_copy.Ok的帮助在我的字段中复制完全相同大小的数据,然后将缓冲区发送到MyStruct,然后在初始化器中使用struct.unpack在调用super()之前拆分为字段?也许这有点太尴尬了,再加上浪费,因为数据将被重新打包。我的目标是使用from_buffer_copy,原因有二。首先,避免所有需要解析的额外内容。因为拆分数据(解包或其他)并对每个变量名的值进行排序是浪费的(我必须对我的所有结构进行排序)。接下来,我想了解为什么我们可以在实例化之后调用from_buffer_copy,而不是在初始化期间调用。这真是一个奇怪的问题。它就像一个符咒。非常感谢您的中肯回答!
import ctypes
class MyStruct(ctypes.Structure):
    _fields_= [('id', ctypes.c_uint),('perm', ctypes.c_uint)]
    def __new__(cls, buf):
        return cls.from_buffer_copy(buf)

    def __init__(self, data):
        pass  ## data is already present in class

ms = MyStruct("\xAA\xAA\xAA\xAA\x11\x11\x11\x11")
print ms.id, ms.perm