Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Lib/test/support/warnings_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ def _filterwarnings(filters, quiet=False):
registry.clear()
# Because test_warnings swap the module, we need to look up in the
# sys.modules dictionary.
wmod = sys.modules['warnings']
wmod = sys.modules.get('warnings', warnings)
with wmod.catch_warnings(record=True) as w:
# Set filter "always" to record all warnings.
wmod.simplefilter("always")
Expand Down
17 changes: 17 additions & 0 deletions Lib/test/test_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,23 @@ def test_ignored_deprecations_are_silent(self):
messages = [str(w.message) for w in warning_objs]
self.assertEqual(len(messages), 0, messages)

def test_check_warnings_when_module_is_not_in_sys_modules(self):
warnings_module = sys.modules.pop("warnings", None)
original_catch_warnings = warnings_helper.warnings.catch_warnings
try:
def catch_warnings(*, record):
return original_catch_warnings(
record=record, module=warnings_helper.warnings
)

warnings_helper.warnings.catch_warnings = catch_warnings
with warnings_helper.check_warnings(quiet=True):
pass
finally:
warnings_helper.warnings.catch_warnings = original_catch_warnings
if warnings_module is not None:
sys.modules["warnings"] = warnings_module

def test_import_module(self):
import_helper.import_module("ftplib")
self.assertRaises(unittest.SkipTest,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix ``test.support.warnings_helper.check_warnings()`` failing with a
``KeyError`` when lazy imports have not populated ``sys.modules``.
Loading