Skip to content

[3.14] gh-149117: Set ImportError.name on errors from runpy.run_module/run_path (gh-149159)#149257

Merged
ncoghlan merged 1 commit intopython:3.14from
miss-islington:backport-ff35fe4-3.14
May 2, 2026
Merged

[3.14] gh-149117: Set ImportError.name on errors from runpy.run_module/run_path (gh-149159)#149257
ncoghlan merged 1 commit intopython:3.14from
miss-islington:backport-ff35fe4-3.14

Conversation

@miss-islington
Copy link
Copy Markdown
Contributor

@miss-islington miss-islington commented May 2, 2026

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 ff35fe4)

Co-authored-by: W. H. Wang mattwang44@gmail.com

…ule`/`run_path` (pythongh-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 ff35fe4)

Co-authored-by: W. H. Wang <mattwang44@gmail.com>
@ncoghlan ncoghlan merged commit 753064e into python:3.14 May 2, 2026
56 checks passed
@miss-islington miss-islington deleted the backport-ff35fe4-3.14 branch May 2, 2026 02:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type-bug An unexpected behavior, bug, or error

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants