]> git.phdru.name Git - dotfiles.git/commitdiff
Feat(lib/python/init.py): Stop using old pyreadlinew32
authorOleg Broytman <phd@phdru.name>
Wed, 19 Mar 2025 15:54:17 +0000 (18:54 +0300)
committerOleg Broytman <phd@phdru.name>
Wed, 19 Mar 2025 15:54:17 +0000 (18:54 +0300)
lib/python/init.py

index 1d365c62f9214c649be8f4dc65268a619865457c..733e0f2a4d30651a7fa8af41c05a4170dfebfb58 100644 (file)
@@ -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