Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Circular import error forthreading when usingimportlib.util.LazyLoader with a custom finder #127116

Open
Labels
@Sachaa-Thanasius

Description

@Sachaa-Thanasius

Bug report

Bug description:

When using a custom finder (onsys.meta_path) that wraps a module spec's loader withimportlib.util.LazyLoader, the threading import withinimportlib.util.LazyLoader.exec_module is attemptedwith the custom finder, causing a circular import error.

Not sure if this is more of a feature request than a bug report, but the behavior was certainly surprising at first glance. Hopefully there's a way to better support this use case from within the stdlib without too much burden. Otherwise, users of LazyLoader in this manner would be forced to either a) maintain an exclusion list of modules that their finder ignores, potentially including all of threading's dependencies or b) always import threading before using such finders, which shouldn't be necessary, especially if threading isn't directly used by the user code.

Reproducer

importimportlib.utilimportsysclassLazyFinder:"""A module spec finder that wraps a spec's loader, if it exists, with LazyLoader."""@classmethoddeffind_spec(cls,fullname:str,path=None,target=None,/):forfinderinsys.meta_path:iffinderisnotcls:spec=finder.find_spec(fullname,path,target)ifspecisnotNone:breakelse:raiseModuleNotFoundError(...)ifspec.loaderisnotNone:spec.loader=importlib.util.LazyLoader(spec.loader)returnspecclassLazyFinderContext:"""Temporarily "lazify" some types of import statements in the runtime context."""def__enter__(self):ifLazyFindernotinsys.meta_path:sys.meta_path.insert(0,LazyFinder)def__exit__(self,*exc_info):try:sys.meta_path.remove(LazyFinder)exceptValueError:passwithLazyFinderContext():importinspect

Expected Output

No error.

Actual Output

> python scratch.pyTraceback (most recent call last):  File "/home/thanos/projects/personal/pycc/scratch.py", line 40, in <module>    import inspect  File "<frozen importlib._bootstrap>", line 1360, in _find_and_load  File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked  File "<frozen importlib._bootstrap>", line 935, in _load_unlocked  File "<frozen importlib.util>", line 257, in exec_module  File "<frozen importlib._bootstrap>", line 1360, in _find_and_load  File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked  File "<frozen importlib._bootstrap>", line 935, in _load_unlocked  File "<frozen importlib.util>", line 267, in exec_moduleAttributeError: partially initialized module 'threading' has no attribute 'RLock' (most likely due to a circular import)

Commentary

This is technically caused by this code:

defexec_module(self,module):
"""Make the module load lazily."""
# Threading is only needed for lazy loading, and importlib.util can
# be pulled in at interpreter startup, so defer until needed.
importthreading

However, it is based on the fair assumptions thatLazyLoader a) isn't critical to CPython startup, and b) won't be used in a circular fashion with a custom finder. However, there are use cases for such a finder (e.g.scientific-python/lazy-loader#121 (comment), one place this issue was discovered). While finders utilizing the lazy loader could work around this with an exclusion list of modules (e.g. mercurial's lazy loader does), I think users would find usingLazyLoader easier with the finder-related import hooks if that wasn't necessary.

Based on the commit history, a top-level import forthreading breaksgevent, so I'd rather not repeat that. I'm not very familiar withgevent, but if using_thread.RLock is fine inimportlib._bootstrap for the module locks, then maybe that could be used inimportlib.util as well?

EDIT: Updated code snippet to be consistent with output; accidentally used a different import while testing. Still, same result.

CPython versions tested on:

3.12, 3.13

Operating systems tested on:

Linux, Windows

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp