]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-109653: Defer importing `warnings` in several modules (#110286)
authorAlex Waygood <Alex.Waygood@Gmail.com>
Wed, 4 Oct 2023 05:09:43 +0000 (06:09 +0100)
committerGitHub <noreply@github.com>
Wed, 4 Oct 2023 05:09:43 +0000 (06:09 +0100)
Lib/argparse.py
Lib/calendar.py
Lib/getpass.py
Lib/shutil.py
Lib/tarfile.py
Misc/NEWS.d/next/Library/2023-10-03-15-17-03.gh-issue-109653.9DYOMD.rst [new file with mode: 0644]

index dfc98695f64e0a57629b168fb05bc95b50c8a5f3..a32884db80d1ea1f5818cc696b63a184187ff017 100644 (file)
@@ -89,8 +89,6 @@ import os as _os
 import re as _re
 import sys as _sys
 
-import warnings
-
 from gettext import gettext as _, ngettext
 
 SUPPRESS = '==SUPPRESS=='
@@ -910,6 +908,7 @@ class BooleanOptionalAction(Action):
         #   parser.add_argument('-f', action=BooleanOptionalAction, type=int)
         for field_name in ('type', 'choices', 'metavar'):
             if locals()[field_name] is not _deprecated_default:
+                import warnings
                 warnings._deprecated(
                     field_name,
                     "{name!r} is deprecated as of Python 3.12 and will be "
@@ -1700,6 +1699,7 @@ class _ArgumentGroup(_ActionsContainer):
         self._group_actions.remove(action)
 
     def add_argument_group(self, *args, **kwargs):
+        import warnings
         warnings.warn(
             "Nesting argument groups is deprecated.",
             category=DeprecationWarning,
@@ -1728,6 +1728,7 @@ class _MutuallyExclusiveGroup(_ArgumentGroup):
         self._group_actions.remove(action)
 
     def add_mutually_exclusive_group(self, *args, **kwargs):
+        import warnings
         warnings.warn(
             "Nesting mutually exclusive groups is deprecated.",
             category=DeprecationWarning,
index 2a4deb70a0111f66b52b6ba51760df181c8176fd..03469d8ac96bcd927b2783de413372bbcf038025 100644 (file)
@@ -10,7 +10,6 @@ import datetime
 from enum import IntEnum, global_enum
 import locale as _locale
 from itertools import repeat
-import warnings
 
 __all__ = ["IllegalMonthError", "IllegalWeekdayError", "setfirstweekday",
            "firstweekday", "isleap", "leapdays", "weekday", "monthrange",
@@ -44,6 +43,7 @@ class IllegalWeekdayError(ValueError):
 
 def __getattr__(name):
     if name in ('January', 'February'):
+        import warnings
         warnings.warn(f"The '{name}' attribute is deprecated, use '{name.upper()}' instead",
                       DeprecationWarning, stacklevel=2)
         if name == 'January':
index 6970d8adfbab3673de681ef05059732dffa8f64c..8b42c0a536b4c4bb458cc9b8c7a3316d081a7a2a 100644 (file)
@@ -18,7 +18,6 @@ import contextlib
 import io
 import os
 import sys
-import warnings
 
 __all__ = ["getpass","getuser","GetPassWarning"]
 
@@ -118,6 +117,7 @@ def win_getpass(prompt='Password: ', stream=None):
 
 
 def fallback_getpass(prompt='Password: ', stream=None):
+    import warnings
     warnings.warn("Can not control echo on the terminal.", GetPassWarning,
                   stacklevel=2)
     if not stream:
index a278b74fab2ddb3b81db1491a2f656280e5fc25b..0fed0117a63234be2df5fc33273099db3bb86483 100644 (file)
@@ -10,7 +10,6 @@ import stat
 import fnmatch
 import collections
 import errno
-import warnings
 
 try:
     import zlib
@@ -723,6 +722,7 @@ def rmtree(path, ignore_errors=False, onerror=None, *, onexc=None, dir_fd=None):
     """
 
     if onerror is not None:
+        import warnings
         warnings.warn("onerror argument is deprecated, use onexc instead",
                       DeprecationWarning, stacklevel=2)
 
index 726f9f50ba2e726e5addb203f57a45bd42d38bba..ec32f9ba49b03f6b199fb28f902af21f517db578 100755 (executable)
@@ -46,7 +46,6 @@ import time
 import struct
 import copy
 import re
-import warnings
 
 try:
     import pwd
@@ -2219,6 +2218,7 @@ class TarFile(object):
         if filter is None:
             filter = self.extraction_filter
             if filter is None:
+                import warnings
                 warnings.warn(
                     'Python 3.14 will, by default, filter extracted tar '
                     + 'archives and reject files or modify their metadata. '
diff --git a/Misc/NEWS.d/next/Library/2023-10-03-15-17-03.gh-issue-109653.9DYOMD.rst b/Misc/NEWS.d/next/Library/2023-10-03-15-17-03.gh-issue-109653.9DYOMD.rst
new file mode 100644 (file)
index 0000000..92e5a1c
--- /dev/null
@@ -0,0 +1,3 @@
+Slightly improve the import time of several standard-library modules by
+deferring imports of :mod:`warnings` within those modules. Patch by Alex
+Waygood.