Skip to content

gh-156909: Fix AST.__repr__ call getting the _fields attribute - #157490

Merged
encukou merged 1 commit into
python:mainfrom
johnslavik:gh-156909
Sep 15, 2026
Merged

encukou merged 1 commit into
python:mainfrom
johnslavik:gh-156909

Conversation

@johnslavik

@johnslavik johnslavik commented Sep 14, 2026

Copy link
Copy Markdown
Member

Proof this works:

❯ cat t.py
import ast

class FieldsMissingMeta(type):
    def __getattribute__(self, name):
        if armed and name == '_fields':
            # PyObject_GetOptionalAttr() returns 0 now, *fields is NULL.
            # The returned sentinel 0 is not handled.
            raise AttributeError
        return type.__getattribute__(self, name)

class FieldsMissing(ast.Del, metaclass=FieldsMissingMeta):
    pass

armed = False  # don't raise during construction
f = FieldsMissing()
armed = True  # raise in repr()
repr(f)  # problem is in ast_repr_max_depth()

❯ ./python t.py
Traceback (most recent call last):
  File "/Volumes/projects/python/cpython.gh-156021/t.py", line 17, in <module>
    repr(f)  # problem is in ast_repr_max_depth()
    ~~~~^^^
  File "/Volumes/projects/python/cpython.gh-156021/t.py", line 8, in __getattribute__
    raise AttributeError
AttributeError: type object 'FieldsMissing' has no attribute '_fields'

❯ every-python run main -- python t.py
Segmentation fault: 11     every-python run main -- python t.py

(every-python = https://github.com/savannahostrowski/every-python)

I didn't include a regression test as I find it quite unrealistic. LMK if you'd like the regrtest added anyway.

I also think we can skip news as this isn't a realistic crash.

@johnslavik
johnslavik requested a review from encukou September 14, 2026 12:25
@johnslavik johnslavik added needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes needs backport to 3.15 pre-release feature fixes, bugs and security fixes labels Sep 14, 2026
@johnslavik

Copy link
Copy Markdown
Member Author

I'll take care of the backports if there are any conflicts 👍🏽

@encukou
encukou merged commit 1bfcc3c into python:main Sep 15, 2026
102 of 104 checks passed
@miss-islington-app

Copy link
Copy Markdown

Thanks @johnslavik for the PR, and @encukou for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14, 3.15.
🐍🍒⛏🤖

@miss-islington-app

Copy link
Copy Markdown

Sorry, @johnslavik and @encukou, I could not cleanly backport this to 3.15 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker 1bfcc3cc871d3fab21b77cdc47cdbfcf89fd8b5b 3.15

@miss-islington-app

Copy link
Copy Markdown

Sorry, @johnslavik and @encukou, I could not cleanly backport this to 3.14 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker 1bfcc3cc871d3fab21b77cdc47cdbfcf89fd8b5b 3.14

@miss-islington-app

Copy link
Copy Markdown

Sorry, @johnslavik and @encukou, I could not cleanly backport this to 3.13 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker 1bfcc3cc871d3fab21b77cdc47cdbfcf89fd8b5b 3.13

@encukou

encukou commented Sep 15, 2026

Copy link
Copy Markdown
Member

Thank you!

I'll take care of the backports if there are any conflicts 👍🏽

There'll be conflicts for 3.15, but once that gets merged you can tell Miss Islington to backport from there to 3.13 & 3.14.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes needs backport to 3.15 pre-release feature fixes, bugs and security fixes skip news

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants