Skip to content

Commit 94db4cc

Browse files
[3.13] gh-122191: Fix test_warnings failure if run with -Werror (GH-122222) (GH-122256)
__spec__.loader is now required in the module globals (see gh-86298). (cherry picked from commit 9b4fe9b) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
1 parent 4e77165 commit 94db4cc

File tree

1 file changed

+23
-13
lines changed

1 file changed

+23
-13
lines changed

Lib/test/test_warnings/__init__.py

Lines changed: 23 additions & 13 deletions
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
@@ -885,37 +886,46 @@ def test_issue31285(self):
885886
# warn_explicit() should neither raise a SystemError nor cause an
886887
# assertion failure, in case the return value of get_source() has a
887888
# bad splitlines() method.
888-
def get_bad_loader(splitlines_ret_val):
889+
get_source_called = []
890+
def get_module_globals(*, splitlines_ret_val):
891+
class BadSource(str):
892+
def splitlines(self):
893+
return splitlines_ret_val
894+
889895
class BadLoader:
890896
def get_source(self, fullname):
891-
class BadSource(str):
892-
def splitlines(self):
893-
return splitlines_ret_val
897+
get_source_called.append(splitlines_ret_val)
894898
return BadSource('spam')
895-
return BadLoader()
899+
900+
loader = BadLoader()
901+
spec = importlib.machinery.ModuleSpec('foobar', loader)
902+
return {'__loader__': loader,
903+
'__spec__': spec,
904+
'__name__': 'foobar'}
905+
896906

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

911+
linecache.clearcache()
901912
with support.captured_stderr() as stderr:
902913
wmod.warn_explicit(
903914
'foo', UserWarning, 'bar', 1,
904-
module_globals={'__loader__': get_bad_loader(42),
905-
'__name__': 'foobar'})
915+
module_globals=get_module_globals(splitlines_ret_val=42))
906916
self.assertIn('UserWarning: foo', stderr.getvalue())
917+
self.assertEqual(get_source_called, [42])
907918

908-
show = wmod._showwarnmsg
909-
try:
919+
linecache.clearcache()
920+
with support.swap_attr(wmod, '_showwarnmsg', None):
910921
del wmod._showwarnmsg
911922
with support.captured_stderr() as stderr:
912923
wmod.warn_explicit(
913924
'eggs', UserWarning, 'bar', 1,
914-
module_globals={'__loader__': get_bad_loader([42]),
915-
'__name__': 'foobar'})
925+
module_globals=get_module_globals(splitlines_ret_val=[42]))
916926
self.assertIn('UserWarning: eggs', stderr.getvalue())
917-
finally:
918-
wmod._showwarnmsg = show
927+
self.assertEqual(get_source_called, [42, [42]])
928+
linecache.clearcache()
919929

920930
@support.cpython_only
921931
def test_issue31411(self):

0 commit comments

Comments
 (0)