mirror of https://github.com/python/cpython
gh-122191: Fix test_warnings failure if run with -Werror (GH-122222)
__spec__.loader is now required in the module globals (see gh-86298).
This commit is contained in:
parent
5592399313
commit
9b4fe9b718
|
@ -1,6 +1,7 @@
|
||||||
from contextlib import contextmanager
|
from contextlib import contextmanager
|
||||||
import linecache
|
import linecache
|
||||||
import os
|
import os
|
||||||
|
import importlib
|
||||||
import inspect
|
import inspect
|
||||||
from io import StringIO
|
from io import StringIO
|
||||||
import re
|
import re
|
||||||
|
@ -887,37 +888,46 @@ class _WarningsTests(BaseTest, unittest.TestCase):
|
||||||
# warn_explicit() should neither raise a SystemError nor cause an
|
# warn_explicit() should neither raise a SystemError nor cause an
|
||||||
# assertion failure, in case the return value of get_source() has a
|
# assertion failure, in case the return value of get_source() has a
|
||||||
# bad splitlines() method.
|
# bad splitlines() method.
|
||||||
def get_bad_loader(splitlines_ret_val):
|
get_source_called = []
|
||||||
|
def get_module_globals(*, splitlines_ret_val):
|
||||||
|
class BadSource(str):
|
||||||
|
def splitlines(self):
|
||||||
|
return splitlines_ret_val
|
||||||
|
|
||||||
class BadLoader:
|
class BadLoader:
|
||||||
def get_source(self, fullname):
|
def get_source(self, fullname):
|
||||||
class BadSource(str):
|
get_source_called.append(splitlines_ret_val)
|
||||||
def splitlines(self):
|
|
||||||
return splitlines_ret_val
|
|
||||||
return BadSource('spam')
|
return BadSource('spam')
|
||||||
return BadLoader()
|
|
||||||
|
loader = BadLoader()
|
||||||
|
spec = importlib.machinery.ModuleSpec('foobar', loader)
|
||||||
|
return {'__loader__': loader,
|
||||||
|
'__spec__': spec,
|
||||||
|
'__name__': 'foobar'}
|
||||||
|
|
||||||
|
|
||||||
wmod = self.module
|
wmod = self.module
|
||||||
with original_warnings.catch_warnings(module=wmod):
|
with original_warnings.catch_warnings(module=wmod):
|
||||||
wmod.filterwarnings('default', category=UserWarning)
|
wmod.filterwarnings('default', category=UserWarning)
|
||||||
|
|
||||||
|
linecache.clearcache()
|
||||||
with support.captured_stderr() as stderr:
|
with support.captured_stderr() as stderr:
|
||||||
wmod.warn_explicit(
|
wmod.warn_explicit(
|
||||||
'foo', UserWarning, 'bar', 1,
|
'foo', UserWarning, 'bar', 1,
|
||||||
module_globals={'__loader__': get_bad_loader(42),
|
module_globals=get_module_globals(splitlines_ret_val=42))
|
||||||
'__name__': 'foobar'})
|
|
||||||
self.assertIn('UserWarning: foo', stderr.getvalue())
|
self.assertIn('UserWarning: foo', stderr.getvalue())
|
||||||
|
self.assertEqual(get_source_called, [42])
|
||||||
|
|
||||||
show = wmod._showwarnmsg
|
linecache.clearcache()
|
||||||
try:
|
with support.swap_attr(wmod, '_showwarnmsg', None):
|
||||||
del wmod._showwarnmsg
|
del wmod._showwarnmsg
|
||||||
with support.captured_stderr() as stderr:
|
with support.captured_stderr() as stderr:
|
||||||
wmod.warn_explicit(
|
wmod.warn_explicit(
|
||||||
'eggs', UserWarning, 'bar', 1,
|
'eggs', UserWarning, 'bar', 1,
|
||||||
module_globals={'__loader__': get_bad_loader([42]),
|
module_globals=get_module_globals(splitlines_ret_val=[42]))
|
||||||
'__name__': 'foobar'})
|
|
||||||
self.assertIn('UserWarning: eggs', stderr.getvalue())
|
self.assertIn('UserWarning: eggs', stderr.getvalue())
|
||||||
finally:
|
self.assertEqual(get_source_called, [42, [42]])
|
||||||
wmod._showwarnmsg = show
|
linecache.clearcache()
|
||||||
|
|
||||||
@support.cpython_only
|
@support.cpython_only
|
||||||
def test_issue31411(self):
|
def test_issue31411(self):
|
||||||
|
|
Loading…
Reference in New Issue