Closed
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: abc
The 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: abc
There is currently no test for this nested case so I think we should add one.