Skip to content

Commit 2197bd9

Browse files
eli-schwartzAA-Turner
authored andcommitted
pythongh-118761: Redudce the import time of optparse (python#128899)
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 <[email protected]>
1 parent f1315a6 commit 2197bd9

File tree

2 files changed

+4
-1
lines changed

2 files changed

+4
-1
lines changed

Lib/optparse.py

+2-1
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]))
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)