From 3644456720cf6ea3579551e5ed7586f3bfdd7041 Mon Sep 17 00:00:00 2001 From: AlexWaygood Date: Tue, 3 Oct 2023 14:48:53 +0100 Subject: [PATCH] Defer importing `warnings` in several modules --- Lib/hmac.py | 3 ++- Lib/pathlib.py | 5 ++++- Lib/wsgiref/validate.py | 4 +++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/Lib/hmac.py b/Lib/hmac.py index 8b4f920db954ca8..163c98245031226 100644 --- a/Lib/hmac.py +++ b/Lib/hmac.py @@ -3,7 +3,6 @@ Implements the HMAC algorithm as described by RFC 2104. """ -import warnings as _warnings try: import _hashlib as _hashopenssl except ImportError: @@ -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) diff --git a/Lib/pathlib.py b/Lib/pathlib.py index 9e6d0754eccf3ea..762fe7b5ea689d7 100644 --- a/Lib/pathlib.py +++ b/Lib/pathlib.py @@ -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 @@ -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}") @@ -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}") @@ -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 " @@ -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)) diff --git a/Lib/wsgiref/validate.py b/Lib/wsgiref/validate.py index 1a1853cd63a0d2a..2ad1a85e6d93901 100644 --- a/Lib/wsgiref/validate.py +++ b/Lib/wsgiref/validate.py @@ -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]') @@ -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, ' @@ -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) @@ -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"