Skip to content

gh-87465: Bypass sys.modules when finding module specs in pyclbr - #161864

Open
yhay81 wants to merge 1 commit into
python:mainfrom
yhay81:gh-87465-pyclbr-find-spec
Open

yhay81 wants to merge 1 commit into
python:mainfrom
yhay81:gh-87465-pyclbr-find-spec

Conversation

@yhay81

@yhay81 yhay81 commented Nov 12, 2026

Copy link
Copy Markdown

pyclbr._readmodule() previously called importlib.util._find_spec_from_path(), which checks sys.modules before searching search_path. Because pyclbr maintains its own _modules cache and inspects source files from the search path without importing them, consulting sys.modules caused two problems:

  1. Browsing any module with import __main__ (such as pdb or doctest) when sys.modules['__main__'].__spec__ is None (or another synthetic module in sys.modules without __spec__) raised ValueError instead of treating an unresolvable import as ModuleNotFoundError.
  2. When __main__.py existed on the given search path, sys.modules['__main__'] shadowed it with the running process's own __main__ module.

Switch pyclbr._readmodule() to call importlib._bootstrap._find_spec(fullmodule, search_path) directly, remove the temporary_main_spec() workaround from Lib/test/test_pyclbr.py, and add regression tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant