Skip to content

Commit 9b4fe9b

Browse files
gh-122191: Fix test_warnings failure if run with -Werror (GH-122222)
__spec__.loader is now required in the module globals (see gh-86298).
1 parent 5592399 commit 9b4fe9b

File tree

1 file changed

+23
-13
lines changed

1 file changed

+23
-13
lines changed

Lib/test/test_warnings/__init__.py

+23-13
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from contextlib import contextmanager
22
import linecache
33
import os
4+
import importlib
45
import inspect
56
from io import StringIO
67
import re
@@ -887,37 +888,46 @@ def test_issue31285(self):
887888
# warn_explicit() should neither raise a SystemError nor cause an
888889
# assertion failure, in case the return value of get_source() has a
889890
# bad splitlines() method.
890-
def get_bad_loader(splitlines_ret_val):
891+
get_source_called = []
892+
def get_module_globals(*, splitlines_ret_val):
893+
class BadSource(str):
894+
def splitlines(self):
895+
return splitlines_ret_val
896+
891897
class BadLoader:
892898
def get_source(self, fullname):
893-
class BadSource(str):
894-
def splitlines(self):
895-
return splitlines_ret_val
899+
get_source_called.append(splitlines_ret_val)
896900
return BadSource('spam')
897-
return BadLoader()
901+
902+
loader = BadLoader()
903+
spec = importlib.machinery.ModuleSpec('foobar', loader)
904+
return {'__loader__': loader,
905+
'__spec__': spec,
906+
'__name__': 'foobar'}
907+
898908

899909
wmod = self.module
900910
with original_warnings.catch_warnings(module=wmod):
901911
wmod.filterwarnings('default', category=UserWarning)
902912

913+
linecache.clearcache()
903914
with support.captured_stderr() as stderr:
904915
wmod.warn_explicit(
905916
'foo', UserWarning, 'bar', 1,
906-
module_globals={'__loader__': get_bad_loader(42),
907-
'__name__': 'foobar'})
917+
module_globals=get_module_globals(splitlines_ret_val=42))
908918
self.assertIn('UserWarning: foo', stderr.getvalue())
919+
self.assertEqual(get_source_called, [42])
909920

910-
show = wmod._showwarnmsg
911-
try:
921+
linecache.clearcache()
922+
with support.swap_attr(wmod, '_showwarnmsg', None):
912923
del wmod._showwarnmsg
913924
with support.captured_stderr() as stderr:
914925
wmod.warn_explicit(
915926
'eggs', UserWarning, 'bar', 1,
916-
module_globals={'__loader__': get_bad_loader([42]),
917-
'__name__': 'foobar'})
927+
module_globals=get_module_globals(splitlines_ret_val=[42]))
918928
self.assertIn('UserWarning: eggs', stderr.getvalue())
919-
finally:
920-
wmod._showwarnmsg = show
929+
self.assertEqual(get_source_called, [42, [42]])
930+
linecache.clearcache()
921931

922932
@support.cpython_only
923933
def test_issue31411(self):

0 commit comments

Comments
 (0)