From 3b0c05dd61d4300f215f41146ed7a3422db18c4e Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Mon, 21 Oct 2024 07:53:21 +0100 Subject: [PATCH 1/2] gh-125519: Improve traceback if `importlib.reload()` is called with a non-module object (GH-125520) (cherry picked from commit c5c21fee7ae1ea689a351caa454c98e716a6e537) Co-authored-by: Alex Waygood --- Lib/importlib/__init__.py | 2 +- Lib/test/test_importlib/test_api.py | 15 +++++++++++++++ ...2024-10-15-14-01-03.gh-issue-125519.TqGh6a.rst | 2 ++ 3 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2024-10-15-14-01-03.gh-issue-125519.TqGh6a.rst diff --git a/Lib/importlib/__init__.py b/Lib/importlib/__init__.py index f38fe5c1ab461a..a7d57561ead046 100644 --- a/Lib/importlib/__init__.py +++ b/Lib/importlib/__init__.py @@ -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) diff --git a/Lib/test/test_importlib/test_api.py b/Lib/test/test_importlib/test_api.py index 2a35f3dcb7210c..5b48a5926383e5 100644 --- a/Lib/test/test_importlib/test_api.py +++ b/Lib/test/test_importlib/test_api.py @@ -8,6 +8,7 @@ import sys from test.support import import_helper from test.support import os_helper +import traceback import types import unittest @@ -353,6 +354,20 @@ def test_module_missing_spec(self): 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 diff --git a/Misc/NEWS.d/next/Library/2024-10-15-14-01-03.gh-issue-125519.TqGh6a.rst b/Misc/NEWS.d/next/Library/2024-10-15-14-01-03.gh-issue-125519.TqGh6a.rst new file mode 100644 index 00000000000000..e6062625104590 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-10-15-14-01-03.gh-issue-125519.TqGh6a.rst @@ -0,0 +1,2 @@ +Improve traceback if :func:`importlib.reload` is called with an object that +is not a module. Patch by Alex Waygood. From c24abc73e0043af474fb68e9f1f8c3669d2ca5a0 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Mon, 21 Oct 2024 09:04:27 +0100 Subject: [PATCH 2/2] Update test_api.py --- Lib/test/test_importlib/test_api.py | 1 + 1 file changed, 1 insertion(+) diff --git a/Lib/test/test_importlib/test_api.py b/Lib/test/test_importlib/test_api.py index 5b48a5926383e5..3bf8d7ac9c4022 100644 --- a/Lib/test/test_importlib/test_api.py +++ b/Lib/test/test_importlib/test_api.py @@ -8,6 +8,7 @@ import sys from test.support import import_helper from test.support import os_helper +from test import support import traceback import types import unittest