From 14086ed75d165b827b20de0101324bd8467d93c4 Mon Sep 17 00:00:00 2001 From: Enrico Granata Date: Wed, 10 Feb 2016 02:12:42 +0000 Subject: [PATCH] Change lldb.value.__int__() so that it takes into account the signedness of the value being cast to return a Python number with the proper value The explicit APIs on SBValue obviously remain if one wants to be explicit in intent, or override this guess, but since __int__() has to pick one, an educated guess is definitely better than than always going to signed regardless Fixes rdar://24556976 llvm-svn: 260349 --- .../test/python_api/value/TestValueAPI.py | 9 ++++ .../lldbsuite/test/python_api/value/main.c | 4 ++ lldb/scripts/Python/python-extensions.swig | 51 +++++++++++++++++-- 3 files changed, 61 insertions(+), 3 deletions(-) diff --git a/lldb/packages/Python/lldbsuite/test/python_api/value/TestValueAPI.py b/lldb/packages/Python/lldbsuite/test/python_api/value/TestValueAPI.py index 11b5d3c890a3..2a53177d28af 100644 --- a/lldb/packages/Python/lldbsuite/test/python_api/value/TestValueAPI.py +++ b/lldb/packages/Python/lldbsuite/test/python_api/value/TestValueAPI.py @@ -134,3 +134,12 @@ class ValueAPITestCase(TestBase): val_a = target.EvaluateExpression('a') self.assertTrue(val_s.GetChildMemberWithName('a').AddressOf(), VALID_VARIABLE) self.assertTrue(val_a.Cast(val_i.GetType()).AddressOf(), VALID_VARIABLE) + + self.assertTrue(int(lldb.value(frame0.FindVariable('uinthex'))) == 3768803088, 'uinthex == 3768803088') + self.assertTrue(int(lldb.value(frame0.FindVariable('sinthex'))) == -526164208, 'sinthex == -526164208') + + self.assertTrue(frame0.FindVariable('uinthex').GetValueAsUnsigned() == 3768803088, 'unsigned uinthex == 3768803088') + self.assertTrue(frame0.FindVariable('sinthex').GetValueAsUnsigned() == 3768803088, 'unsigned sinthex == 3768803088') + + self.assertTrue(frame0.FindVariable('uinthex').GetValueAsSigned() == -526164208, 'signed uinthex == -526164208') + self.assertTrue(frame0.FindVariable('sinthex').GetValueAsSigned() == -526164208, 'signed sinthex == -526164208') diff --git a/lldb/packages/Python/lldbsuite/test/python_api/value/main.c b/lldb/packages/Python/lldbsuite/test/python_api/value/main.c index a00795750de0..2ebe3ad303cf 100644 --- a/lldb/packages/Python/lldbsuite/test/python_api/value/main.c +++ b/lldb/packages/Python/lldbsuite/test/python_api/value/main.c @@ -7,6 +7,7 @@ // //===----------------------------------------------------------------------===// #include +#include // This simple program is to test the lldb Python API SBValue.GetChildAtIndex(). @@ -38,6 +39,9 @@ struct MyStruct int main (int argc, char const *argv[]) { + uint32_t uinthex = 0xE0A35F10; + int32_t sinthex = 0xE0A35F10; + int i; MyInt a = 12345; struct MyStruct s = { 11, 22 }; diff --git a/lldb/scripts/Python/python-extensions.swig b/lldb/scripts/Python/python-extensions.swig index fae7f401bf13..1e43e53a514d 100644 --- a/lldb/scripts/Python/python-extensions.swig +++ b/lldb/scripts/Python/python-extensions.swig @@ -1024,11 +1024,13 @@ class value(object): return complex (int(self)) def __int__(self): + is_num,is_sign = is_numeric_type(self.sbvalue.GetType().GetCanonicalType().GetBasicType()) + if is_num and not is_sign: return self.sbvalue.GetValueAsUnsigned() return self.sbvalue.GetValueAsSigned() - + def __long__(self): - return self.sbvalue.GetValueAsSigned() - + return self.__int__() + def __float__(self): return float (self.sbvalue.GetValueAsSigned()) @@ -1084,4 +1086,47 @@ class SBSyntheticValueProvider(object): return False +%} + +%pythoncode %{ + +# given an lldb.SBBasicType it returns a tuple +# (is_numeric, is_signed) +# the value of is_signed is undefined if is_numeric == false +def is_numeric_type(basic_type): + if basic_type == eBasicTypeInvalid: return (False,False) + if basic_type == eBasicTypeVoid: return (False,False) + if basic_type == eBasicTypeChar: return (True,False) + if basic_type == eBasicTypeSignedChar: return (True,True) + if basic_type == eBasicTypeUnsignedChar: return (True,False) + if basic_type == eBasicTypeWChar: return (True,False) + if basic_type == eBasicTypeSignedWChar: return (True,True) + if basic_type == eBasicTypeUnsignedWChar: return (True,False) + if basic_type == eBasicTypeChar16: return (True,False) + if basic_type == eBasicTypeChar32: return (True,False) + if basic_type == eBasicTypeShort: return (True,True) + if basic_type == eBasicTypeUnsignedShort: return (True,False) + if basic_type == eBasicTypeInt: return (True,True) + if basic_type == eBasicTypeUnsignedInt: return (True,False) + if basic_type == eBasicTypeLong: return (True,True) + if basic_type == eBasicTypeUnsignedLong: return (True,False) + if basic_type == eBasicTypeLongLong: return (True,True) + if basic_type == eBasicTypeUnsignedLongLong: return (True,False) + if basic_type == eBasicTypeInt128: return (True,True) + if basic_type == eBasicTypeUnsignedInt128: return (True,False) + if basic_type == eBasicTypeBool: return (False,False) + if basic_type == eBasicTypeHalf: return (True,True) + if basic_type == eBasicTypeFloat: return (True,True) + if basic_type == eBasicTypeDouble: return (True,True) + if basic_type == eBasicTypeLongDouble: return (True,True) + if basic_type == eBasicTypeFloatComplex: return (True,True) + if basic_type == eBasicTypeDoubleComplex: return (True,True) + if basic_type == eBasicTypeLongDoubleComplex: return (True,True) + if basic_type == eBasicTypeObjCID: return (False,False) + if basic_type == eBasicTypeObjCClass: return (False,False) + if basic_type == eBasicTypeObjCSel: return (False,False) + if basic_type == eBasicTypeNullPtr: return (False,False) + #if basic_type == eBasicTypeOther: + return (False,False) + %} \ No newline at end of file