Skip to content

ctypes: Add a testcase for nested _as_parameter_ lookup #107461

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
tomasr8 opened this issue Jul 30, 2023 · 0 comments
Closed

ctypes: Add a testcase for nested _as_parameter_ lookup #107461

tomasr8 opened this issue Jul 30, 2023 · 0 comments
Labels
topic-ctypes type-feature A feature request or enhancement

Comments

@tomasr8
Copy link
Member

tomasr8 commented Jul 30, 2023

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

@tomasr8 tomasr8 added the type-feature A feature request or enhancement label Jul 30, 2023
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Feb 1, 2024
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Feb 1, 2024
serhiy-storchaka pushed a commit that referenced this issue Feb 1, 2024
…ookup (GH-107462) (GH-114858)

(cherry picked from commit 0bf42da)

Co-authored-by: Tomas R <[email protected]>
serhiy-storchaka pushed a commit that referenced this issue Feb 1, 2024
…ookup (GH-107462) (GH-114859)

(cherry picked from commit 0bf42da)

Co-authored-by: Tomas R <[email protected]>
aisk pushed a commit to aisk/cpython that referenced this issue Feb 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic-ctypes type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

3 participants