Skip to content

Commit 8a00c9a

Browse files
graingertpicnixz
andauthored
gh-128770: raise warnings as errors in test suite - except for test_socket which still logs warnings, and internal test warnings that are now logged (#128973)
Co-authored-by: Bénédikt Tran <[email protected]>
1 parent 972a295 commit 8a00c9a

15 files changed

+232
-137
lines changed

Lib/test/libregrtest/main.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -640,9 +640,9 @@ def _add_ci_python_opts(self, python_opts, keep_environ):
640640
if not sys.stdout.write_through:
641641
python_opts.append('-u')
642642

643-
# Add warnings filter 'default'
643+
# Add warnings filter 'error'
644644
if 'default' not in sys.warnoptions:
645-
python_opts.extend(('-W', 'default'))
645+
python_opts.extend(('-W', 'error'))
646646

647647
# Error on bytes/str comparison
648648
if sys.flags.bytes_warning < 2:

Lib/test/support/__init__.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import contextlib
77
import functools
88
import inspect
9+
import logging
910
import _opcode
1011
import os
1112
import re
@@ -404,7 +405,7 @@ def skip_if_buildbot(reason=None):
404405
try:
405406
isbuildbot = getpass.getuser().lower() == 'buildbot'
406407
except (KeyError, OSError) as err:
407-
warnings.warn(f'getpass.getuser() failed {err}.', RuntimeWarning)
408+
logging.getLogger(__name__).warning('getpass.getuser() failed %s.', err, exc_info=err)
408409
isbuildbot = False
409410
return unittest.skipIf(isbuildbot, reason)
410411

@@ -1089,8 +1090,7 @@ def start(self):
10891090
try:
10901091
f = open(self.procfile, 'r')
10911092
except OSError as e:
1092-
warnings.warn('/proc not available for stats: {}'.format(e),
1093-
RuntimeWarning)
1093+
logging.getLogger(__name__).warning('/proc not available for stats: %s', e, exc_info=e)
10941094
sys.stderr.flush()
10951095
return
10961096

Lib/test/support/numbers.py

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# These are shared with test_tokenize and other test modules.
2+
#
3+
# Note: since several test cases filter out floats by looking for "e" and ".",
4+
# don't add hexadecimal literals that contain "e" or "E".
5+
VALID_UNDERSCORE_LITERALS = [
6+
'0_0_0',
7+
'4_2',
8+
'1_0000_0000',
9+
'0b1001_0100',
10+
'0xffff_ffff',
11+
'0o5_7_7',
12+
'1_00_00.5',
13+
'1_00_00.5e5',
14+
'1_00_00e5_1',
15+
'1e1_0',
16+
'.1_4',
17+
'.1_4e1',
18+
'0b_0',
19+
'0x_f',
20+
'0o_5',
21+
'1_00_00j',
22+
'1_00_00.5j',
23+
'1_00_00e5_1j',
24+
'.1_4j',
25+
'(1_2.5+3_3j)',
26+
'(.5_6j)',
27+
]
28+
INVALID_UNDERSCORE_LITERALS = [
29+
# Trailing underscores:
30+
'0_',
31+
'42_',
32+
'1.4j_',
33+
'0x_',
34+
'0b1_',
35+
'0xf_',
36+
'0o5_',
37+
'0 if 1_Else 1',
38+
# Underscores in the base selector:
39+
'0_b0',
40+
'0_xf',
41+
'0_o5',
42+
# Old-style octal, still disallowed:
43+
'0_7',
44+
'09_99',
45+
# Multiple consecutive underscores:
46+
'4_______2',
47+
'0.1__4',
48+
'0.1__4j',
49+
'0b1001__0100',
50+
'0xffff__ffff',
51+
'0x___',
52+
'0o5__77',
53+
'1e1__0',
54+
'1e1__0j',
55+
# Underscore right before a dot:
56+
'1_.4',
57+
'1_.4j',
58+
# Underscore right after a dot:
59+
'1._4',
60+
'1._4j',
61+
'._5',
62+
'._5j',
63+
# Underscore right after a sign:
64+
'1.0e+_1',
65+
'1.0e+_1j',
66+
# Underscore right before j:
67+
'1.4_j',
68+
'1.4e5_j',
69+
# Underscore right before e:
70+
'1_e1',
71+
'1.4_e1',
72+
'1.4_e1j',
73+
# Underscore right after e:
74+
'1e_1',
75+
'1.4e_1',
76+
'1.4e_1j',
77+
# Complex cases with parens:
78+
'(1+1.5_j_)',
79+
'(1+1.5_j)',
80+
]

Lib/test/support/os_helper.py

+24-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import collections.abc
22
import contextlib
33
import errno
4+
import logging
45
import os
56
import re
67
import stat
@@ -405,8 +406,12 @@ def _waitfor(func, pathname, waitall=False):
405406
# Increase the timeout and try again
406407
time.sleep(timeout)
407408
timeout *= 2
408-
warnings.warn('tests may fail, delete still pending for ' + pathname,
409-
RuntimeWarning, stacklevel=4)
409+
logging.getLogger(__name__).warning(
410+
'tests may fail, delete still pending for %s',
411+
pathname,
412+
stack_info=True,
413+
stacklevel=4,
414+
)
410415

411416
def _unlink(filename):
412417
_waitfor(os.unlink, filename)
@@ -521,9 +526,14 @@ def temp_dir(path=None, quiet=False):
521526
except OSError as exc:
522527
if not quiet:
523528
raise
524-
warnings.warn(f'tests may fail, unable to create '
525-
f'temporary directory {path!r}: {exc}',
526-
RuntimeWarning, stacklevel=3)
529+
logging.getLogger(__name__).warning(
530+
"tests may fail, unable to create temporary directory %r: %s",
531+
path,
532+
exc,
533+
exc_info=exc,
534+
stack_info=True,
535+
stacklevel=3,
536+
)
527537
if dir_created:
528538
pid = os.getpid()
529539
try:
@@ -554,9 +564,15 @@ def change_cwd(path, quiet=False):
554564
except OSError as exc:
555565
if not quiet:
556566
raise
557-
warnings.warn(f'tests may fail, unable to change the current working '
558-
f'directory to {path!r}: {exc}',
559-
RuntimeWarning, stacklevel=3)
567+
logging.getLogger(__name__).warning(
568+
'tests may fail, unable to change the current working directory '
569+
'to %r: %s',
570+
path,
571+
exc,
572+
exc_info=exc,
573+
stack_info=True,
574+
stacklevel=3,
575+
)
560576
try:
561577
yield os.getcwd()
562578
finally:

Lib/test/test_complex.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
import sys
33
from test import support
44
from test.support.testcase import ComplexesAreIdenticalMixin
5-
from test.test_grammar import (VALID_UNDERSCORE_LITERALS,
6-
INVALID_UNDERSCORE_LITERALS)
5+
from test.support.numbers import (
6+
VALID_UNDERSCORE_LITERALS,
7+
INVALID_UNDERSCORE_LITERALS,
8+
)
79

810
from random import random
911
from math import isnan, copysign

Lib/test/test_decimal.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
with the corresponding argument.
2525
"""
2626

27+
import logging
2728
import math
2829
import os, sys
2930
import operator
@@ -5955,8 +5956,9 @@ def tearDownModule():
59555956
if C: C.setcontext(ORIGINAL_CONTEXT[C].copy())
59565957
P.setcontext(ORIGINAL_CONTEXT[P].copy())
59575958
if not C:
5958-
warnings.warn('C tests skipped: no module named _decimal.',
5959-
UserWarning)
5959+
logging.getLogger(__name__).warning(
5960+
'C tests skipped: no module named _decimal.'
5961+
)
59605962
if not orig_sys_decimal is sys.modules['decimal']:
59615963
raise TestFailed("Internal error: unbalanced number of changes to "
59625964
"sys.modules['decimal'].")

Lib/test/test_float.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99

1010
from test import support
1111
from test.support.testcase import FloatsAreIdenticalMixin
12-
from test.test_grammar import (VALID_UNDERSCORE_LITERALS,
13-
INVALID_UNDERSCORE_LITERALS)
12+
from test.support.numbers import (
13+
VALID_UNDERSCORE_LITERALS,
14+
INVALID_UNDERSCORE_LITERALS,
15+
)
1416
from math import isinf, isnan, copysign, ldexp
1517
import math
1618

Lib/test/test_grammar.py

+4-82
Original file line numberDiff line numberDiff line change
@@ -18,88 +18,10 @@
1818
import typing
1919
from test.typinganndata import ann_module2
2020
import test
21-
22-
# These are shared with test_tokenize and other test modules.
23-
#
24-
# Note: since several test cases filter out floats by looking for "e" and ".",
25-
# don't add hexadecimal literals that contain "e" or "E".
26-
VALID_UNDERSCORE_LITERALS = [
27-
'0_0_0',
28-
'4_2',
29-
'1_0000_0000',
30-
'0b1001_0100',
31-
'0xffff_ffff',
32-
'0o5_7_7',
33-
'1_00_00.5',
34-
'1_00_00.5e5',
35-
'1_00_00e5_1',
36-
'1e1_0',
37-
'.1_4',
38-
'.1_4e1',
39-
'0b_0',
40-
'0x_f',
41-
'0o_5',
42-
'1_00_00j',
43-
'1_00_00.5j',
44-
'1_00_00e5_1j',
45-
'.1_4j',
46-
'(1_2.5+3_3j)',
47-
'(.5_6j)',
48-
]
49-
INVALID_UNDERSCORE_LITERALS = [
50-
# Trailing underscores:
51-
'0_',
52-
'42_',
53-
'1.4j_',
54-
'0x_',
55-
'0b1_',
56-
'0xf_',
57-
'0o5_',
58-
'0 if 1_Else 1',
59-
# Underscores in the base selector:
60-
'0_b0',
61-
'0_xf',
62-
'0_o5',
63-
# Old-style octal, still disallowed:
64-
'0_7',
65-
'09_99',
66-
# Multiple consecutive underscores:
67-
'4_______2',
68-
'0.1__4',
69-
'0.1__4j',
70-
'0b1001__0100',
71-
'0xffff__ffff',
72-
'0x___',
73-
'0o5__77',
74-
'1e1__0',
75-
'1e1__0j',
76-
# Underscore right before a dot:
77-
'1_.4',
78-
'1_.4j',
79-
# Underscore right after a dot:
80-
'1._4',
81-
'1._4j',
82-
'._5',
83-
'._5j',
84-
# Underscore right after a sign:
85-
'1.0e+_1',
86-
'1.0e+_1j',
87-
# Underscore right before j:
88-
'1.4_j',
89-
'1.4e5_j',
90-
# Underscore right before e:
91-
'1_e1',
92-
'1.4_e1',
93-
'1.4_e1j',
94-
# Underscore right after e:
95-
'1e_1',
96-
'1.4e_1',
97-
'1.4e_1j',
98-
# Complex cases with parens:
99-
'(1+1.5_j_)',
100-
'(1+1.5_j)',
101-
]
102-
21+
from test.support.numbers import (
22+
VALID_UNDERSCORE_LITERALS,
23+
INVALID_UNDERSCORE_LITERALS,
24+
)
10325

10426
class TokenTests(unittest.TestCase):
10527

Lib/test/test_hashlib.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import importlib
1111
import io
1212
import itertools
13+
import logging
1314
import os
1415
import sys
1516
import sysconfig
@@ -113,7 +114,11 @@ def _conditional_import_module(self, module_name):
113114
return importlib.import_module(module_name)
114115
except ModuleNotFoundError as error:
115116
if self._warn_on_extension_import and module_name in builtin_hashes:
116-
warnings.warn(f'Did a C extension fail to compile? {error}')
117+
logging.getLogger(__name__).warning(
118+
'Did a C extension fail to compile? %s',
119+
error,
120+
exc_info=error,
121+
)
117122
return None
118123

119124
def __init__(self, *args, **kwargs):

Lib/test/test_int.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
import unittest
44
from unittest import mock
55
from test import support
6-
from test.test_grammar import (VALID_UNDERSCORE_LITERALS,
7-
INVALID_UNDERSCORE_LITERALS)
6+
from test.support.numbers import (
7+
VALID_UNDERSCORE_LITERALS,
8+
INVALID_UNDERSCORE_LITERALS,
9+
)
810

911
try:
1012
import _pylong

Lib/test/test_interpreters/utils.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from collections import namedtuple
22
import contextlib
33
import json
4+
import logging
45
import os
56
import os.path
67
#import select
@@ -66,8 +67,8 @@ def pack_exception(exc=None):
6667
def unpack_exception(packed):
6768
try:
6869
data = json.loads(packed)
69-
except json.decoder.JSONDecodeError:
70-
warnings.warn('incomplete exception data', RuntimeWarning)
70+
except json.decoder.JSONDecodeError as e:
71+
logging.getLogger(__name__).warning('incomplete exception data', exc_info=e)
7172
print(packed if isinstance(packed, str) else packed.decode('utf-8'))
7273
return None
7374
exc = types.SimpleNamespace(**data)

Lib/test/test_pty.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,10 @@ def test_openpty(self):
135135
new_dim = tty.tcgetwinsize(pty.STDIN_FILENO)
136136
self.assertEqual(new_dim, target_dim,
137137
"pty.STDIN_FILENO window size unchanged")
138-
except OSError:
139-
warnings.warn("Failed to set pty.STDIN_FILENO window size.")
138+
except OSError as e:
139+
logging.getLogger(__name__).warning(
140+
"Failed to set pty.STDIN_FILENO window size.", exc_info=e,
141+
)
140142
pass
141143

142144
try:

0 commit comments

Comments
 (0)