FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-153903: Fix ctypes wrap_dll_function() with string annotations by vstinner · Pull Request #154042 · python/cpython · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (3) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
2 changes: 1 addition & 1 deletion Lib/ctypes/util.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ def wrap_dll_function(dll):
def decorator(func):
name = func.__name__
ptr = getattr(dll, name)
annotations = annotationlib.get_annotations(func)
annotations = annotationlib.get_annotations(func, eval_str=True)

try:
restype = annotations.pop("return")
Expand Down
5 changes: 5 additions & 0 deletions Lib/test/test_ctypes/test_funcptr.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,11 @@ def noexist():
def PyObject_GetAttrString(op: ctypes.py_object, attr: ctypes.c_char_p):
pass

def test_wrap_dll_function_str_ann(self):
from test.test_ctypes import wrap_str_ann
version = wrap_str_ann.Py_GetVersion()
self.assertIsInstance(version, bytes)


if __name__ == '__main__':
unittest.main()
13 changes: 13 additions & 0 deletions Lib/test/test_ctypes/wrap_str_ann.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from __future__ import annotations
import ctypes.util

def test_ann() -> ctypes.c_char_p:
...

# Check that "from __future__ import annotations" works as expected
if not isinstance(test_ann.__annotations__['return'], str):
raise Exception("annotations must be strings")

@ctypes.util.wrap_dll_function(ctypes.pythonapi)
def Py_GetVersion() -> ctypes.c_char_p:
...
Loading

Back | FazBrowse Home | New Git URL