Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 2197bd9

Browse filesBrowse 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 <9087854+AA-Turner@users.noreply.github.com>
1 parent f1315a6 commit 2197bd9
Copy full SHA for 2197bd9

File tree

Expand file treeCollapse file tree

2 files changed

+4
-1
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+4
-1
lines changed

‎Lib/optparse.py

Copy file name to clipboardExpand all lines: Lib/optparse.py
+2-1Lines 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]))
+2Lines 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)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.