Skip to content

Commit 4328fbf

Browse files
committed
fixup(5165105), fix(builtins/method): bytes/bytearray __eq__ results wrong
1 parent ecf2bd9 commit 4328fbf

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

Objects/byteobjects.nim

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,17 @@ import ../Utils/addr0
1010
#XXX: Nim's string ops has bugs for NUL('\0') char, e.g. len('1\02') gives 2
1111
declarePyType Bytes(tpToken):
1212
items: seq[char]
13-
setHash: bool
14-
privateHash: Hash
13+
setHash{.private.}: bool
14+
privateHash{.private.}: Hash
1515

1616
declarePyType ByteArray(reprLock, mutable):
1717
items: seq[char]
1818

19-
proc hash*(self: PyBytesObject): Hash = Py_HashBuffer(self.items)
19+
proc hash*(self: PyBytesObject): Hash =
20+
if self.setHash: return self.privateHash
21+
self.setHash = true
22+
result = Py_HashBuffer(self.items)
23+
self.privateHash = result
2024

2125
type PyBytesWriter* = object
2226
#overallocate*: bool
@@ -46,6 +50,8 @@ proc `$`(self: seq[char]): string =
4650

4751
type PyByteLike = PyBytesObject or PyByteArrayObject
4852

53+
proc `==`*(a, b: PyBytesObject): bool {. inline .} = a.hash == b.hash and a.items == b.items
54+
proc `==`*(a, b: PyByteArrayObject): bool {. inline .} = a.items == b.items
4955
proc len*(s: PyByteLike): int {. inline, cdecl .} = s.items.len
5056
proc `$`*(s: PyByteLike): string = $s.items
5157
iterator items*(s: PyByteLike): char =

0 commit comments

Comments
 (0)