public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH v2 0/3] Port libstdc++ pretty printers to Python 3
       [not found] <1400701554-18062-1-git-send-email-naesten@gmail.com>
@ 2014-07-11  2:48 ` Samuel Bronson
  2014-07-11  2:49   ` [PATCH v2 1/3] Make libstdc++ testsuite work with pre-color GCC versions again Samuel Bronson
                     ` (2 more replies)
  0 siblings, 3 replies; 11+ messages in thread
From: Samuel Bronson @ 2014-07-11  2:48 UTC (permalink / raw)
  To: libstdc++, GCC Patches
  Cc: Samuel Bronson, Jonathan Wakely, GDB Patches, Tom Tromey

[-- Attachment #1: Type: text/plain, Size: 883 bytes --]

I've ported the libstdc++ pretty printers to work with Python 3 as
well as Python 2, and made a couple of other changes that I think are
improvements along the way.  The important change here is the final
patch; the others just made things easier at some point along the way.

I started with a patch by Mathias Klose; when he sent it out, he said
it should be backported to (what I assume were) all release branches.

Samuel Bronson (3):
  Make libstdc++ testsuite work with pre-color GCC versions again
  libstdc++ testsuite: Turn off GDB's auto-load, list loaded libs
  Port libstdc++ pretty-printers to Python 2 + Python 3

 libstdc++-v3/python/libstdcxx/v6/printers.py | 110 +++++++++++++++++++--------
 libstdc++-v3/testsuite/lib/gdb-test.exp      |  15 +++-
 libstdc++-v3/testsuite/lib/libstdc++.exp     |   4 +-
 3 files changed, 91 insertions(+), 38 deletions(-)

-- 
2.0.1


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH v2 1/3] Make libstdc++ testsuite work with pre-color GCC versions again
  2014-07-11  2:48 ` [PATCH v2 0/3] Port libstdc++ pretty printers to Python 3 Samuel Bronson
@ 2014-07-11  2:49   ` Samuel Bronson
  2014-07-11 15:33     ` Jonathan Wakely
  2014-07-11  3:00   ` [PATCH v2 2/3] libstdc++ testsuite: Turn off GDB's auto-load, list loaded libs Samuel Bronson
  2014-07-11  3:22   ` [PATCH v2 3/3] Port libstdc++ pretty-printers to Python 2 + Python 3 Samuel Bronson
  2 siblings, 1 reply; 11+ messages in thread
From: Samuel Bronson @ 2014-07-11  2:49 UTC (permalink / raw)
  To: libstdc++, GCC Patches
  Cc: Samuel Bronson, Jonathan Wakely, GDB Patches, Tom Tromey

[-- Attachment #1: Type: text/plain, Size: 1048 bytes --]


When I try to build & test just libstdc++, or to run the testsuite
from trunk against my installed libstdc++, the testsuite tries to pass
"-fdiagnostics-color=never" to the system GCC, which is too old to
know what that is.

Since I really just want to test a patch for the gdb pretty-printers,
and since evidently my machine is too puny to actually build GCC, this
is a bit problematic.

According to the documentation, setting GCC_COLORS to "" in the
environment should be just as effective, while it clearly can't cause
older GCCs to freak out, so that's just what I've done.

(I've also taken the liberty of swapping the "set ccflags" and "set
cxxflags" lines here so that ccflags doesn't end up with two
"-DLOCALEDIR" flags.)

libstdc++-v3/

	* testsuite/lib/libstdc++.exp (libstdc++_init): Set $GCC_COLORS=""
          instead of insisting that GCC understand -fdiagnostics-color=never

Signed-off-by: Samuel Bronson <naesten@gmail.com>
---
 libstdc++-v3/testsuite/lib/libstdc++.exp | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: v2-0001-Make-libstdc-testsuite-work-with-pre-color-GCC-ve.patch --]
[-- Type: text/x-patch; name="v2-0001-Make-libstdc-testsuite-work-with-pre-color-GCC-ve.patch", Size: 772 bytes --]

diff --git a/libstdc++-v3/testsuite/lib/libstdc++.exp b/libstdc++-v3/testsuite/lib/libstdc++.exp
index d91bed6..0fdfbdc 100644
--- a/libstdc++-v3/testsuite/lib/libstdc++.exp
+++ b/libstdc++-v3/testsuite/lib/libstdc++.exp
@@ -289,9 +289,11 @@ proc libstdc++_init { testfile } {
 
     v3track cxxflags 2
 
+    # Should be as good as -fdiagnostics-color=never, but more compatible
+    setenv GCC_COLORS ""
     # Always use MO files built by this test harness.
-    set cxxflags "-fdiagnostics-color=never $cxxflags -DLOCALEDIR=\".\""
     set ccflags "$cxxflags -DLOCALEDIR=\".\""
+    set cxxflags "$cxxflags -DLOCALEDIR=\".\""
 
     # If a PCH file is available, use it.  We must delay performing
     # this check until $cxx and such have been initialized because we

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH v2 2/3] libstdc++ testsuite: Turn off GDB's auto-load, list loaded libs
  2014-07-11  2:48 ` [PATCH v2 0/3] Port libstdc++ pretty printers to Python 3 Samuel Bronson
  2014-07-11  2:49   ` [PATCH v2 1/3] Make libstdc++ testsuite work with pre-color GCC versions again Samuel Bronson
