From: Simon Marchi <simon.marchi@polymtl.ca>
To: gdb-patches@sourceware.org
Cc: Simon Marchi <simon.marchi@polymtl.ca>
Subject: [PATCH 1/2] Fix sorting of enum values in FlagEnumerationPrinter
Date: Tue, 19 Jan 2016 04:23:00 -0000 [thread overview]
Message-ID: <1453177390-13881-1-git-send-email-simon.marchi@polymtl.ca> (raw)
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 <class 'AttributeError'> '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 <type 'exceptions.AttributeError'> '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 <string.h>
+
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
};
--
2.7.0
next reply other threads:[~2016-01-19 4:23 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-01-19 4:23 Simon Marchi [this message]
2016-01-19 4:23 ` [PATCH 2/2] Fix enum flag with Python 3 Simon Marchi
2016-01-19 11:03 ` Pedro Alves
2016-01-19 16:08 ` Simon Marchi
2016-01-19 11:02 ` [PATCH 1/2] Fix sorting of enum values in FlagEnumerationPrinter Pedro Alves
2016-01-19 16:41 ` Simon Marchi
2016-01-20 14:41 ` Pedro Alves
2016-01-20 18:03 ` Simon Marchi
2016-01-20 18:12 ` Simon Marchi
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1453177390-13881-1-git-send-email-simon.marchi@polymtl.ca \
--to=simon.marchi@polymtl.ca \
--cc=gdb-patches@sourceware.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox