From patchwork Thu Oct 31 00:24:01 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matthias Klose X-Patchwork-Id: 287368 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 463DB2C03B2 for ; Thu, 31 Oct 2013 11:24:29 +1100 (EST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :message-id:date:from:mime-version:to:cc:subject:content-type; q=dns; s=default; b=YKPgTjVl3tODr/sd7/S6BGmjaphrvnh8lkq4WZ1yjct jJnZlAL0CXGlJ1SQA+nHl4XSq0pTE0eyFs1nO+dAZV7YkBGE46fGmsWLM4vLeWAu Ag0mKwFuPKq67RB2RoNa9BP9tWOhUJ6iia4BGIkpSns7XQGyBpcyz9KgqOuALbuk = DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :message-id:date:from:mime-version:to:cc:subject:content-type; s=default; bh=KzGJ/Bq7C75lOjmmdixZJllQ9IA=; b=b/G6Uj0O2P0jVSPZZ rRsbCm/s06YOSrbZ2nLYDuWQWXTeiPfhZ2E5aBpwsJIbffN5anrPURyBSEvo177F Uvpy942m2TcNiJHUO044cQDnHUgf2jVoaNYCRj4PXAiIs2Sk+rJYhSZQKMMw9zsd VgVPqJ31xYzhPyaQ509/QJ3Nz0= Received: (qmail 8734 invoked by alias); 31 Oct 2013 00:24:13 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 8712 invoked by uid 89); 31 Oct 2013 00:24:12 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-3.4 required=5.0 tests=AWL, BAYES_00, RCVD_IN_DNSWL_LOW autolearn=ham version=3.3.2 X-Spam-User: qpsmtpd, 2 recipients X-HELO: einhorn.in-berlin.de Received: from einhorn.in-berlin.de (HELO einhorn.in-berlin.de) (192.109.42.8) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES256-SHA encrypted) ESMTPS; Thu, 31 Oct 2013 00:24:07 +0000 X-Envelope-From: doko@ubuntu.com Received: from [10.36.247.124] (sccc-66-78-236-243.smartcity.com [66.78.236.243]) (authenticated bits=0) by einhorn.in-berlin.de (8.13.6/8.13.6/Debian-1) with ESMTP id r9V0O1Wu013740; Thu, 31 Oct 2013 01:24:02 +0100 Message-ID: <5271A321.2000002@ubuntu.com> Date: Thu, 31 Oct 2013 01:24:01 +0100 From: Matthias Klose User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.0 MIME-Version: 1.0 To: GCC Patches , "libstdc++@gcc.gnu.org" CC: Tom Tromey Subject: [patch] make the libstdc++ pretty printers compatible with both Python 2 and Python3 Starting with gdb 7.6, gdb can be linked with both Python 2.x and Python 3.x. Therefore the pretty printers should be compatible with both Python versions. This patch should be backported to 4.7 and 4.8 as well. Ok for the trunk? Matthias * python/libstdcxx/v6/printers.py: Make pretty printers compatible with Both python 2.x and 3.x. Index: python/libstdcxx/v6/printers.py =================================================================== --- python/libstdcxx/v6/printers.py (revision 204231) +++ python/libstdcxx/v6/printers.py (working copy) @@ -15,8 +15,14 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . +# Keep this module compatible with both Python 2 and Python 3 + import gdb -import itertools +import sys +if sys.version_info[0] < 3: + import itertools + map = itertools.imap + zip = itertools.izip import re # Try to use the new-style pretty-printing if available. @@ -51,7 +57,7 @@ # anything fancier here. field = typ.fields()[0] if not field.is_base_class: - raise ValueError, "Cannot find type %s::%s" % (str(orig), name) + raise ValueError("Cannot find type %s::%s" % (str(orig), name)) typ = field.type class SharedPointerPrinter: @@ -97,7 +103,7 @@ 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() @@ -105,6 +111,7 @@ count = self.count self.count = self.count + 1 return ('[%d]' % count, elt['_M_data']) + next = __next__ def __init__(self, typename, val): self.typename = typename @@ -144,7 +151,7 @@ def __iter__(self): return self - def next(self): + def __next__(self): if self.base == 0: raise StopIteration elt = self.base.cast(self.nodetype).dereference() @@ -152,6 +159,7 @@ count = self.count self.count = self.count + 1 return ('[%d]' % count, elt['_M_data']) + next = __next__ def __init__(self, typename, val): self.val = val @@ -198,7 +206,7 @@ def __iter__(self): return self - def next(self): + def __next__(self): count = self.count self.count = self.count + 1 if self.bitvec: @@ -220,6 +228,7 @@ elt = self.item.dereference() self.item = self.item + 1 return ('[%d]' % count, elt) + next = __next__ def __init__(self, typename, val): self.typename = typename @@ -276,20 +285,20 @@ # Set the actual head to the first pair. self.head = self.head.cast (nodes[0].type) elif len (nodes) != 0: - raise ValueError, "Top of tuple tree does not consist of a single node." + raise ValueError("Top of tuple tree does not consist of a single node.") self.count = 0 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: raise StopIteration # Check that this iteration has an expected structure. if len (nodes) != 2: - raise ValueError, "Cannot parse more than 2 nodes in a tuple tree." + raise ValueError("Cannot parse more than 2 nodes in a tuple tree.") # - Left node is the next recursion parent. # - Right node is the actual class contained in the tuple. @@ -309,6 +318,7 @@ return ('[%d]' % self.count, impl) else: return ('[%d]' % self.count, impl['_M_head_impl']) + next = __next__ def __init__ (self, typename, val): self.typename = typename @@ -353,7 +363,7 @@ def __len__(self): return int (self.size) - def next(self): + def __next__(self): if self.count == self.size: raise StopIteration result = self.node @@ -374,6 +384,7 @@ node = parent self.node = node return result + next = __next__ # This is a pretty printer for std::_Rb_tree_iterator (which is # std::map::iterator), and has nothing to do with the RbtreeIterator @@ -414,9 +425,9 @@ 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()['_M_value_field'] self.pair = n item = n['first'] @@ -425,6 +436,7 @@ result = ('[%d]' % self.count, item) self.count = self.count + 1 return result + next = __next__ def __init__ (self, typename, val): self.typename = typename @@ -456,14 +468,15 @@ 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()['_M_value_field'] # FIXME: this is weird ... what to do? # Maybe a 'set' display hint? result = ('[%d]' % self.count, item) self.count = self.count + 1 return result + next = __next__ def __init__ (self, typename, val): self.typename = typename @@ -534,7 +547,7 @@ def __iter__(self): return self - def next(self): + def __next__(self): if self.p == self.last: raise StopIteration @@ -551,6 +564,7 @@ self.end = self.p + self.buffer_size return result + next = __next__ def __init__(self, typename, val): self.typename = typename @@ -572,7 +586,7 @@ size = self.buffer_size * delta_n + delta_s + delta_e - return '%s with %d elements' % (self.typename, long (size)) + return '%s with %d elements' % (self.typename, int (size)) def children(self): start = self.val['_M_impl']['_M_start'] @@ -635,7 +649,7 @@ def __iter__ (self): return self - def next (self): + def __next__ (self): if self.node == 0: raise StopIteration node = self.node.cast(self.node_type) @@ -649,6 +663,7 @@ break self.bucket = self.bucket + 1 return result + next = __next__ class StdHashtableIterator: def __init__(self, hash): @@ -658,7 +673,7 @@ 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() @@ -666,6 +681,7 @@ valptr = elt['_M_storage'].address valptr = valptr.cast(elt.type.template_argument(0).pointer()) return valptr.dereference() + next = __next__ class Tr1UnorderedSetPrinter: "Print a tr1::unordered_set" @@ -687,10 +703,10 @@ return '[%d]' % i def children (self): - counter = itertools.imap (self.format_count, itertools.count()) + counter = map (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 zip (counter, Tr1HashtableIterator (self.hashtable())) + return zip (counter, StdHashtableIterator (self.hashtable())) class Tr1UnorderedMapPrinter: "Print a tr1::unordered_map" @@ -722,17 +738,17 @@ return '[%d]' % i def children (self): - counter = itertools.imap (self.format_count, itertools.count()) + counter = map (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 (map (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 zip (counter, data) + data = self.flatten (map (self.format_one, StdHashtableIterator (self.hashtable()))) # Zip the two iterators together. - return itertools.izip (counter, data) - + return zip (counter, data) + def display_hint (self): return 'map' @@ -748,7 +764,7 @@ def __iter__(self): return self - def next(self): + def __next__(self): if self.base == 0: raise StopIteration elt = self.base.cast(self.nodetype).dereference() @@ -758,6 +774,7 @@ valptr = elt['_M_storage'].address valptr = valptr.cast(elt.type.template_argument(0).pointer()) return ('[%d]' % count, valptr.dereference()) + next = __next__ def __init__(self, typename, val): self.val = val @@ -808,7 +825,7 @@ # A small sanity check. # FIXME if not self.compiled_rx.match(name + '<>'): - raise ValueError, 'libstdc++ programming error: "%s" does not match' % name + raise ValueError('libstdc++ programming error: "%s" does not match' % name) printer = RxPrinter(name, function) self.subprinters.append(printer) self.lookup[name] = printer