@ 2014-07-11  3:00   ` Samuel Bronson
  2014-07-11 15:33     ` Jonathan Wakely
  2014-07-11  3:22   ` [PATCH v2 3/3] Port libstdc++ pretty-printers to Python 2 + Python 3 Samuel Bronson
  2 siblings, 1 reply; 11+ messages in thread
From: Samuel Bronson @ 2014-07-11  3:00 UTC (permalink / raw)
  To: libstdc++, GCC Patches
  Cc: Samuel Bronson, Jonathan Wakely, GDB Patches, Tom Tromey

[-- Attachment #1: Type: text/plain, Size: 553 bytes --]


We load our pretty-printers explicitly, and we shouldn't need any other
random -gdb.gdb or -gdb.py files from anywhere, so in this patch we turn
that off by running "set auto-load no".

Also, run "info share" so that the list of loaded libraries ends up in
the logs for the GDB tests.

libstdc++-v3/

	* testsuite/lib/gdb-test.exp (gdb-test): Turn off GDB's auto-load, list
	  loaded libs

Signed-off-by: Samuel Bronson <naesten@gmail.com>
---
 libstdc++-v3/testsuite/lib/gdb-test.exp | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: v2-0002-libstdc-testsuite-Turn-off-GDB-s-auto-load-list-l.patch --]
[-- Type: text/x-patch; name="v2-0002-libstdc-testsuite-Turn-off-GDB-s-auto-load-list-l.patch", Size: 1359 bytes --]

diff --git a/libstdc++-v3/testsuite/lib/gdb-test.exp b/libstdc++-v3/testsuite/lib/gdb-test.exp
index 1a72a44..6d7ea9d 100644
--- a/libstdc++-v3/testsuite/lib/gdb-test.exp
+++ b/libstdc++-v3/testsuite/lib/gdb-test.exp
@@ -116,10 +116,18 @@ proc gdb-test { marker {selector {}} } {
     global gdb_tests
 
     set fd [open $cmd_file "w"]
+    # We don't want the system copy of the pretty-printers loaded
+    puts $fd "set auto-load no"
+    # Now that we've disabled auto-load, it's safe to set the target file
+    puts $fd "file ./$output_file"
+    # Load & register *our* copy of the pretty-printers
     puts $fd "source $pycode"
     puts $fd "python register_libstdcxx_printers(None)"
+    # And start the program
     puts $fd "break $line"
     puts $fd "run"
+    # So we can verify that we're using the right libs ...
+    puts $fd "info share"
 
     set count 0
     foreach {var result kind} $gdb_tests {
@@ -147,8 +155,7 @@ proc gdb-test { marker {selector {}} } {
     puts $fd "quit"
     close $fd
 
-    send_log "Spawning: $gdb_name -nx -nw -quiet -batch -x $cmd_file ./$output_file\n"
-    set res [remote_spawn target "$gdb_name -nx -nw -quiet -batch -x $cmd_file ./$output_file"]
+    set res [remote_spawn target "$gdb_name -nx -nw -quiet -batch -x $cmd_file "]
     if { $res < 0 || $res == "" } {
 	unsupported "$testname"
 	return

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH v2 3/3] Port libstdc++ pretty-printers to Python 2 + Python 3
  2014-07-11  2:48 ` [PATCH v2 0/3] Port libstdc++ pretty printers to Python 3 Samuel Bronson
  2014-07-11  2:49   ` [PATCH v2 1/3] Make libstdc++ testsuite work with pre-color GCC versions again Samuel Bronson
  2014-07-11  3:00   ` [PATCH v2 2/3] libstdc++ testsuite: Turn off GDB's auto-load, list loaded libs Samuel Bronson
