Skip to content

Crash in GenericAlias repr() with a self-mutating list #143823

@JelleZijlstra

Description

@JelleZijlstra

Crash report

What happened?

This snippet causes a segfault on current main:

import types
lst = []
class X:
    def __repr__(self):
        lst.clear()
        return "x"
        
lst += [X(), 1]
ga = types.GenericAlias(int, lst)
print(repr(ga))

This is because the ga_repr_items_list() function does not properly handle the case where the list is mutated while we're iterating over it.

Similar to #143635 but a distinct code path.

CPython versions tested on:

CPython main branch, 3.14

Operating systems tested on:

macOS

Output from running 'python -VV' on the command line:

Python 3.15.0a4+ (heads/main:1857a40807d, Jan 13 2026, 19:01:52) [Clang 15.0.0 (clang-1500.3.9.4)]

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic-typingtype-crashA hard crash of the interpreter, possibly with a core dump

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions