Skip to content

[3.13] gh-128770: raise warnings as errors in test suite - except for test_s… #131802

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Mar 29, 2025
4 changes: 2 additions & 2 deletions Lib/test/libregrtest/main.py
Original file line number Diff line number Diff line change
@@ -630,9 +630,9 @@ def _add_ci_python_opts(self, python_opts, keep_environ):
if not sys.stdout.write_through:
python_opts.append('-u')

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

# Error on bytes/str comparison
if sys.flags.bytes_warning < 2:
6 changes: 3 additions & 3 deletions Lib/test/support/__init__.py
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@
import contextlib
import dataclasses
import functools
import logging
import _opcode
import os
import re
@@ -386,7 +387,7 @@ def skip_if_buildbot(reason=None):
try:
isbuildbot = getpass.getuser().lower() == 'buildbot'
except (KeyError, OSError) as err:
warnings.warn(f'getpass.getuser() failed {err}.', RuntimeWarning)
logging.getLogger(__name__).warning('getpass.getuser() failed %s.', err, exc_info=err)
isbuildbot = False
return unittest.skipIf(isbuildbot, reason)

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

80 changes: 80 additions & 0 deletions Lib/test/support/numbers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# These are shared with test_tokenize and other test modules.
#
# Note: since several test cases filter out floats by looking for "e" and ".",
# don't add hexadecimal literals that contain "e" or "E".
VALID_UNDERSCORE_LITERALS = [
'0_0_0',
'4_2',
'1_0000_0000',
'0b1001_0100',
'0xffff_ffff',
'0o5_7_7',
'1_00_00.5',
'1_00_00.5e5',
'1_00_00e5_1',
'1e1_0',
'.1_4',
'.1_4e1',
'0b_0',
'0x_f',
'0o_5',
'1_00_00j',
'1_00_00.5j',
'1_00_00e5_1j',
'.1_4j',
'(1_2.5+3_3j)',
'(.5_6j)',
]
INVALID_UNDERSCORE_LITERALS = [
# Trailing underscores:
'0_',
'42_',
'1.4j_',
'0x_',
'0b1_',
'0xf_',
'0o5_',
'0 if 1_Else 1',
# Underscores in the base selector:
'0_b0',
'0_xf',
'0_o5',
# Old-style octal, still disallowed:
'0_7',
'09_99',
# Multiple consecutive underscores:
'4_______2',
'0.1__4',
'0.1__4j',
'0b1001__0100',
'0xffff__ffff',
'0x___',
'0o5__77',
'1e1__0',
'1e1__0j',
# Underscore right before a dot:
'1_.4',
'1_.4j',
# Underscore right after a dot:
'1._4',
'1._4j',
'._5',
'._5j',
# Underscore right after a sign:
'1.0e+_1',
'1.0e+_1j',
# Underscore right before j:
'1.4_j',
'1.4e5_j',
# Underscore right before e:
'1_e1',
'1.4_e1',
'1.4_e1j',
# Underscore right after e:
'1e_1',
'1.4e_1',
'1.4e_1j',
# Complex cases with parens:
'(1+1.5_j_)',
'(1+1.5_j)',
]
32 changes: 24 additions & 8 deletions Lib/test/support/os_helper.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import collections.abc
import contextlib
import errno
import logging
import os
import re
import stat
@@ -378,8 +379,12 @@ def _waitfor(func, pathname, waitall=False):
# Increase the timeout and try again
time.sleep(timeout)
timeout *= 2
warnings.warn('tests may fail, delete still pending for ' + pathname,
RuntimeWarning, stacklevel=4)
logging.getLogger(__name__).warning(
'tests may fail, delete still pending for %s',
pathname,
stack_info=True,
stacklevel=4,
)

def _unlink(filename):
_waitfor(os.unlink, filename)
@@ -494,9 +499,14 @@ def temp_dir(path=None, quiet=False):
except OSError as exc:
if not quiet:
raise
warnings.warn(f'tests may fail, unable to create '
f'temporary directory {path!r}: {exc}',
RuntimeWarning, stacklevel=3)
logging.getLogger(__name__).warning(
"tests may fail, unable to create temporary directory %r: %s",
path,
exc,
exc_info=exc,
stack_info=True,
stacklevel=3,
)
if dir_created:
pid = os.getpid()
try:
@@ -527,9 +537,15 @@ def change_cwd(path, quiet=False):
except OSError as exc:
if not quiet:
raise
warnings.warn(f'tests may fail, unable to change the current working '
f'directory to {path!r}: {exc}',
RuntimeWarning, stacklevel=3)
logging.getLogger(__name__).warning(
'tests may fail, unable to change the current working directory '
'to %r: %s',
path,
exc,
exc_info=exc,
stack_info=True,
stacklevel=3,
)
try:
yield os.getcwd()
finally:
6 changes: 4 additions & 2 deletions Lib/test/test_complex.py
Original file line number Diff line number Diff line change
@@ -2,8 +2,10 @@
import sys
from test import support
from test.support.testcase import ComplexesAreIdenticalMixin
from test.test_grammar import (VALID_UNDERSCORE_LITERALS,
INVALID_UNDERSCORE_LITERALS)
from test.support.numbers import (
VALID_UNDERSCORE_LITERALS,
INVALID_UNDERSCORE_LITERALS,
)

