From fc5db841e35ef36ff5f3468d6b5595241dc39386 Mon Sep 17 00:00:00 2001 From: Fernando Schauenburg Date: Wed, 26 Feb 2020 16:36:32 +0100 Subject: [PATCH] Clean up globals() for python interactive sessions Previously the global namespace of all interactive python session were polluted with all the stuff we did in this initialization script. For example, globals()['__doc__'] would show the docstring from this file. While none of this was ciritical, it could be confusing sometimes. So this commit fixes that. --- dotfiles/.config/python/startup.py | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/dotfiles/.config/python/startup.py b/dotfiles/.config/python/startup.py index ca9a82a..bb3e542 100644 --- a/dotfiles/.config/python/startup.py +++ b/dotfiles/.config/python/startup.py @@ -1,16 +1,16 @@ -"""Enable default readline configuration on interactive prompts, by -registering a sys.__interactivehook__. - -If the readline module can be imported, the hook will set the Tab key as -completion key and register a history file, using -$XDG_CACHE_HOME/python/history if XDG_CACHE_HOME iset and .python_history -otherwise. -""" -import os -import sys - -def register_readline(): +# Improve interactive prompts by configuring readline. This is done by +# registering a custom hook as sys.__interactivehook__. +# +# If the readline module can be imported, the hook will: +# 1. Set the Tab key as completion key. +# 2. Initialize readline (e.g. from .inputrc). +# 3. Register a history file, using: +# - $XDG_CACHE_HOME/python/history if XDG_CACHE_HOME iset +# - .python_history otherwise. +def configure_readline(): import atexit + import os + try: import readline import rlcompleter @@ -64,5 +64,6 @@ def register_readline(): atexit.register(write_history) -sys.__interactivehook__ = register_readline - +import sys +sys.__interactivehook__ = configure_readline +del sys, configure_readline # we don't want any of this in globals()