]> git.ipfire.org Git - thirdparty/Python/cpython.git/commit
[3.13] 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:51:06 +0000 (04:51 +0200)
committerGitHub <noreply@github.com>
Sat, 2 May 2026 02:51:06 +0000 (02:51 +0000)
commitbb2c05b398f23f21ffc9a312032714e3a6dec315
treea699d33712593ce3cd9344a5a383edce97651a62
parentbed659fa1d1528ad239626d39a2f944cb202be07
[3.13] gh-149117: Set `ImportError.name` on errors from `runpy.run_module`/`run_path` (gh-149159) (#149258)

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]