Skip to content

Commit

Permalink
gh-118761: improve optparse import time by delaying textwrap import
Browse files Browse the repository at this point in the history
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.

See: #1269
See: 8110837
  • Loading branch information
eli-schwartz committed Jan 16, 2025
1 parent d05140f commit 4484efc
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Lib/optparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@
"""

import sys, os
import textwrap
from gettext import gettext as _, ngettext


Expand Down Expand Up @@ -254,6 +253,7 @@ def _format_text(self, text):
"""
text_width = max(self.width - self.current_indent, 11)
indent = " "*self.current_indent
import textwrap
return textwrap.fill(text,
text_width,
initial_indent=indent,
Expand Down Expand Up @@ -309,6 +309,7 @@ def format_option(self, option):
result.append(opts)
if option.help:
help_text = self.expand_default(option)
import textwrap
help_lines = textwrap.wrap(help_text, self.help_width)
result.append("%*s%s\n" % (indent_first, "", help_lines[0]))
result.extend(["%*s%s\n" % (self.help_position, "", line)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Reduce the import time of :mod:`optparse` when no help text is printed.
Patch by Eli Schwartz.

0 comments on commit 4484efc

Please sign in to comment.