Skip to content

Commit 621b0ad

Browse files
committed
Make hashing fail for inexact arbs.
1 parent c24aa2f commit 621b0ad

File tree

2 files changed

+14
-13
lines changed

2 files changed

+14
-13
lines changed

src/flint/test/test_arb.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -126,21 +126,20 @@ def arb_pi(prec):
126126
(arb(10), arb(10), True),
127127
(arb(10), arb(11), False),
128128
(arb(10.0), arb(10), True),
129-
(
130-
arb(mid=10, rad=2),
131-
arb(mid=10, rad=2),
132-
True,
133-
),
134-
(
135-
arb(mid=10, rad=2),
136-
arb(mid=10, rad=3),
137-
False,
138-
),
139-
(arb_pi(100), arb_pi(100), True),
140-
(arb_pi(100), arb_pi(1000), False),
141129
]:
142130
assert (hash(x) == hash(y)) == expected
143131

132+
for x in [
133+
arb(mid=10, rad=2),
134+
arb_pi(100),
135+
]:
136+
try:
137+
hash(x)
138+
except ValueError:
139+
pass
140+
else:
141+
assert False, f"Expected {x} to raise an error if hashed, but succeeded."
142+
144143

145144

146145
# Tests for arithmetic functions in `flint.arb`.

src/flint/types/arb.pyx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,9 @@ cdef class arb(flint_scalar):
529529

530530
def __hash__(self):
531531
"""Hash."""
532-
return hash((self.mid().man_exp(), self.rad().man_exp()))
532+
if self.is_exact():
533+
return hash((self.mid().man_exp(), self.rad().man_exp()))
534+
raise ValueError(f"Cannot hash non-exact arb: {self}. See pull/341 for details.")
533535

534536
def __contains__(self, other):
535537
other = any_as_arb(other)

0 commit comments

Comments
 (0)