]> git.ipfire.org Git - thirdparty/Python/cpython.git/commit
gh-149117: Set `ImportError.name` on errors from `runpy.run_module`/`run_path` (gh...
authorW. H. Wang <mattwang44@gmail.com>
Sat, 2 May 2026 02:27:23 +0000 (10:27 +0800)
committerGitHub <noreply@github.com>
Sat, 2 May 2026 02:27:23 +0000 (12:27 +1000)
commitff35fe4633cc6d3a30f6af8281dfa641783c1d07
treefcfbd77bdced457be7916b033a2cb00aa513668e
parentbe9c7cb4b50f8007b61018538c10d0a92c44cf17
gh-149117: Set `ImportError.name` on errors from `runpy.run_module`/`run_path` (gh-149159)

Set ImportError.name on errors from runpy.run_module/run_path

`runpy.run_module()` and `runpy.run_path()` now set the `name` attribute
of the `ImportError` they raise to the requested module name, matching
the behaviour of a regular import statement (previously `name` was
always `None`, which broke introspection).

The `name=` kwarg is gated on `issubclass(error, ImportError)` because
`_get_module_details()` is also used by `_run_module_as_main()` with
a private `_Error` sentinel class. `_Error` does not subclass
ImportError, and `BaseException.__init__` rejects unknown kwargs at
the C level, so passing `name=` unconditionally would break the
`python -m foo` codepath.
Lib/runpy.py
Lib/test/test_runpy.py
Misc/NEWS.d/next/Library/2026-04-29-16-11-27.gh-issue-149117.yEeTYd.rst [new file with mode: 0644]