Skip to content
This repository was archived by the owner on Jan 30, 2023. It is now read-only.

Commit fa16bc7

Browse files
committed
Merge branch 'u/saraedum/ticket/16251' of git://trac.sagemath.org/sage into ticket/16316
2 parents 7a0f094 + 62c9681 commit fa16bc7

File tree

4 files changed

+32
-6
lines changed

4 files changed

+32
-6
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
from sage.structure.element cimport CommutativeRingElement
22

33
cdef class MPolynomial(CommutativeRingElement):
4-
cdef long _hash_c(self)
4+
cdef long _hash_c(self) except -1

src/sage/rings/polynomial/multi_polynomial.pyx

+14-1
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,7 @@ cdef class MPolynomial(CommutativeRingElement):
551551
D[ETuple(tmp)] = a
552552
return D
553553

554-
cdef long _hash_c(self):
554+
cdef long _hash_c(self) except -1:
555555
"""
556556
This hash incorporates the variable name in an effort to respect the obvious inclusions
557557
into multi-variable polynomial rings.
@@ -577,6 +577,19 @@ cdef class MPolynomial(CommutativeRingElement):
577577
sage: f=x/y
578578
sage: f.subs({x:1})
579579
1/y
580+
581+
TESTS:
582+
583+
Verify that :trac:`16251` has been resolved, i.e., polynomials with
584+
unhashable coefficients are unhashable::
585+
586+
sage: K.<a> = Qq(9)
587+
sage: R.<t,s> = K[]
588+
sage: hash(t)
589+
Traceback (most recent call last):
590+
...
591+
TypeError: unhashable type: 'sage.rings.padics.padic_ZZ_pX_CR_element.pAdicZZpXCRElement'
592+
580593
"""
581594
cdef long result = 0 # store it in a c-int and just let the overflowing additions wrap
582595
cdef long result_mon

src/sage/rings/polynomial/polynomial_element.pxd

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ cdef class Polynomial(CommutativeAlgebraElement):
1414
cdef char _is_gen
1515
cdef CompiledPolynomialFunction _compiled
1616
cpdef Polynomial truncate(self, long n)
17-
cdef long _hash_c(self)
17+
cdef long _hash_c(self) except -1
1818
cpdef constant_coefficient(self)
1919
cpdef Polynomial _new_constant_poly(self, a, Parent P)
2020

src/sage/rings/polynomial/polynomial_element.pyx

+16-3
Original file line numberDiff line numberDiff line change
@@ -835,7 +835,7 @@ cdef class Polynomial(CommutativeAlgebraElement):
835835
def __hash__(self):
836836
return self._hash_c()
837837

838-
cdef long _hash_c(self):
838+
cdef long _hash_c(self) except -1:
839839
"""
840840
This hash incorporates the variable name in an effort to respect
841841
the obvious inclusions into multi-variable polynomial rings.
@@ -858,6 +858,19 @@ cdef class Polynomial(CommutativeAlgebraElement):
858858
sage: R.<x>=IntegerModRing(11)[]
859859
sage: hash(R.0)==hash(FractionField(R).0) # respect inclusions into the fraction field
860860
True
861+
862+
TESTS:
863+
864+
Verify that :trac:`16251` has been resolved, i.e., polynomials with
865+
unhashable coefficients are unhashable::
866+
867+
sage: K.<a> = Qq(9)
868+
sage: R.<t> = K[]
869+
sage: hash(t)
870+
Traceback (most recent call last):
871+
...
872+
TypeError: unhashable type: 'sage.rings.padics.padic_ZZ_pX_CR_element.pAdicZZpXCRElement'
873+
861874
"""
862875
cdef long result = 0 # store it in a c-int and just let the overflowing additions wrap
863876
cdef long result_mon
@@ -866,9 +879,9 @@ cdef class Polynomial(CommutativeAlgebraElement):
866879
cdef int i
867880
for i from 0<= i <= self.degree():
868881
if i == 1:
869-
# we delay the hashing until now to not waste it one a constant poly
882+
# we delay the hashing until now to not waste it on a constant poly
870883
var_name_hash = hash((<ParentWithGens>self._parent)._names[0])
871-
# I'm assuming (incorrectly) that hashes of zero indicate that the element is 0.
884+
# I'm assuming (incorrectly) that hashes of zero indicate that the element is 0.
872885
# This assumption is not true, but I think it is true enough for the purposes and it
873886
# it allows us to write fast code that omits terms with 0 coefficients. This is
874887
# important if we want to maintain the '==' relationship with sparse polys.

0 commit comments

Comments
 (0)