Skip to content

types.LazyImportType is not initialized #145452

@johnslavik

Description

@johnslavik

Bug report

Bug description:

At runtime, types.LazyImportType is an uninitialized static type (unless you e.g. access types.LazyImportType.resolve first).

This can lead to funky behaviors:

lazy import wave, traceback

try:
    help(globals()['wave'].resolve)  # However, a call ".resolve()" would work
except AttributeError as e:
    print(e)  # 'lazy_import' object has no attribute 'resolve'

try:
    help(globals()['wave'].resolve)
except AttributeError:
    # traceback.format_exc() initializes types.LazyImportType as a side effect.
    # This prints "AttributeError: 'lazy_import' object has no attribute 'resolve'".
    print(traceback.format_exc().splitlines()[-1])
 
print(globals()['wave'].resolve)  # Succeeds.

The solution I found is to add &PyLazyImport_Type to static_types[], the array of types initialized in _PyTypes_InitTypes during interpreter startup.

CPython versions tested on:

CPython main branch

Operating systems tested on:

macOS

Linked PRs

Metadata

Metadata

Assignees

Labels

3.15new features, bugs and security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or error

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions