Skip to content

Commit

Permalink
pythongh-122191: Fix test_warnings failure if run with -Werror
Browse files Browse the repository at this point in the history
__spec__.loader is now required in the module globals (see pythongh-86298).
  • Loading branch information
serhiy-storchaka committed Jul 24, 2024
1 parent f067efa commit cdbc98d
Showing 1 changed file with 18 additions and 17 deletions.
35 changes: 18 additions & 17 deletions Lib/test/test_warnings/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from contextlib import contextmanager
import linecache
import os
import importlib
import inspect
from io import StringIO
import re
Expand Down Expand Up @@ -887,37 +888,37 @@ def test_issue31285(self):
# warn_explicit() should neither raise a SystemError nor cause an
# assertion failure, in case the return value of get_source() has a
# bad splitlines() method.
def get_bad_loader(splitlines_ret_val):
class BadLoader:
def get_source(self, fullname):
class BadSource(str):
def splitlines(self):
return splitlines_ret_val
return BadSource('spam')
return BadLoader()

class BadLoader:
def get_source(self, fullname):
class BadSource(str):
def splitlines(self):
return splitlines_ret_val
return BadSource('spam')

loader = BadLoader()
spec = importlib.machinery.ModuleSpec('foobar', loader)
module_globals = {'__loader__': loader,
'__spec__': spec,
'__name__': 'foobar'}
wmod = self.module
with original_warnings.catch_warnings(module=wmod):
wmod.filterwarnings('default', category=UserWarning)

splitlines_ret_val = 42
with support.captured_stderr() as stderr:
wmod.warn_explicit(
'foo', UserWarning, 'bar', 1,
module_globals={'__loader__': get_bad_loader(42),
'__name__': 'foobar'})
module_globals=module_globals)
self.assertIn('UserWarning: foo', stderr.getvalue())

show = wmod._showwarnmsg
try:
with support.swap_attr(wmod, '_showwarnmsg', None):
del wmod._showwarnmsg
splitlines_ret_val = [42]
with support.captured_stderr() as stderr:
wmod.warn_explicit(
'eggs', UserWarning, 'bar', 1,
module_globals={'__loader__': get_bad_loader([42]),
'__name__': 'foobar'})
module_globals=module_globals)
self.assertIn('UserWarning: eggs', stderr.getvalue())
finally:
wmod._showwarnmsg = show

@support.cpython_only
def test_issue31411(self):
Expand Down

0 comments on commit cdbc98d

Please sign in to comment.