Skip to content

Commit

Permalink
Defer importing warnings in several modules
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexWaygood committed Oct 3, 2023
1 parent 77f06b1 commit 3644456
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
3 changes: 2 additions & 1 deletion Lib/hmac.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
Implements the HMAC algorithm as described by RFC 2104.
"""

import warnings as _warnings
try:
import _hashlib as _hashopenssl
except ImportError:
Expand Down Expand Up @@ -84,11 +83,13 @@ def _init_old(self, key, msg, digestmod):
if hasattr(self._inner, 'block_size'):
blocksize = self._inner.block_size
if blocksize < 16:
import warnings as _warnings
_warnings.warn('block_size of %d seems too small; using our '
'default of %d.' % (blocksize, self.blocksize),
RuntimeWarning, 2)
blocksize = self.blocksize
else:
import warnings as _warnings
_warnings.warn('No block_size attribute on given digest object; '
'Assuming %d.' % (self.blocksize),
RuntimeWarning, 2)
Expand Down
5 changes: 4 additions & 1 deletion Lib/pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import posixpath
import re
import sys
import warnings
from _collections_abc import Sequence
from errno import ENOENT, ENOTDIR, EBADF, ELOOP, EINVAL
from stat import S_ISDIR, S_ISLNK, S_ISREG, S_ISSOCK, S_ISBLK, S_ISCHR, S_ISFIFO
Expand Down Expand Up @@ -643,6 +642,7 @@ def relative_to(self, other, /, *_deprecated, walk_up=False):
the path.
"""
if _deprecated:
import warnings
msg = ("support for supplying more than one positional argument "
"to pathlib.PurePath.relative_to() is deprecated and "
"scheduled for removal in Python {remove}")
Expand All @@ -665,6 +665,7 @@ def is_relative_to(self, other, /, *_deprecated):
"""Return True if the path is relative to another path or False.
"""
if _deprecated:
import warnings
msg = ("support for supplying more than one argument to "
"pathlib.PurePath.is_relative_to() is deprecated and "
"scheduled for removal in Python {remove}")
Expand Down Expand Up @@ -1115,6 +1116,7 @@ def _glob(self, pattern, case_sensitive, follow_symlinks):
# GH-65238: pathlib doesn't preserve trailing slash. Add it back.
pattern_parts.append('')
if pattern_parts[-1] == '**':
import warnings
# GH-70303: '**' only matches directories. Add trailing slash.
warnings.warn(
"Pattern ending '**' will match files and directories in a "
Expand Down Expand Up @@ -1445,6 +1447,7 @@ class Path(_PathBase):

def __init__(self, *args, **kwargs):
if kwargs:
import warnings
msg = ("support for supplying keyword arguments to pathlib.PurePath "
"is deprecated and scheduled for removal in Python {remove}")
warnings._deprecated("pathlib.PurePath(**kwargs)", msg, remove=(3, 14))
Expand Down
4 changes: 3 additions & 1 deletion Lib/wsgiref/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@

import re
import sys
import warnings

header_re = re.compile(r'^[a-zA-Z][a-zA-Z0-9\-_]*$')
bad_header_value_re = re.compile(r'[\000-\037]')
Expand Down Expand Up @@ -310,6 +309,7 @@ def check_environ(environ):
"(use %s instead)" % (key, key[5:]))

if 'QUERY_STRING' not in environ:
import warnings
warnings.warn(
'QUERY_STRING is not in the WSGI environment; the cgi '
'module will use sys.argv when this variable is missing, '
Expand All @@ -335,6 +335,7 @@ def check_environ(environ):
# @@: these need filling out:
if environ['REQUEST_METHOD'] not in (
'GET', 'HEAD', 'POST', 'OPTIONS', 'PATCH', 'PUT', 'DELETE', 'TRACE'):
import warnings
warnings.warn(
"Unknown REQUEST_METHOD: %r" % environ['REQUEST_METHOD'],
WSGIWarning)
Expand Down Expand Up @@ -378,6 +379,7 @@ def check_status(status):
status_int = int(status_code)
assert_(status_int >= 100, "Status code is invalid: %r" % status_int)
if len(status) < 4 or status[3] != ' ':
import warnings
warnings.warn(
"The status string (%r) should be a three-digit integer "
"followed by a single space and a status explanation"
Expand Down

0 comments on commit 3644456

Please sign in to comment.