From 6811218817ccc034161dfe1b14d51eb9edc05579 Mon Sep 17 00:00:00 2001 From: Stephen Finucane Date: Wed, 18 May 2022 12:02:14 +0100 Subject: Defer loading cmd2 We were importing cmd2 purely so we could do some exception transformation. However, this is only needed if we're in interactive mode. Avoid both the import of cmd2 and the transformation of the exceptions unless this is the case. This speeds up import time by ~30% for the demoapp on my machine (~160mS after compared to ~210mS before) Change-Id: I2356dc9803b4d0eef3528c6d057207509932e6b2 Signed-off-by: Stephen Finucane --- cliff/app.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/cliff/app.py b/cliff/app.py index 798b41f..10a4941 100644 --- a/cliff/app.py +++ b/cliff/app.py @@ -20,8 +20,6 @@ import logging.handlers import os import sys -import cmd2 - from cliff import _argparse from . import complete from . import help @@ -403,7 +401,12 @@ class App(object): try: parsed_args = cmd_parser.parse_args(sub_argv) except SystemExit as ex: - raise cmd2.exceptions.Cmd2ArgparseError from ex + if self.interactive_mode: + # Defer importing cmd2 as it is a slow import + import cmd2 + raise cmd2.exceptions.Cmd2ArgparseError from ex + else: + raise ex result = cmd.run(parsed_args) except BrokenPipeError as err1: result = _SIGPIPE_EXIT -- cgit v1.2.1