From 99d965635ae2ac4bffdc318ee05b96c59262d165 Mon Sep 17 00:00:00 2001 From: donBarbos Date: Tue, 18 Feb 2025 00:06:08 +0400 Subject: gh-118761: Improve import time of `cmd` module (#130056) * Improve import time of `cmd` module * Remove string import --- Lib/cmd.py | 11 ++++++++--- .../Library/2025-02-13-02-03-38.gh-issue-118761.le_qEg.rst | 2 ++ 2 files changed, 10 insertions(+), 3 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2025-02-13-02-03-38.gh-issue-118761.le_qEg.rst diff --git a/Lib/cmd.py b/Lib/cmd.py index c333e09..438b88a 100644 --- a/Lib/cmd.py +++ b/Lib/cmd.py @@ -42,12 +42,15 @@ listings of documented functions, miscellaneous topics, and undocumented functions respectively. """ -import inspect, string, sys +import sys __all__ = ["Cmd"] PROMPT = '(Cmd) ' -IDENTCHARS = string.ascii_letters + string.digits + '_' +IDENTCHARS = ('ABCDEFGHIJKLMNOPQRSTUVWXYZ' + 'abcdefghijklmnopqrstuvwxyz' + '0123456789' + '_') class Cmd: """A simple framework for writing line-oriented command interpreters. @@ -303,9 +306,11 @@ class Cmd: try: func = getattr(self, 'help_' + arg) except AttributeError: + from inspect import cleandoc + try: doc=getattr(self, 'do_' + arg).__doc__ - doc = inspect.cleandoc(doc) + doc = cleandoc(doc) if doc: self.stdout.write("%s\n"%str(doc)) return diff --git a/Misc/NEWS.d/next/Library/2025-02-13-02-03-38.gh-issue-118761.le_qEg.rst b/Misc/NEWS.d/next/Library/2025-02-13-02-03-38.gh-issue-118761.le_qEg.rst new file mode 100644 index 0000000..4a5b7f6 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-02-13-02-03-38.gh-issue-118761.le_qEg.rst @@ -0,0 +1,2 @@ +Improve import time of :mod:`cmd` by lazy importing :mod:`inspect` and +removing :mod:`string`. Patch by Semyon Moroz. -- cgit v0.12