from random import random
from math import isnan, copysign
6 changes: 4 additions & 2 deletions Lib/test/test_decimal.py
Original file line number Diff line number Diff line change
@@ -24,6 +24,7 @@
with the corresponding argument.
"""

import logging
import math
import os, sys
import operator
@@ -5932,8 +5933,9 @@ def tearDownModule():
if C: C.setcontext(ORIGINAL_CONTEXT[C].copy())
P.setcontext(ORIGINAL_CONTEXT[P].copy())
if not C:
warnings.warn('C tests skipped: no module named _decimal.',
UserWarning)
logging.getLogger(__name__).warning(
'C tests skipped: no module named _decimal.'
)
if not orig_sys_decimal is sys.modules['decimal']:
raise TestFailed("Internal error: unbalanced number of changes to "
"sys.modules['decimal'].")
6 changes: 4 additions & 2 deletions Lib/test/test_float.py
Original file line number Diff line number Diff line change
@@ -9,8 +9,10 @@

from test import support
from test.support.testcase import FloatsAreIdenticalMixin
from test.test_grammar import (VALID_UNDERSCORE_LITERALS,
INVALID_UNDERSCORE_LITERALS)
from test.support.numbers import (
VALID_UNDERSCORE_LITERALS,
INVALID_UNDERSCORE_LITERALS,
)
from math import isinf, isnan, copysign, ldexp
import math

86 changes: 4 additions & 82 deletions Lib/test/test_grammar.py
Original file line number Diff line number Diff line change
@@ -16,88 +16,10 @@
import typing
from test.typinganndata import ann_module2
import test

# These are shared with test_tokenize and other test modules.
#
# Note: since several test cases filter out floats by looking for "e" and ".",
# don't add hexadecimal literals that contain "e" or "E".
VALID_UNDERSCORE_LITERALS = [
'0_0_0',
'4_2',
'1_0000_0000',
'0b1001_0100',
'0xffff_ffff',
'0o5_7_7',
'1_00_00.5',
'1_00_00.5e5',
'1_00_00e5_1',
'1e1_0',
'.1_4',
'.1_4e1',
'0b_0',
'0x_f',
'0o_5',
'1_00_00j',
'1_00_00.5j',
'1_00_00e5_1j',
'.1_4j',
'(1_2.5+3_3j)',
'(.5_6j)',
]
INVALID_UNDERSCORE_LITERALS = [
# Trailing underscores:
'0_',
'42_',
'1.4j_',
'0x_',
'0b1_',
'0xf_',
'0o5_',
'0 if 1_Else 1',
# Underscores in the base selector:
'0_b0',
'0_xf',
'0_o5',
# Old-style octal, still disallowed:
'0_7',
'09_99',
# Multiple consecutive underscores:
'4_______2',
'0.1__4',
'0.1__4j',
'0b1001__0100',
'0xffff__ffff',
'0x___',
'0o5__77',
'1e1__0',
'1e1__0j',
# Underscore right before a dot:
'1_.4',
'1_.4j',
# Underscore right after a dot:
'1._4',
'1._4j',
'._5',
'._5j',
# Underscore right after a sign:
'1.0e+_1',
'1.0e+_1j',
# Underscore right before j:
'1.4_j',
'1.4e5_j',
# Underscore right before e:
'1_e1',
'1.4_e1',
'1.4_e1j',
# Underscore right after e:
'1e_1',
'1.4e_1',
'1.4e_1j',
# Complex cases with parens:
'(1+1.5_j_)',
'(1+1.5_j)',
]

from test.support.numbers import (
VALID_UNDERSCORE_LITERALS,
INVALID_UNDERSCORE_LITERALS,
)

class TokenTests(unittest.TestCase):

7 changes: 6 additions & 1 deletion Lib/test/test_hashlib.py
Original file line number Diff line number Diff line change
@@ -10,6 +10,7 @@
import importlib
import io
import itertools
import logging
import os
import sys
import sysconfig
@@ -113,7 +114,11 @@ def _conditional_import_module(self, module_name):
return importlib.import_module(module_name)
except ModuleNotFoundError as error:
if self._warn_on_extension_import and module_name in builtin_hashes:
warnings.warn(f'Did a C extension fail to compile? {error}')
logging.getLogger(__name__).warning(
'Did a C extension fail to compile? %s',
error,
exc_info=error,
)
return None

def __init__(self, *args, **kwargs):
6 changes: 4 additions & 2 deletions Lib/test/test_int.py
Original file line number Diff line number Diff line change
@@ -4,8 +4,10 @@
import unittest
from unittest import mock
from test import support
from test.test_grammar import (VALID_UNDERSCORE_LITERALS,
INVALID_UNDERSCORE_LITERALS)
from test.support.numbers import (
VALID_UNDERSCORE_LITERALS,
INVALID_UNDERSCORE_LITERALS,
)

try:
import _pylong
5 changes: 3 additions & 2 deletions Lib/test/test_interpreters/utils.py
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@
import contextlib
import json
import io
import logging
import os
import os.path
import pickle
@@ -69,8 +70,8 @@ def pack_exception(exc=None):
def unpack_exception(packed):
try:
data = json.loads(packed)
except json.decoder.JSONDecodeError:
warnings.warn('incomplete exception data', RuntimeWarning)
except json.decoder.JSONDecodeError as e:
logging.getLogger(__name__).warning('incomplete exception data', exc_info=e)
print(packed if isinstance(packed, str) else packed.decode('utf-8'))
return None
exc = types.SimpleNamespace(**data)
6 changes: 4 additions & 2 deletions Lib/test/test_pty.py
Original file line number Diff line number Diff line change
@@ -135,8 +135,10 @@ def test_openpty(self):
new_dim = tty.tcgetwinsize(pty.STDIN_FILENO)
self.assertEqual(new_dim, target_dim,
"pty.STDIN_FILENO window size unchanged")
except OSError:
warnings.warn("Failed to set pty.STDIN_FILENO window size.")
except OSError as e:
logging.getLogger(__name__).warning(
"Failed to set pty.STDIN_FILENO window size.", exc_info=e,
)
pass

try:
Loading