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

Skip to content

Commit 10cd152

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 10cd152

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

@@ -252,6 +251,7 @@ def _format_text(self, text):
252251
Format a paragraph of free-form text for inclusion in the
253252
help output at the current indentation level.
254253
"""
254+
import textwrap
255255
text_width = max(self.width - self.current_indent, 11)
256256
indent = " "*self.current_indent
257257
return textwrap.fill(text,
@@ -308,6 +308,7 @@ def format_option(self, option):
308308
indent_first = 0
309309
result.append(opts)
310310
if option.help:
311+
import textwrap
311312
help_text = self.expand_default(option)
312313
help_lines = textwrap.wrap(help_text, self.help_width)
313314
result.append("%*s%s\n" % (indent_first, "", help_lines[0]))
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