本文介绍了在继承的ctypes.Structure类的构造函数中调用from_buffer_copy的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

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

一切正常,这里的 id 0xAAAAAAAA ,而 perm 等于 0x11111111 .

Everythings works fine, here id will be 0xAAAAAAAA and perm equals to 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 :

经过研究,我发现 from_buffer_copy ctypes._CData 方法.在文档中,我们可以看到 _CData 类是非-公共课.

After some research i found that from_buffer_copy is a ctypes._CData method. On the documentation we can read that _CData class is non-public class.

这是我的问题.我想在构造函数中使用 from_buffer_copy ,但目前看来它不可调用".你可以帮帮我吗 ?

So here my problem. I would like to use from_buffer_copy in the constructor but it looks 'not callable' at this moment. Could you help me ?

提前感谢您的回复问候

PS:我不想使用样式 super(MyStruct,self).__ init __(id = 0x44444444,perm = 0x11111111),因为在我的真实代码中,我的 fields 变量.

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.

推荐答案

问题是from_buffer_copy从给定的缓冲区创建 new 结构实例.当您在__init__中使用它时,该结构已经创建完毕,因此将无法使用from_buffer_copy(这就是为什么它仅作为类方法而不是实例方法可用的原因).

The problem is that from_buffer_copy creates new Structure instance from the given buffer. 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).

一个简单的解决方案是继续使用MyStruct.from_buffer_copy或编写另一个工厂函数来满足您的需求.如果您坚持使用MyStruct(buffer),则可以为此目的使用__new__,因为它在实例创建之前被称为 :

An easy solution is just to keep using MyStruct.from_buffer_copy as you have or write another factory function to suit your needs. 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:

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

这篇关于在继承的ctypes.Structure类的构造函数中调用from_buffer_copy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 05:55