Commit 210bd208 authored by Barry Warsaw's avatar Barry Warsaw

Implement a `pp' command, which is like `p' except that it

pretty-prints the value of its expression argument.
parent c2f7757e
...@@ -11,6 +11,7 @@ import bdb ...@@ -11,6 +11,7 @@ import bdb
from repr import Repr from repr import Repr
import os import os
import re import re
import pprint
# Create a custom safe Repr instance and increase its maxstring. # Create a custom safe Repr instance and increase its maxstring.
# The default of 30 truncates error messages too easily. # The default of 30 truncates error messages too easily.
...@@ -532,19 +533,29 @@ class Pdb(bdb.Bdb, cmd.Cmd): ...@@ -532,19 +533,29 @@ class Pdb(bdb.Bdb, cmd.Cmd):
print '*** Not yet returned!' print '*** Not yet returned!'
do_rv = do_retval do_rv = do_retval
def do_p(self, arg): def _getval(self, arg):
try: try:
value = eval(arg, self.curframe.f_globals, return eval(arg, self.curframe.f_globals,
self.curframe.f_locals) self.curframe.f_locals)
except: except:
t, v = sys.exc_info()[:2] t, v = sys.exc_info()[:2]
if type(t) == type(''): if isinstance(t, str):
exc_type_name = t exc_type_name = t
else: exc_type_name = t.__name__ else: exc_type_name = t.__name__
print '***', exc_type_name + ':', `v` print '***', exc_type_name + ':', `v`
return raise
print `value` def do_p(self, arg):
try:
print repr(self._getval(arg))
except:
pass
def do_pp(self, arg):
try:
pprint.pprint(self._getval(arg))
except:
pass
def do_list(self, arg): def do_list(self, arg):
self.lastcmd = 'list' self.lastcmd = 'list'
...@@ -817,6 +828,10 @@ Print the arguments of the current function.""" ...@@ -817,6 +828,10 @@ Print the arguments of the current function."""
print """p expression print """p expression
Print the value of the expression.""" Print the value of the expression."""
def help_pp(self):
print """pp expression
Pretty-print the value of the expression."""
def help_exec(self): def help_exec(self):
print """(!) statement print """(!) statement
Execute the (one-line) statement in the context of Execute the (one-line) statement in the context of
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment