Ist es möglich, CTypes.struktur mit regelmäßigen Pythonfeldern zu mischen?Python

Python-Programme
Anonymous
 Ist es möglich, CTypes.struktur mit regelmäßigen Pythonfeldern zu mischen?

Post by Anonymous »

Nur so geradlinig, scheint in Python nicht zu funktionieren. < /p>

Code: Select all

import ctypes

class cA(ctypes.Structure):
_fields_ = [("a", ctypes.c_ubyte)]

def __init__(self):
super().__init__()
self.c = 3

z = cA.from_buffer_copy(b'0')
print (z)
print (z.c)
print (dir(z))
< /code>
Traceback (most recent call last):
File "polymorph.py", line 12, in 
print (z.c)
AttributeError: 'cA' object has no attribute 'c'
< /code>
So ctypes.Structure
factory classMethod von_buffer_copy () scheint den Standardkonstruktor nicht zu verwenden. /> Wie bei der Klasse CA: < /p>
Hinzufügen zuklassen

Code: Select all

@classmethod
def from_buffer_copy(cls,buffer, offset=0):
#obj = super().from_buffer_copy(b'2',offset)
obj = cls.from_buffer_copy(b'2',offset)
obj.__init__()
return obj
< /code>
Traceback (most recent call last):
File "polymorph.py", line 17, in 
z = cA.from_buffer_copy(b'0')
File "polymorph.py", line 13, in from_buffer_copy
obj = cls.from_buffer_copy(b'2',offset)
File "polymorph.py", line 13, in from_buffer_copy
obj = cls.from_buffer_copy(b'2',offset)
File "polymorph.py", line 13, in from_buffer_copy
obj = cls.from_buffer_copy(b'2',offset)
[Previous line repeated 996 more times]
RecursionError: maximum recursion depth exceeded
< /code>
This creates a RecursionError
, da die gleiche Klassenmethode immer wieder aufgerufen wird, aber struktur.from_buffer_copy kann auch nicht aufgerufen werden, da es _fields _ . Es erhöht einen AttributeError :

Code: Select all

Traceback (most recent call last):
File "polymorph.py", line 17, in 
z = cA.from_buffer_copy(b'0')
File "polymorph.py", line 12, in from_buffer_copy
obj = super().from_buffer_copy(b'2',offset)
AttributeError: 'super' object has no attribute 'from_buffer_copy'
< /code>
This seems to be similar to Using super with a class method
Looks like a special Python 3 thing either.
So ctypes.Structure< /code> scheint eine wirklich besondere Art von Klasse zu sein.import ctypes

class A:
def __init__(self):
self.a = "Tex"

@classmethod
def factory(cls):
return cls()

class B(A):
def __init__(self):
super().__init__()
self.b = "bTex"

z = B.factory()
print (z)
print (z.b)
print (dir(z))
< /code>
So ... Is there a way to mix ctypes.Structure
mit regelmäßigen Python -Klassenfeldern oder besser einfach nicht und besser einbetten ctypes.structure innerhalb anderer regulärer Klassen dafür, um dies nur umzuarbeiten?

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post