Skip to content

gh-104533: Fix @ctypes.util.struct with string annotations#154040

Merged
vstinner merged 1 commit into
python:mainfrom
vstinner:struct_eval_str
Jul 19, 2026
Merged

gh-104533: Fix @ctypes.util.struct with string annotations#154040
vstinner merged 1 commit into
python:mainfrom
vstinner:struct_eval_str

Conversation

@vstinner

@vstinner vstinner commented Jul 19, 2026

Copy link
Copy Markdown
Member

Fix @ctypes.util.struct on structures declared in a module which uses from __future__ import annotations. The decorator now calls get_annotations() with eval_str=True.

Fix @ctypes.util.struct on structures declared in a module which uses
"from __future__ import annotations". The decorator now calls
get_annotations() with eval_str=True.
@vstinner

Copy link
Copy Markdown
Member Author

With this change, the following example:

from ctypes.util import struct

@struct
class Point:
    x: 'invalid_name'

now fails with NameError:

Traceback (most recent call last):
  File "/home/vstinner/python/main/ann.py", line 3, in <module>
    @struct
     ^^^^^^
  File "/home/vstinner/python/main/Lib/ctypes/util.py", line 576, in struct
    return process_the_struct(class_or_none)
  File "/home/vstinner/python/main/Lib/ctypes/util.py", line 515, in _process_struct
    annotations = annotationlib.get_annotations(decorated_class, eval_str=True)
  File "/home/vstinner/python/main/Lib/annotationlib.py", line 1078, in get_annotations
    else eval(_rewrite_star_unpack(value), globals, locals)
         ~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<string>", line 1, in <module>
NameError: name 'invalid_name' is not defined

The traceback starts on line 3 of ann.py, which is where the bug occurs: the traceback is correct and useful.

def test_string_annotations(self):
from test.test_ctypes import struct_str_ann
Point = struct_str_ann.Point
fields = [['x', c_int], ['y', c_int]]

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line should be updated if PR gh-154038 is merged first.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Crap. I missed my own comment when I merged this PR, and now the pre-commit CI fails on the main branch :-( I wrote #154091 to fix the CI.

@vstinner

Copy link
Copy Markdown
Member Author

cc @ZeroIntensity

@vstinner

Copy link
Copy Markdown
Member Author

I found this bug while trying to convert ctypes.Structure structures to @ctypes.util.struct in Lib/_pyrepl/windows_console.py which uses from __future__ import annotations.

@vstinner

Copy link
Copy Markdown
Member Author

@ctypes.util.wrap_dll_function() has the same bug: see PR gh-154042 which fixes it.

@vstinner
vstinner merged commit 8367448 into python:main Jul 19, 2026
56 checks passed
@vstinner
vstinner deleted the struct_eval_str branch July 19, 2026 10:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants