我正在尝试编写一个 Python 函数来格式化Foundation.Decimal, 用作类型摘要器。我把它发布在这个答案中。我还将它包含在这个答案的底部,并带有额外的调试打印。
我现在发现了一个错误,但我不知道该错误是在我的函数中,还是在 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
Run Code Online (Sandbox Code Playgroud)
上面,7调试器输出来自我的类型摘要器,并且是正确的。
3> var dict = [String: Decimal]()
dict: [String : Decimal] = 0 key/value pairs
4> dict["x"] = dec
5> dict["x"]
$R0: Decimal? = 7
Run Code Online (Sandbox Code Playgroud)
以上,7再次来自我的类型总结器,并且是正确的。
6> dict
$R1: [String : Decimal] = 1 key/value pair {
[0] = {
key = "x"
value = 0
}
}
Run Code Online (Sandbox Code Playgroud)
上面的0(in value = 0) 来自我的类型摘要,并且是不正确的。它应该是7。
那么为什么它为零呢?我的 Python 函数被赋予了一个SBValue. 它GetData()要求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
Run Code Online (Sandbox Code Playgroud)
上面,我们可以看到加载地址是假的,但是 的字节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
}
}
Run Code Online (Sandbox Code Playgroud)
上面我们可以看到加载地址仍然是假的,现在的字节SBData是不正确的。仍然SBData包含 20 个字节(a 的正确数字Foundation.Decimal,又名NSDecimal),但现在00已在前面插入了四个字节,并且最后四个字节已被删除。
这是我的具体问题:
我是否错误地使用了 lldb API,从而得到了错误的答案?如果是这样,我做错了什么以及我应该如何纠正?
如果我正确使用 lldb API,那么这是 lldb 中的错误,还是 Swift 编译器发出不正确的元数据?如何找出哪个工具存在错误?(因为如果其中一个工具存在错误,我想提交错误报告。)
如果这是 lldb 或 Swift 中的错误,我该如何解决该问题,以便Decimal在 a 是 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"')
Run Code Online (Sandbox Code Playgroud)
这看起来像一个 lldb 错误。请通过http://bugs.swift.org针对 lldb 提交有关此问题的错误。
背景知识:在字典案件中,你的背后发生了一些魔法。我无法在 REPL 中显示这一点,但如果您有一个 [String : Decimal] 数组作为某些实际代码中的局部变量,并且执行以下操作:
(lldb) frame variable --raw dec_array
(Swift.Dictionary<Swift.String, Foundation.Decimal>) dec_array = {
_variantBuffer = native {
native = {
_storage = 0x0000000100d05780 {
Swift._SwiftNativeNSDictionary = {}
bucketCount = {
_value = 2
}
count = {
_value = 1
}
initializedEntries = {
values = {
_rawValue = 0x0000000100d057d0
}
bitCount = {
_value = 2
}
}
keys = {
_rawValue = 0x0000000100d057d8
}
values = {
_rawValue = 0x0000000100d057f8
}
seed = {
0 = {
_value = -5794706384231184310
}
1 = {
_value = 8361200869849021207
}
}
}
}
cocoa = {
cocoaDictionary = 0x00000001000021b0
}
}
}
Run Code Online (Sandbox Code Playgroud)
Swift Dictionary 实际上并不包含任何明显的字典元素,当然也不包含 ivars。因此,lldb 有一个用于 Swift 字典的“合成子提供程序”,它为字典的键和值组成 SBValues,它是格式化程序正在处理的那些合成子提供者之一。
这也是加载地址为-1的原因。这实际上意味着“这是一个合成的东西,其数据 lldb 直接管理,而不是程序中某个地址的东西。” REPL 结果也是如此,它们更像是 lldb 维护的虚构内容。但是,如果您查看 Decimal 类型的局部变量,您会看到一个有效的加载地址,因为它位于内存中的某个位置。
无论如何,显然我们用来表示字典值的 Synthetic 子代 Decimal 对象没有正确设置数据的开头。有趣的是,如果您制作一个 [Decimal : String] 字典,则关键字段的 SBData 是正确的,并且您的格式化程序可以工作。只是价值观不正确而已。
我对以字符串作为值的字典尝试了同样的操作,并且 SBData 看起来是正确的。Decimal 有一些有趣的地方。不管怎样,感谢您的关注,请提交错误。
| 归档时间: |
|
| 查看次数: |
273 次 |
| 最近记录: |