@ 2014-07-11  3:22   ` Samuel Bronson
  2014-07-11  3:45     ` [PATCH v3 " Samuel Bronson
  2 siblings, 1 reply; 11+ messages in thread
From: Samuel Bronson @ 2014-07-11  3:22 UTC (permalink / raw)
  To: libstdc++, GCC Patches
  Cc: Samuel Bronson, Jonathan Wakely, GDB Patches, Tom Tromey, Mathias Klose

[-- Attachment #1: Type: text/plain, Size: 858 bytes --]


Loosely based on Mathias Klose' earlier patch
<http://patchwork.ozlabs.org/patch/287368/>.

Tested with:

$ make check-target-libstdc++-v3 RUNTESTFLAGS='--directory libstdc++-prettyprinters'

libstdc++-v3/

	PR libstdc++/58962
	* python/libstdcxx/v6/printers.py: Port to Python 2+3
	  (imap): New compat function.
	  (izip): Likewise.
	  (Iterator): New mixin to allow writing iterators in Python 3 style
	  regardless of which version we're running on.
	  [Python3] (long) New compat alias for "int".
	* testsuite/lib/gdb-test.exp: Port to Python 2+3 (print syntax)

Signed-off-by: Mathias Klose <doko@ubuntu.com>
Signed-off-by: Samuel Bronson <naesten@gmail.com>
---
 libstdc++-v3/python/libstdcxx/v6/printers.py | 110 +++++++++++++++++++--------
 libstdc++-v3/testsuite/lib/gdb-test.exp      |   4 +-
 2 files changed, 79 insertions(+), 35 deletions(-)


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: v2-0003-Port-libstdc-pretty-printers-to-Python-2-Python-3.patch --]
[-- Type: text/x-patch; name="v2-0003-Port-libstdc-pretty-printers-to-Python-2-Python-3.patch", Size: 10971 bytes --]

diff --git a/libstdc++-v3/python/libstdcxx/v6/printers.py b/libstdc++-v3/python/libstdcxx/v6/printers.py
index 623a815..313b08d 100644
--- a/libstdc++-v3/python/libstdcxx/v6/printers.py
+++ b/libstdc++-v3/python/libstdcxx/v6/printers.py
@@ -1,4 +1,4 @@
-# Pretty-printers for libstc++.
+# Pretty-printers for libstdc++.
 
 # Copyright (C) 2008-2014 Free Software Foundation, Inc.
 
@@ -18,6 +18,50 @@
 import gdb
 import itertools
 import re
+import sys
+
+### Python 2 + Python 3 compatability code
+
+# Resources about compatability:
+#
+#  * <http://pythonhosted.org/six/>: Documentation of the "six" module
+
+# FIXME: The handling of e.g. std::basic_string (at least on char)
+# probably needs updating to work with Python 3's new string rules.
+#
+# In particular, Python 3 has a separate type (called byte) for
+# bytestrings, and a special b"" syntax for the byte literals; the old
+# str() type has been redefined to always store Unicode text.
+#
+# We probably can't do much about this until GDB get their act
+# together: <https://sourceware.org/bugzilla/show_bug.cgi?id=17138>
+
+if sys.version_info[0] > 2:
+    ### Python 3 stuff
+    Iterator = object
+    # Python 3 folds these into the normal functions.
+    imap = map
+    izip = zip
+    # Also, int subsumes long
+    long = int
+else:
+    ### Python 2 stuff
+    class Iterator:
+        """Compatability mixin for iterators
+
+        Instead of writing next() methods for iterators, write
+        __next__() methods and use this mixin to make them work in
+        Python 2 as well as Python 3.
+
+        Idea stolen from the "six" documentation:
+        <http://pythonhosted.org/six/#six.Iterator>
+        """
+
+        def next(self):
+            return self.__next__()
+
+    # In Python 2, we still need these from itertools
+    from itertools import imap, izip
 
 # Try to use the new-style pretty-printing if available.
 _use_gdb_pp = True
@@ -87,7 +131,7 @@ class UniquePointerPrinter:
 class StdListPrinter:
     "Print a std::list"
 
-    class _iterator:
+    class _iterator(Iterator):
         def __init__(self, nodetype, head):
             self.nodetype = nodetype
             self.base = head['_M_next']
@@ -97,7 +141,7 @@ class StdListPrinter:
         def __iter__(self):
             return self
 
-        def next(self):
+        def __next__(self):
             if self.base == self.head:
                 raise StopIteration
             elt = self.base.cast(self.nodetype).dereference()
@@ -135,7 +179,7 @@ class StdListIteratorPrinter:
 class StdSlistPrinter:
     "Print a __gnu_cxx::slist"
 
-    class _iterator:
+    class _iterator(Iterator):
         def __init__(self, nodetype, head):
             self.nodetype = nodetype
             self.base = head['_M_head']['_M_next']
@@ -144,7 +188,7 @@ class StdSlistPrinter:
         def __iter__(self):
             return self
 
-        def next(self):
+        def __next__(self):
             if self.base == 0:
                 raise StopIteration
             elt = self.base.cast(self.nodetype).dereference()
@@ -180,7 +224,7 @@ class StdSlistIteratorPrinter:
 class StdVectorPrinter:
     "Print a std::vector"
 
-    class _iterator:
+    class _iterator(Iterator):
         def __init__ (self, start, finish, bitvec):
             self.bitvec = bitvec
             if bitvec:
@@ -198,7 +242,7 @@ class StdVectorPrinter:
         def __iter__(self):
             return self
 
-        def next(self):
+        def __next__(self):
             count = self.count
             self.count = self.count + 1
             if self.bitvec:
@@ -265,7 +309,7 @@ class StdVectorIteratorPrinter:
 class StdTuplePrinter:
     "Print a std::tuple"
 
-    class _iterator:
+    class _iterator(Iterator):
         def __init__ (self, head):
             self.head = head
 
@@ -282,7 +326,7 @@ class StdTuplePrinter:
         def __iter__ (self):
             return self
 
-        def next (self):
+        def __next__ (self):
             nodes = self.head.type.fields ()
             # Check for further recursions in the inheritance tree.
             if len (nodes) == 0:
@@ -341,7 +385,7 @@ class StdStackOrQueuePrinter:
             return self.visualizer.display_hint ()
         return None
 
-class RbtreeIterator:
+class RbtreeIterator(Iterator):
     def __init__(self, rbtree):
         self.size = rbtree['_M_t']['_M_impl']['_M_node_count']
         self.node = rbtree['_M_t']['_M_impl']['_M_header']['_M_left']
@@ -353,7 +397,7 @@ class RbtreeIterator:
     def __len__(self):
         return int (self.size)
 
-    def next(self):
+    def __next__(self):
         if self.count == self.size:
             raise StopIteration
         result = self.node
@@ -422,7 +466,7 @@ class StdMapPrinter:
     "Print a std::map or std::multimap"
 
     # Turn an RbtreeIterator into a pretty-print iterator.
-    class _iter:
+    class _iter(Iterator):
         def __init__(self, rbiter, type):
             self.rbiter = rbiter
             self.count = 0
@@ -431,9 +475,9 @@ class StdMapPrinter:
         def __iter__(self):
             return self
 
-        def next(self):
+        def __next__(self):
             if self.count % 2 == 0:
-                n = self.rbiter.next()
+                n = next(self.rbiter)
                 n = n.cast(self.type).dereference()
                 n = get_value_from_Rb_tree_node(n)
                 self.pair = n
@@ -465,7 +509,7 @@ class StdSetPrinter:
     "Print a std::set or std::multiset"
 
     # Turn an RbtreeIterator into a pretty-print iterator.
-    class _iter:
+    class _iter(Iterator):
         def __init__(self, rbiter, type):
             self.rbiter = rbiter
             self.count = 0
@@ -474,8 +518,8 @@ class StdSetPrinter:
         def __iter__(self):
             return self
 
-        def next(self):
-            item = self.rbiter.next()
+        def __next__(self):
+            item = next(self.rbiter)
             item = item.cast(self.type).dereference()
             item = get_value_from_Rb_tree_node(item)
             # FIXME: this is weird ... what to do?
@@ -541,7 +585,7 @@ class StdBitsetPrinter:
 class StdDequePrinter:
     "Print a std::deque"
 
-    class _iter:
+    class _iter(Iterator):
         def __init__(self, node, start, end, last, buffer_size):
             self.node = node
             self.p = start
@@ -553,7 +597,7 @@ class StdDequePrinter:
         def __iter__(self):
             return self
 
-        def next(self):
+        def __next__(self):
             if self.p == self.last:
                 raise StopIteration
 
@@ -638,7 +682,7 @@ class StdStringPrinter:
     def display_hint (self):
         return 'string'
 
-class Tr1HashtableIterator:
+class Tr1HashtableIterator(Iterator):
     def __init__ (self, hash):
         self.buckets = hash['_M_buckets']
         self.bucket = 0
@@ -654,7 +698,7 @@ class Tr1HashtableIterator:
     def __iter__ (self):
         return self
 
-    def next (self):
+    def __next__ (self):
         if self.node == 0:
             raise StopIteration
         node = self.node.cast(self.node_type)
@@ -669,7 +713,7 @@ class Tr1HashtableIterator:
                 self.bucket = self.bucket + 1
         return result
 
-class StdHashtableIterator:
+class StdHashtableIterator(Iterator):
     def __init__(self, hash):
         self.node = hash['_M_before_begin']['_M_nxt']
         self.node_type = find_type(hash.type, '__node_type').pointer()
@@ -677,7 +721,7 @@ class StdHashtableIterator:
     def __iter__(self):
         return self
 
-    def next(self):
+    def __next__(self):
         if self.node == 0:
             raise StopIteration
         elt = self.node.cast(self.node_type).dereference()
@@ -706,10 +750,10 @@ class Tr1UnorderedSetPrinter:
         return '[%d]' % i
 
     def children (self):
-        counter = itertools.imap (self.format_count, itertools.count())
+        counter = imap (self.format_count, itertools.count())
         if self.typename.startswith('std::tr1'):
-            return itertools.izip (counter, Tr1HashtableIterator (self.hashtable()))
-        return itertools.izip (counter, StdHashtableIterator (self.hashtable()))
+            return izip (counter, Tr1HashtableIterator (self.hashtable()))
+        return izip (counter, StdHashtableIterator (self.hashtable()))
 
 class Tr1UnorderedMapPrinter:
     "Print a tr1::unordered_map"
@@ -741,15 +785,15 @@ class Tr1UnorderedMapPrinter:
         return '[%d]' % i
 
     def children (self):
-        counter = itertools.imap (self.format_count, itertools.count())
+        counter = imap (self.format_count, itertools.count())
         # Map over the hash table and flatten the result.
         if self.typename.startswith('std::tr1'):
-            data = self.flatten (itertools.imap (self.format_one, Tr1HashtableIterator (self.hashtable())))
+            data = self.flatten (imap (self.format_one, Tr1HashtableIterator (self.hashtable())))
             # Zip the two iterators together.
-            return itertools.izip (counter, data)
-        data = self.flatten (itertools.imap (self.format_one, StdHashtableIterator (self.hashtable())))
+            return izip (counter, data)
+        data = self.flatten (imap (self.format_one, StdHashtableIterator (self.hashtable())))
         # Zip the two iterators together.
-        return itertools.izip (counter, data)
+        return izip (counter, data)
         
 
     def display_hint (self):
@@ -758,7 +802,7 @@ class Tr1UnorderedMapPrinter:
 class StdForwardListPrinter:
     "Print a std::forward_list"
 
-    class _iterator:
+    class _iterator(Iterator):
         def __init__(self, nodetype, head):
             self.nodetype = nodetype
             self.base = head['_M_next']
@@ -767,7 +811,7 @@ class StdForwardListPrinter:
         def __iter__(self):
             return self
 
-        def next(self):
+        def __next__(self):
             if self.base == 0:
                 raise StopIteration
             elt = self.base.cast(self.nodetype).dereference()
diff --git a/libstdc++-v3/testsuite/lib/gdb-test.exp b/libstdc++-v3/testsuite/lib/gdb-test.exp
index 6d7ea9d..9cb6ecf 100644
--- a/libstdc++-v3/testsuite/lib/gdb-test.exp
+++ b/libstdc++-v3/testsuite/lib/gdb-test.exp
@@ -91,7 +91,7 @@ proc gdb-test { marker {selector {}} } {
 	}
     }
 
