Skip to content

Fix test failures under PyPy #99

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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