Skip to content

gh-75221: Search __main__ only in the executed directory or zipfile - #155256

Open
serhiy-storchaka wants to merge 2 commits into
python:mainfrom
serhiy-storchaka:gh-75221-runpy-main-search
Open

serhiy-storchaka wants to merge 2 commits into
python:mainfrom
serhiy-storchaka:gh-75221-runpy-main-search

Conversation

@serhiy-storchaka

Copy link
Copy Markdown
Member

runpy.run_path() and the execution of a directory or a zipfile searched the __main__ module in the whole sys.path, so an existing __main__ module located elsewhere could be executed instead of failing.

As a result, running Lib/test/test_runpy.py directly ran the whole test suite.

The __main__ module is now searched only in sys.path[0] -- the executed directory or zipfile.

…file

runpy.run_path() and the execution of a directory or a zipfile searched the
__main__ module in the whole sys.path, so an existing __main__ module
located elsewhere could be executed instead of failing.  As a result,
running Lib/test/test_runpy.py directly ran the whole test suite.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@read-the-docs-community

read-the-docs-community Bot commented Aug 5, 2026 •

Copy link
Copy Markdown

@github-actions

github-actions Bot commented Nov 6, 2026

Copy link
Copy Markdown

This PR is stale because it has been open for 90 days with no activity.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Nov 6, 2026

@ncoghlan ncoghlan left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM (my only concern was the new pkgutil dependency, and I convinced myself that the reduction in potentially confusing behaviour is worth the implicit imports of additional modules that are frequently going to be imported by the code being executed anyway)

Comment thread Lib/runpy.py
kwargs = {"name": main_name} if issubclass(error, ImportError) else {}
saved_main = sys.modules[main_name]
del sys.modules[main_name]
from pkgutil import get_importer

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The pkgutil dependency gave me a momentary pause for concern, but that's still a fairly low level module. Compared to importing runpy itself, 3.15 shows me the following extra imports:

import time:        61 |         61 |     itertools
import time:        82 |         82 |     keyword
import time:        40 |         40 |       _operator
import time:       194 |        233 |     operator
import time:       119 |        119 |     reprlib
import time:        47 |         47 |     _collections
import time:       658 |       1198 |   collections
import time:        32 |         32 |     _functools
import time:       363 |        394 |   functools
import time:       128 |        128 |     _weakrefset
import time:       234 |        361 |   weakref
import time:       269 |       2221 | pkgutil

@ncoghlan ncoghlan removed the stale Stale PR or inactive for long period of time. label Nov 16, 2026
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.

2 participants