Skip to content

Commit

Permalink
make Sage a run-time dependency not a build-time dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
malb committed Oct 18, 2016
1 parent c4a918d commit afa6252
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 54 deletions.
16 changes: 0 additions & 16 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,6 @@
config_pxi.append("DEF HAVE_QD=False")


# SAGE
have_sage = False

try:
import sage
have_sage = True
except ImportError:
pass

if have_sage:
from sage.env import SAGE_SRC
fplll["include_dirs"].append(SAGE_SRC)
config_pxi.append("DEF HAVE_SAGE=True")
else:
config_pxi.append("DEF HAVE_SAGE=False")

# NUMPY

try:
Expand Down
5 changes: 0 additions & 5 deletions src/fpylll/config.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,5 @@ ELSE:
have_qd = False
float_types = float_types + ("dpe", "mpfr")

IF HAVE_SAGE:
have_sage = True
ELSE:
have_sage = False

default_strategy = default_strategy_c().c_str()
default_strategy_path = default_strategy_path_c().c_str()
29 changes: 3 additions & 26 deletions src/fpylll/io.pxd
Original file line number Diff line number Diff line change
@@ -1,32 +1,9 @@
include "fpylll/config.pxi"

from cpython.int cimport PyInt_AS_LONG
from fplll.fplll cimport Z_NR
from gmp.mpz cimport mpz_t, mpz_set_si, mpz_set
from gmp.mpz cimport mpz_t
from gmp.types cimport mpz_srcptr
from fpylll.gmp.pylong cimport mpz_get_pyintlong, mpz_set_pylong

cdef int assign_Z_NR_mpz(Z_NR[mpz_t]& t, value) except -1

IF HAVE_SAGE:
from sage.rings.integer cimport Integer
cdef Integer mpz_get_python(mpz_srcptr z)
ELSE:
from fpylll.gmp.pylong cimport mpz_get_pyintlong as mpz_get_python

cdef inline int assign_mpz(mpz_t& t, value) except -1:
"""
Assign Python integer to Z_NR[mpz_t]
"""
if isinstance(value, int):
mpz_set_si(t, PyInt_AS_LONG(value))
return 0
if isinstance(value, long):
mpz_set_pylong(t, value)
return 0
IF HAVE_SAGE:
if isinstance(value, Integer):
mpz_set(t, (<Integer>value).value)
return 0

raise NotImplementedError("Type '%s' not supported"%type(value))
cdef int assign_mpz(mpz_t& t, value) except -1
cdef object mpz_get_python(mpz_srcptr z)
41 changes: 34 additions & 7 deletions src/fpylll/io.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@
include "fpylll/config.pxi"
include "cysignals/signals.pxi"


from cpython.int cimport PyInt_AS_LONG
from fpylll.gmp.mpz cimport mpz_init, mpz_clear, mpz_set
from fpylll.gmp.pylong cimport mpz_get_pyintlong, mpz_set_pylong
from gmp.mpz cimport mpz_t, mpz_set_si, mpz_set

IF HAVE_SAGE:
from sage.ext.stdsage cimport PY_NEW
try:
from sage.all import ZZ
have_sage = True
except ImportError:
have_sage = False

cdef int assign_Z_NR_mpz(Z_NR[mpz_t]& t, value) except -1:
"""
Expand All @@ -19,9 +26,29 @@ cdef int assign_Z_NR_mpz(Z_NR[mpz_t]& t, value) except -1:
finally:
mpz_clear(tmp)

cdef int assign_mpz(mpz_t& t, value) except -1:
"""
Assign Python integer to Z_NR[mpz_t]
"""
if isinstance(value, int):
mpz_set_si(t, PyInt_AS_LONG(value))
return 0
if isinstance(value, long):
mpz_set_pylong(t, value)
return 0
if have_sage:
from sage.rings.integer import Integer
if isinstance(value, Integer):
value = long(value)
mpz_set_pylong(t, value)
return 0

raise NotImplementedError("Type '%s' not supported"%type(value))

IF HAVE_SAGE:
cdef Integer mpz_get_python(mpz_srcptr z):
cdef Integer x = PY_NEW(Integer)
mpz_set(x.value, z)
return x
cdef object mpz_get_python(mpz_srcptr z):
r = mpz_get_pyintlong(z)
if have_sage:
from sage.rings.integer import Integer
return Integer(r)
else:
return r

0 comments on commit afa6252

Please sign in to comment.