Skip to content

ctypes: Add a testcase for nested _as_parameter_ lookup #107461

Closed
@tomasr8

Description

@tomasr8

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.

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions