Skip to content

Commit 3c06656

Browse files
graingertpicnixz
andcommitted
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]> (cherry picked from commit 8a00c9a)
1 parent b2dbc4f commit 3c06656

File tree

14 files changed

+231
-135
lines changed

14 files changed

+231
-135
lines changed

Lib/test/libregrtest/main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -611,9 +611,9 @@ def _add_ci_python_opts(self, python_opts, keep_environ):
611611
if not sys.stdout.write_through:
612612
python_opts.append('-u')
613613

614-
# Add warnings filter 'default'
614+
# Add warnings filter 'error'
615615
if 'default' not in sys.warnoptions:
616-
python_opts.extend(('-W', 'default'))
616+
python_opts.extend(('-W', 'error'))
617617

618618
# Error on bytes/str comparison
619619
if sys.flags.bytes_warning < 2:

Lib/test/support/__init__.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
import dataclasses
88
import functools
99
import opcode
10+
import inspect
11+
import logging
12+
import _opcode
1013
import os
1114
import re
1215
import stat
@@ -382,7 +385,7 @@ def skip_if_buildbot(reason=None):
382385
try:
383386
isbuildbot = getpass.getuser().lower() == 'buildbot'
384387
except (KeyError, OSError) as err:
385-
warnings.warn(f'getpass.getuser() failed {err}.', RuntimeWarning)
388+
logging.getLogger(__name__).warning('getpass.getuser() failed %s.', err, exc_info=err)
386389
isbuildbot = False
387390
return unittest.skipIf(isbuildbot, reason)
388391

@@ -1027,8 +1030,7 @@ def start(self):
10271030
try:
10281031
f = open(self.procfile, 'r')
10291032
except OSError as e:
1030-
warnings.warn('/proc not available for stats: {}'.format(e),
1031-
RuntimeWarning)
1033+
logging.getLogger(__name__).warning('/proc not available for stats: %s', e, exc_info=e)
10321034
sys.stderr.flush()
10331035
return
10341036

Lib/test/support/numbers.py

Lines changed: 80 additions & 0 deletions
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

Lines changed: 24 additions & 8 deletions
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
@@ -361,8 +362,12 @@ def _waitfor(func, pathname, waitall=False):
361362
# Increase the timeout and try again
362363
time.sleep(timeout)
363364
timeout *= 2
364-
warnings.warn('tests may fail, delete still pending for ' + pathname,
365-
RuntimeWarning, stacklevel=4)
365+
logging.getLogger(__name__).warning(
366+
'tests may fail, delete still pending for %s',
367+
pathname,
368+
stack_info=True,
369+
stacklevel=4,
370+
)
366371

367372
def _unlink(filename):
368373
_waitfor(os.unlink, filename)
@@ -477,9 +482,14 @@ def temp_dir(path=None, quiet=False):
477482
except OSError as exc:
478483
if not quiet:
479484
raise
480-
warnings.warn(f'tests may fail, unable to create '
481-
f'temporary directory {path!r}: {exc}',
482-
RuntimeWarning, stacklevel=3)
485+
logging.getLogger(__name__).warning(
486+
"tests may fail, unable to create temporary directory %r: %s",
487+
path,
488+
exc,
489+
exc_info=exc,
490+
stack_info=True,
491+
stacklevel=3,
492+
)
483493
if dir_created:
484494
pid = os.getpid()
485495
try:
@@ -510,9 +520,15 @@ def change_cwd(path, quiet=False):
510520
except OSError as exc:
511521
if not quiet:
512522
raise
513-
warnings.warn(f'tests may fail, unable to change the current working '
514-
f'directory to {path!r}: {exc}',
515-
RuntimeWarning, stacklevel=3)
523+
logging.getLogger(__name__).warning(
524+
'tests may fail, unable to change the current working directory '
525+
'to %r: %s',
526+
path,
527+
exc,
528+
exc_info=exc,
529+
stack_info=True,
530+
stacklevel=3,
531+
)
516532
try:
517533
yield os.getcwd()
518534
finally:

Lib/test/test_complex.py

Lines changed: 4 additions & 2 deletions
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

Lines changed: 4 additions & 2 deletions
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
@@ -5963,8 +5964,9 @@ def tearDownModule():
59635964
if C: C.setcontext(ORIGINAL_CONTEXT[C].copy())
59645965
P.setcontext(ORIGINAL_CONTEXT[P].copy())
59655966
if not C:
5966-
warnings.warn('C tests skipped: no module named _decimal.',
5967-
UserWarning)
5967+
logging.getLogger(__name__).warning(
5968+
'C tests skipped: no module named _decimal.'
5969+
)
59685970
if not orig_sys_decimal is sys.modules['decimal']:
59695971
raise TestFailed("Internal error: unbalanced number of changes to "
59705972
"sys.modules['decimal'].")

Lib/test/test_float.py

Lines changed: 4 additions & 2 deletions
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

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

10224
class TokenTests(unittest.TestCase):
10325

Lib/test/test_hashlib.py

Lines changed: 6 additions & 1 deletion
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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
import unittest
55
from unittest import mock
66
from test import support
7-
from test.test_grammar import (VALID_UNDERSCORE_LITERALS,
8-
INVALID_UNDERSCORE_LITERALS)
7+
from test.support.numbers import (
8+
VALID_UNDERSCORE_LITERALS,
9+
INVALID_UNDERSCORE_LITERALS,
10+
)
911

1012
try:
1113
import _pylong

0 commit comments

Comments
 (0)