diff options
author | David Malcolm <[email protected]> | 2010-02-27 00:57:47 -0500 |
---|---|---|
committer | David Malcolm <[email protected]> | 2010-02-27 00:57:47 -0500 |
commit | 18d4855f7baee27c3f7941e3d092d00951830298 (patch) | |
tree | 9faf918994317ebf4d921ffe0767206b0d862a49 | |
parent | 150a0120ecbbf16091ddcbe1d0bd752619abc352 (diff) | |
download | libpython-18d4855f7baee27c3f7941e3d092d00951830298.tar.gz libpython-18d4855f7baee27c3f7941e3d092d00951830298.tar.xz libpython-18d4855f7baee27c3f7941e3d092d00951830298.zip |
Fix some spacing issues detected by pep8.py
-rw-r--r-- | libpython.py | 91 |
1 files changed, 62 insertions, 29 deletions
diff --git a/libpython.py b/libpython.py index 2f1e0ec..7f13522 100644 --- a/libpython.py +++ b/libpython.py @@ -13,7 +13,7 @@ # Red Hat trademarks are not licensed under LGPLv2.1. No permission is # granted to use or replicate Red Hat trademarks that are incorporated in # this software or its documentation. -# +# # Red Hat Author(s): David Hugh Malcolm <[email protected]> ''' From gdb 7 onwards, gdb's build can be configured --with-python, allowing gdb @@ -49,30 +49,34 @@ TODO: better handling of "instance" import gdb -Py_TPFLAGS_INT_SUBCLASS = (1L<<23) -Py_TPFLAGS_LONG_SUBCLASS = (1L<<24) -Py_TPFLAGS_LIST_SUBCLASS = (1L<<25) -Py_TPFLAGS_TUPLE_SUBCLASS = (1L<<26) -Py_TPFLAGS_STRING_SUBCLASS = (1L<<27) -Py_TPFLAGS_UNICODE_SUBCLASS = (1L<<28) -Py_TPFLAGS_DICT_SUBCLASS = (1L<<29) -Py_TPFLAGS_BASE_EXC_SUBCLASS = (1L<<30) -Py_TPFLAGS_TYPE_SUBCLASS = (1L<<31) +Py_TPFLAGS_INT_SUBCLASS = (1L << 23) +Py_TPFLAGS_LONG_SUBCLASS = (1L << 24) +Py_TPFLAGS_LIST_SUBCLASS = (1L << 25) +Py_TPFLAGS_TUPLE_SUBCLASS = (1L << 26) +Py_TPFLAGS_STRING_SUBCLASS = (1L << 27) +Py_TPFLAGS_UNICODE_SUBCLASS = (1L << 28) +Py_TPFLAGS_DICT_SUBCLASS = (1L << 29) +Py_TPFLAGS_BASE_EXC_SUBCLASS = (1L << 30) +Py_TPFLAGS_TYPE_SUBCLASS = (1L << 31) + class NullPyObjectPtr(RuntimeError): pass + def safety_limit(val): # Given a integer value from the process being debugged, limit it to some # safety threshold so that arbitrary breakage within said process doesn't # break the gdb process too much (e.g. sizes of iterations, sizes of lists) return min(val, 100) + def safe_range(val): # As per range, but don't trust the value too much: cap it to a safety # threshold in case the data was corrupted return xrange(safety_limit(val)) + class PyObjectPtr(object): """ Class wrapping a gdb.Value that's a either a (PyObject*) within the @@ -86,7 +90,7 @@ class PyObjectPtr(object): """ _typename = 'PyObject' - def __init__(self, gdbval, cast_to = None): + def __init__(self, gdbval, cast_to=None): if cast_to: self._gdbval = gdbval.cast(cast_to) else: @@ -99,10 +103,10 @@ class PyObjectPtr(object): Various libpython types are defined using the "PyObject_HEAD" and "PyObject_VAR_HEAD" macros. - - In Python 2, this these are defined so that "ob_type" and (for a var + + In Python 2, this these are defined so that "ob_type" and (for a var object) "ob_size" are fields of the type in question. - + In Python 3, this is defined as an embedded PyVarObject type thus: PyVarObject ob_base; so that the "ob_size" field is located insize the "ob_base" field, and @@ -112,8 +116,9 @@ class PyObjectPtr(object): raise NullPyObjectPtr(self) if name == 'ob_type': - return self._gdbval.cast(PyObjectPtr.get_gdb_type()).dereference()[name] - + pyo_ptr = self._gdbval.cast(PyObjectPtr.get_gdb_type()) + return pyo_ptr.dereference()[name] + if name == 'ob_size': try: # Python 2: @@ -121,7 +126,7 @@ class PyObjectPtr(object): except RuntimeError: # Python 3: return self._gdbval.dereference()['ob_base'][name] - + # General case: look it up inside the object: return self._gdbval.dereference()[name] @@ -142,12 +147,14 @@ class PyObjectPtr(object): For example, a PyIntObject* with ob_ival 42 in the inferior process should result in an int(42) in this process. ''' + class FakeRepr(object): """ Class representing a non-descript PyObject* value in the inferior process for when we don't have a custom scraper, intended to have a sane repr(). """ + def __init__(self, tp_name, address): self.tp_name = tp_name self.address = address @@ -180,9 +187,10 @@ class PyObjectPtr(object): File "<string>", line 1, in <module> NotImplementedError: Symbol type not yet supported in Python scripts. Error while executing Python code. - + For now, we use tp_flags, after doing some string comparisons on the - tp_name for some special-cases that don't seem to be visible through flags + tp_name for some special-cases that don't seem to be visible through + flags ''' try: tp_name = t.field('tp_name').string() @@ -195,7 +203,7 @@ class PyObjectPtr(object): #print 'tp_flags = 0x%08x' % tp_flags #print 'tp_name = %r' % tp_name - name_map = {'bool' : PyBoolObjectPtr, + name_map = {'bool': PyBoolObjectPtr, 'classobj': PyClassObjectPtr, 'instance': PyInstanceObjectPtr, 'NoneType': PyNoneStructPtr, @@ -232,7 +240,6 @@ class PyObjectPtr(object): Try to locate the appropriate derived class dynamically, and cast the pointer accordingly. ''' - # try: p = PyObjectPtr(gdbval) cls = cls.subclass_from_type(p.type()) @@ -247,6 +254,7 @@ class PyObjectPtr(object): def get_gdb_type(cls): return gdb.lookup_type(cls._typename).pointer() + class PyBoolObjectPtr(PyObjectPtr): """ Class wrapping a gdb.Value that's a PyBoolObject* i.e. one of the two @@ -260,6 +268,7 @@ class PyBoolObjectPtr(PyObjectPtr): else: return False + class PyClassObjectPtr(PyObjectPtr): """ Class wrapping a gdb.Value that's a PyClassObject* i.e. a <classobj> @@ -267,6 +276,7 @@ class PyClassObjectPtr(PyObjectPtr): """ _typename = 'PyClassObject' + class PyCodeObjectPtr(PyObjectPtr): """ Class wrapping a gdb.Value that's a PyCodeObject* i.e. a <code> instance @@ -274,6 +284,7 @@ class PyCodeObjectPtr(PyObjectPtr): """ _typename = 'PyCodeObject' + class PyDictObjectPtr(PyObjectPtr): """ Class wrapping a gdb.Value that's a PyDictObject* i.e. a dict instance @@ -283,7 +294,7 @@ class PyDictObjectPtr(PyObjectPtr): def proxyval(self): result = {} - for i in safe_range(self.field('ma_mask')+1): + for i in safe_range(self.field('ma_mask') + 1): ep = self.field('ma_table') + i pvalue = PyObjectPtr.from_pyobject_ptr(ep['me_value']) if not pvalue.is_null(): @@ -291,19 +302,24 @@ class PyDictObjectPtr(PyObjectPtr): result[pkey.proxyval()] = pvalue.proxyval() return result + class PyInstanceObjectPtr(PyObjectPtr): _typename = 'PyInstanceObject' def proxyval(self): + class InstanceProxy(object): + def __init__(self, cl_name, attrdict, address): self.cl_name = cl_name self.attrdict = attrdict self.address = address def __repr__(self): - kwargs = ', '.join(["%s=%r"%(arg, val) for arg, val in self.attrdict.iteritems()]) - return '<%s(%s) at remote 0x%x>' % (self.cl_name, kwargs, self.address) + kwargs = ', '.join(["%s=%r" % (arg, val) + for arg, val in self.attrdict.iteritems()]) + return '<%s(%s) at remote 0x%x>' % (self.cl_name, + kwargs, self.address) # Get name of class: in_class = PyObjectPtr.from_pyobject_ptr(self.field('in_class')) @@ -314,6 +330,7 @@ class PyInstanceObjectPtr(PyObjectPtr): return InstanceProxy(cl_name, in_dict, long(self._gdbval)) + class PyIntObjectPtr(PyObjectPtr): _typename = 'PyIntObject' @@ -321,6 +338,7 @@ class PyIntObjectPtr(PyObjectPtr): result = int_from_int(self.field('ob_ival')) return result + class PyListObjectPtr(PyObjectPtr): _typename = 'PyListObject' @@ -334,6 +352,7 @@ class PyListObjectPtr(PyObjectPtr): for i in safe_range(int_from_int(self.field('ob_size')))] return result + class PyLongObjectPtr(PyObjectPtr): _typename = 'PyLongObject' @@ -345,7 +364,7 @@ class PyLongObjectPtr(PyObjectPtr): digit ob_digit[1]; }; - with this description: + with this description: The absolute value of a number is equal to SUM(for i=0 through abs(ob_size)-1) ob_digit[i] * 2**(SHIFT*i) Negative numbers are represented with ob_size < 0; @@ -367,27 +386,33 @@ class PyLongObjectPtr(PyObjectPtr): # FIXME: I haven't yet tested this case SHIFT = 30L - digits = [long(ob_digit[i]) * 2**(SHIFT*i) for i in safe_range(abs(ob_size))] + digits = [long(ob_digit[i]) * 2**(SHIFT*i) + for i in safe_range(abs(ob_size))] result = sum(digits) if ob_size < 0: result = -result return result + class PyNoneStructPtr(PyObjectPtr): """ Class wrapping a gdb.Value that's a PyObject* pointing to the singleton (we hope) _Py_NoneStruct with ob_type PyNone_Type """ _typename = 'PyObject' + def proxyval(self): return None + class PyFrameObjectPtr(PyObjectPtr): _typename = 'PyFrameObject' + def __str__(self): fi = FrameInfo(self) return str(fi) + class PyStringObjectPtr(PyObjectPtr): _typename = 'PyStringObject' @@ -402,6 +427,7 @@ class PyStringObjectPtr(PyObjectPtr): def proxyval(self): return str(self) + class PyTupleObjectPtr(PyObjectPtr): _typename = 'PyTupleObject' @@ -415,9 +441,11 @@ class PyTupleObjectPtr(PyObjectPtr): for i in safe_range(int_from_int(self.field('ob_size')))]) return result + class PyTypeObjectPtr(PyObjectPtr): _typename = 'PyTypeObject' + class PyUnicodeObjectPtr(PyObjectPtr): _typename = 'PyUnicodeObject' @@ -437,9 +465,11 @@ class PyUnicodeObjectPtr(PyObjectPtr): result = u''.join([unichr(ucs) for ucs in Py_UNICODEs]) return result + def int_from_int(gdbval): return int(str(gdbval)) + def stringify(val): # TODO: repr() puts everything on one line; pformat can be nicer, but # can lead to v.long results; this function isolates the choice @@ -449,6 +479,7 @@ def stringify(val): from pprint import pformat return pformat(val) + class FrameInfo: ''' Class representing all of the information we can scrape about a @@ -473,7 +504,7 @@ class FrameInfo: value = value.proxyval() #print 'value=%s' % value self.locals.append((str(name), value)) - + def __str__(self): return ('File %s, line %i, in %s (%s)' % (self.co_filename, @@ -482,6 +513,7 @@ class FrameInfo: ', '.join(['%s=%s' % (k, stringify(v)) for k, v in self.locals])) ) + class PyObjectPtrPrinter: "Prints a (PyObject*)" @@ -492,6 +524,7 @@ class PyObjectPtrPrinter: proxyval = PyObjectPtr.from_pyobject_ptr(self.gdbval).proxyval() return stringify(proxyval) + class PyFrameObjectPtrPrinter(PyObjectPtrPrinter): "Prints a (PyFrameObject*)" @@ -500,6 +533,7 @@ class PyFrameObjectPtrPrinter(PyObjectPtrPrinter): fi = FrameInfo(pyop) return str(fi) + def pretty_printer_lookup(gdbval): type = gdbval.type.unqualified() if type.code == gdb.TYPE_CODE_PTR: @@ -567,8 +601,7 @@ def pybt(): print fi, except RuntimeError: print '(unable to print python frame; corrupt data?)', - - + for i, gdbframe in enumerate(gdb.selected_thread().frames()): #print dir(gdbframe), gdbframe.name() if 'PyEval_EvalFrameEx' == gdbframe.name(): |