diff -r 4cdb9f04494b Tools/gdb/libpython.py --- a/Tools/gdb/libpython.py Sun Oct 20 20:31:43 2013 +0200 +++ b/Tools/gdb/libpython.py Sun Oct 20 18:05:22 2013 -0700 @@ -40,11 +40,21 @@ The module also extends gdb with some python-specific commands. ''' -from __future__ import with_statement + +# NOTE: some gdbs are linked with Python 3, so this file should be dual-syntax +# compatible (2.6+ and 3.0+). See #19308. + +from __future__ import print_function, with_statement import gdb import locale import sys +if sys.version_info[0] >= 3: + unicode = str + unichr = chr + xrange = range + long = int + # Look up the gdb.Type for some standard types: _type_char_ptr = gdb.lookup_type('char').pointer() # char* _type_unsigned_char_ptr = gdb.lookup_type('unsigned char').pointer() # unsigned char* @@ -58,23 +68,27 @@ SIZEOF_VOID_P = _type_void_ptr.sizeof -Py_TPFLAGS_HEAPTYPE = (1L << 9) +Py_TPFLAGS_HEAPTYPE = (1 << 9) -Py_TPFLAGS_LONG_SUBCLASS = (1L << 24) -Py_TPFLAGS_LIST_SUBCLASS = (1L << 25) -Py_TPFLAGS_TUPLE_SUBCLASS = (1L << 26) -Py_TPFLAGS_BYTES_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_LONG_SUBCLASS = (1 << 24) +Py_TPFLAGS_LIST_SUBCLASS = (1 << 25) +Py_TPFLAGS_TUPLE_SUBCLASS = (1 << 26) +Py_TPFLAGS_BYTES_SUBCLASS = (1 << 27) +Py_TPFLAGS_UNICODE_SUBCLASS = (1 << 28) +Py_TPFLAGS_DICT_SUBCLASS = (1 << 29) +Py_TPFLAGS_BASE_EXC_SUBCLASS = (1 << 30) +Py_TPFLAGS_TYPE_SUBCLASS = (1 << 31) MAX_OUTPUT_LEN=1024 hexdigits = "0123456789abcdef" -ENCODING = locale.getpreferredencoding() +if sys.version_info[:2] >= (3, 1): + # Python 3.1 and above use UTF-8 for C-strings + ENCODING = 'utf-8' +else: + ENCODING = locale.getpreferredencoding() class NullPyObjectPtr(RuntimeError): pass @@ -90,7 +104,7 @@ 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)) + return xrange(safety_limit(int(val))) def write_unicode(file, text): # Write a byte or unicode string to file. Unicode strings are encoded to @@ -98,7 +112,7 @@ # UnicodeEncodeError. if isinstance(text, unicode): text = text.encode(ENCODING, 'backslashreplace') - file.write(text) + file.write(unicode(text, ENCODING)) def os_fsencode(filename): if not isinstance(filename, unicode): @@ -115,7 +129,7 @@ else: byte = char.encode(encoding) encoded.append(byte) - return ''.join(encoded) + return b''.join(encoded) class StringTruncated(RuntimeError): pass @@ -322,8 +336,8 @@ # class return cls - #print 'tp_flags = 0x%08x' % tp_flags - #print 'tp_name = %r' % tp_name + #print('tp_flags = 0x%08x' % tp_flags) + #print('tp_name = %r' % tp_name) name_map = {'bool': PyBoolObjectPtr, 'classobj': PyClassObjectPtr, @@ -733,14 +747,14 @@ ''' ob_size = long(self.field('ob_size')) if ob_size == 0: - return 0L + return 0 ob_digit = self.field('ob_digit') if gdb.lookup_type('digit').sizeof == 2: - SHIFT = 15L + SHIFT = 15 else: - SHIFT = 30L + SHIFT = 30 digits = [long(ob_digit[i]) * 2**(SHIFT*i) for i in safe_range(abs(ob_size))] @@ -1373,7 +1387,7 @@ Not all builds have a gdb.Frame.select method; seems to be present on Fedora 12 onwards, but absent on Ubuntu buildbot''' if not hasattr(self._gdbframe, 'select'): - print ('Unable to select frame: ' + print('Unable to select frame: ' 'this build of gdb does not expose a gdb.Frame.select method') return False self._gdbframe.select() @@ -1595,12 +1609,12 @@ # py-list requires an actual PyEval_EvalFrameEx frame: frame = Frame.get_selected_bytecode_frame() if not frame: - print 'Unable to locate gdb frame for python bytecode interpreter' + print('Unable to locate gdb frame for python bytecode interpreter') return pyop = frame.get_pyop() if not pyop or pyop.is_optimized_out(): - print 'Unable to read information on python frame' + print('Unable to read information on python frame') return filename = pyop.filename() @@ -1656,9 +1670,9 @@ frame = iter_frame if move_up: - print 'Unable to find an older python frame' + print('Unable to find an older python frame') else: - print 'Unable to find a newer python frame' + print('Unable to find a newer python frame') class PyUp(gdb.Command): 'Select and print the python stack frame that called this one (if any)' @@ -1740,23 +1754,23 @@ frame = Frame.get_selected_python_frame() if not frame: - print 'Unable to locate python frame' + print('Unable to locate python frame') return pyop_frame = frame.get_pyop() if not pyop_frame: - print 'Unable to read information on python frame' + print('Unable to read information on python frame') return pyop_var, scope = pyop_frame.get_var_by_name(name) if pyop_var: - print ('%s %r = %s' + print('%s %r = %s' % (scope, name, pyop_var.get_truncated_repr(MAX_OUTPUT_LEN))) else: - print '%r not found' % name + print('%r not found' % name) PyPrint() @@ -1774,16 +1788,16 @@ frame = Frame.get_selected_python_frame() if not frame: - print 'Unable to locate python frame' + print('Unable to locate python frame') return pyop_frame = frame.get_pyop() if not pyop_frame: - print 'Unable to read information on python frame' + print('Unable to read information on python frame') return for pyop_name, pyop_value in pyop_frame.iter_locals(): - print ('%s = %s' + print('%s = %s' % (pyop_name.proxyval(set()), pyop_value.get_truncated_repr(MAX_OUTPUT_LEN)))