Skip to content

Segfault compiling a class that binds __classdict__ and contains a generic class #161849

Open

Description

@0z5a

Crash report

What happened?

class C:
    __classdict__ = 1
    class D[T]: pass

A release build of main segfaults while compiling this (in _PyCode_ConstantKey, called from _PyCompile_ConstCacheMergeOne). A debug build aborts with

Assertion failed: (offset > cellvar_offset), function compute_localsplus_info, file assemble.c, line 617.

With an annotated function in place of the generic class the compiler raises SystemError instead:

class C:
    __classdict__ = 1
    def f(x: int): pass
SystemError: compiler_lookup_arg(name='__classdict__') with reftype=5 failed in C; freevars of code __annotate__: ('__classdict__',)

global __classdict__ or for __classdict__ in (): pass in place of the assignment behave the same way. Without the __classdict__ line both classes compile and run.

3.14 behaves like main. On 3.13 the first example segfaults too; the second one compiles there, but def f[T: int](x): pass in its place gives the same kind of SystemError.

gh-149805 fixed the bare annotation __classdict__: a; the cases above still fail on main (35e2c5a).

I have a small change to analyze_name() in Python/symtable.c plus a test that makes these compile and keeps the annotations resolving against the class namespace. I can open a PR if that is wanted.

CPython versions tested on:

3.13, 3.14, CPython main branch

Operating systems tested on:

macOS

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

Python 3.16.0a2+dev (heads/main:35e2c5a, Nov 12 2026, 05:12:02) [Clang 21.0.0 (clang-2100.1.1.101)]

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions