]> git.phdru.name Git - dotfiles.git/blob - lib/python/pdbrc.py
Fix(bin/cp_recode_fname): Fix misspelled message
[dotfiles.git] / lib / python / pdbrc.py
1 # This is startup file for interactive python debugger pdb.
2 # Use it like this in ~/.pdbrc:
3 # import os
4 # x = execfile("pdbrc.py")
5 # See http://wiki.python.org/moin/PdbRcIdea
6
7 def init():
8     import atexit
9     import os
10     import sys
11     import readline
12
13     # Command line history:
14     histfile = os.path.expanduser("~/.pdb_history")
15
16     try:
17         readline.read_history_file(histfile)
18     except IOError:
19         pass
20
21
22     def savehist(histfile=histfile):
23         import os
24         import readline
25
26         histfilesize = os.environ.get('HISTFILESIZE') \
27             or os.environ.get('HISTSIZE')
28         if histfilesize:
29             try:
30                 histfilesize = int(histfilesize)
31             except ValueError:
32                 pass
33             else:
34                 readline.set_history_length(histfilesize)
35         readline.write_history_file(histfile)
36
37     atexit.register(savehist)
38
39
40     def info(type, value, tb):
41         # return to debugger after fatal exception (Python cookbook 14.5):
42         import pdb
43         import sys
44         import traceback
45
46         if hasattr(sys, 'ps1') or not sys.stderr.isatty():
47             sys.__excepthook__(type, value, tb)
48         traceback.print_exception(type, value, tb)
49         print
50         pdb.pm()
51
52     sys.excepthook = info
53
54 init()
55
56 # https://github.com/giampaolo/sysconf/blob/master/home/.pdbrc.py
57 import pdb
58 import rlcompleter
59 #pdb.Pdb.complete = rlcompleter.Completer(locals()).complete
60
61 # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/498182
62 def complete(self, text, state, rlcompleter=rlcompleter):
63     """return the next possible completion for text, using the current frame's
64        local namespace
65
66        This is called successively with state == 0, 1, 2, ... until it
67        returns None.  The completion should begin with 'text'.
68     """
69     # keep a completer class, and make sure that it uses the current local scope 
70     if not hasattr(self, 'completer'):
71         self.completer = rlcompleter.Completer(self.curframe.f_locals)
72     else:
73         self.completer.namespace = self.curframe.f_locals
74     return self.completer.complete(text, state)
75
76 # Cleanup any variables that could otherwise clutter up the namespace.
77 del pdb, rlcompleter