Skip to content

Commit 2fd4fd6

Browse files
Merge pull request #99 from oscarbenjamin/pr_pypy_fix
Fix test failures under PyPy
2 parents b160740 + a068011 commit 2fd4fd6

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

src/flint/test/test.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import operator
44
import pickle
55
import doctest
6+
import platform
67

78
from flint.utils.flint_exceptions import DomainError
89

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

15+
PYPY = platform.python_implementation() == "PyPy"
16+
1417
ctx = flint.ctx
1518

1619
def raises(f, exception):
@@ -141,13 +144,17 @@ def test_fmpz():
141144
for a, b, c, ab_mod_c in pow_mod_examples:
142145
assert pow(a, b, c) == ab_mod_c
143146
assert pow(flint.fmpz(a), b, c) == ab_mod_c
144-
assert pow(a, flint.fmpz(b), c) == ab_mod_c
145-
assert pow(a, b, flint.fmpz(c)) == ab_mod_c
146147
assert pow(flint.fmpz(a), flint.fmpz(b), c) == ab_mod_c
147148
assert pow(flint.fmpz(a), b, flint.fmpz(c)) == ab_mod_c
148-
assert pow(a, flint.fmpz(b), flint.fmpz(c)) == ab_mod_c
149149
assert pow(flint.fmpz(a), flint.fmpz(b), flint.fmpz(c)) == ab_mod_c
150150

151+
# 3-arg pow cannot be made to work with fmpz on PyPy
152+
# https://github.com/flintlib/python-flint/issues/74
153+
if not PYPY:
154+
assert pow(a, flint.fmpz(b), c) == ab_mod_c
155+
assert pow(a, b, flint.fmpz(c)) == ab_mod_c
156+
assert pow(a, flint.fmpz(b), flint.fmpz(c)) == ab_mod_c
157+
151158
assert raises(lambda: pow(flint.fmpz(2), 2, 0), ValueError)
152159
# XXX: Handle negative modulus like int?
153160
assert raises(lambda: pow(flint.fmpz(2), 2, -1), ValueError)

src/flint/types/fmpz_mod_poly.pyx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -880,6 +880,9 @@ cdef class fmpz_mod_poly(flint_poly):
880880
"""
881881
return self[0]
882882

883+
# XXX: Methods like leading_coefficient() that are generic should be moved
884+
# to the flint_poly base class rather than defined here.
885+
883886
def leading_coefficient(self):
884887
"""
885888
Return the leading coefficient of this polynomial.
@@ -889,6 +892,12 @@ cdef class fmpz_mod_poly(flint_poly):
889892
>>> f.leading_coefficient()
890893
fmpz_mod(3, 163)
891894
"""
895+
# XXX: This is a workaround for a Cython/PyPy bug:
896+
# https://github.com/flintlib/python-flint/issues/74
897+
# https://github.com/cython/cython/issues/5776
898+
d = self.degree()
899+
if d < 0:
900+
return self.ctx.mod.zero()
892901
return self[self.degree()]
893902

894903
def reverse(self, degree=None):

0 commit comments

Comments
 (0)