From c2a754144b15d0fb0d5c185ea1385620cb755905 Mon Sep 17 00:00:00 2001 From: hwdev Date: Sat, 14 Nov 2026 10:40:15 +0000 Subject: [PATCH 1/2] [mypyc] Fix leak of an overridden attribute default in non-separate builds Without separate compilation, find_attr_initializers collected the defaults of every class in the MRO, so a subclass that overrides a base default got two initializer stores in __mypyc_defaults_setup. An initializer store doesn't release the previous value, so the base default leaked once per instance. Keep only the last default per name. --- mypyc/irbuild/classdef.py | 12 +++++++----- mypyc/test-data/irbuild-classes.test | 21 +++++++++++++++++++++ 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/mypyc/irbuild/classdef.py b/mypyc/irbuild/classdef.py index cd3d07a5d3aad..0acfcc877374a 100644 --- a/mypyc/irbuild/classdef.py +++ b/mypyc/irbuild/classdef.py @@ -862,7 +862,7 @@ def find_attr_initializers( return set(), [] attrs_with_defaults: set[str] = set() - default_assignments: list[tuple[AssignmentStmt, str]] = [] + default_assignments: dict[str, tuple[AssignmentStmt, str]] = {} # TODO: Support nested statements if builder.options.separate: @@ -882,9 +882,11 @@ def find_attr_initializers( if name is None: continue attrs_with_defaults.add(name) - default_assignments.append((stmt, info.module_name)) + # Keep only the last default per name: initializer stores don't free an earlier value. + default_assignments.pop(name, None) + default_assignments[name] = (stmt, info.module_name) - return attrs_with_defaults, default_assignments + return attrs_with_defaults, list(default_assignments.values()) def generate_attr_defaults_init( @@ -901,8 +903,8 @@ def generate_attr_defaults_init( in filename order, so this class may be IR-built before its base, and a method_decls lookup that depended on the base having been IR-built first would miss. Without separate compilation, find_attr_initializers has - already collected the full MRO's defaults into default_assignments, so - we inline them all as before. + already collected the full MRO's defaults into default_assignments, one + per attribute name, so we inline them all as before. """ cls = builder.mapper.type_to_ir[cdef.info] if cls.builtin_base: diff --git a/mypyc/test-data/irbuild-classes.test b/mypyc/test-data/irbuild-classes.test index b4424cfa9e88d..a62d54ef06536 100644 --- a/mypyc/test-data/irbuild-classes.test +++ b/mypyc/test-data/irbuild-classes.test @@ -1529,6 +1529,27 @@ L0: __mypyc_self__.b = 1 return 1 +[case testDefaultVarsOverriddenInSubclass] +class A: + x = 'a' +class B(A): + x = 'b' +[out] +def A.__mypyc_defaults_setup(__mypyc_self__): + __mypyc_self__ :: __main__.A + r0 :: str +L0: + r0 = 'a' + __mypyc_self__.x = r0 + return 1 +def B.__mypyc_defaults_setup(__mypyc_self__): + __mypyc_self__ :: __main__.B + r0 :: str +L0: + r0 = 'b' + __mypyc_self__.x = r0 + return 1 + [case testSubclassDictSpecalized] from typing import Dict class WelpDict(Dict[str, int]): From f3e360d0410afa4d770e764a60def097b7a58190 Mon Sep 17 00:00:00 2001 From: hwdev Date: Tue, 24 Nov 2026 08:38:55 +0000 Subject: [PATCH 2/2] [mypyc] Release an inherited default before an override in separate builds Under separate compilation, a subclass's __mypyc_defaults_setup calls the parent's setup first, so the parent's default for an overridden attribute is already stored. The initializer store that followed didn't release it. When the setup chains to a parent, store defaults for attributes that an ancestor declares with a regular store, which releases the old value. Separate builds don't treat any attribute as always defined, so the store checks for NULL first. --- mypyc/irbuild/classdef.py | 8 ++++++- mypyc/test-data/run-multimodule.test | 33 ++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/mypyc/irbuild/classdef.py b/mypyc/irbuild/classdef.py index 0acfcc877374a..44defafb2e8aa 100644 --- a/mypyc/irbuild/classdef.py +++ b/mypyc/irbuild/classdef.py @@ -953,7 +953,13 @@ def generate_attr_defaults_init( finally: builder.globals_lookup_module = None init = SetAttr(self_var, lvalue.name, val, stmt.rvalue.line) - init.mark_as_initializer() + # The parent's setup may have stored a default for an inherited attribute + # already, so use a regular store, which releases it. Separate builds don't + # treat any attribute as always defined, so the store checks for NULL first. + if parent_with_defaults is None or not any( + lvalue.name in base.attributes for base in cls.mro[1:] + ): + init.mark_as_initializer() builder.add(init) builder.add(Return(builder.true())) diff --git a/mypyc/test-data/run-multimodule.test b/mypyc/test-data/run-multimodule.test index 3c8b4ceb99957..3c4e94e9e7885 100644 --- a/mypyc/test-data/run-multimodule.test +++ b/mypyc/test-data/run-multimodule.test @@ -2001,6 +2001,39 @@ class Parent: from native import test test() +[case testCrossModuleOverriddenAttrDefaultIsReleased] +import sys +from other import BASE_X, Parent + +class Child(Parent): + x: object = None + y: object = None + +def test() -> None: + getrefcount = getattr(sys, "getrefcount") + c = Child() + assert c.x is None + assert c.y is None + assert Parent().x is BASE_X + rc = getrefcount(BASE_X) + for _ in range(10): + Child() + assert getrefcount(BASE_X) == rc + +[file other.py] +from typing import Final, List + +BASE_X: Final[List[int]] = [1] + +class Parent: + x: object = BASE_X + y: object + z: int = 1 + +[file driver.py] +from native import test +test() + [case testIncrementalCrossModuleInheritedAttrDefaults] # Regression: under separate=True, when only the subclass module is # recompiled (parent loaded from mypy's incremental cache, so its