8000 gh-118761: improve optparse import time by delaying textwrap import · python/cpython@4484efc · GitHub
[go: up one dir, main page]

Skip to content

Commit 4484efc

Browse files
committed
gh-118761: improve optparse import time by delaying textwrap import
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
1 parent d05140f commit 4484efc

File tree

2 files changed

+4
-1
lines changed

2 files changed

+4
-1
lines changed

Lib/optparse.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@
7474
"""
7575

7676
import sys, os
77-
import textwrap
7877
from gettext import gettext as _, ngettext
7978

8079

@@ -254,6 +253,7 @@ def _format_text(self, text):
254253
"""
255254
text_width = max(self.width - self.current_indent, 11)
256255
indent = " "*self.current_indent
256+
import textwrap
257257
return textwrap.fill(text,
258258
text_width,
259259
initial_indent=indent,
@@ -309,6 +309,7 @@ def format_option(self, option):
309309
result.append(opts)
310310
if option.help:
311311
help_text = self.expand_default(option)
312+
import textwrap
312313
help_lines = textwrap.wrap(help_text, self.help_width)
313314
result.append("%*s%s\n" % (indent_first, "", help_lines[0]))
314315
result.extend(["%*s%s\n" % (self.help_position, "", line)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Reduce the import time of :mod:`optparse` when no help text is printed.
2+
Patch by Eli Schwartz.

0 commit comments

Comments
 (0)
0