Skip to content

Commit 18dd42a

Browse files
graingertpicnixz
andcommitted
pythongh-128770: raise warnings as errors in test suite - except for test_socket which still logs warnings, and internal test warnings that are now logged (python#128973)
Co-authored-by: Bénédikt Tran <[email protected]> (cherry picked from commit 8a00c9a)
1 parent 9118cf7 commit 18dd42a

15 files changed

+233
-137
lines changed

Lib/test/libregrtest/main.py

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

633-
# Add warnings filter 'default'
633+
# Add warnings filter 'error'
634634
if 'default' not in sys.warnoptions:
635-
python_opts.extend(('-W', 'default'))
635+
python_opts.extend(('-W', 'error'))
636636

637637
# Error on bytes/str comparison
638638
if sys.flags.bytes_warning < 2:

Lib/test/support/__init__.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import contextlib
77
import dataclasses
88
import functools
9+
import inspect
10+
import logging
911
import _opcode
1012
import os
1113
import re
@@ -386,7 +388,7 @@ def skip_if_buildbot(reason=None):
386388
try:
387389
isbuildbot = getpass.getuser().lower() == 'buildbot'
388390
except (KeyError, OSError) as err:
389-
warnings.warn(f'getpass.getuser() failed {err}.', RuntimeWarning)
391+
logging.getLogger(__name__).warning('getpass.getuser() failed %s.', err, exc_info=err)
390392
isbuildbot = False
391393
return unittest.skipIf(isbuildbot, reason)
392394

@@ -1079,8 +1081,7 @@ def start(self):
10791081
try:
10801082
f = open(self.procfile, 'r')
10811083
except OSError as e:
1082-
warnings.warn('/proc not available for stats: {}'.format(e),
1083-
RuntimeWarning)
1084+
logging.getLogger(__name__).warning('/proc not available for stats: %s', e, exc_info=e)
10841085
sys.stderr.flush()
10851086
return
10861087

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
@@ -378,8 +379,12 @@ def _waitfor(func, pathname, waitall=False):
378379
# Increase the timeout and try again
379380
time.sleep(timeout)
380381
timeout *= 2
381-
warnings.warn('tests may fail, delete still pending for ' + pathname,
382-
RuntimeWarning, stacklevel=4)
382+
logging.getLogger(__name__).warning(
383+
'tests may fail, delete still pending for %s',
384+
pathname,
385+
stack_info=True,
386+
stacklevel=4,
387+
)
383388

384389
def _unlink(filename):
385390
_waitfor(os.unlink, filename)
@@ -494,9 +499,14 @@ def temp_dir(path=None, quiet=False):
494499
except OSError as exc:
495500
if not quiet:
496501
raise
497-
warnings.warn(f'tests may fail, unable to create '
498-
f'temporary directory {path!r}: {exc}',
499-
RuntimeWarning, stacklevel=3)
502+
logging.getLogger(__name__).warning(
503+
"tests may fail, unable to create temporary directory %r: %s",
504+
path,
505+
exc,
506+
exc_info=exc,
507+
stack_info=True,
508+
stacklevel=3,
509+
)
500510
if dir_created:
501511
pid = os.getpid()
502512
try:
@@ -527,9 +537,15 @@ def change_cwd(path, quiet=False):
527537
except OSError as exc:
528538
if not quiet:
529539
raise
530-
warnings.warn(f'tests may fail, unable to change the current working '
531-
f'directory to {path!r}: {exc}',
532-
RuntimeWarning, stacklevel=3)
540+
logging.getLogger(__name__).warning(
541+
'tests may fail, unable to change the current working directory '
542+
'to %r: %s',
543+
path,
544+
exc,
545+
exc_info=exc,
546+
stack_info=True,
547+
stacklevel=3,
548+
)
533549
try:
534550
yield os.getcwd()
535551
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
@@ -5932,8 +5933,9 @@ def tearDownModule():
59325933
if C: C.setcontext(ORIGINAL_CONTEXT[C].copy())
59335934
P.setcontext(ORIGINAL_CONTEXT[P].copy())
59345935
if not C:
5935-
warnings.warn('C tests skipped: no module named _decimal.',
5936-
UserWarning)
5936+
logging.getLogger(__name__).warning(
5937+
'C tests skipped: no module named _decimal.'
5938+
)
59375939
if not orig_sys_decimal is sys.modules['decimal']:
59385940
raise TestFailed("Internal error: unbalanced number of changes to "
59395941
"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

Lib/test/test_interpreters/utils.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import contextlib
33
import json
44
import io
5+
import logging
56
import os
67
import os.path
78
import pickle
@@ -69,8 +70,8 @@ def pack_exception(exc=None):
6970
def unpack_exception(packed):
7071
try:
7172
data = json.loads(packed)
72-
except json.decoder.JSONDecodeError:
73-
warnings.warn('incomplete exception data', RuntimeWarning)
73+
except json.decoder.JSONDecodeError as e:
74+
logging.getLogger(__name__).warning('incomplete exception data', exc_info=e)
7475
print(packed if isinstance(packed, str) else packed.decode('utf-8'))
7576
return None
7677
exc = types.SimpleNamespace(**data)

Lib/test/test_pty.py

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