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

gh-104533: Fix `@ctypes.util.struct` with string annotations by vstinner · Pull Request #154040 · python/cpython · GitHub

Repository navigation

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

vstinner merged 1 commit into
python:mainfrom
vstinner:struct_eval_str

Conversation

vstinner commented Jul 19, 2026 •
edited by bedevere-app Bot
Loading

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.

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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.

Copy link
Copy Markdown
Member Author

cc @ZeroIntensity

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.

Copy link
Copy Markdown
Member Author

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

vstinner merged commit 8367448 into python:main Jul 19, 2026
56 checks passed
vstinner deleted the struct_eval_str branch July 19, 2026 10:05
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
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


Back | FazBrowse Home | New Git URL