Skip to content

Commit 33c0810

Browse files
committed
Make sympy import optional in polypy_test.py
- Wrap sympy import in try/except to handle missing sympy gracefully - Add _sympy_available flag to track sympy availability - Automatically disable sympy checking when sympy is not available - Add guards in SympyWrapper methods to handle missing sympy - This allows tests to run even when sympy is not installed
1 parent 8b2c8c7 commit 33c0810

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

test/python/polypy_test.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import polypy
22
import random
3-
import sympy
3+
4+
try:
5+
import sympy
6+
_sympy_available = True
7+
except ImportError:
8+
_sympy_available = False
9+
sympy = None
410

511
PASS = 0
612
FAIL = 0
@@ -61,7 +67,13 @@ class SympyWrapper:
6167

6268
enabled = True
6369

70+
def __init__(self):
71+
if not _sympy_available:
72+
self.enabled = False
73+
6474
def sympy_from_upolynomial(self, p):
75+
if not _sympy_available:
76+
raise RuntimeError("sympy is not available")
6577
coeffs = p.coefficients()
6678
sympy_p = 0
6779
x = sympy.symbols('x')
@@ -71,13 +83,17 @@ def sympy_from_upolynomial(self, p):
7183
return sympy_p
7284

7385
def sympy_factor(self, p):
86+
if not _sympy_available:
87+
raise RuntimeError("sympy is not available")
7488
sympy_p = self.sympy_from_upolynomial(p)
7589
if (p.ring().modulus() is None):
7690
return sympy.factor_list(sympy_p)
7791
else:
7892
return sympy.factor_list(sympy_p, modulus=p.ring().modulus())
7993

8094
def sympy_gcd(self, p, q):
95+
if not _sympy_available:
96+
raise RuntimeError("sympy is not available")
8197
sympy_p = self.sympy_from_upolynomial(p)
8298
sympy_q = self.sympy_from_upolynomial(q)
8399
if (p.ring().modulus() is None):
@@ -86,6 +102,8 @@ def sympy_gcd(self, p, q):
86102
return sympy.gcd(sympy_p, sympy_q, modulus=p.ring().modulus())
87103

88104
def sympy_extended_gcd(self, p, q):
105+
if not _sympy_available:
106+
raise RuntimeError("sympy is not available")
89107
sympy_p = self.sympy_from_upolynomial(p)
90108
sympy_q = self.sympy_from_upolynomial(q)
91109
if (p.ring().modulus() is None):
@@ -143,9 +161,11 @@ def check_extended_gcd(self, p, q, gcd, u, v):
143161

144162

145163
"""
146-
By default sympy is enabled.
164+
By default sympy is enabled if available.
147165
"""
148166
sympy_checker = SympyWrapper();
167+
if not _sympy_available:
168+
sympy_checker.enabled = False
149169

150170
"""
151171
Initialize the testing.

0 commit comments

Comments
 (0)