Skip to content

Commit 2f03e21

Browse files
committed
Fix hashing of universal constants
1 parent 4142c53 commit 2f03e21

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

cypari2/gen.pyx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -285,16 +285,23 @@ cdef class Gen(Gen_base):
285285
>>> L = pari("[42, 2/3, 3.14]")
286286
>>> hash(L) == hash(L.__copy__())
287287
True
288+
>>> hash(pari.isprime(4)) == hash(pari(0))
289+
True
288290
"""
289291
# There is a bug in PARI/GP where the hash value depends on the
290292
# CLONE bit. So we remove that bit before hashing. See
291293
# https://pari.math.u-bordeaux.fr/cgi-bin/bugreport.cgi?bug=2091
292294
cdef ulong* G = <ulong*>self.g
293295
cdef ulong G0 = G[0]
294-
G[0] &= ~<ulong>CLONEBIT
296+
cdef ulong G0clean = G0 & ~<ulong>CLONEBIT
297+
if G0 != G0clean:
298+
# Only write if we actually need to change something, as
299+
# G may point to read-only memory
300+
G[0] = G0clean
295301
h = hash_GEN(self.g)
296-
# Restore CLONE bit
297-
G[0] = G0
302+
if G0 != G0clean:
303+
# Restore CLONE bit
304+
G[0] = G0
298305
return h
299306

300307
def __iter__(self):

0 commit comments

Comments
 (0)