Skip to content

gh-118761: improve optparse import time by delaying textwrap import #128899

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -252,6 +251,7 @@ def _format_text(self, text):
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,
Expand Down Expand Up @@ -308,6 +308,7 @@ def format_option(self, option):
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]))
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.
Loading