Fix PEP8 and formatting issues in GDB pretty printer.

This commit is contained in:
Kolja Brix 2021-08-26 15:22:28 +00:00 committed by Rasmus Munk Larsen
parent 9abf4d0bec
commit a032397ae4

View File

@ -30,15 +30,16 @@ import re
import itertools import itertools
from bisect import bisect_left from bisect import bisect_left
# Basic row/column iteration code for use with Sparse and Dense matrices # Basic row/column iteration code for use with Sparse and Dense matrices
class _MatrixEntryIterator(object): class _MatrixEntryIterator(object):
def __init__ (self, rows, cols, rowMajor): def __init__(self, rows, cols, row_major):
self.rows = rows self.rows = rows
self.cols = cols self.cols = cols
self.currentRow = 0 self.currentRow = 0
self.currentCol = 0 self.currentCol = 0
self.rowMajor = rowMajor self.rowMajor = row_major
def __iter__(self): def __iter__(self):
return self return self
@ -66,41 +67,42 @@ class _MatrixEntryIterator(object):
self.currentCol = 0 self.currentCol = 0
self.currentRow = self.currentRow + 1 self.currentRow = self.currentRow + 1
return (row, col) return row, col
class EigenMatrixPrinter: class EigenMatrixPrinter:
"Print Eigen Matrix or Array of some kind" """Print Eigen Matrix or Array of some kind"""
def __init__(self, variety, val): def __init__(self, variety, val):
"Extract all the necessary information" """Extract all the necessary information"""
# Save the variety (presumably "Matrix" or "Array") for later usage # Save the variety (presumably "Matrix" or "Array") for later usage
self.variety = variety self.variety = variety
# The gdb extension does not support value template arguments - need to extract them by hand # The gdb extension does not support value template arguments - need to extract them by hand
type = val.type typeinfo = val.type
if type.code == gdb.TYPE_CODE_REF: if typeinfo.code == gdb.TYPE_CODE_REF:
type = type.target() typeinfo = typeinfo.target()
self.type = type.unqualified().strip_typedefs() self.type = typeinfo.unqualified().strip_typedefs()
tag = self.type.tag tag = self.type.tag
regex = re.compile('\<.*\>') regex = re.compile('<.*>')
m = regex.findall(tag)[0][1:-1] m = regex.findall(tag)[0][1:-1]
template_params = m.split(',') template_params = m.split(',')
template_params = [x.replace(" ", "") for x in template_params] template_params = [x.replace(" ", "") for x in template_params]
if template_params[1] == '-0x00000000000000001' or template_params[1] == '-0x000000001' or template_params[1] == '-1': if template_params[1] in ['-0x00000000000000001', '-0x000000001', '-1']:
self.rows = val['m_storage']['m_rows'] self.rows = val['m_storage']['m_rows']
else: else:
self.rows = int(template_params[1]) self.rows = int(template_params[1])
if template_params[2] == '-0x00000000000000001' or template_params[2] == '-0x000000001' or template_params[2] == '-1': if template_params[2] in ['-0x00000000000000001', '-0x000000001', '-1']:
self.cols = val['m_storage']['m_cols'] self.cols = val['m_storage']['m_cols']
else: else:
self.cols = int(template_params[2]) self.cols = int(template_params[2])
self.options = 0 # default value self.options = 0 # default value
if len(template_params) > 3: if len(template_params) > 3:
self.options = template_params[3]; self.options = template_params[3]
self.rowMajor = (int(self.options) & 0x1) self.rowMajor = (int(self.options) & 0x1)
@ -114,50 +116,52 @@ class EigenMatrixPrinter:
self.data = self.data['array'] self.data = self.data['array']
self.data = self.data.cast(self.innerType.pointer()) self.data = self.data.cast(self.innerType.pointer())
class _iterator(_MatrixEntryIterator): class _Iterator(_MatrixEntryIterator):
def __init__ (self, rows, cols, dataPtr, rowMajor): def __init__(self, rows, cols, data_ptr, row_major):
super(EigenMatrixPrinter._iterator, self).__init__(rows, cols, rowMajor) super(EigenMatrixPrinter._Iterator, self).__init__(rows, cols, row_major)
self.dataPtr = dataPtr self.dataPtr = data_ptr
def __next__(self): def __next__(self):
row, col = super(EigenMatrixPrinter._Iterator, self).__next__()
row, col = super(EigenMatrixPrinter._iterator, self).__next__()
item = self.dataPtr.dereference() item = self.dataPtr.dereference()
self.dataPtr = self.dataPtr + 1 self.dataPtr = self.dataPtr + 1
if (self.cols == 1): #if it's a column vector if self.cols == 1: # if it's a column vector
return ('[%d]' % (row,), item) return '[%d]' % (row,), item
elif (self.rows == 1): #if it's a row vector elif self.rows == 1: # if it's a row vector
return ('[%d]' % (col,), item) return '[%d]' % (col,), item
return ('[%d,%d]' % (row, col), item) return '[%d,%d]' % (row, col), item
def children(self): def children(self):
return self._iterator(self.rows, self.cols, self.data, self.rowMajor) return self._Iterator(self.rows, self.cols, self.data, self.rowMajor)
def to_string(self): def to_string(self):
return "Eigen::%s<%s,%d,%d,%s> (data ptr: %s)" % (self.variety, self.innerType, self.rows, self.cols, "RowMajor" if self.rowMajor else "ColMajor", self.data) return "Eigen::%s<%s,%d,%d,%s> (data ptr: %s)" % (
self.variety, self.innerType, self.rows, self.cols,
"RowMajor" if self.rowMajor else "ColMajor", self.data)
class EigenSparseMatrixPrinter: class EigenSparseMatrixPrinter:
"Print an Eigen SparseMatrix" """Print an Eigen SparseMatrix"""
def __init__(self, val): def __init__(self, val):
"Extract all the necessary information" """Extract all the necessary information"""
type = val.type typeinfo = val.type
if type.code == gdb.TYPE_CODE_REF: if typeinfo.code == gdb.TYPE_CODE_REF:
type = type.target() typeinfo = typeinfo.target()
self.type = type.unqualified().strip_typedefs() self.type = typeinfo.unqualified().strip_typedefs()
tag = self.type.tag tag = self.type.tag
regex = re.compile('\<.*\>') regex = re.compile('<.*>')
m = regex.findall(tag)[0][1:-1] m = regex.findall(tag)[0][1:-1]
template_params = m.split(',') template_params = m.split(',')
template_params = [x.replace(" ", "") for x in template_params] template_params = [x.replace(" ", "") for x in template_params]
self.options = 0 self.options = 0
if len(template_params) > 1: if len(template_params) > 1:
self.options = template_params[1]; self.options = template_params[1]
self.rowMajor = (int(self.options) & 0x1) self.rowMajor = (int(self.options) & 0x1)
@ -168,22 +172,23 @@ class EigenSparseMatrixPrinter:
self.data = self.val['m_data'] self.data = self.val['m_data']
self.data = self.data.cast(self.innerType.pointer()) self.data = self.data.cast(self.innerType.pointer())
class _iterator(_MatrixEntryIterator): class _Iterator(_MatrixEntryIterator):
def __init__ (self, rows, cols, val, rowMajor): def __init__(self, rows, cols, val, row_major):
super(EigenSparseMatrixPrinter._iterator, self).__init__(rows, cols, rowMajor) super(EigenSparseMatrixPrinter._Iterator, self).__init__(rows, cols, row_major)
self.val = val self.val = val
def __next__(self): def __next__(self):
row, col = super(EigenSparseMatrixPrinter._Iterator, self).__next__()
row, col = super(EigenSparseMatrixPrinter._iterator, self).__next__()
# repeat calculations from SparseMatrix.h: # repeat calculations from SparseMatrix.h:
outer = row if self.rowMajor else col outer = row if self.rowMajor else col
inner = col if self.rowMajor else row inner = col if self.rowMajor else row
start = self.val['m_outerIndex'][outer] start = self.val['m_outerIndex'][outer]
end = ((start + self.val['m_innerNonZeros'][outer]) if self.val['m_innerNonZeros'] else end = (
self.val['m_outerIndex'][outer+1]) (start + self.val['m_innerNonZeros'][outer])
if self.val['m_innerNonZeros'] else self.val['m_outerIndex'][outer+1]
)
# and from CompressedStorage.h: # and from CompressedStorage.h:
data = self.val['m_data'] data = self.val['m_data']
@ -196,20 +201,19 @@ class EigenSparseMatrixPrinter:
indices = [data['m_indices'][x] for x in range(int(start), int(end)-1)] indices = [data['m_indices'][x] for x in range(int(start), int(end)-1)]
# find the index with binary search # find the index with binary search
idx = int(start) + bisect_left(indices, inner) idx = int(start) + bisect_left(indices, inner)
if ((idx < end) and (data['m_indices'][idx] == inner)): if idx < end and data['m_indices'][idx] == inner:
item = data['m_values'][idx] item = data['m_values'][idx]
else: else:
item = 0 item = 0
return ('[%d,%d]' % (row, col), item) return '[%d,%d]' % (row, col), item
def children(self): def children(self):
if self.data: if self.data:
return self._iterator(self.rows(), self.cols(), self.val, self.rowMajor) return self._Iterator(self.rows(), self.cols(), self.val, self.rowMajor)
return iter([]) # empty matrix, for now return iter([]) # empty matrix, for now
def rows(self): def rows(self):
return self.val['m_outerSize'] if self.rowMajor else self.val['m_innerSize'] return self.val['m_outerSize'] if self.rowMajor else self.val['m_innerSize']
@ -228,16 +232,17 @@ class EigenSparseMatrixPrinter:
return "Eigen::SparseMatrix<%s>, %s, %s major, %s" % ( return "Eigen::SparseMatrix<%s>, %s, %s major, %s" % (
self.innerType, dimensions, layout, status) self.innerType, dimensions, layout, status)
class EigenQuaternionPrinter: class EigenQuaternionPrinter:
"Print an Eigen Quaternion" """Print an Eigen Quaternion"""
def __init__(self, val): def __init__(self, val):
"Extract all the necessary information" """Extract all the necessary information"""
# The gdb extension does not support value template arguments - need to extract them by hand # The gdb extension does not support value template arguments - need to extract them by hand
type = val.type typeinfo = val.type
if type.code == gdb.TYPE_CODE_REF: if typeinfo.code == gdb.TYPE_CODE_REF:
type = type.target() typeinfo = typeinfo.target()
self.type = type.unqualified().strip_typedefs() self.type = typeinfo.unqualified().strip_typedefs()
self.innerType = self.type.template_argument(0) self.innerType = self.type.template_argument(0)
self.val = val self.val = val
@ -245,9 +250,9 @@ class EigenQuaternionPrinter:
self.data = self.val['m_coeffs']['m_storage']['m_data']['array'] self.data = self.val['m_coeffs']['m_storage']['m_data']['array']
self.data = self.data.cast(self.innerType.pointer()) self.data = self.data.cast(self.innerType.pointer())
class _iterator: class _Iterator:
def __init__ (self, dataPtr): def __init__(self, data_ptr):
self.dataPtr = dataPtr self.dataPtr = data_ptr
self.currentElement = 0 self.currentElement = 0
self.elementNames = ['x', 'y', 'z', 'w'] self.elementNames = ['x', 'y', 'z', 'w']
@ -260,18 +265,18 @@ class EigenQuaternionPrinter:
def __next__(self): def __next__(self):
element = self.currentElement element = self.currentElement
if self.currentElement >= 4: #there are 4 elements in a quanternion if self.currentElement >= 4: # there are 4 elements in a quaternion
raise StopIteration raise StopIteration
self.currentElement = self.currentElement + 1 self.currentElement = self.currentElement + 1
item = self.dataPtr.dereference() item = self.dataPtr.dereference()
self.dataPtr = self.dataPtr + 1 self.dataPtr = self.dataPtr + 1
return ('[%s]' % (self.elementNames[element],), item) return '[%s]' % (self.elementNames[element],), item
def children(self): def children(self):
return self._iterator(self.data) return self._Iterator(self.data)
def to_string(self): def to_string(self):
return "Eigen::Quaternion<%s> (data ptr: %s)" % (self.innerType, self.data) return "Eigen::Quaternion<%s> (data ptr: %s)" % (self.innerType, self.data)
@ -301,25 +306,27 @@ def build_eigen_dictionary ():
pretty_printers_dict[re.compile('^Eigen::SparseMatrix<.*>$')] = lambda val: EigenSparseMatrixPrinter(val) pretty_printers_dict[re.compile('^Eigen::SparseMatrix<.*>$')] = lambda val: EigenSparseMatrixPrinter(val)
pretty_printers_dict[re.compile('^Eigen::Array<.*>$')] = lambda val: EigenMatrixPrinter("Array", val) pretty_printers_dict[re.compile('^Eigen::Array<.*>$')] = lambda val: EigenMatrixPrinter("Array", val)
def register_eigen_printers(obj):
"Register eigen pretty-printers with objfile Obj"
if obj == None: def register_eigen_printers(obj):
"""Register eigen pretty-printers with objfile Obj"""
if obj is None:
obj = gdb obj = gdb
obj.pretty_printers.append(lookup_function) obj.pretty_printers.append(lookup_function)
def lookup_function(val): def lookup_function(val):
"Look-up and return a pretty-printer that can print va." """Look-up and return a pretty-printer that can print val."""
type = val.type typeinfo = val.type
if type.code == gdb.TYPE_CODE_REF: if typeinfo.code == gdb.TYPE_CODE_REF:
type = type.target() typeinfo = typeinfo.target()
type = type.unqualified().strip_typedefs() typeinfo = typeinfo.unqualified().strip_typedefs()
typename = type.tag typename = typeinfo.tag
if typename == None: if typename is None:
return None return None
for function in pretty_printers_dict: for function in pretty_printers_dict:
@ -328,6 +335,7 @@ def lookup_function(val):
return None return None
pretty_printers_dict = {} pretty_printers_dict = {}
build_eigen_dictionary() build_eigen_dictionary()