Skip to content

LOAD_ATTR_MODULE bypasses built-in data descriptors on exact module objects #162565

Backlog

Description

@lagergren-oai

Bug report

On current main at 79494e2, warmed attribute reads on an exact types.ModuleType object can return dictionary entries named __dict__ or __class__ instead of the built-in data descriptor results. Cold reads and getattr() return the descriptor results on the same object.

import dis
import types

module = types.ModuleType("example")
namespace = vars(module)
namespace["__dict__"] = "shadow dict"
namespace["__class__"] = "shadow class"


def read_dict(owner):
    return owner.__dict__


def read_class(owner):
    return owner.__class__


print("Exact module:", type(module) is types.ModuleType)
for name, read, expected in (
    ("__dict__", read_dict, namespace),
    ("__class__", read_class, types.ModuleType),
):
    print(name)
    print("  cold uses descriptor:", read(module) is expected)
    for _ in range(100):
        read(module)
    print("  warm uses descriptor:", read(module) is expected)
    print("  warm returns shadow:", read(module) is namespace[name])
    print("  getattr uses descriptor:", getattr(module, name) is expected)
    print("  attribute opcodes:", [
        op.opname for op in dis.get_instructions(read, adaptive=True)
        if "LOAD_ATTR" in op.opname
    ])

Observed output:

Exact module: True
__dict__
  cold uses descriptor: True
  warm uses descriptor: False
  warm returns shadow: True
  getattr uses descriptor: True
  attribute opcodes: ['LOAD_ATTR_MODULE']
__class__
  cold uses descriptor: True
  warm uses descriptor: False
  warm returns shadow: True
  getattr uses descriptor: True
  attribute opcodes: ['LOAD_ATTR_MODULE']

Expected: both cold and warmed reads preserve descriptor precedence. __dict__ should return the module namespace and __class__ should return types.ModuleType, regardless of these dictionary entries. Thus warm uses descriptor should be True, and warm returns shadow should be False for both names.

This is distinct from #156399: no module subclass or user-defined descriptor is involved. Its fix, #156474, is already an ancestor of the tested commit and restricts module specialization to exact modules. The existing test_load_attr_module_subclass_with_data_descriptor passes on this build while the example above still produces the wrong warmed results.

In the tested source, module specialization selects the dictionary entry without checking for a data descriptor on the module type. The specialized instruction then reads that entry directly.

CPython versions tested on

CPython main branch, 3.16.0a2+dev, clean checkout at 79494e2b0f7, fetched and built on 2026-11-24. Released versions were not checked for this report.

Operating systems tested on

macOS 27.2.1, ARM64, Apple Clang 21.0.0. Fresh debug build configured with --with-pydebug --without-ensurepip; GIL enabled, JIT unavailable. The example was run with ./python.exe -I repro.py.

The related subclass control was run with:

./python.exe -I -m test test_opcache -m test_load_attr_module_subclass_with_data_descriptor -v

Result: one test passed; this was a focused check, not a full test-suite run.

Prepared with Codex assistance; the included reproducer and subclass control were executed on the stated upstream checkout.

Linked PRs

Activity

  1. added
    type-bug
    interpreter-core
    on Nov 24, 2026
  2. lagergren commented on Nov 24, 2026

    @lagergren

    A fix is proposed in #162568.

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

    interpreter-coretype-bug

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions