From: Simon Marchi <simon.marchi@polymtl.ca>
To: gdb-patches@sourceware.org
Cc: Simon Marchi <simon.marchi@polymtl.ca>
Subject: [PATCH 2/2] Fix enum flag with Python 3
Date: Tue, 19 Jan 2016 04:23:00 -0000 [thread overview]
Message-ID: <1453177390-13881-2-git-send-email-simon.marchi@polymtl.ca> (raw)
In-Reply-To: <1453177390-13881-1-git-send-email-simon.marchi@polymtl.ca>
Using Python 3.5 (I assume it's the same with 3.4 and lower, but I didn't
test), I see this:
print (enum flag_enum) (FLAG_1)^M
Python Exception <class 'TypeError'> %x format: an integer is required, not gdb.Value: ^M
$7 = ^M
(gdb) FAIL: gdb.python/py-pp-maint.exp: print FLAG_1
Apparently, this idiom, where v is a gdb.Value, was possible with Python 2,
but not with Python 3:
'%x' % v
In Python 2, it would automatically get converted to an integer. To solve
it, I simply added wrapped v in a call to int().
'%x' % int(v)
In Python 2, the int type is implemented with a "long" in C, so on x86-32 it's
32-bits. I was worried that doing int(v) would truncate the value and give
wrong results for enum values > 32-bits. However, the int type != the int
function. The int function does the right thing, selecting the right integer
type for the given value. I tested with large enum values on x86-32 and
Python 2, and everything works as expected.
gdb/ChangeLog:
* python/lib/gdb/printing.py (_EnumInstance.to_string): Explicitly
convert gdb.Value to integer type using int().
---
gdb/python/lib/gdb/printing.py | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/gdb/python/lib/gdb/printing.py b/gdb/python/lib/gdb/printing.py
index 0b4a152..63c3aeb 100644
--- a/gdb/python/lib/gdb/printing.py
+++ b/gdb/python/lib/gdb/printing.py
@@ -239,7 +239,7 @@ class _EnumInstance:
if not any_found or v != 0:
# Leftover value.
flag_list.append('<unknown: 0x%x>' % v)
- return "0x%x [%s]" % (self.val, " | ".join(flag_list))
+ return "0x%x [%s]" % (int(self.val), " | ".join(flag_list))
class FlagEnumerationPrinter(PrettyPrinter):
"""A pretty-printer which can be used to print a flag-style enumeration.
--
2.7.0
next prev parent 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 [PATCH 1/2] Fix sorting of enum values in FlagEnumerationPrinter Simon Marchi
2016-01-19 4:23 ` Simon Marchi [this message]
2016-01-19 11:03 ` [PATCH 2/2] Fix enum flag with Python 3 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-2-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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).