我正在尝试编写一个Python函数来格式化Foundation.Decimal
,用作类型摘要器。我把它发布在这个答案 https://stackoverflow.com/a/52749749/77567。我还将它包含在这个答案的底部,并带有额外的调试打印。
我现在发现了一个错误,但我不知道该错误是在我的函数中,还是在 lldb 中,或者可能在 Swift 编译器中。
这是演示该错误的文字记录。我加载我的类型摘要器~/.lldbinit
,因此 Swift REPL 使用它。
:; xcrun swift
registering Decimal type summaries
Welcome to Apple Swift version 4.2 (swiftlang-1000.11.37.1 clang-1000.11.45.1). Type :help for assistance.
1> import Foundation
2> let dec: Decimal = 7
dec: Decimal = 7
上面,7
调试器输出来自我的类型摘要器并且是正确的。
3> var dict = [String: Decimal]()
dict: [String : Decimal] = 0 key/value pairs
4> dict["x"] = dec
5> dict["x"]
$R0: Decimal? = 7
上面,7
再次来自我的类型总结器,并且是正确的。
6> dict
$R1: [String : Decimal] = 1 key/value pair {
[0] = {
key = "x"
value = 0
}
}
上面,0
(in value = 0
) 来自我的类型总结器,并且是不正确。它应该是7
.
那么为什么它为零呢?我的 Python 函数被赋予SBValue
。它调用GetData()
on the SBValue
得到一个SBData
。我向函数添加了调试打印以打印中的字节SBData
,并打印结果sbValue.GetLoadAddress()
。这是带有这些调试打印的文字记录:
:; xcrun swift
registering Decimal type summaries
Welcome to Apple Swift version 4.2 (swiftlang-1000.11.37.1 clang-1000.11.45.1). Type :help for assistance.
1> import Foundation
2> let dec: Decimal = 7
dec: Decimal = loadAddress: ffffffffffffffff
data: 00 21 00 00 07 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
7
上面,我们可以看到加载地址是假的,但是加载的字节SBData
是正确的(字节 1,21
,包含长度和标志;字节 4,“07”,是有效数的第一个字节)。
3> var dict = [String: Decimal]()
dict: [String : Decimal] = 0 key/value pairs
4> dict["x"] = dec
5> dict
$R0: [String : Decimal] = 1 key/value pair {
[0] = {
key = "x"
value = loadAddress: ffffffffffffffff
data: 00 00 00 00 00 21 00 00 07 00 00 00 00 00 00 00 00 00 00 00
0
}
}
上面,我们可以看到加载地址仍然是假的,现在的字节SBData
是不正确的。这SBData
仍然包含 20 个字节(正确的数字Foundation.Decimal
, aka NSDecimal
),但现在有四个00
字节已插入到前面,最后四个字节已被删除。
这是我的具体问题:
我是否错误地使用了 lldb API,从而得到了错误的答案?如果是这样,我做错了什么以及我应该如何纠正?
如果我正确使用 lldb API,那么这是 lldb 中的错误,还是 Swift 编译器发出不正确的元数据?如何找出哪个工具存在错误? (因为如果其中一个工具存在错误,我想提交错误报告。)
如果这是 lldb 或 Swift 中的错误,我该如何解决该问题,以便我可以格式化Decimal
当它是 a 的一部分时正确地Dictionary
?
这是我的类型格式化程序,带有调试打印:
# Decimal / NSDecimal support for lldb
#
# Put this file somewhere, e.g. ~/.../lldb/Decimal.py
# Then add this line to ~/.lldbinit:
# command script import ~/.../lldb/Decimal.py
import lldb
def stringForDecimal(sbValue, internal_dict):
from decimal import Decimal, getcontext
print(' loadAddress: %x' % sbValue.GetLoadAddress())
sbData = sbValue.GetData()
if not sbData.IsValid():
raise Exception('unable to get data: ' + sbError.GetCString())
if sbData.GetByteSize() != 20:
raise Exception('expected data to be 20 bytes but found ' + repr(sbData.GetByteSize()))
sbError = lldb.SBError()
exponent = sbData.GetSignedInt8(sbError, 0)
if sbError.Fail():
raise Exception('unable to read exponent byte: ' + sbError.GetCString())
flags = sbData.GetUnsignedInt8(sbError, 1)
if sbError.Fail():
raise Exception('unable to read flags byte: ' + sbError.GetCString())
length = flags & 0xf
isNegative = (flags & 0x10) != 0
debugString = ''
for i in range(20):
debugString += ' %02x' % sbData.GetUnsignedInt8(sbError, i)
print(' data:' + debugString)
if length == 0 and isNegative:
return 'NaN'
if length == 0:
return '0'
getcontext().prec = 200
value = Decimal(0)
scale = Decimal(1)
for i in range(length):
digit = sbData.GetUnsignedInt16(sbError, 4 + 2 * i)
if sbError.Fail():
raise Exception('unable to read memory: ' + sbError.GetCString())
value += scale * Decimal(digit)
scale *= 65536
value = value.scaleb(exponent)
if isNegative:
value = -value
return str(value)
def __lldb_init_module(debugger, internal_dict):
print('registering Decimal type summaries')
debugger.HandleCommand('type summary add Foundation.Decimal -F "' + __name__ + '.stringForDecimal"')
debugger.HandleCommand('type summary add NSDecimal -F "' + __name__ + '.stringForDecimal"')