Commit b465df68 authored by Mark Florisson's avatar Mark Florisson

Add py-globals command

Align the values of py-locals, py-globals, cy locals and cy globals according to the maximum length of the variable names
parent ef747e9a
...@@ -248,9 +248,9 @@ class CythonBase(object): ...@@ -248,9 +248,9 @@ class CythonBase(object):
return False return False
def print_cython_var_if_initialized(self, varname): def print_cython_var_if_initialized(self, varname, max_name_length=None):
try: try:
self.cy.print_.invoke(varname, True) self.cy.print_.invoke(varname, True, max_name_length)
except gdb.GdbError: except gdb.GdbError:
# variable not initialized yet # variable not initialized yet
pass pass
...@@ -731,13 +731,18 @@ class CyPrint(CythonCommand): ...@@ -731,13 +731,18 @@ class CyPrint(CythonCommand):
command_class = gdb.COMMAND_DATA command_class = gdb.COMMAND_DATA
@dispatch_on_frame(c_command='print', python_command='py-print') @dispatch_on_frame(c_command='print', python_command='py-print')
def invoke(self, name, from_tty): def invoke(self, name, from_tty, max_name_length=None):
cname = self.cy.cy_cname.invoke(name, string=True) cname = self.cy.cy_cname.invoke(name, string=True)
try: try:
print '%s = %s' % (name, gdb.parse_and_eval(cname)) value = gdb.parse_and_eval(cname)
except RuntimeError, e: except RuntimeError, e:
raise gdb.GdbError("Variable %s is not initialized yet." % (name,)) raise gdb.GdbError("Variable %s is not initialized yet." % (name,))
else:
if max_name_length is None:
print '%s = %s' % (name, value)
else:
print '%-*s = %s' % (max_name_length, name, value)
def complete(self): def complete(self):
if self.is_cython_function(): if self.is_cython_function():
f = self.get_cython_function() f = self.get_cython_function()
...@@ -757,8 +762,10 @@ class CyLocals(CythonCommand): ...@@ -757,8 +762,10 @@ class CyLocals(CythonCommand):
@dispatch_on_frame(c_command='info locals', python_command='py-locals') @dispatch_on_frame(c_command='info locals', python_command='py-locals')
def invoke(self, args, from_tty): def invoke(self, args, from_tty):
for varname in self.get_cython_function().locals: local_cython_vars = self.get_cython_function().locals
self.print_cython_var_if_initialized(varname) max_name_length = len(max(local_cython_vars, key=len))
for varname in local_cython_vars:
self.print_cython_var_if_initialized(varname, max_name_length)
class CyGlobals(CythonCommand): class CyGlobals(CythonCommand):
...@@ -783,10 +790,15 @@ class CyGlobals(CythonCommand): ...@@ -783,10 +790,15 @@ class CyGlobals(CythonCommand):
""")) """))
m = m.cast(PyModuleObject.pointer()) m = m.cast(PyModuleObject.pointer())
d = libpython.PyObjectPtr.from_pyobject_ptr(m['md_dict']) pyobject_dict = libpython.PyObjectPtr.from_pyobject_ptr(m['md_dict'])
module_globals = self.get_cython_function().module.globals
# - 2 for the surrounding quotes
max_name_length = max(len(max(module_globals, key=len)),
len(max(pyobject_dict.iteritems())) - 2)
seen = set() seen = set()
for k, v in d.iteritems(): for k, v in pyobject_dict.iteritems():
# Note: k and v are values in the inferior, they are # Note: k and v are values in the inferior, they are
# libpython.PyObjectPtr objects # libpython.PyObjectPtr objects
...@@ -796,12 +808,12 @@ class CyGlobals(CythonCommand): ...@@ -796,12 +808,12 @@ class CyGlobals(CythonCommand):
v = v.get_truncated_repr(libpython.MAX_OUTPUT_LEN) v = v.get_truncated_repr(libpython.MAX_OUTPUT_LEN)
seen.add(k) seen.add(k)
print '%s = %s' % (k, v) print '%-*s = %s' % (max_name_length, k, v)
module_globals = self.get_cython_function().module.globals
for varname in seen.symmetric_difference(module_globals): for varname in seen.symmetric_difference(module_globals):
self.print_cython_var_if_initialized(varname) self.print_cython_var_if_initialized(varname, max_name_length)
# Functions # Functions
class CyCName(gdb.Function, CythonBase): class CyCName(gdb.Function, CythonBase):
......
...@@ -1427,12 +1427,6 @@ PyPrint() ...@@ -1427,12 +1427,6 @@ PyPrint()
class PyLocals(gdb.Command): class PyLocals(gdb.Command):
'Look up the given python variable name, and print it' 'Look up the given python variable name, and print it'
def __init__(self):
gdb.Command.__init__ (self,
"py-locals",
gdb.COMMAND_DATA,
gdb.COMPLETE_NONE)
def invoke(self, args, from_tty): def invoke(self, args, from_tty):
name = str(args) name = str(args)
...@@ -1447,12 +1441,29 @@ class PyLocals(gdb.Command): ...@@ -1447,12 +1441,29 @@ class PyLocals(gdb.Command):
print 'Unable to read information on python frame' print 'Unable to read information on python frame'
return return
for pyop_name, pyop_value in pyop_frame.iter_locals(): namespace = self.get_namespace(pyop_frame)
print ('%s = %s' namespace = [(name.proxyval(set()), val) for name, val in namespace]
% (pyop_name.proxyval(set()),
pyop_value.get_truncated_repr(MAX_OUTPUT_LEN))) name, val = max(namespace, key=lambda (name, val): len(name))
max_name_length = len(name)
for name, pyop_value in namespace:
value = pyop_value.get_truncated_repr(MAX_OUTPUT_LEN)
print ('%-*s = %s' % (max_name_length, name, value))
def get_namespace(self, pyop_frame):
return pyop_frame.iter_locals()
class PyGlobals(PyLocals):
'List all the globals in the currently select Python frame'
def get_namespace(self, pyop_frame):
return pyop_frame.iter_globals()
PyLocals() PyLocals("py-locals", gdb.COMMAND_DATA, gdb.COMPLETE_NONE)
PyGlobals("py-globals", gdb.COMMAND_DATA, gdb.COMPLETE_NONE)
class _LoggingState(object): class _LoggingState(object):
......
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