From ea9e0d70899af252297e9603ecf992e8ea0eff6e Mon Sep 17 00:00:00 2001 From: Oleg Broytman Date: Wed, 19 Mar 2025 18:54:17 +0300 Subject: [PATCH] Feat(lib/python/init.py): Stop using old pyreadlinew32 --- lib/python/init.py | 94 ++++++++++++++++++++++------------------------ 1 file changed, 44 insertions(+), 50 deletions(-) diff --git a/lib/python/init.py b/lib/python/init.py index 1d365c6..733e0f2 100644 --- a/lib/python/init.py +++ b/lib/python/init.py @@ -18,64 +18,58 @@ def init(): # readline/pyreadline - pyreadlinew32_startup = os.path.join( - sys.prefix, 'lib', 'site-packages', - 'pyreadline', 'configuration', 'startup.py') + # From Bruce Edge: + # https://mail.python.org/pipermail/python-list/2001-March/062888.html - if os.path.exists(pyreadlinew32_startup): - execfile(pyreadlinew32_startup) - - else: - # From Bruce Edge: - # https://mail.python.org/pipermail/python-list/2001-March/062888.html - - try: - import rlcompleter # noqa: need for completion - import readline - initfile = os.environ.get('INPUTRC') \ - or os.path.expanduser('~/.inputrc') - readline.read_init_file(initfile) - - # if 'libedit' in readline.__doc__: - # readline.parse_and_bind("bind ^I rl_complete") - # else: - # readline.parse_and_bind("tab: complete") - - histfiles = ['~/.python_history'] - # if 'VIRTUAL_ENV' in os.environ: - # histfiles.append('$VIRTUAL_ENV/.python_history') - for histfile in histfiles: - try: - histfile = os.path.expandvars(histfile) - histfile = os.path.expanduser(histfile) - readline.read_history_file(histfile) - except IOError: - pass # No such file - - def savehist(): - histsize = os.environ.get('HISTSIZE') - if histsize: - try: - histsize = int(histsize) - except ValueError: - pass - else: - readline.set_history_length(histsize) - histfile = histfiles[-1] + try: + import rlcompleter # noqa: need for completion + import readline + initfile = os.environ.get('INPUTRC') \ + or os.path.expanduser('~/.inputrc') + readline.read_init_file(initfile) + + # if 'libedit' in readline.__doc__: + # readline.parse_and_bind("bind ^I rl_complete") + # else: + # readline.parse_and_bind("tab: complete") + + histfiles = ['~/.python_history'] + # if 'VIRTUAL_ENV' in os.environ: + # histfiles.append('$VIRTUAL_ENV/.python_history') + for histfile in histfiles: + try: histfile = os.path.expandvars(histfile) histfile = os.path.expanduser(histfile) + readline.read_history_file(histfile) + except IOError: + pass # No such file + + def savehist(): + histsize = os.environ.get('HISTSIZE') + if histsize: try: - readline.write_history_file(histfile) - except IOError: + histsize = int(histsize) + except ValueError: pass + else: + readline.set_history_length(histsize) + histfile = histfiles[-1] + histfile = os.path.expandvars(histfile) + histfile = os.path.expanduser(histfile) + try: + readline.write_history_file(histfile) + except IOError: + pass + + import atexit + atexit.register(savehist) import atexit - atexit.register(savehist) - except (ImportError, AttributeError): - # no readline or atexit, or readline doesn't have - # {read,write}_history_file - ignore the error - pass + except (ImportError, AttributeError): + # no readline or atexit, or readline doesn't have + # {read,write}_history_file - ignore the error + pass # terminal -- 2.39.5