Skip to content

Commit de61be7

Browse files
committed
fixup(4328fbf),bypass(NIM-BUG): provide alt impl for Py_Hash for bytes by disallow setting when JS
1 parent 6bab12b commit de61be7

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

Objects/hash_def.nim

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,26 @@ type PyHash_FuncDef* = object
2828
hash_bits*,
2929
seed_bits*: int
3030

31+
const Py_SupHashBuffer* = not defined(js) #XXX:NIM-BUG: js has bug to run hashData
32+
3133
var PyHash_Func = PyHash_FuncDef(
3234
hash: hashData,
3335
name: ALGO,
3436
hash_bits: 64,
3537
seed_bits: 8*sizeof(int),
3638
)
3739
proc PyHash_GetFuncDef*: PyHash_FuncDef{.inline.} = PyHash_Func
38-
proc PyHash_SetFuncDef*(x: PyHash_FuncDef) = PyHash_Func = x
3940

40-
proc Py_HashBuffer*(p: pointer, n: int): Hash{.raises: [].} = PyHash_Func.hash(p, n)
41-
proc Py_HashBuffer*[T](p: openArray[T]): Hash = Py_HashBuffer(p.addr0, p.len * sizeof T)
41+
when Py_SupHashBuffer:
42+
proc PyHash_SetFuncDef*(x: PyHash_FuncDef) = PyHash_Func = x
43+
proc Py_HashBuffer*(p: pointer, n: int): Hash{.raises: [].} = PyHash_Func.hash(p, n)
44+
proc Py_HashBuffer*[T](p: openArray[T]): Hash = Py_HashBuffer(p.addr0, p.len * sizeof T)
45+
else:
46+
const notSup = "set Py_HashBuffer is not supported in this platform"
47+
proc PyHash_SetFuncDef*(x: PyHash_FuncDef){.error: notSup.}
48+
proc Py_HashBuffer*(p: pointer, n: int): Hash{.raises: [], error: notSup.}
49+
proc Py_HashBuffer*[T](p: openArray[T]): Hash =
50+
for i in p:
51+
result = result !& cast[int](i)
52+
result = !$result
4253

43-
const Py_SupHashBuffer* = not defined(js)

Objects/stringobject.nim

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -169,12 +169,7 @@ template itemSize*(self: UnicodeVariant): int =
169169

170170
proc hashImpl(self: UnicodeVariant): Hash {. inline, cdecl .} =
171171
template forLoop(ls): untyped =
172-
when Py_SupHashBuffer:
173-
Py_HashBuffer(ls.addr0, ls.len * self.itemSize)
174-
else:
175-
for i in ls:
176-
result = result !& cast[int](i)
177-
result = !$result
172+
Py_HashBuffer(ls.addr0, ls.len * self.itemSize)
178173
if self.ascii:
179174
forLoop self.asciiStr
180175
else:

0 commit comments

Comments
 (0)