简体   繁体   English

在继承的ctypes.Structure类的构造函数中调用from_buffer_copy

[英]Calling from_buffer_copy in the constructor of inherited ctypes.Structure class

I have the following piece of code: 我有以下代码:

class MyStruct(ctypes.Structure):
      _fields_= [('id', ctypes.uint),
                 ('perm', ctypes.uint)]

When the class is defined, i'm able to copy data from buffer directly on my fields. 定义了类后,我就可以直接从字段上的缓冲区复制数据。 Eg: 例如:

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

Everythings works fine, here id will be 0xAAAAAAAA and perm equals to 0x11111111 . 逝去的正常工作,在这里ID0xAAAAAAAA烫发等于0x11111111。

Now, i tried to do the same thing during the instantiation, with the following code: 现在,我尝试使用以下代码在实例化期间执行相同的操作:

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

But my code raises an error with the following statement : 但是我的代码使用以下语句引发了错误:

AttributeError: 'MyStruct' object has no attribute 'from_buffer_copy' AttributeError:“ MyStruct”对象没有属性“ from_buffer_copy”

After some research i found that from_buffer_copy is a ctypes._CData method. 经过一番研究,我发现from_buffer_copy是一个ctypes._CData方法。 On the documentation we can read that _CData class is non-public class . 在文档中,我们可以看到_CData是非公共类

So here my problem. 所以这是我的问题。 I would like to use from_buffer_copy in the constructor but it looks 'not callable' at this moment. 我想在构造函数中使用from_buffer_copy ,但此刻它看起来“不可调用”。 Could you help me ? 你可以帮帮我吗 ?

Thanks in advance for your reply Regards 在此先感谢您的回复

PS: I don't want to use the style super(MyStruct,self).__init__(id=0x44444444,perm=0x11111111) because on my real code there are a lot of arguments on my fields variable. PS:我不想使用样式super(MyStruct,self).__init__(id=0x44444444,perm=0x11111111)因为在我的实际代码中,我的fields变量上有很多参数。

The problem is that from_buffer_copy creates new Structure instance from the given buffer. 问题是from_buffer_copy从给定的缓冲区创建新的 Structure实例。 When you use it inside __init__, the Structure has already been created and thus there would be no way to make use of from_buffer_copy (and this is why it is only available as a class method, not an instance method). 当您在__init__中使用它时,已经创建了该结构,因此将无法使用from_buffer_copy(这就是为什么它仅作为类方法而不是实例方法可用的原因)。

An easy solution is just to keep using MyStruct.from_buffer_copy as you have or write another factory function to suit your needs. 一个简单的解决方案是继续使用MyStruct.from_buffer_copy或编写另一个工厂函数来满足您的需求。 If you insist on using using MyStruct(buffer), then you can use __new__for this purpose since it is called before the instance has been created: 如果您坚持使用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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM