Skip to content
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

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

Open
wants to merge 26 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
a6bb15e
Reapply "gh-128770: raise warnings as errors in test suite - except f…
graingert Jan 18, 2025
baab6a3
convert internal test warnings into logger calls
graingert Jan 18, 2025
c933487
Apply suggestions from code review
graingert Jan 18, 2025
9e532fa
test that support logs instead of warns
graingert Jan 18, 2025
c06c3db
Merge branch 'main' into warnings-as-error-2
graingert Jan 18, 2025
ba36dc0
Merge branch 'main' into warnings-as-error-2
graingert Jan 18, 2025
7123093
Merge branch 'main' into warnings-as-error-2
graingert Jan 18, 2025
33c84e6
Merge branch 'main' into warnings-as-error-2
graingert Jan 18, 2025
104f427
Update test_hashlib.py
graingert Jan 18, 2025
1b44669
Merge branch 'main' into warnings-as-error-2
graingert Jan 19, 2025
36bba4e
Merge branch 'main' into warnings-as-error-2
graingert Jan 20, 2025
b5fc0df
sort logging import
graingert Jan 20, 2025
6bdaad4
Update Lib/test/test_support.py
graingert Jan 20, 2025
3473aab
Update Lib/test/test_support.py
graingert Jan 20, 2025
b49e3f9
Update Lib/test/test_support.py
graingert Jan 20, 2025
b92cbec
Update Lib/test/test_support.py
graingert Jan 20, 2025
fbb99a3
Apply suggestions from code review
graingert Jan 20, 2025
f6909e8
move logging into alphabetical position
graingert Jan 21, 2025
3b895f2
do the hanging indent
graingert Jan 21, 2025
8e17c8a
use indexing rather than unpacking one item
graingert Jan 21, 2025
7ecc3b0
Merge branch 'main' into warnings-as-error-2
graingert Jan 21, 2025
9af76bd
Merge branch 'main' into warnings-as-error-2
graingert Jan 22, 2025
8250358
Merge branch 'main' into warnings-as-error-2
graingert Jan 22, 2025
b1334ea
Merge branch 'main' into warnings-as-error-2
graingert Jan 25, 2025
94fd5b7
Merge branch 'main' into warnings-as-error-2
graingert Jan 25, 2025
1af4d6f
prevent SyntaxError/SyntaxWarning in test_fstring
graingert Jan 25, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Lib/test/libregrtest/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -642,9 +642,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:
Expand Down
6 changes: 3 additions & 3 deletions Lib/test/support/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import contextlib
import functools
import inspect
import logging
import _opcode
import os
import re
Expand Down Expand Up @@ -388,7 +389,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)

Expand Down Expand Up @@ -1067,8 +1068,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

Expand Down
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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
graingert marked this conversation as resolved.
Show resolved Hide resolved
stack_info=True,
stacklevel=3,
)
if dir_created:
pid = os.getpid()
try:
Expand Down Expand Up @@ -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,
graingert marked this conversation as resolved.
Show resolved Hide resolved
stack_info=True,
stacklevel=3,
)
try:
yield os.getcwd()
finally:
Expand Down
6 changes: 4 additions & 2 deletions Lib/test/test_decimal.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
with the corresponding argument.
"""

import logging
import math
import os, sys
import operator
Expand Down Expand Up @@ -5946,8 +5947,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'].")
Expand Down
7 changes: 6 additions & 1 deletion Lib/test/test_hashlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import importlib
import io
import itertools
import logging
import os
import sys
import sysconfig
Expand Down Expand Up @@ -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):
Expand Down
5 changes: 3 additions & 2 deletions Lib/test/test_interpreters/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from collections import namedtuple
import contextlib
import json
import logging
import os
import os.path
#import select
Expand Down Expand Up @@ -66,8 +67,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)
Expand Down
6 changes: 4 additions & 2 deletions Lib/test/test_pty.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
36 changes: 29 additions & 7 deletions Lib/test/test_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import threading
import time
import traceback
import warnings
from weakref import proxy
try:
import multiprocessing
Expand Down Expand Up @@ -198,6 +199,24 @@ def socket_setdefaulttimeout(timeout):
socket.setdefaulttimeout(old_timeout)


@contextlib.contextmanager
def downgrade_malformed_data_warning():
# This warning happens on macos and win, but does not always happen on linux.
if sys.platform not in {"win32", "darwin"}:
yield
return

with warnings.catch_warnings():
# TODO: gh-110012, we should investigate why this warning is happening
# and fix it properly.
warnings.filterwarnings(
action="always",
message="received malformed or improperly-truncated ancillary data",
category=RuntimeWarning,
)
yield


HAVE_SOCKET_CAN = _have_socket_can()

HAVE_SOCKET_CAN_ISOTP = _have_socket_can_isotp()
Expand Down Expand Up @@ -3946,8 +3965,9 @@ def checkTruncatedArray(self, ancbuf, maxdata, mindata=0):
# mindata and maxdata bytes when received with buffer size
# ancbuf, and that any complete file descriptor numbers are
# valid.
msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock,
len(MSG), ancbuf)
with downgrade_malformed_data_warning(): # TODO: gh-110012
msg, ancdata, flags, addr = self.doRecvmsg(self.serv_sock,
len(MSG), ancbuf)
self.assertEqual(msg, MSG)
self.checkRecvmsgAddress(addr, self.cli_addr)
self.checkFlags(flags, eor=True, checkset=socket.MSG_CTRUNC)
Expand Down Expand Up @@ -4298,8 +4318,9 @@ def testSingleCmsgTruncInData(self):
self.serv_sock.setsockopt(socket.IPPROTO_IPV6,
socket.IPV6_RECVHOPLIMIT, 1)
self.misc_event.set()
msg, ancdata, flags, addr = self.doRecvmsg(
self.serv_sock, len(MSG), socket.CMSG_LEN(SIZEOF_INT) - 1)
with downgrade_malformed_data_warning(): # TODO: gh-110012
msg, ancdata, flags, addr = self.doRecvmsg(
self.serv_sock, len(MSG), socket.CMSG_LEN(SIZEOF_INT) - 1)

self.assertEqual(msg, MSG)
self.checkRecvmsgAddress(addr, self.cli_addr)
Expand Down Expand Up @@ -4402,9 +4423,10 @@ def testSecondCmsgTruncInData(self):
self.serv_sock.setsockopt(socket.IPPROTO_IPV6,
socket.IPV6_RECVTCLASS, 1)
self.misc_event.set()
msg, ancdata, flags, addr = self.doRecvmsg(
self.serv_sock, len(MSG),
socket.CMSG_SPACE(SIZEOF_INT) + socket.CMSG_LEN(SIZEOF_INT) - 1)
with downgrade_malformed_data_warning(): # TODO: gh-110012
msg, ancdata, flags, addr = self.doRecvmsg(
self.serv_sock, len(MSG),
socket.CMSG_SPACE(SIZEOF_INT) + socket.CMSG_LEN(SIZEOF_INT) - 1)

self.assertEqual(msg, MSG)
self.checkRecvmsgAddress(addr, self.cli_addr)
Expand Down
Loading
Loading