mirror of
https://github.com/python/cpython.git
synced 2025-07-24 11:44:31 +00:00
[3.13] gh-125519: Improve traceback if importlib.reload()
is called with a non-module object (GH-125520) (#125768)
gh-125519: Improve traceback if `importlib.reload()` is called with a non-module object (GH-125520)
(cherry picked from commit c5c21fee7a
)
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
This commit is contained in:
parent
73bd5bd18f
commit
6b31b2dab1
3 changed files with 19 additions and 1 deletions
|
@ -103,7 +103,7 @@ def reload(module):
|
|||
try:
|
||||
name = module.__name__
|
||||
except AttributeError:
|
||||
raise TypeError("reload() argument must be a module")
|
||||
raise TypeError("reload() argument must be a module") from None
|
||||
|
||||
if sys.modules.get(name) is not module:
|
||||
raise ImportError(f"module {name} not in sys.modules", name=name)
|
||||
|
|
|
@ -8,6 +8,8 @@ import os.path
|
|||
import sys
|
||||
from test.support import import_helper
|
||||
from test.support import os_helper
|
||||
from test import support
|
||||
import traceback
|
||||
import types
|
||||
import unittest
|
||||
|
||||
|
@ -353,6 +355,20 @@ class ReloadTests:
|
|||
with self.assertRaises(ModuleNotFoundError):
|
||||
self.init.reload(module)
|
||||
|
||||
def test_reload_traceback_with_non_str(self):
|
||||
# gh-125519
|
||||
with support.captured_stdout() as stdout:
|
||||
try:
|
||||
self.init.reload("typing")
|
||||
except TypeError as exc:
|
||||
traceback.print_exception(exc, file=stdout)
|
||||
else:
|
||||
self.fail("Expected TypeError to be raised")
|
||||
printed_traceback = stdout.getvalue()
|
||||
self.assertIn("TypeError", printed_traceback)
|
||||
self.assertNotIn("AttributeError", printed_traceback)
|
||||
self.assertNotIn("module.__spec__.name", printed_traceback)
|
||||
|
||||
|
||||
(Frozen_ReloadTests,
|
||||
Source_ReloadTests
|
||||
|
|
|
@ -0,0 +1,2 @@
|
|||
Improve traceback if :func:`importlib.reload` is called with an object that
|
||||
is not a module. Patch by Alex Waygood.
|
Loading…
Add table
Add a link
Reference in a new issue