diff --git a/Lib/codeop.py b/Lib/codeop.py index adf000ba29f88c..41136e998530c9 100644 --- a/Lib/codeop.py +++ b/Lib/codeop.py @@ -33,7 +33,6 @@ """ import __future__ -import warnings _features = [getattr(__future__, fname) for fname in __future__.all_feature_names] @@ -57,6 +56,8 @@ def _maybe_compile(compiler, source, filename, symbol): if symbol != "eval": source = "pass" # Replace it with a 'pass' statement + import warnings + # Disable compiler warnings when checking for incomplete input. with warnings.catch_warnings(): warnings.simplefilter("ignore", (SyntaxWarning, DeprecationWarning)) diff --git a/Lib/ctypes/_layout.py b/Lib/ctypes/_layout.py index e30db598ab22e1..adda3f9a6f2acc 100644 --- a/Lib/ctypes/_layout.py +++ b/Lib/ctypes/_layout.py @@ -5,8 +5,6 @@ """ import sys -import warnings -import struct from _ctypes import CField, buffer_info import ctypes diff --git a/Lib/hmac.py b/Lib/hmac.py index 8b4eb2fe741e60..0577c151b6583c 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: @@ -81,15 +80,17 @@ def _init_old(self, key, msg, digestmod): self._inner = digest_cons() self.digest_size = self._inner.digest_size + import warnings + if hasattr(self._inner, 'block_size'): blocksize = self._inner.block_size if blocksize < 16: - _warnings.warn('block_size of %d seems too small; using our ' - 'default of %d.' % (blocksize, self.blocksize), - RuntimeWarning, 2) + warnings.warn('block_size of %d seems too small; using our ' + 'default of %d.' % (blocksize, self.blocksize), + RuntimeWarning, 2) blocksize = self.blocksize else: - _warnings.warn('No block_size attribute on given digest object; ' + warnings.warn('No block_size attribute on given digest object; ' 'Assuming %d.' % (self.blocksize), RuntimeWarning, 2) blocksize = self.blocksize diff --git a/Lib/importlib/metadata/__init__.py b/Lib/importlib/metadata/__init__.py index 8ce62dd864fc27..8b18df081d2c49 100644 --- a/Lib/importlib/metadata/__init__.py +++ b/Lib/importlib/metadata/__init__.py @@ -12,7 +12,6 @@ import zipfile import operator import textwrap -import warnings import functools import itertools import posixpath @@ -341,6 +340,7 @@ def __new__(cls, *args, **kwargs): if getattr(getattr(cls, name), '__isabstractmethod__', False) } if abstract: + import warnings warnings.warn( f"Unimplemented abstract methods {abstract}", DeprecationWarning, diff --git a/Lib/importlib/resources/_common.py b/Lib/importlib/resources/_common.py index 4e9014c45a056e..d69c21aae2fb85 100644 --- a/Lib/importlib/resources/_common.py +++ b/Lib/importlib/resources/_common.py @@ -6,7 +6,6 @@ import types import importlib import inspect -import warnings import itertools from typing import Union, Optional, cast @@ -35,6 +34,7 @@ def wrapper(anchor=undefined, package=undefined): if package is not undefined: if anchor is not undefined: return func(anchor, package) + import warnings warnings.warn( "First parameter to files is renamed to 'anchor'", DeprecationWarning, diff --git a/Lib/importlib/resources/_functional.py b/Lib/importlib/resources/_functional.py index f59416f2dd627d..3ce523f560bdcd 100644 --- a/Lib/importlib/resources/_functional.py +++ b/Lib/importlib/resources/_functional.py @@ -1,7 +1,5 @@ """Simplified function-based API for importlib.resources""" -import warnings - from ._common import files, as_file @@ -51,6 +49,7 @@ def contents(anchor, *path_names): The iterable returns :class:`str` resources (e.g. files). The iterable does not recurse into subdirectories. """ + import warnings warnings.warn( "importlib.resources.contents is deprecated. " "Use files(anchor).iterdir() instead.", diff --git a/Lib/importlib/resources/readers.py b/Lib/importlib/resources/readers.py index 70fc7e2b9c0145..7ccd45e0f52ebf 100644 --- a/Lib/importlib/resources/readers.py +++ b/Lib/importlib/resources/readers.py @@ -6,7 +6,6 @@ import pathlib import operator import re -import warnings import zipfile from collections.abc import Iterator @@ -194,6 +193,7 @@ def _ensure_traversable(path): if not isinstance(path, str): return path + import warnings warnings.warn( "String arguments are deprecated. Pass a Traversable instead.", DeprecationWarning, diff --git a/Lib/logging/__init__.py b/Lib/logging/__init__.py index aa9b79d8cab4bb..66b1587978ee63 100644 --- a/Lib/logging/__init__.py +++ b/Lib/logging/__init__.py @@ -23,7 +23,7 @@ To use, simply 'import logging' and log away! """ -import sys, os, time, io, re, traceback, warnings, weakref, collections.abc +import sys, os, time, io, re, traceback, weakref, collections.abc from types import GenericAlias from string import Template @@ -1531,6 +1531,7 @@ def warning(self, msg, *args, **kwargs): self._log(WARNING, msg, args, **kwargs) def warn(self, msg, *args, **kwargs): + import warnings warnings.warn("The 'warn' method is deprecated, " "use 'warning' instead", DeprecationWarning, 2) self.warning(msg, *args, **kwargs) @@ -1912,6 +1913,7 @@ def warning(self, msg, *args, **kwargs): self.log(WARNING, msg, *args, **kwargs) def warn(self, msg, *args, **kwargs): + import warnings warnings.warn("The 'warn' method is deprecated, " "use 'warning' instead", DeprecationWarning, 2) self.warning(msg, *args, **kwargs) @@ -2180,6 +2182,7 @@ def warning(msg, *args, **kwargs): root.warning(msg, *args, **kwargs) def warn(msg, *args, **kwargs): + import warnings warnings.warn("The 'warn' function is deprecated, " "use 'warning' instead", DeprecationWarning, 2) warning(msg, *args, **kwargs) @@ -2299,6 +2302,7 @@ def _showwarning(message, category, filename, lineno, file=None, line=None): if _warnings_showwarning is not None: _warnings_showwarning(message, category, filename, lineno, file, line) else: + import warnings s = warnings.formatwarning(message, category, filename, lineno, line) logger = getLogger("py.warnings") if not logger.handlers: @@ -2316,9 +2320,11 @@ def captureWarnings(capture): global _warnings_showwarning if capture: if _warnings_showwarning is None: + import warnings _warnings_showwarning = warnings.showwarning warnings.showwarning = _showwarning else: if _warnings_showwarning is not None: + import warnings warnings.showwarning = _warnings_showwarning _warnings_showwarning = None diff --git a/Lib/pkgutil.py b/Lib/pkgutil.py index b84d72f2395d45..8772a66791a3c9 100644 --- a/Lib/pkgutil.py +++ b/Lib/pkgutil.py @@ -8,8 +8,6 @@ import os import os.path import sys -from types import ModuleType -import warnings __all__ = [ 'get_importer', 'iter_importers', diff --git a/Lib/pydoc.py b/Lib/pydoc.py index 1839b88fec28b1..52712ef9034ade 100644 --- a/Lib/pydoc.py +++ b/Lib/pydoc.py @@ -71,7 +71,6 @@ class or function within a module or module in a package. If the import time import tokenize import urllib.parse -import warnings from annotationlib import Format from collections import deque from reprlib import Repr @@ -376,6 +375,7 @@ def sort_attributes(attrs, object): def ispackage(path): """Guess whether a path refers to a package directory.""" + import warnings warnings.warn('The pydoc.ispackage() function is deprecated', DeprecationWarning, stacklevel=2) if os.path.isdir(path): @@ -394,6 +394,7 @@ def source_synopsis(file): if tok_type == tokenize.STRING: string += tok_string elif tok_type == tokenize.NEWLINE: + import warnings with warnings.catch_warnings(): # Ignore the "invalid escape sequence" warning. warnings.simplefilter("ignore", SyntaxWarning) @@ -457,6 +458,7 @@ def __init__(self, filename, exc_info): self.value = exc_info self.tb = exc_info.__traceback__ else: + import warnings warnings.warn("A tuple value for exc_info is deprecated, use an exception instance", DeprecationWarning) @@ -2294,6 +2296,7 @@ def callback(path, modname, desc): print(modname, desc and '- ' + desc) def onerror(modname): pass + import warnings with warnings.catch_warnings(): warnings.filterwarnings('ignore') # ignore problems during import ModuleScanner().run(callback, key, onerror=onerror) @@ -2543,6 +2546,7 @@ def callback(path, modname, desc): modname = modname[:-9] + ' (package)' search_result.append((modname, desc and '- ' + desc)) + import warnings with warnings.catch_warnings(): warnings.filterwarnings('ignore') # ignore problems during import def onerror(modname): diff --git a/Lib/rlcompleter.py b/Lib/rlcompleter.py index 23eb0020f42e8a..88e3c19910ab1d 100644 --- a/Lib/rlcompleter.py +++ b/Lib/rlcompleter.py @@ -35,7 +35,6 @@ import keyword import re import __main__ -import warnings __all__ = ["Completer"] @@ -89,6 +88,7 @@ def complete(self, text, state): return None if state == 0: + import warnings with warnings.catch_warnings(action="ignore"): if "." in text: self.matches = self.attr_matches(text) diff --git a/Lib/ssl.py b/Lib/ssl.py index 05df4ad7f0f05c..53e1afbf8bad55 100644 --- a/Lib/ssl.py +++ b/Lib/ssl.py @@ -262,7 +262,6 @@ class _TLSMessageType: import socket as _socket import base64 # for DER-to-PEM translation import errno -import warnings socket_error = OSError # keep that public name in module namespace @@ -429,6 +428,7 @@ class SSLContext(_SSLContext): def __new__(cls, protocol=None, *args, **kwargs): if protocol is None: + import warnings warnings.warn( "ssl.SSLContext() without protocol argument is deprecated.", category=DeprecationWarning, @@ -473,6 +473,7 @@ def wrap_bio(self, incoming, outgoing, server_side=False, ) def set_npn_protocols(self, npn_protocols): + import warnings warnings.warn( "ssl NPN is deprecated, use ALPN instead", DeprecationWarning, @@ -521,8 +522,10 @@ def _load_windows_store_certs(self, storename, purpose): try: self.load_verify_locations(cadata=cert) except SSLError as exc: + import warnings warnings.warn(f"Bad certificate in Windows certificate store: {exc!s}") except PermissionError: + import warnings warnings.warn("unable to enumerate Windows certificate store") def load_default_certs(self, purpose=Purpose.SERVER_AUTH): @@ -914,6 +917,7 @@ def selected_npn_protocol(self): """Return the currently selected NPN protocol as a string, or ``None`` if a next protocol was not negotiated or if NPN is not supported by one of the peers.""" + import warnings warnings.warn( "ssl NPN is deprecated, use ALPN instead", DeprecationWarning, @@ -1183,6 +1187,7 @@ def get_unverified_chain(self): @_sslcopydoc def selected_npn_protocol(self): self._checkClosed() + import warnings warnings.warn( "ssl NPN is deprecated, use ALPN instead", DeprecationWarning, diff --git a/Lib/tempfile.py b/Lib/tempfile.py index 0eb9ddeb6ac377..dc13bf5acdf276 100644 --- a/Lib/tempfile.py +++ b/Lib/tempfile.py @@ -37,7 +37,6 @@ # Imports. import functools as _functools -import warnings as _warnings import io as _io import os as _os import shutil as _shutil @@ -480,7 +479,8 @@ def __del__(self): close_called = self.close_called self.cleanup() if not close_called: - _warnings.warn(self.warn_message, ResourceWarning) + import warnings + warnings.warn(self.warn_message, ResourceWarning) class _TemporaryFileWrapper: @@ -769,7 +769,8 @@ def __iter__(self): def __del__(self): if not self.closed: - _warnings.warn( + import warnings + warnings.warn( "Unclosed file {!r}".format(self), ResourceWarning, stacklevel=2, @@ -953,7 +954,8 @@ def onexc(func, path, exc): def _cleanup(cls, name, warn_message, ignore_errors=False, delete=True): if delete: cls._rmtree(name, ignore_errors=ignore_errors) - _warnings.warn(warn_message, ResourceWarning) + import warnings + warnings.warn(warn_message, ResourceWarning) def __repr__(self): return "<{} {!r}>".format(self.__class__.__name__, self.name) diff --git a/Lib/traceback.py b/Lib/traceback.py index 31c73efcef5a52..fe155c5f6677dd 100644 --- a/Lib/traceback.py +++ b/Lib/traceback.py @@ -5,7 +5,6 @@ import linecache import sys import textwrap -import warnings from contextlib import suppress import _colorize from _colorize import ANSIColors @@ -1178,6 +1177,7 @@ def from_exception(cls, exc, *args, **kwargs): @property def exc_type(self): + import warnings warnings.warn('Deprecated in 3.13. Use exc_type_str instead.', DeprecationWarning, stacklevel=2) return self._exc_type diff --git a/Lib/urllib/parse.py b/Lib/urllib/parse.py index 9d51f4c6812b57..01079591e10a59 100644 --- a/Lib/urllib/parse.py +++ b/Lib/urllib/parse.py @@ -36,7 +36,6 @@ import math import re import types -import warnings import ipaddress __all__ = ["urlparse", "urlunparse", "urljoin", "urldefrag", @@ -825,6 +824,7 @@ def _unquote(s): qs = bytes(memoryview(qs)) except TypeError: if not qs: + import warnings warnings.warn(f"Accepting {type(qs).__name__} objects with " f"false value in urllib.parse.parse_qsl() is " f"deprecated as of 3.14", @@ -1081,6 +1081,7 @@ def urlencode(query, doseq=False, safe='', encoding=None, errors=None, def to_bytes(url): + import warnings warnings.warn("urllib.parse.to_bytes() is deprecated as of 3.8", DeprecationWarning, stacklevel=2) return _to_bytes(url) @@ -1114,6 +1115,7 @@ def unwrap(url): def splittype(url): + import warnings warnings.warn("urllib.parse.splittype() is deprecated as of 3.8, " "use urllib.parse.urlparse() instead", DeprecationWarning, stacklevel=2) @@ -1135,6 +1137,7 @@ def _splittype(url): def splithost(url): + import warnings warnings.warn("urllib.parse.splithost() is deprecated as of 3.8, " "use urllib.parse.urlparse() instead", DeprecationWarning, stacklevel=2) @@ -1158,6 +1161,7 @@ def _splithost(url): def splituser(host): + import warnings warnings.warn("urllib.parse.splituser() is deprecated as of 3.8, " "use urllib.parse.urlparse() instead", DeprecationWarning, stacklevel=2) @@ -1171,6 +1175,7 @@ def _splituser(host): def splitpasswd(user): + import warnings warnings.warn("urllib.parse.splitpasswd() is deprecated as of 3.8, " "use urllib.parse.urlparse() instead", DeprecationWarning, stacklevel=2) @@ -1184,6 +1189,7 @@ def _splitpasswd(user): def splitport(host): + import warnings warnings.warn("urllib.parse.splitport() is deprecated as of 3.8, " "use urllib.parse.urlparse() instead", DeprecationWarning, stacklevel=2) @@ -1207,6 +1213,7 @@ def _splitport(host): def splitnport(host, defport=-1): + import warnings warnings.warn("urllib.parse.splitnport() is deprecated as of 3.8, " "use urllib.parse.urlparse() instead", DeprecationWarning, stacklevel=2) @@ -1231,6 +1238,7 @@ def _splitnport(host, defport=-1): def splitquery(url): + import warnings warnings.warn("urllib.parse.splitquery() is deprecated as of 3.8, " "use urllib.parse.urlparse() instead", DeprecationWarning, stacklevel=2) @@ -1246,6 +1254,7 @@ def _splitquery(url): def splittag(url): + import warnings warnings.warn("urllib.parse.splittag() is deprecated as of 3.8, " "use urllib.parse.urlparse() instead", DeprecationWarning, stacklevel=2) @@ -1261,6 +1270,7 @@ def _splittag(url): def splitattr(url): + import warnings warnings.warn("urllib.parse.splitattr() is deprecated as of 3.8, " "use urllib.parse.urlparse() instead", DeprecationWarning, stacklevel=2) @@ -1275,6 +1285,7 @@ def _splitattr(url): def splitvalue(attr): + import warnings warnings.warn("urllib.parse.splitvalue() is deprecated as of 3.8, " "use urllib.parse.parse_qsl() instead", DeprecationWarning, stacklevel=2) diff --git a/Lib/xml/etree/ElementTree.py b/Lib/xml/etree/ElementTree.py index ce67d7d7d54748..8ce589fe875b79 100644 --- a/Lib/xml/etree/ElementTree.py +++ b/Lib/xml/etree/ElementTree.py @@ -94,7 +94,6 @@ import sys import re -import warnings import io import collections import collections.abc @@ -200,6 +199,7 @@ def __len__(self): return len(self._children) def __bool__(self): + import warnings warnings.warn( "Testing an element's truth value will always return True in " "future versions. " @@ -602,6 +602,7 @@ def find(self, path, namespaces=None): # assert self._root is not None if path[:1] == "/": path = "." + path + import warnings warnings.warn( "This search is broken in 1.3 and earlier, and will be " "fixed in a future version. If you rely on the current " @@ -624,6 +625,7 @@ def findtext(self, path, default=None, namespaces=None): # assert self._root is not None if path[:1] == "/": path = "." + path + import warnings warnings.warn( "This search is broken in 1.3 and earlier, and will be " "fixed in a future version. If you rely on the current " @@ -646,6 +648,7 @@ def findall(self, path, namespaces=None): # assert self._root is not None if path[:1] == "/": path = "." + path + import warnings warnings.warn( "This search is broken in 1.3 and earlier, and will be " "fixed in a future version. If you rely on the current " @@ -668,6 +671,7 @@ def iterfind(self, path, namespaces=None): # assert self._root is not None if path[:1] == "/": path = "." + path + import warnings warnings.warn( "This search is broken in 1.3 and earlier, and will be " "fixed in a future version. If you rely on the current " @@ -1700,6 +1704,7 @@ def _default(self, text): if hasattr(self.target, "doctype"): self.target.doctype(name, pubid, system[1:-1]) elif hasattr(self, "doctype"): + import warnings warnings.warn( "The doctype() method of XMLParser is ignored. " "Define doctype() method on the TreeBuilder target.", diff --git a/Misc/NEWS.d/next/Library/2025-02-07-05-55-08.gh-issue-118761.aayL_A.rst b/Misc/NEWS.d/next/Library/2025-02-07-05-55-08.gh-issue-118761.aayL_A.rst new file mode 100644 index 00000000000000..1c57361385c42a --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-02-07-05-55-08.gh-issue-118761.aayL_A.rst @@ -0,0 +1,2 @@ +Improve import time of various stdlib modules by lazy import ``warnings``. Patch by +Semyon Moroz.
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: