]> git.ipfire.org Git - thirdparty/Python/cpython.git/commit
[3.14] gh-149117: Set `ImportError.name` on errors from `runpy.run_module`/`run_path...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Sat, 2 May 2026 02:54:02 +0000 (04:54 +0200)
committerGitHub <noreply@github.com>
Sat, 2 May 2026 02:54:02 +0000 (02:54 +0000)
commit753064e5b57842958e162ed1ee02f72f75fd0ee9
tree002cd62ad1e306f43cf4e7872afe580d53949e1d
parent312a596bcf7757e88e16a97f65ce11187a8ba7c6
[3.14] gh-149117: Set `ImportError.name` on errors from `runpy.run_module`/`run_path` (gh-149159) (#149257)

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.
(cherry picked from commit ff35fe4633cc6d3a30f6af8281dfa641783c1d07)

Co-authored-by: W. H. Wang <mattwang44@gmail.com>
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]