]> git.phdru.name Git - dotfiles.git/commitdiff
pdbrc.py: Move global code into function init()
authorOleg Broytman <phd@phdru.name>
Fri, 29 Sep 2017 13:14:05 +0000 (16:14 +0300)
committerOleg Broytman <phd@phdru.name>
Fri, 29 Sep 2017 13:14:05 +0000 (16:14 +0300)
lib/python/pdbrc.py

index aeb3db171e85765dbca815d79251a7da4481de39..97bd40115cc971d9f286757e6e961333a1e67dc0 100644 (file)
@@ -1,54 +1,63 @@
-import atexit
-import os
-import sys
-import readline
+# This is startup file for interactive python debugger pdb.
+# Use it like this in ~/.pdbrc:
+# import os
+# x = execfile("pdbrc.py")
+# See http://wiki.python.org/moin/PdbRcIdea
+
+def init():
+    import atexit
+    import os
+    import sys
+    import readline
 
-# Command line history:
-histfile = os.path.expanduser("~/.pdb_history")
+    # Command line history:
+    histfile = os.path.expanduser("~/.pdb_history")
 
-try:
-    readline.read_history_file(histfile)
-except IOError:
-    pass
+    try:
+        readline.read_history_file(histfile)
+    except IOError:
+        pass
 
 
-def savehist(histfile=histfile):
-    import os
-    import readline
+    def savehist(histfile=histfile):
+        import os
+        import readline
 
-    histfilesize = os.environ.get('HISTFILESIZE') \
-        or os.environ.get('HISTSIZE')
-    if histfilesize:
-        try:
-            histfilesize = int(histfilesize)
-        except ValueError:
-            pass
-        else:
-            readline.set_history_length(histfilesize)
-    readline.write_history_file(histfile)
+        histfilesize = os.environ.get('HISTFILESIZE') \
+            or os.environ.get('HISTSIZE')
+        if histfilesize:
+            try:
+                histfilesize = int(histfilesize)
+            except ValueError:
+                pass
+            else:
+                readline.set_history_length(histfilesize)
+        readline.write_history_file(histfile)
 
-atexit.register(savehist)
+    atexit.register(savehist)
 
 
-def info(type, value, tb):
-    # return to debugger after fatal exception (Python cookbook 14.5):
-    import pdb
-    import sys
-    import traceback
+    def info(type, value, tb):
+        # return to debugger after fatal exception (Python cookbook 14.5):
+        import pdb
+        import sys
+        import traceback
 
-    if hasattr(sys, 'ps1') or not sys.stderr.isatty():
-        sys.__excepthook__(type, value, tb)
-    traceback.print_exception(type, value, tb)
-    print
-    pdb.pm()
+        if hasattr(sys, 'ps1') or not sys.stderr.isatty():
+            sys.__excepthook__(type, value, tb)
+        traceback.print_exception(type, value, tb)
+        print
+        pdb.pm()
 
-sys.excepthook = info
+    sys.excepthook = info
 
+init()
 
 # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/498182
+# https://github.com/giampaolo/sysconf/blob/master/home/.pdbrc.py
 import pdb
 import rlcompleter
-pdb.Pdb.complete = rlcompleter.Completer().complete
+pdb.Pdb.complete = rlcompleter.Completer(locals()).complete
 
 # Cleanup any variables that could otherwise clutter up the namespace.
-del atexit, info, os, pdb, readline, rlcompleter, savehist, sys
+del pdb, readline, rlcompleter