-
-
Notifications
You must be signed in to change notification settings - Fork 33.5k
Closed
Labels
Description
A ctypes function can be called with custom objects if they define the _as_parameter_ attribute:
from ctypes import *
printf = CDLL('libc.so.6').printf
class MyString:
_as_parameter_ = b"abc"
printf(b"String: %s\n", MyString())
# String: abcThe attribute is evaluated recursively so nested objects also work:
from ctypes import *
printf = CDLL('libc.so.6').printf
class Foo:
_as_parameter_ = b"abc"
class MyString:
_as_parameter_ = Foo()
printf(b"String: %s\n", MyString())
# String: abcThere is currently no test for this nested case so I think we should add one.