Skip to content

Commit baab6a3

Browse files
committed
convert internal test warnings into logger calls
1 parent a6bb15e commit baab6a3

File tree

6 files changed

+35
-19
lines changed

6 files changed

+35
-19
lines changed

Lib/test/support/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import types
1818
import unittest
1919
import warnings
20+
import logging
2021

2122

2223
__all__ = [
@@ -387,7 +388,7 @@ def skip_if_buildbot(reason=None):
387388
try:
388389
isbuildbot = getpass.getuser().lower() == 'buildbot'
389390
except (KeyError, OSError) as err:
390-
warnings.warn(f'getpass.getuser() failed {err}.', RuntimeWarning)
391+
logging.getLogger(__name__).warning('getpass.getuser() failed %s.', err, exc_info=err)
391392
isbuildbot = False
392393
return unittest.skipIf(isbuildbot, reason)
393394

@@ -1066,8 +1067,7 @@ def start(self):
10661067
try:
10671068
f = open(self.procfile, 'r')
10681069
except OSError as e:
1069-
warnings.warn('/proc not available for stats: {}'.format(e),
1070-
RuntimeWarning)
1070+
logging.getLogger(__name__).warning('/proc not available for stats: %s', e, exc_info=e)
10711071
sys.stderr.flush()
10721072
return
10731073

Lib/test/support/os_helper.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import logging
12
import collections.abc
23
import contextlib
34
import errno
@@ -378,8 +379,7 @@ 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('tests may fail, delete still pending for %s', pathname)
383383

384384
def _unlink(filename):
385385
_waitfor(os.unlink, filename)
@@ -494,9 +494,12 @@ def temp_dir(path=None, quiet=False):
494494
except OSError as exc:
495495
if not quiet:
496496
raise
497-
warnings.warn(f'tests may fail, unable to create '
498-
f'temporary directory {path!r}: {exc}',
499-
RuntimeWarning, stacklevel=3)
497+
logger.getLogger(__name__).warning(
498+
"tests may fail, unable to create temporary directory %r: %s",
499+
path,
500+
exc,
501+
exc_info=exc,
502+
)
500503
if dir_created:
501504
pid = os.getpid()
502505
try:
@@ -527,9 +530,13 @@ def change_cwd(path, quiet=False):
527530
except OSError as exc:
528531
if not quiet:
529532
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)
533+
logging.getLogger(__name__).warning(
534+
'tests may fail, unable to change the current working directory '
535+
'to %r: %s',
536+
path,
537+
exc,
538+
exc_info=exc,
539+
)
533540
try:
534541
yield os.getcwd()
535542
finally:

Lib/test/test_decimal.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
you're working through IDLE, you can import this test module and call test()
2424
with the corresponding argument.
2525
"""
26-
26+
import logging
2727
import math
2828
import os, sys
2929
import operator
@@ -5946,8 +5946,9 @@ def tearDownModule():
59465946
if C: C.setcontext(ORIGINAL_CONTEXT[C].copy())
59475947
P.setcontext(ORIGINAL_CONTEXT[P].copy())
59485948
if not C:
5949-
warnings.warn('C tests skipped: no module named _decimal.',
5950-
UserWarning)
5949+
logging.getLogger(__name__).warning(
5950+
'C tests skipped: no module named _decimal.'
5951+
)
59515952
if not orig_sys_decimal is sys.modules['decimal']:
59525953
raise TestFailed("Internal error: unbalanced number of changes to "
59535954
"sys.modules['decimal'].")

Lib/test/test_hashlib.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# Licensed to PSF under a Contributor Agreement.
55
#
66

7+
import logging
78
import array
89
from binascii import unhexlify
910
import hashlib
@@ -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_interpreters/utils.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import logging
12
from collections import namedtuple
23
import contextlib
34
import json
@@ -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

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)