From patchwork Tue Jan 19 04:23:09 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Marchi X-Patchwork-Id: 10446 Received: (qmail 2821 invoked by alias); 19 Jan 2016 04:23:16 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 1843 invoked by uid 89); 19 Jan 2016 04:23:16 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=0.7 required=5.0 tests=BAYES_20, SPF_SOFTFAIL autolearn=no version=3.3.2 spammy=270, 2.7.0, H*Ad:D*ca, sk:excepti X-HELO: smtp.electronicbox.net Received: from smtp.electronicbox.net (HELO smtp.electronicbox.net) (96.127.255.83) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 19 Jan 2016 04:23:15 +0000 Received: from localhost.localdomain (cable-192.222.137.139.electronicbox.net [192.222.137.139]) by smtp.electronicbox.net (Postfix) with ESMTP id 3B8E3440E78; Mon, 18 Jan 2016 23:23:13 -0500 (EST) From: Simon Marchi To: gdb-patches@sourceware.org Cc: Simon Marchi Subject: [PATCH 1/2] Fix sorting of enum values in FlagEnumerationPrinter Date: Mon, 18 Jan 2016 23:23:09 -0500 Message-Id: <1453177390-13881-1-git-send-email-simon.marchi@polymtl.ca> The lambda function used to sort the enumerator list does not work properly. This list consists of tuples, (enum label, enum value). The key function returns x.enumval. enumval not being defined for a tuple, we see this exception in the test log: Python Exception 'tuple' object has no attribute 'enumval' The function should return the second item of the tuple, which is the enumval. The pretty-printer still worked mostly correctly, except that the enumeration values were not sorted. The test still passed because the enumeration values are already sorted where they are defined. The test also passed despite the exception being printed, because the right output was printed after the exception: print (enum flag_enum) (FLAG_1) Python Exception 'tuple' objecthas no attribute 'enumval':M $7 = 0x1 [FLAG_1] (gdb) PASS: gdb.python/py-pp-maint.exp: print FLAG_1 To properly test the sorting, I changed the order in which the enumeration values are defined. gdb/ChangeLog: * python/lib/gdb/printing.py (FlagEnumerationPrinter.__call__): Fix enumerators sort key function. gdb/testsuite/ChangeLog: * gdb.python/py-pp-main.c (enum flag_enum): Change enum values definition order. --- gdb/python/lib/gdb/printing.py | 2 +- gdb/testsuite/gdb.python/py-pp-maint.c | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/gdb/python/lib/gdb/printing.py b/gdb/python/lib/gdb/printing.py index 263d3ba..0b4a152 100644 --- a/gdb/python/lib/gdb/printing.py +++ b/gdb/python/lib/gdb/printing.py @@ -263,7 +263,7 @@ class FlagEnumerationPrinter(PrettyPrinter): self.enumerators.append((field.name, field.enumval)) # Sorting the enumerators by value usually does the right # thing. - self.enumerators.sort(key = lambda x: x.enumval) + self.enumerators.sort(key = lambda x: x[1]) if self.enabled: return _EnumInstance(self.enumerators, val) diff --git a/gdb/testsuite/gdb.python/py-pp-maint.c b/gdb/testsuite/gdb.python/py-pp-maint.c index 657dfd7..dc282e9 100644 --- a/gdb/testsuite/gdb.python/py-pp-maint.c +++ b/gdb/testsuite/gdb.python/py-pp-maint.c @@ -17,11 +17,14 @@ #include + enum flag_enum { - FLAG_1 = 1, + /* Define the enumration values in an unsorted manner to verify that we + effectively sort them by value. */ FLAG_2 = 2, FLAG_3 = 4, + FLAG_1 = 1, ALL = FLAG_1 | FLAG_2 | FLAG_3 };