Skip to content

Commit

Permalink
Merge pull request #99 from oscarbenjamin/pr_pypy_fix
Browse files Browse the repository at this point in the history
Fix test failures under PyPy
  • Loading branch information
oscarbenjamin authored Oct 21, 2023
2 parents b160740 + a068011 commit 2fd4fd6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/flint/test/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import operator
import pickle
import doctest
import platform

from flint.utils.flint_exceptions import DomainError

Expand All @@ -11,6 +12,8 @@
if sys.version_info[0] >= 3:
long = int

PYPY = platform.python_implementation() == "PyPy"

ctx = flint.ctx

def raises(f, exception):
Expand Down Expand Up @@ -141,13 +144,17 @@ def test_fmpz():
for a, b, c, ab_mod_c in pow_mod_examples:
assert pow(a, b, c) == ab_mod_c
assert pow(flint.fmpz(a), b, c) == ab_mod_c
assert pow(a, flint.fmpz(b), c) == ab_mod_c
assert pow(a, b, flint.fmpz(c)) == ab_mod_c
assert pow(flint.fmpz(a), flint.fmpz(b), c) == ab_mod_c
assert pow(flint.fmpz(a), b, flint.fmpz(c)) == ab_mod_c
assert pow(a, flint.fmpz(b), flint.fmpz(c)) == ab_mod_c
assert pow(flint.fmpz(a), flint.fmpz(b), flint.fmpz(c)) == ab_mod_c

# 3-arg pow cannot be made to work with fmpz on PyPy
# https://github.com/flintlib/python-flint/issues/74
if not PYPY:
assert pow(a, flint.fmpz(b), c) == ab_mod_c
assert pow(a, b, flint.fmpz(c)) == ab_mod_c
assert pow(a, flint.fmpz(b), flint.fmpz(c)) == ab_mod_c

assert raises(lambda: pow(flint.fmpz(2), 2, 0), ValueError)
# XXX: Handle negative modulus like int?
assert raises(lambda: pow(flint.fmpz(2), 2, -1), ValueError)
Expand Down
9 changes: 9 additions & 0 deletions src/flint/types/fmpz_mod_poly.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -880,6 +880,9 @@ cdef class fmpz_mod_poly(flint_poly):
"""
return self[0]

# XXX: Methods like leading_coefficient() that are generic should be moved
# to the flint_poly base class rather than defined here.

def leading_coefficient(self):
"""
Return the leading coefficient of this polynomial.
Expand All @@ -889,6 +892,12 @@ cdef class fmpz_mod_poly(flint_poly):
>>> f.leading_coefficient()
fmpz_mod(3, 163)
"""
# XXX: This is a workaround for a Cython/PyPy bug:
# https://github.com/flintlib/python-flint/issues/74
# https://github.com/cython/cython/issues/5776
d = self.degree()
if d < 0:
return self.ctx.mod.zero()
return self[self.degree()]

def reverse(self, degree=None):
Expand Down

0 comments on commit 2fd4fd6

Please sign in to comment.