public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH 1/2] Fix sorting of enum values in FlagEnumerationPrinter
@ 2016-01-19  4:23 Simon Marchi
  2016-01-19  4:23 ` [PATCH 2/2] Fix enum flag with Python 3 Simon Marchi
  2016-01-19 11:02 ` [PATCH 1/2] Fix sorting of enum values in FlagEnumerationPrinter Pedro Alves
  0 siblings, 2 replies; 9+ messages in thread
From: Simon Marchi @ 2016-01-19  4:23 UTC (permalink / raw)
  To: gdb-patches; +Cc: Simon Marchi

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

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

end of thread, other threads:[~2016-01-20 18:12 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-01-19  4:23 [PATCH 1/2] Fix sorting of enum values in FlagEnumerationPrinter Simon Marchi
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

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).