8000 gh-86298: Ensure that __loader__ and __spec__.loader agree in warnings.warn_explicit() by warsaw · Pull Request #97803 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-86298: Ensure that __loader__ and __spec__.loader agree in warnings.warn_explicit() #97803

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Oct 7, 2022
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add a test for the __loader__ == __spec__.loader cases
  • Loading branch information
warsaw committed Oct 4, 2022
commit 317e253b6de0bf5f1c2cb95960559e692eea62fb
24 changes: 24 additions & 0 deletions Lib/test/test_warnings/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import re
import sys
import textwrap
from types import ModuleType, SimpleNamespace
import unittest
from test import support
from test.support import import_helper
Expand Down Expand Up @@ -885,6 +886,29 @@ def test_issue31566(self):
support.swap_item(globals(), '__file__', None):
self.assertRaises(UserWarning, self.module.warn, 'bar')

@support.cpython_only
def test_gh86298(self):
# warn_explicit() itself issues an ImportWarning when a module and
# module_globals are both given, and the named module has a __loader__
# != its __spec__.loader.
bar = ModuleType('bar')
# First, make the __loader__ != the __spec__.loader. This should
# trigger the ImportWarning inside warn_explicit().
bar.__loader__ = object()
bar.__spec__ = SimpleNamespace(loader=object())
with original_warnings.catch_warnings():
self.assertWarns(
ImportWarning, self.module.warn_explicit,
'warning!', RuntimeWarning,
'bar.py', 2, module='bar knee', module_globals=bar.__dict__)
# Now make the __loader__ == __spec__.loader. This will not raise the exception.
bar.__spec__.loader = bar.__loader__
with original_warnings.catch_warnings():
self.module.filterwarnings('error', category=ImportWarning)
self.module.warn_explicit(
'warning!', RuntimeWarning,
'bar.py', 2, module='bar knee', module_globals=bar.__dict__)


class WarningsDisplayTests(BaseTest):

Expand Down
0