Code: Select all
@dataclasses.dataclass
class Point:
x = ctypes.c_int(0)
y = ctypes.c_int(0)
@dataclasses.dataclass
class Point2:
x = ctypes.c_int(0)
y = ctypes.c_int(0)
@dataclasses.dataclass
class TestObj:
point1 = Point()
point2 = Point()
übergeben
Code: Select all
#include
void inc(int *x, int *y)
{
*x = *x + 1;
*y = *y + 1;
}
< /code>
über einen Wrapper: < /p>
def inc(test: TestObj):
_inc = lib.inc
_inc.argtypes = [ctypes.POINTER(ctypes.c_int), ctypes.POINTER(ctypes.c_int)]
_inc.restype = None
_inc(test.point1.x,test.point1.y)
Code: Select all
test = TestObj()
print(f"addressof test.point1.x = {ctypes.addressof(test.point1.x)}")
print(f"test.point1.x.value = {test.point1.x.value}")
print(f"addressof test.point1.y = {ctypes.addressof(test.point1.y)}")
print(f"test.point1.y.value = {test.point1.y.value}")
print(f"addressof test.point2.x = {ctypes.addressof(test.point2.x)}")
print(f"test.point2.x.value = {test.point2.x.value}")
print(f"addressof test.point2.y = {ctypes.addressof(test.point2.y)}")
print(f"test.point2.y.value = {test.point2.y.value}")
inc(test)
print("after pass to inc")
print(f"addressof test.point1.x = {ctypes.addressof(test.point1.x)}")
print(f"test.point1.x.value = {test.point1.x.value}")
print(f"addressof test.point1.y = {ctypes.addressof(test.point1.y)}")
print(f"test.point1.y.value = {test.point1.y.value}")
print(f"addressof test.point2.x = {ctypes.addressof(test.point2.x)}")
print(f"test.point2.x.value = {test.point2.x.value}")
print(f"addressof test.point2.y = {ctypes.addressof(test.point2.y)}")
print(f"test.point2.y.value = {test.point2.y.value}")
< /code>
führt zu: < /p>
addressof test.point1.x = 124872721403536
test.point1.x.value = 0
addressof test.point1.y = 124872719995280
test.point1.y.value = 0
addressof test.point2.x = 124872721403536
test.point2.x.value = 0
addressof test.point2.y = 124872719995280
test.point2.y.value = 0
after pass to inc
addressof test.point1.x = 124872721403536
test.point1.x.value = 1
addressof test.point1.y = 124872719995280
test.point1.y.value = 1
addressof test.point2.x = 124872721403536
test.point2.x.value = 1
addressof test.point2.y = 124872719995280
test.point2.y.value = 1
neu definieren
Code: Select all
@dataclasses.dataclass
class TestObj:
point1 = Point()
point2 = Point2()
Code: Select all
addressof test.point1.x = 133142487043728
test.point1.x.value = 0
addressof test.point1.y = 133142485635472
test.point1.y.value = 0
addressof test.point2.x = 133142485635728
test.point2.x.value = 0
addressof test.point2.y = 133142485635984
test.point2.y.value = 0
after pass to inc
addressof test.point1.x = 133142487043728
test.point1.x.value = 1
addressof test.point1.y = 133142485635472
test.point1.y.value = 1
addressof test.point2.x = 133142485635728
test.point2.x.value = 0
addressof test.point2.y = 133142485635984
test.point2.y.value = 0