chiark / gitweb /
dot/ipython-key-bindings.py: Don't leak names into the toplevel environment.
authorMark Wooding <mdw@distorted.org.uk>
Mon, 4 May 2020 00:00:51 +0000 (01:00 +0100)
committerMark Wooding <mdw@distorted.org.uk>
Mon, 4 May 2020 00:15:29 +0000 (01:15 +0100)
It seems that this file, unlike the `ipython-config.py', is evaluated in
the same toplevel environment as is used for the interactive session.  I
initially tried to do this by deleting all the names after I'd finished
using them, but the `inhibit_history_search' function is looked up by
name from the `prev_line' and `next_line' bindings, so that doesn't
work.  Instead, lambda-bind everything by wrapping a function around the
whole lot; the evaluate the function to make everything happen, and
delete the function name, leaving the bindings functions in its orphaned
environment.

dot/ipython-key-bindings.py

index 558d335cf1e333a44e074db1c43784ca1c9b37bb..8795404d519adba26e893b329bd2893a0b26d548 100644 (file)
@@ -1,19 +1,21 @@
 ### -*-python -*-
 
-import IPython as IPY
-import prompt_toolkit as PTK
-
-def ding():
-  with open('/dev/tty', 'w') as f: f.write('\a')
-
-## Key bindings.  Alas, IPython's attempt at Emacs keybindings is abysmal.
-K = PTK.keys.Keys
-F = PTK.filters
-BUF = PTK.enums.DEFAULT_BUFFER
-ipy = IPY.get_ipython()
-try: pt = ipy.pt_cli
-except AttributeError: pass
-else:
+def __mdw_hack_bindings():
+
+  import IPython as IPY
+  import prompt_toolkit as PTK
+
+  def ding():
+    with open('/dev/tty', 'w') as f: f.write('\a')
+
+  ## Key bindings.  Alas, IPython's attempt at Emacs keybindings is abysmal.
+  K = PTK.keys.Keys
+  F = PTK.filters
+  BUF = PTK.enums.DEFAULT_BUFFER
+  ipy = IPY.get_ipython()
+  try: pt = ipy.pt_cli
+  except AttributeError: return
+
   reg = pt.application.key_bindings_registry
   try: bind = reg.add_binding
   except AttributeError:
@@ -45,3 +47,6 @@ else:
 
   bind(K.Escape, u'p')(lambda ev: ev.current_buffer.history_backward())
   bind(K.Escape, u'n')(lambda ev: ev.current_buffer.history_forward())
+
+__mdw_hack_bindings()
+del __mdw_hack_bindings