From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 3807 invoked by alias); 19 Jan 2016 04:23:17 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Received: (qmail 1824 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, H*r:sk:electro 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 64546440E79; Mon, 18 Jan 2016 23:23:13 -0500 (EST) From: Simon Marchi To: gdb-patches@sourceware.org Cc: Simon Marchi Subject: [PATCH 2/2] Fix enum flag with Python 3 Date: Tue, 19 Jan 2016 04:23:00 -0000 Message-Id: <1453177390-13881-2-git-send-email-simon.marchi@polymtl.ca> In-Reply-To: <1453177390-13881-1-git-send-email-simon.marchi@polymtl.ca> References: <1453177390-13881-1-git-send-email-simon.marchi@polymtl.ca> X-SW-Source: 2016-01/txt/msg00409.txt.bz2 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 %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('' % 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