-    set do_whatis_tests [gdb_batch_check "python print gdb.type_printers" \
+    set do_whatis_tests [gdb_batch_check "python print(gdb.type_printers)" \
 			   "\\\[\\\]"]
     if {!$do_whatis_tests} {
 	send_log "skipping 'whatis' tests - gdb too old"
@@ -259,6 +259,6 @@ proc gdb_batch_check {command pattern} {
 # but not earlier versions.
 # Return 1 if the version is ok, 0 otherwise.
 proc gdb_version_check {} {
-    return [gdb_batch_check "python print gdb.lookup_global_symbol" \
+    return [gdb_batch_check "python print(gdb.lookup_global_symbol)" \
 	      "<built-in function lookup_global_symbol>"]
 }

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH v3 3/3] Port libstdc++ pretty-printers to Python 2 + Python 3
  2014-07-11  3:22   ` [PATCH v2 3/3] Port libstdc++ pretty-printers to Python 2 + Python 3 Samuel Bronson
@ 2014-07-11  3:45     ` Samuel Bronson
  2014-07-11 13:49       ` Jonathan Wakely
  2014-07-21 17:06       ` Tom Tromey
  0 siblings, 2 replies; 11+ messages in thread
From: Samuel Bronson @ 2014-07-11  3:45 UTC (permalink / raw)
  To: libstdc++, GCC Patches
  Cc: Samuel Bronson, Jonathan Wakely, GDB Patches, Tom Tromey, Matthias Klose

[-- Attachment #1: Type: text/plain, Size: 923 bytes --]


Loosely based on Matthias Klose's earlier patch
<http://patchwork.ozlabs.org/patch/287368/>.

This time with his name spelled correctly; sorry about that!

Tested with:

$ make check-target-libstdc++-v3 RUNTESTFLAGS='--directory libstdc++-prettyprinters'

libstdc++-v3/

	PR libstdc++/58962
	* python/libstdcxx/v6/printers.py: Port to Python 2+3
	  (imap): New compat function.
	  (izip): Likewise.
	  (Iterator): New mixin to allow writing iterators in Python 3 style
	  regardless of which version we're running on.
	  [Python3] (long) New compat alias for "int".
	* testsuite/lib/gdb-test.exp: Port to Python 2+3 (print syntax)

Signed-off-by: Matthias Klose <doko@ubuntu.com>
Signed-off-by: Samuel Bronson <naesten@gmail.com>
---
 libstdc++-v3/python/libstdcxx/v6/printers.py | 110 +++++++++++++++++++--------
 libstdc++-v3/testsuite/lib/gdb-test.exp      |   4 +-
 2 files changed, 79 insertions(+), 35 deletions(-)


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: v2-0003-Port-libstdc-pretty-printers-to-Python-2-Python-3.patch --]
[-- Type: text/x-patch; name=v2-0003-Port-libstdc-pretty-printers-to-Python-2-Python-3.patch, Size: 10971 bytes --]

diff --git a/libstdc++-v3/python/libstdcxx/v6/printers.py b/libstdc++-v3/python/libstdcxx/v6/printers.py
index 623a815..313b08d 100644
--- a/libstdc++-v3/python/libstdcxx/v6/printers.py
+++ b/libstdc++-v3/python/libstdcxx/v6/printers.py
@@ -1,4 +1,4 @@
-# Pretty-printers for libstc++.
+# Pretty-printers for libstdc++.
 
 # Copyright (C) 2008-2014 Free Software Foundation, Inc.
 
@@ -18,6 +18,50 @@
 import gdb
 import itertools
 import re
+import sys
+
+### Python 2 + Python 3 compatability code
+
+# Resources about compatability:
+#
+#  * <http://pythonhosted.org/six/>: Documentation of the "six" module
+
+# FIXME: The handling of e.g. std::basic_string (at least on char)
+# probably needs updating to work with Python 3's new string rules.
+#
+# In particular, Python 3 has a separate type (called byte) for
+# bytestrings, and a special b"" syntax for the byte literals; the old
+# str() type has been redefined to always store Unicode text.
+#
+# We probably can't do much about this until GDB get their act
+# together: <https://sourceware.org/bugzilla/show_bug.cgi?id=17138>
+
+if sys.version_info[0] > 2:
+    ### Python 3 stuff
+    Iterator = object
+    # Python 3 folds these into the normal functions.
+    imap = map
+    izip = zip
+    # Also, int subsumes long
+    long = int
+else:
+    ### Python 2 stuff
+    class Iterator:
+        """Compatability mixin for iterators
+
+        Instead of writing next() methods for iterators, write
+        __next__() methods and use this mixin to make them work in
+        Python 2 as well as Python 3.
+
+        Idea stolen from the "six" documentation:
+        <http://pythonhosted.org/six/#six.Iterator>
+        """
+
+        def next(self):
+            return self.__next__()
+
+    # In Python 2, we still need these from itertools
+    from itertools import imap, izip
 
 # Try to use the new-style pretty-printing if available.
 _use_gdb_pp = True
@@ -87,7 +131,7 @@ class UniquePointerPrinter:
 class StdListPrinter:
     "Print a std::list"
 
-    class _iterator:
+    class _iterator(Iterator):
         def __init__(self, nodetype, head):
             self.nodetype = nodetype
             self.base = head['_M_next']
@@ -97,7 +141,7 @@ class StdListPrinter:
         def __iter__(self):
             return self
 
-        def next(self):
+        def __next__(self):
             if self.base == self.head:
                 raise StopIteration
             elt = self.base.cast(self.nodetype).dereference()
@@ -135,7 +179,7 @@ class StdListIteratorPrinter:
 class StdSlistPrinter:
     "Print a __gnu_cxx::slist"
 
-    class _iterator:
+    class _iterator(Iterator):
         def __init__(self, nodetype, head):
             self.nodetype = nodetype
             self.base = head['_M_head']['_M_next']
@@ -144,7 +188,7 @@ class StdSlistPrinter:
         def __iter__(self):
             return self
 
-        def next(self):
+        def __next__(self):
             if self.base == 0:
                 raise StopIteration
             elt = self.base.cast(self.nodetype).dereference()
@@ -180,7 +224,7 @@ class StdSlistIteratorPrinter:
 class StdVectorPrinter:
     "Print a std::vector"
 
-    class _iterator:
+    class _iterator(Iterator):
         def __init__ (self, start, finish, bitvec):
             self.bitvec = bitvec
             if bitvec:
@@ -198,7 +242,7 @@ class StdVectorPrinter:
         def __iter__(self):
             return self
 
-        def next(self):
+        def __next__(self):
             count = self.count
             self.count = self.count + 1
             if self.bitvec:
@@ -265,7 +309,7 @@ class StdVectorIteratorPrinter:
 class StdTuplePrinter:
     "Print a std::tuple"
 
-    class _iterator:
+    class _iterator(Iterator):
         def __init__ (self, head):
             self.head = head
 
@@ -282,7 +326,7 @@ class StdTuplePrinter:
         def __iter__ (self):
             return self
 
-        def next (self):
+        def __next__ (self):
             nodes = self.head.type.fields ()
             # Check for further recursions in the inheritance tree.
             if len (nodes) == 0:
@@ -341,7 +385,7 @@ class StdStackOrQueuePrinter:
             return self.visualizer.display_hint ()
         return None
 
-class RbtreeIterator:
+class RbtreeIterator(Iterator):
     def __init__(self, rbtree):
         self.size = rbtree['_M_t']['_M_impl']['_M_node_count']
         self.node = rbtree['_M_t']['_M_impl']['_M_header']['_M_left']
@@ -353,7 +397,7 @@ class RbtreeIterator:
     def __len__(self):
         return int (self.size)
 
-    def next(self):
+    def __next__(self):
         if self.count == self.size:
             raise StopIteration
         result = self.node
@@ -422,7 +466,7 @@ class StdMapPrinter:
     "Print a std::map or std::multimap"
 
     # Turn an RbtreeIterator into a pretty-print iterator.
-    class _iter:
+    class _iter(Iterator):
         def __init__(self, rbiter, type):
             self.rbiter = rbiter
             self.count = 0
@@ -431,9 +475,9 @@ class StdMapPrinter:
         def __iter__(self):
             return self
 
-        def next(self):
+        def __next__(self):
             if self.count % 2 == 0:
-                n = self.rbiter.next()
+                n = next(self.rbiter)
                 n = n.cast(self.type).dereference()
                 n = get_value_from_Rb_tree_node(n)
                 self.pair = n
@@ -465,7 +509,7 @@ class StdSetPrinter:
     "Print a std::set or std::multiset"
 
     # Turn an RbtreeIterator into a pretty-print iterator.
-    class _iter:
+    class _iter(Iterator):
         def __init__(self, rbiter, type):
             self.rbiter = rbiter
             self.count = 0
@@ -474,8 +518,8 @@ class StdSetPrinter:
         def __iter__(self):
             return self
 
-        def next(self):
-            item = self.rbiter.next()
+        def __next__(self):
+            item = next(self.rbiter)
             item = item.cast(self.type).dereference()
             item = get_value_from_Rb_tree_node(item)
             # FIXME: this is weird ... what to do?
@@ -541,7 +585,7 @@ class StdBitsetPrinter:
 class StdDequePrinter:
     "Print a std::deque"
 
-    class _iter:
+    class _iter(Iterator):
         def __init__(self, node, start, end, last, buffer_size):
             self.node = node
             self.p = start
@@ -553,7 +597,7 @@ class StdDequePrinter:
         def __iter__(self):
             return self
 
-        def next(self):
+        def __next__(self):
             if self.p == self.last:
                 raise StopIteration
 
@@ -638,7 +682,7 @@ class StdStringPrinter:
     def display_hint (self):
         return 'string'
 
-class Tr1HashtableIterator:
+class Tr1HashtableIterator(Iterator):
     def __init__ (self, hash):
         self.buckets = hash['_M_buckets']
         self.bucket = 0
@@ -654,7 +698,7 @@ class Tr1HashtableIterator:
     def __iter__ (self):
         return self
 
-    def next (self):
+    def __next__ (self):
         if self.node == 0:
             raise StopIteration
         node = self.node.cast(self.node_type)
@@ -669,7 +713,7 @@ class Tr1HashtableIterator:
                 self.bucket = self.bucket + 1
         return result
 
-class StdHashtableIterator:
+class StdHashtableIterator(Iterator):
     def __init__(self, hash):
         self.node = hash['_M_before_begin']['_M_nxt']
         self.node_type = find_type(hash.type, '__node_type').pointer()
@@ -677,7 +721,7 @@ class StdHashtableIterator:
     def __iter__(self):
         return self
 
-    def next(self):
+    def __next__(self):
         if self.node == 0:
             raise StopIteration
         elt = self.node.cast(self.node_type).dereference()
@@ -706,10 +750,10 @@ class Tr1UnorderedSetPrinter:
         return '[%d]' % i
 
     def children (self):
-        counter = itertools.imap (self.format_count, itertools.count())
+        counter = imap (self.format_count, itertools.count())
         if self.typename.startswith('std::tr1'):
-            return itertools.izip (counter, Tr1HashtableIterator (self.hashtable()))
-        return itertools.izip (counter, StdHashtableIterator (self.hashtable()))
+            return izip (counter, Tr1HashtableIterator (self.hashtable()))
+        return izip (counter, StdHashtableIterator (self.hashtable()))
 
 class Tr1UnorderedMapPrinter:
     "Print a tr1::unordered_map"
@@ -741,15 +785,15 @@ class Tr1UnorderedMapPrinter:
         return '[%d]' % i
 
     def children (self):
-        counter = itertools.imap (self.format_count, itertools.count())
+        counter = imap (self.format_count, itertools.count())
         # Map over the hash table and flatten the result.
         if self.typename.startswith('std::tr1'):
-            data = self.flatten (itertools.imap (self.format_one, Tr1HashtableIterator (self.hashtable())))
+            data = self.flatten (imap (self.format_one, Tr1HashtableIterator (self.hashtable())))
             # Zip the two iterators together.
-            return itertools.izip (counter, data)
-        data = self.flatten (itertools.imap (self.format_one, StdHashtableIterator (self.hashtable())))
+            return izip (counter, data)
+        data = self.flatten (imap (self.format_one, StdHashtableIterator (self.hashtable())))
         # Zip the two iterators together.
-        return itertools.izip (counter, data)
+        return izip (counter, data)
         
 
     def display_hint (self):
@@ -758,7 +802,7 @@ class Tr1UnorderedMapPrinter:
 class StdForwardListPrinter:
     "Print a std::forward_list"
 
-    class _iterator:
+    class _iterator(Iterator):
         def __init__(self, nodetype, head):
             self.nodetype = nodetype
             self.base = head['_M_next']
@@ -767,7 +811,7 @@ class StdForwardListPrinter:
         def __iter__(self):
             return self
 
-        def next(self):
+        def __next__(self):
             if self.base == 0:
                 raise StopIteration
             elt = self.base.cast(self.nodetype).dereference()
diff --git a/libstdc++-v3/testsuite/lib/gdb-test.exp b/libstdc++-v3/testsuite/lib/gdb-test.exp
index 6d7ea9d..9cb6ecf 100644
--- a/libstdc++-v3/testsuite/lib/gdb-test.exp
+++ b/libstdc++-v3/testsuite/lib/gdb-test.exp
@@ -91,7 +91,7 @@ proc gdb-test { marker {selector {}} } {
 	}
     }
 
-    set do_whatis_tests [gdb_batch_check "python print gdb.type_printers" \
+    set do_whatis_tests [gdb_batch_check "python print(gdb.type_printers)" \
 			   "\\\[\\\]"]
     if {!$do_whatis_tests} {
 	send_log "skipping 'whatis' tests - gdb too old"
@@ -259,6 +259,6 @@ proc gdb_batch_check {command pattern} {
 # but not earlier versions.
 # Return 1 if the version is ok, 0 otherwise.
 proc gdb_version_check {} {
-    return [gdb_batch_check "python print gdb.lookup_global_symbol" \
+    return [gdb_batch_check "python print(gdb.lookup_global_symbol)" \
 	      "<built-in function lookup_global_symbol>"]
 }

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v3 3/3] Port libstdc++ pretty-printers to Python 2 + Python 3
  2014-07-11  3:45     ` [PATCH v3 " Samuel Bronson
@ 2014-07-11 13:49       ` Jonathan Wakely
  2014-07-21 17:06       ` Tom Tromey
  1 sibling, 0 replies; 11+ messages in thread
From: Jonathan Wakely @ 2014-07-11 13:49 UTC (permalink / raw)
  To: Samuel Bronson
  Cc: libstdc++, GCC Patches, GDB Patches, Tom Tromey, Matthias Klose

On 10/07/14 22:48 -0400, Samuel Bronson wrote:
>	PR libstdc++/58962
>	* python/libstdcxx/v6/printers.py: Port to Python 2+3
>	  (imap): New compat function.
>	  (izip): Likewise.
>	  (Iterator): New mixin to allow writing iterators in Python 3 style
>	  regardless of which version we're running on.
>	  [Python3] (long) New compat alias for "int".
>	* testsuite/lib/gdb-test.exp: Port to Python 2+3 (print syntax)

Thanks, I've committed this with a few tiny changes to the comments
(s/compatability/compatibility/ and the comment about your GDB PR).

I'm not planning to backport this myself, but if the patch applies
cleanly or if someone else does the work then I have no objections to
it going on the release branches (after the branch reopens post-4.9.1
of course).

Thanks very much for doing this.

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v2 1/3] Make libstdc++ testsuite work with pre-color GCC versions again
  2014-07-11  2:49   ` [PATCH v2 1/3] Make libstdc++ testsuite work with pre-color GCC versions again Samuel Bronson
@ 2014-07-11 15:33     ` Jonathan Wakely
  0 siblings, 0 replies; 11+ messages in thread
From: Jonathan Wakely @ 2014-07-11 15:33 UTC (permalink / raw)
  To: Samuel Bronson; +Cc: libstdc++, GCC Patches, Tom Tromey

On 10/07/14 22:48 -0400, Samuel Bronson wrote:
>
>When I try to build & test just libstdc++, or to run the testsuite
>from trunk against my installed libstdc++, the testsuite tries to pass
>"-fdiagnostics-color=never" to the system GCC, which is too old to
>know what that is.
>
>Since I really just want to test a patch for the gdb pretty-printers,
>and since evidently my machine is too puny to actually build GCC, this
>is a bit problematic.
>
>According to the documentation, setting GCC_COLORS to "" in the
>environment should be just as effective, while it clearly can't cause
>older GCCs to freak out, so that's just what I've done.
>
>(I've also taken the liberty of swapping the "set ccflags" and "set
>cxxflags" lines here so that ccflags doesn't end up with two
>"-DLOCALEDIR" flags.)
>
>libstdc++-v3/
>
>	* testsuite/lib/libstdc++.exp (libstdc++_init): Set $GCC_COLORS=""
>          instead of insisting that GCC understand -fdiagnostics-color=never

Committed to trunk - thanks.

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v2 2/3] libstdc++ testsuite: Turn off GDB's auto-load, list loaded libs
  2014-07-11  3:00   ` [PATCH v2 2/3] libstdc++ testsuite: Turn off GDB's auto-load, list loaded libs Samuel Bronson
@ 2014-07-11 15:33     ` Jonathan Wakely
  0 siblings, 0 replies; 11+ messages in thread
From: Jonathan Wakely @ 2014-07-11 15:33 UTC (permalink / raw)
  To: Samuel Bronson; +Cc: libstdc++, GCC Patches, GDB Patches, Tom Tromey

On 10/07/14 22:48 -0400, Samuel Bronson wrote:
>
>We load our pretty-printers explicitly, and we shouldn't need any other
>random -gdb.gdb or -gdb.py files from anywhere, so in this patch we turn
>that off by running "set auto-load no".
>
>Also, run "info share" so that the list of loaded libraries ends up in
>the logs for the GDB tests.
>
>libstdc++-v3/
>
>	* testsuite/lib/gdb-test.exp (gdb-test): Turn off GDB's auto-load, list
>	  loaded libs

Committed to trunk - thanks.

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v3 3/3] Port libstdc++ pretty-printers to Python 2 + Python 3
  2014-07-11  3:45     ` [PATCH v3 " Samuel Bronson
  2014-07-11 13:49       ` Jonathan Wakely
@ 2014-07-21 17:06       ` Tom Tromey
  2014-07-26  6:33         ` Samuel Bronson
  1 sibling, 1 reply; 11+ messages in thread
From: Tom Tromey @ 2014-07-21 17:06 UTC (permalink / raw)
  To: Samuel Bronson
  Cc: libstdc++, GCC Patches, Jonathan Wakely, GDB Patches, Matthias Klose

>>>>> "Samuel" == Samuel Bronson <naesten@gmail.com> writes:

Samuel> +# FIXME: The handling of e.g. std::basic_string (at least on char)
Samuel> +# probably needs updating to work with Python 3's new string rules.
Samuel> +#
Samuel> +# In particular, Python 3 has a separate type (called byte) for
Samuel> +# bytestrings, and a special b"" syntax for the byte literals; the old
Samuel> +# str() type has been redefined to always store Unicode text.
Samuel> +#
Samuel> +# We probably can't do much about this until GDB get their act
Samuel> +# together: <https://sourceware.org/bugzilla/show_bug.cgi?id=17138>

I don't think this comment is applicable.
The libstdc++ pretty-printers use gdb.Value.lazy_string, not the
built-in Python types.

Tom

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v3 3/3] Port libstdc++ pretty-printers to Python 2 + Python 3
  2014-07-21 17:06       ` Tom Tromey
@ 2014-07-26  6:33         ` Samuel Bronson
  2014-07-30 14:20           ` Tom Tromey
  0 siblings, 1 reply; 11+ messages in thread
From: Samuel Bronson @ 2014-07-26  6:33 UTC (permalink / raw)
  To: Tom Tromey
  Cc: libstdc++, GCC Patches, Jonathan Wakely, GDB Patches, Matthias Klose

Tom Tromey <tromey@redhat.com> writes:

>>>>>> "Samuel" == Samuel Bronson <naesten@gmail.com> writes:
>
> Samuel> +# FIXME: The handling of e.g. std::basic_string (at least on char)
> Samuel> +# probably needs updating to work with Python 3's new string rules.
> Samuel> +#
> Samuel> +# In particular, Python 3 has a separate type (called byte) for
> Samuel> +# bytestrings, and a special b"" syntax for the byte literals; the old
> Samuel> +# str() type has been redefined to always store Unicode text.
> Samuel> +#
> Samuel> +# We probably can't do much about this until GDB get their act
> Samuel> +# together: <https://sourceware.org/bugzilla/show_bug.cgi?id=17138>
>
> I don't think this comment is applicable.
> The libstdc++ pretty-printers use gdb.Value.lazy_string, not the
> built-in Python types.

Hmm, doesn't that just make it a timebomb -- a value that will explode
if, at some point in the future, someone tries to treat it as a string?

-- 
Hi! I'm a .signature virus! Copy me into your ~/.signature to help me spread!

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v3 3/3] Port libstdc++ pretty-printers to Python 2 + Python 3
  2014-07-26  6:33         ` Samuel Bronson
@ 2014-07-30 14:20           ` Tom Tromey
  0 siblings, 0 replies; 11+ messages in thread
From: Tom Tromey @ 2014-07-30 14:20 UTC (permalink / raw)
  To: Samuel Bronson
  Cc: libstdc++, GCC Patches, Jonathan Wakely, GDB Patches, Matthias Klose

>> I don't think this comment is applicable.
>> The libstdc++ pretty-printers use gdb.Value.lazy_string, not the
>> built-in Python types.

Samuel> Hmm, doesn't that just make it a timebomb -- a value that will
Samuel> explode if, at some point in the future, someone tries to treat
Samuel> it as a string?

I don't think so.
The results from the pretty-printer, when called from gdb, are generally
either printed or discarded.
In any case the comment still doesn't seem correct.

Tom

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2014-07-30 14:04 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <1400701554-18062-1-git-send-email-naesten@gmail.com>
2014-07-11  2:48 ` [PATCH v2 0/3] Port libstdc++ pretty printers to Python 3 Samuel Bronson
2014-07-11  2:49   ` [PATCH v2 1/3] Make libstdc++ testsuite work with pre-color GCC versions again Samuel Bronson
2014-07-11 15:33     ` Jonathan Wakely
2014-07-11  3:00   ` [PATCH v2 2/3] libstdc++ testsuite: Turn off GDB's auto-load, list loaded libs Samuel Bronson
2014-07-11 15:33     ` Jonathan Wakely
2014-07-11  3:22   ` [PATCH v2 3/3] Port libstdc++ pretty-printers to Python 2 + Python 3 Samuel Bronson
2014-07-11  3:45     ` [PATCH v3 " Samuel Bronson
2014-07-11 13:49       ` Jonathan Wakely
2014-07-21 17:06       ` Tom Tromey
2014-07-26  6:33         ` Samuel Bronson
2014-07-30 14:20           ` Tom Tromey

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).