cpython icon indicating copy to clipboard operation
cpython copied to clipboard

Segfault compiling a class that binds `__classdict__` and contains a generic class

Open 0z5a opened this issue 2 weeks ago • 0 comments

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)]

Linked PRs

  • gh-161933
  • gh-162020
  • gh-162021

0z5a avatar Nov 12 '26 07:11 0z5a