From 014fc5a4de843d735132bd1c7b8c2e457309dd35 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Wed, 26 Apr 2017 11:32:44 +0300 Subject: [PATCH 1/4] bpo-30166: Import command-line parsing modules only when needed. --- Lib/code.py | 2 +- Lib/doctest.py | 3 ++- Lib/http/server.py | 3 ++- Lib/idlelib/pyshell.py | 12 +++++++----- Lib/profile.py | 5 +++-- Lib/tabnanny.py | 2 +- Lib/trace.py | 3 ++- 7 files changed, 18 insertions(+), 12 deletions(-) diff --git a/Lib/code.py b/Lib/code.py index 23295f4cf59610a..6b12efb3fe4103b 100644 --- a/Lib/code.py +++ b/Lib/code.py @@ -7,7 +7,6 @@ import sys import traceback -import argparse from codeop import CommandCompiler, compile_command __all__ = ["InteractiveInterpreter", "InteractiveConsole", "interact", @@ -303,6 +302,7 @@ def interact(banner=None, readfunc=None, local=None, exitmsg=None): if __name__ == "__main__": + import argparse parser = argparse.ArgumentParser() parser.add_argument('-q', action='store_true', help="don't print version and copyright messages") diff --git a/Lib/doctest.py b/Lib/doctest.py index 0b78544d8d0ee0f..5e5bc21a0386709 100644 --- a/Lib/doctest.py +++ b/Lib/doctest.py @@ -93,7 +93,6 @@ def _test(): ] import __future__ -import argparse import difflib import inspect import linecache @@ -2741,6 +2740,8 @@ def get(self): def _test(): + import argparse + parser = argparse.ArgumentParser(description="doctest runner") parser.add_argument('-v', '--verbose', action='store_true', default=False, help='print very verbose output for all tests') diff --git a/Lib/http/server.py b/Lib/http/server.py index 429490b73a88b7c..7b3e701fb7a7635 100644 --- a/Lib/http/server.py +++ b/Lib/http/server.py @@ -87,7 +87,6 @@ "SimpleHTTPRequestHandler", "CGIHTTPRequestHandler", ] -import argparse import copy import datetime import email.utils @@ -1227,6 +1226,8 @@ def test(HandlerClass=BaseHTTPRequestHandler, sys.exit(0) if __name__ == '__main__': + import argparse + parser = argparse.ArgumentParser() parser.add_argument('--cgi', action='store_true', help='Run as CGI Server') diff --git a/Lib/idlelib/pyshell.py b/Lib/idlelib/pyshell.py index 8ddc18951c7bb40..46595c7d4b6d354 100755 --- a/Lib/idlelib/pyshell.py +++ b/Lib/idlelib/pyshell.py @@ -18,11 +18,10 @@ raise SystemExit(1) from code import InteractiveInterpreter -import getopt import linecache import os import os.path -from platform import python_version, system +from platform import python_version import re import socket import subprocess @@ -31,14 +30,13 @@ import tokenize import warnings -from idlelib import testing # bool value +import idlelib from idlelib.colorizer import ColorDelegator from idlelib.config import idleConf from idlelib import debugger from idlelib import debugger_r from idlelib.editor import EditorWindow, fixwordbreaks from idlelib.filelist import FileList -from idlelib import macosx from idlelib.outwin import OutputWindow from idlelib import rpc from idlelib.run import idle_formatwarning, PseudoInputFile, PseudoOutputFile @@ -1371,6 +1369,10 @@ def fix_x11_paste(root): """ def main(): + import getopt + from platform import system + from idlelib import macosx + global flist, root, use_subprocess capture_warnings(True) @@ -1451,7 +1453,7 @@ def main(): # Setup root. Don't break user code run in IDLE process. # Don't change environment when testing. - if use_subprocess and not testing: + if use_subprocess and not idlelib.testing: NoDefaultRoot() root = Tk(className="Idle") root.withdraw() diff --git a/Lib/profile.py b/Lib/profile.py index 5d0e9685a4f1aaf..a122e3d1078a2d7 100755 --- a/Lib/profile.py +++ b/Lib/profile.py @@ -25,10 +25,8 @@ import sys -import os import time import marshal -from optparse import OptionParser __all__ = ["run", "runctx", "Profile"] @@ -552,6 +550,9 @@ def f(m, f1=f1): #**************************************************************************** def main(): + import os + from optparse import OptionParser + usage = "profile.py [-o output_file_path] [-s sort] scriptfile [arg] ..." parser = OptionParser(usage=usage) parser.allow_interspersed_args = False diff --git a/Lib/tabnanny.py b/Lib/tabnanny.py index bfb670c9027d769..0be51e68b5dc74b 100755 --- a/Lib/tabnanny.py +++ b/Lib/tabnanny.py @@ -22,7 +22,6 @@ import os import sys -import getopt import tokenize if not hasattr(tokenize, 'NL'): raise ValueError("tokenize.NL doesn't exist -- tokenize module too old") @@ -40,6 +39,7 @@ def errprint(*args): sys.stderr.write("\n") def main(): + import getopt global verbose, filename_only try: opts, args = getopt.getopt(sys.argv[1:], "qv") diff --git a/Lib/trace.py b/Lib/trace.py index ae154615fa3af4a..e443edd6061bbe5 100755 --- a/Lib/trace.py +++ b/Lib/trace.py @@ -48,7 +48,7 @@ r.write_results(show_missing=True, coverdir="/tmp") """ __all__ = ['Trace', 'CoverageResults'] -import argparse + import linecache import os import re @@ -609,6 +609,7 @@ def results(self): callers=self._callers) def main(): + import argparse parser = argparse.ArgumentParser() parser.add_argument('--version', action='version', version='trace 2.0') From a9f649f4062f694acf7aabbb1db29c050893d051 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Wed, 26 Apr 2017 23:58:07 +0300 Subject: [PATCH 2/4] Always add an empty line after imports. --- Lib/code.py | 1 + Lib/tabnanny.py | 1 + 2 files changed, 2 insertions(+) diff --git a/Lib/code.py b/Lib/code.py index 6b12efb3fe4103b..d8106ae612c4b40 100644 --- a/Lib/code.py +++ b/Lib/code.py @@ -303,6 +303,7 @@ def interact(banner=None, readfunc=None, local=None, exitmsg=None): if __name__ == "__main__": import argparse + parser = argparse.ArgumentParser() parser.add_argument('-q', action='store_true', help="don't print version and copyright messages") diff --git a/Lib/tabnanny.py b/Lib/tabnanny.py index 0be51e68b5dc74b..7973f26f98b8b24 100755 --- a/Lib/tabnanny.py +++ b/Lib/tabnanny.py @@ -40,6 +40,7 @@ def errprint(*args): def main(): import getopt + global verbose, filename_only try: opts, args = getopt.getopt(sys.argv[1:], "qv") From 7973dde23c1410a5edab35c91e839aab4d98b82f Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Thu, 27 Apr 2017 08:22:14 +0300 Subject: [PATCH 3/4] Move 'import idlelib' into main() too. --- Lib/idlelib/pyshell.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/idlelib/pyshell.py b/Lib/idlelib/pyshell.py index 46595c7d4b6d354..5b0e5b267642ab2 100755 --- a/Lib/idlelib/pyshell.py +++ b/Lib/idlelib/pyshell.py @@ -30,7 +30,6 @@ import tokenize import warnings -import idlelib from idlelib.colorizer import ColorDelegator from idlelib.config import idleConf from idlelib import debugger @@ -1371,6 +1370,7 @@ def fix_x11_paste(root): def main(): import getopt from platform import system + from idlelib import testing # bool value from idlelib import macosx global flist, root, use_subprocess @@ -1453,7 +1453,7 @@ def main(): # Setup root. Don't break user code run in IDLE process. # Don't change environment when testing. - if use_subprocess and not idlelib.testing: + if use_subprocess and not testing: NoDefaultRoot() root = Tk(className="Idle") root.withdraw() From b897bfdab11cb84839b228d94f5ede6ff01ad45a Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Thu, 27 Apr 2017 08:23:25 +0300 Subject: [PATCH 4/4] Update outdated comment in profile.py. --- Lib/profile.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/profile.py b/Lib/profile.py index a122e3d1078a2d7..5ceeddc075fc6be 100755 --- a/Lib/profile.py +++ b/Lib/profile.py @@ -177,7 +177,7 @@ def get_time_timer(timer=timer, sum=sum): self.t = self.get_time() self.simulate_call('profiler') - # Heavily optimized dispatch routine for os.times() timer + # Heavily optimized dispatch routine for time.process_time() timer def trace_dispatch(self, frame, event, arg): timer = self.timer