|
1 | 1 | from contextlib import contextmanager
|
2 | 2 | import linecache
|
3 | 3 | import os
|
| 4 | +import importlib |
4 | 5 | import inspect
|
5 | 6 | from io import StringIO
|
6 | 7 | import re
|
@@ -887,37 +888,46 @@ def test_issue31285(self):
|
887 | 888 | # warn_explicit() should neither raise a SystemError nor cause an
|
888 | 889 | # assertion failure, in case the return value of get_source() has a
|
889 | 890 | # 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 | + |
891 | 897 | class BadLoader:
|
892 | 898 | 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) |
896 | 900 | 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 | + |
898 | 908 |
|
899 | 909 | wmod = self.module
|
900 | 910 | with original_warnings.catch_warnings(module=wmod):
|
901 | 911 | wmod.filterwarnings('default', category=UserWarning)
|
902 | 912 |
|
| 913 | + linecache.clearcache() |
903 | 914 | with support.captured_stderr() as stderr:
|
904 | 915 | wmod.warn_explicit(
|
905 | 916 | '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)) |
908 | 918 | self.assertIn('UserWarning: foo', stderr.getvalue())
|
| 919 | + self.assertEqual(get_source_called, [42]) |
909 | 920 |
|
910 |
| - show = wmod._showwarnmsg |
911 |
| - try: |
| 921 | + linecache.clearcache() |
| 922 | + with support.swap_attr(wmod, '_showwarnmsg', None): |
912 | 923 | del wmod._showwarnmsg
|
913 | 924 | with support.captured_stderr() as stderr:
|
914 | 925 | wmod.warn_explicit(
|
915 | 926 | '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])) |
918 | 928 | self.assertIn('UserWarning: eggs', stderr.getvalue())
|
919 |
| - finally: |
920 |
| - wmod._showwarnmsg = show |
| 929 | + self.assertEqual(get_source_called, [42, [42]]) |
| 930 | + linecache.clearcache() |
921 | 931 |
|
922 | 932 | @support.cpython_only
|
923 | 933 | def test_issue31411(self):
|
|
0 commit comments