1
# DP: libstdc++ pretty-printers: Backport Python 3 support from mainline
3
2014-08-13 Samuel Bronson <naesten@gmail.com>
5
Backport r212453 from trunk
6
2014-07-11 Samuel Bronson <naesten@gmail.com>
7
Matthias Klose <doko@ubuntu.com>
10
* python/libstdcxx/v6/printers.py: Port to Python 2+3
11
(imap): New compat function.
13
(Iterator): New mixin to allow writing iterators in Python 3 style
14
regardless of which version we're running on.
15
[Python3] (long) New compat alias for "int".
16
* testsuite/lib/gdb-test.exp: Port to Python 2+3 (print syntax)
18
Backport r210625 from trunk
19
2014-05-19 Jonathan Wakely <jwakely@redhat.com>
21
* python/libstdcxx/v6/printers.py: Use Python3 raise syntax.
23
--- a/src/libstdc++-v3/python/libstdcxx/v6/printers.py
24
+++ b/src/libstdc++-v3/python/libstdcxx/v6/printers.py
26
-# Pretty-printers for libstc++.
27
+# Pretty-printers for libstdc++.
29
# Copyright (C) 2008-2013 Free Software Foundation, Inc.
37
+### Python 2 + Python 3 compatibility code
39
+# Resources about compatibility:
41
+# * <http://pythonhosted.org/six/>: Documentation of the "six" module
43
+# FIXME: The handling of e.g. std::basic_string (at least on char)
44
+# probably needs updating to work with Python 3's new string rules.
46
+# In particular, Python 3 has a separate type (called byte) for
47
+# bytestrings, and a special b"" syntax for the byte literals; the old
48
+# str() type has been redefined to always store Unicode text.
50
+# We probably can't do much about this until this GDB PR is addressed:
51
+# <https://sourceware.org/bugzilla/show_bug.cgi?id=17138>
53
+if sys.version_info[0] > 2:
56
+ # Python 3 folds these into the normal functions.
59
+ # Also, int subsumes long
64
+ """Compatibility mixin for iterators
66
+ Instead of writing next() methods for iterators, write
67
+ __next__() methods and use this mixin to make them work in
68
+ Python 2 as well as Python 3.
70
+ Idea stolen from the "six" documentation:
71
+ <http://pythonhosted.org/six/#six.Iterator>
75
+ return self.__next__()
77
+ # In Python 2, we still need these from itertools
78
+ from itertools import imap, izip
80
# Try to use the new-style pretty-printing if available.
82
@@ -51,7 +95,7 @@ def find_type(orig, name):
83
# anything fancier here.
84
field = typ.fields()[0]
85
if not field.is_base_class:
86
- raise ValueError, "Cannot find type %s::%s" % (str(orig), name)
87
+ raise ValueError("Cannot find type %s::%s" % (str(orig), name))
90
class SharedPointerPrinter:
91
@@ -87,7 +131,7 @@ class UniquePointerPrinter:
96
+ class _iterator(Iterator):
97
def __init__(self, nodetype, head):
98
self.nodetype = nodetype
99
self.base = head['_M_next']
100
@@ -97,7 +141,7 @@ class StdListPrinter:
105
+ def __next__(self):
106
if self.base == self.head:
108
elt = self.base.cast(self.nodetype).dereference()
109
@@ -135,7 +179,7 @@ class StdListIteratorPrinter:
110
class StdSlistPrinter:
111
"Print a __gnu_cxx::slist"
114
+ class _iterator(Iterator):
115
def __init__(self, nodetype, head):
116
self.nodetype = nodetype
117
self.base = head['_M_head']['_M_next']
118
@@ -144,7 +188,7 @@ class StdSlistPrinter:
123
+ def __next__(self):
126
elt = self.base.cast(self.nodetype).dereference()
127
@@ -180,7 +224,7 @@ class StdSlistIteratorPrinter:
128
class StdVectorPrinter:
129
"Print a std::vector"
132
+ class _iterator(Iterator):
133
def __init__ (self, start, finish, bitvec):
136
@@ -198,7 +242,7 @@ class StdVectorPrinter:
141
+ def __next__(self):
143
self.count = self.count + 1
145
@@ -265,7 +309,7 @@ class StdVectorIteratorPrinter:
146
class StdTuplePrinter:
150
+ class _iterator(Iterator):
151
def __init__ (self, head):
154
@@ -276,20 +320,20 @@ class StdTuplePrinter:
155
# Set the actual head to the first pair.
156
self.head = self.head.cast (nodes[0].type)
157
elif len (nodes) != 0:
158
- raise ValueError, "Top of tuple tree does not consist of a single node."
159
+ raise ValueError("Top of tuple tree does not consist of a single node.")
166
+ def __next__ (self):
167
nodes = self.head.type.fields ()
168
# Check for further recursions in the inheritance tree.
171
# Check that this iteration has an expected structure.
173
- raise ValueError, "Cannot parse more than 2 nodes in a tuple tree."
174
+ raise ValueError("Cannot parse more than 2 nodes in a tuple tree.")
176
# - Left node is the next recursion parent.
177
# - Right node is the actual class contained in the tuple.
178
@@ -341,7 +385,7 @@ class StdStackOrQueuePrinter:
179
return self.visualizer.display_hint ()
182
-class RbtreeIterator:
183
+class RbtreeIterator(Iterator):
184
def __init__(self, rbtree):
185
self.size = rbtree['_M_t']['_M_impl']['_M_node_count']
186
self.node = rbtree['_M_t']['_M_impl']['_M_header']['_M_left']
187
@@ -353,7 +397,7 @@ class RbtreeIterator:
189
return int (self.size)
192
+ def __next__(self):
193
if self.count == self.size:
196
@@ -405,7 +449,7 @@ class StdMapPrinter:
197
"Print a std::map or std::multimap"
199
# Turn an RbtreeIterator into a pretty-print iterator.
201
+ class _iter(Iterator):
202
def __init__(self, rbiter, type):
205
@@ -414,9 +458,9 @@ class StdMapPrinter:
210
+ def __next__(self):
211
if self.count % 2 == 0:
212
- n = self.rbiter.next()
213
+ n = next(self.rbiter)
214
n = n.cast(self.type).dereference()['_M_value_field']
217
@@ -447,7 +491,7 @@ class StdSetPrinter:
218
"Print a std::set or std::multiset"
220
# Turn an RbtreeIterator into a pretty-print iterator.
222
+ class _iter(Iterator):
223
def __init__(self, rbiter, type):
226
@@ -456,8 +500,8 @@ class StdSetPrinter:
231
- item = self.rbiter.next()
232
+ def __next__(self):
233
+ item = next(self.rbiter)
234
item = item.cast(self.type).dereference()['_M_value_field']
235
# FIXME: this is weird ... what to do?
236
# Maybe a 'set' display hint?
237
@@ -522,7 +566,7 @@ class StdBitsetPrinter:
238
class StdDequePrinter:
242
+ class _iter(Iterator):
243
def __init__(self, node, start, end, last, buffer_size):
246
@@ -534,7 +578,7 @@ class StdDequePrinter:
251
+ def __next__(self):
252
if self.p == self.last:
255
@@ -619,7 +663,7 @@ class StdStringPrinter:
256
def display_hint (self):
259
-class Tr1HashtableIterator:
260
+class Tr1HashtableIterator(Iterator):
261
def __init__ (self, hash):
262
self.node = hash['_M_bbegin']['_M_node']['_M_nxt']
263
self.node_type = find_type(hash.type, '__node_type').pointer()
264
@@ -627,7 +671,7 @@ class Tr1HashtableIterator:
269
+ def __next__ (self):
272
node = self.node.cast(self.node_type)
273
@@ -655,8 +699,8 @@ class Tr1UnorderedSetPrinter:
277
- counter = itertools.imap (self.format_count, itertools.count())
278
- return itertools.izip (counter, Tr1HashtableIterator (self.hashtable()))
279
+ counter = imap (self.format_count, itertools.count())
280
+ return izip (counter, Tr1HashtableIterator (self.hashtable()))
282
class Tr1UnorderedMapPrinter:
283
"Print a tr1::unordered_map"
284
@@ -688,11 +732,11 @@ class Tr1UnorderedMapPrinter:
288
- counter = itertools.imap (self.format_count, itertools.count())
289
+ counter = imap (self.format_count, itertools.count())
290
# Map over the hash table and flatten the result.
291
- data = self.flatten (itertools.imap (self.format_one, Tr1HashtableIterator (self.hashtable())))
292
+ data = self.flatten (imap (self.format_one, Tr1HashtableIterator (self.hashtable())))
293
# Zip the two iterators together.
294
- return itertools.izip (counter, data)
295
+ return izip (counter, data)
297
def display_hint (self):
299
@@ -700,7 +744,7 @@ class Tr1UnorderedMapPrinter:
300
class StdForwardListPrinter:
301
"Print a std::forward_list"
304
+ class _iterator(Iterator):
305
def __init__(self, nodetype, head):
306
self.nodetype = nodetype
307
self.base = head['_M_next']
308
@@ -709,7 +753,7 @@ class StdForwardListPrinter:
313
+ def __next__(self):
316
elt = self.base.cast(self.nodetype).dereference()
317
@@ -764,7 +808,7 @@ class Printer(object):
318
# A small sanity check.
320
if not self.compiled_rx.match(name + '<>'):
321
- raise ValueError, 'libstdc++ programming error: "%s" does not match' % name
322
+ raise ValueError('libstdc++ programming error: "%s" does not match' % name)
323
printer = RxPrinter(name, function)
324
self.subprinters.append(printer)
325
self.lookup[name] = printer
326
--- a/src/libstdc++-v3/testsuite/lib/gdb-test.exp
327
+++ b/src/libstdc++-v3/testsuite/lib/gdb-test.exp
328
@@ -91,7 +91,7 @@ proc gdb-test { marker {selector {}} } {
332
- set do_whatis_tests [gdb_batch_check "python print gdb.type_printers" \
333
+ set do_whatis_tests [gdb_batch_check "python print(gdb.type_printers)" \
335
if {!$do_whatis_tests} {
336
send_log "skipping 'whatis' tests - gdb too old"
337
@@ -252,6 +252,6 @@ proc gdb_batch_check {command pattern} {
338
# but not earlier versions.
339
# Return 1 if the version is ok, 0 otherwise.
340
proc gdb_version_check {} {
341
- return [gdb_batch_check "python print gdb.lookup_global_symbol" \
342
+ return [gdb_batch_check "python print(gdb.lookup_global_symbol)" \
343
"<built-in function lookup_global_symbol>"]