]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-118761: Redudce the import time of ``optparse`` (#128899)
authorEli Schwartz <eschwartz@gentoo.org>
Mon, 20 Jan 2025 00:03:19 +0000 (19:03 -0500)
committerGitHub <noreply@github.com>
Mon, 20 Jan 2025 00:03:19 +0000 (00:03 +0000)
The same change was made, and for the same reason, by ``argparse`` back in
2017. The ``textwrap`` module is only used when printing help text, so most
invocations will never need it imported.

Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
Lib/optparse.py
Misc/NEWS.d/next/Library/2025-01-15-18-54-48.gh-issue-118761.G1dv6E.rst [new file with mode: 0644]

index cbe3451ced8bc372187630a8f3ba98d998b68c5d..38cf16d21efffa658c0ef3fa81bb1f9fc3306a29 100644 (file)
@@ -74,7 +74,6 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 """
 
 import sys, os
-import textwrap
 from gettext import gettext as _, ngettext
 
 
@@ -252,6 +251,7 @@ class HelpFormatter:
         Format a paragraph of free-form text for inclusion in the
         help output at the current indentation level.
         """
+        import textwrap
         text_width = max(self.width - self.current_indent, 11)
         indent = " "*self.current_indent
         return textwrap.fill(text,
@@ -308,6 +308,7 @@ class HelpFormatter:
             indent_first = 0
         result.append(opts)
         if option.help:
+            import textwrap
             help_text = self.expand_default(option)
             help_lines = textwrap.wrap(help_text, self.help_width)
             result.append("%*s%s\n" % (indent_first, "", help_lines[0]))
diff --git a/Misc/NEWS.d/next/Library/2025-01-15-18-54-48.gh-issue-118761.G1dv6E.rst b/Misc/NEWS.d/next/Library/2025-01-15-18-54-48.gh-issue-118761.G1dv6E.rst
new file mode 100644 (file)
index 0000000..4144ef8
--- /dev/null
@@ -0,0 +1,2 @@
+Reduce the import time of :mod:`optparse` when no help text is printed.
+Patch by Eli Schwartz.