Skip to content

Commit

Permalink
sagemathgh-36111: using PyLong API instead of legacy PyInt API
Browse files Browse the repository at this point in the history
    
Fixes sagemath#36081

### 📝 Checklist

- [x] The title is concise, informative, and self-explanatory.
- [x] I have linked a relevant issue or discussion.
    
URL: sagemath#36111
Reported by: Frédéric Chapoton
Reviewer(s): Matthias Köppe
  • Loading branch information
Release Manager committed Sep 10, 2023
2 parents aa1cfd7 + 52d9d5e commit 88ad3a1
Show file tree
Hide file tree
Showing 26 changed files with 55 additions and 165 deletions.
4 changes: 1 addition & 3 deletions src/sage/arith/long.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ Fast conversion of Python objects to C long
from libc.limits cimport LONG_MIN, LONG_MAX

from cpython.object cimport Py_SIZE
from cpython.int cimport PyInt_AS_LONG
from cpython.long cimport PyLong_AsLong
from cpython.number cimport PyNumber_Index, PyIndex_Check
from cpython.longintrepr cimport py_long, PyLong_SHIFT, digit

Expand Down Expand Up @@ -84,7 +82,7 @@ cdef enum:
cdef inline bint integer_check_long(x, long* value, int* err) except -1:
"""
Return whether ``x`` is some integer type. This is true for the
Python types ``int`` and ``long``, for Sage Integers and for types
Python type ``int``, for Sage Integers and for types
implementing ``__index__``.
If possible, compute the value of this integer as C long and store
Expand Down
4 changes: 2 additions & 2 deletions src/sage/data_structures/bounded_integer_sequences.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ AUTHORS:
from cysignals.signals cimport sig_check, sig_on, sig_off
from sage.data_structures.bitset_base cimport *

from cpython.int cimport PyInt_FromSize_t
from cpython.long cimport PyLong_FromSize_t
from cpython.slice cimport PySlice_GetIndicesEx
from sage.libs.flint.flint cimport FLINT_BIT_COUNT as BIT_COUNT
from sage.structure.richcmp cimport richcmp_not_equal, rich_to_bool
Expand Down Expand Up @@ -295,7 +295,7 @@ cdef biseq_getitem_py(biseq_t S, mp_size_t index):
"""
cdef size_t out = biseq_getitem(S, index)
return PyInt_FromSize_t(out)
return PyLong_FromSize_t(out)

cdef inline void biseq_inititem(biseq_t S, mp_size_t index, size_t item):
"""
Expand Down
8 changes: 4 additions & 4 deletions src/sage/geometry/triangulation/triangulations.cc
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ triangulations_ptr init_triangulations
compact_simplices seed;
for (int i=0; i<PySequence_Size(py_seed); i++) {
PyObject* simplex = PySequence_GetItem(py_seed,i);
seed.push_back(PyInt_AS_LONG(simplex));
seed.push_back(PyLong_AsLong(simplex));
Py_DECREF(simplex);
}

Expand All @@ -133,7 +133,7 @@ triangulations_ptr init_triangulations
vertices simplex;
for (int k=0; k<PySequence_Size(py_simplex); k++) {
PyObject* py_vertex = PySequence_GetItem(py_simplex,k);
simplex.insert(simplex.begin(), PyInt_AS_LONG(py_vertex));
simplex.insert(simplex.begin(), PyLong_AsLong(py_vertex));
Py_DECREF(py_vertex);
}
pos.push_back(simplex);
Expand All @@ -146,7 +146,7 @@ triangulations_ptr init_triangulations
vertices simplex;
for (int k=0; k<PySequence_Size(py_simplex); k++) {
PyObject* py_vertex = PySequence_GetItem(py_simplex,k);
simplex.insert(simplex.begin(), PyInt_AS_LONG(py_vertex));
simplex.insert(simplex.begin(), PyLong_AsLong(py_vertex));
Py_DECREF(py_vertex);
}
neg.push_back(simplex);
Expand Down Expand Up @@ -195,7 +195,7 @@ PyObject* next_triangulation(triangulations_ptr t)
const compact_simplices& triang = t->next_triangulation();
PyObject* py_triang = PyTuple_New(triang.size());
for (size_t i=0; i<triang.size(); i++)
PyTuple_SET_ITEM(py_triang, i, PyInt_FromLong(triang[i]));
PyTuple_SET_ITEM(py_triang, i, PyLong_FromLong(triang[i]));

return py_triang;
}
Expand Down
3 changes: 0 additions & 3 deletions src/sage/geometry/triangulation/triangulations.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
#include "data.h"
#include <Python.h>

#define PyInt_FromLong PyLong_FromLong
#define PyInt_AsLong PyLong_AsLong
#define PyInt_AS_LONG PyLong_AS_LONG

class triangulations: public std::vector<compact_simplices>
{
Expand Down
3 changes: 1 addition & 2 deletions src/sage/libs/gmp/pylong.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ AUTHORS:


from cpython.object cimport Py_SIZE
from cpython.int cimport PyInt_FromLong
from cpython.long cimport PyLong_FromLong
from cpython.longintrepr cimport _PyLong_New, py_long, digit, PyLong_SHIFT
from .mpz cimport *
Expand Down Expand Up @@ -85,7 +84,7 @@ cdef mpz_get_pyintlong(mpz_srcptr z):
if the value is too large.
"""
if mpz_fits_slong_p(z):
return PyInt_FromLong(mpz_get_si(z))
return PyLong_FromLong(mpz_get_si(z))
return mpz_get_pylong_large(z)


Expand Down
1 change: 0 additions & 1 deletion src/sage/libs/mpmath/ext_impl.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ See if :trac:`15118` is fixed::
# http://www.gnu.org/licenses/
#*****************************************************************************

from cpython.int cimport *
from cpython.long cimport *
from cpython.float cimport *
from cpython.complex cimport *
Expand Down
1 change: 0 additions & 1 deletion src/sage/libs/mpmath/ext_main.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ context class, and related utilities.
# http://www.gnu.org/licenses/
#*****************************************************************************

from cpython.int cimport *
from cpython.long cimport *
from cpython.float cimport *
from cpython.complex cimport *
Expand Down
4 changes: 2 additions & 2 deletions src/sage/matrix/matrix_integer_sparse.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ TESTS::
from cysignals.memory cimport check_calloc, sig_free
from cysignals.signals cimport sig_on, sig_off

from cpython.int cimport PyInt_FromSize_t
from cpython.long cimport PyLong_FromSize_t

from sage.ext.stdsage cimport PY_NEW
from sage.ext.mod_int cimport *
Expand Down Expand Up @@ -723,7 +723,7 @@ cdef class Matrix_integer_sparse(Matrix_sparse):

del M

return PyInt_FromSize_t(r)
return PyLong_FromSize_t(r)

def _det_linbox(self):
r"""
Expand Down
8 changes: 4 additions & 4 deletions src/sage/modular/arithgroup/farey.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1010,7 +1010,7 @@ PyObject* FareySymbol::word_problem(const mpz_t a, const mpz_t b,
LLT_algorithm(M, p, beta1);
wd = PyList_New(p.size());
for(i=0; i<p.size(); i++) {
PyList_SetItem(wd, i, PyInt_FromLong(p[i]));
PyList_SetItem(wd, i, PyLong_FromLong(p[i]));
}
*beta = beta1;
return wd;
Expand Down Expand Up @@ -1085,7 +1085,7 @@ PyObject* FareySymbol::get_fractions() const {
PyObject* FareySymbol::get_pairings() const {
PyObject* pairing_list = PyList_New(pairing.size());
for(size_t i=0; i<pairing.size(); i++) {
PyObject* m = PyInt_FromLong(long(pairing[i]));
PyObject* m = PyLong_FromLong(long(pairing[i]));
PyList_SetItem(pairing_list, i, m);
}
return pairing_list;
Expand All @@ -1104,8 +1104,8 @@ PyObject* FareySymbol::get_paired_sides() const {
for(vector<int>::const_iterator i=p.begin(); i!=p.end(); i++) {
vector<int>::const_iterator j = find(pairing.begin(), pairing.end(), *i);
vector<int>::const_iterator k = find(j+1, pairing.end(), *i);
PyObject* J = PyInt_FromLong(long(j-pairing.begin()));
PyObject* K = PyInt_FromLong(long(k-pairing.begin()));
PyObject* J = PyLong_FromLong(long(j-pairing.begin()));
PyObject* K = PyLong_FromLong(long(k-pairing.begin()));
PyObject* tuple = PyTuple_New(2);
PyTuple_SetItem(tuple, 0, J);
PyTuple_SetItem(tuple, 1, K);
Expand Down
3 changes: 0 additions & 3 deletions src/sage/modular/arithgroup/farey.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@
#include <gmpxx.h>
#include "sl2z.hpp"

#define PyInt_FromLong PyLong_FromLong
#define PyInt_AsLong PyLong_AsLong
#define PyInt_AS_LONG PyLong_AS_LONG

//--- pure virtual base class for helper class for membership test --------

Expand Down
6 changes: 3 additions & 3 deletions src/sage/rings/complex_arb.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ import sage.categories.fields
cimport sage.rings.abc
cimport sage.rings.rational

from cpython.int cimport PyInt_AS_LONG
from cpython.long cimport PyLong_AsLong
from cpython.object cimport Py_EQ, Py_NE
from cpython.complex cimport PyComplex_FromDoubles

Expand Down Expand Up @@ -2732,7 +2732,7 @@ cdef class ComplexBall(RingElement):
cdef ComplexBall self = val
cdef ComplexBall res = self._new()
if is_small_python_int(shift):
acb_mul_2exp_si(res.value, self.value, PyInt_AS_LONG(shift))
acb_mul_2exp_si(res.value, self.value, PyLong_AsLong(shift))
elif isinstance(shift, Integer):
sig_on()
fmpz_init(tmpz)
Expand Down Expand Up @@ -2874,7 +2874,7 @@ cdef class ComplexBall(RingElement):
cdef ComplexBall res = self._new()
if is_small_python_int(expo):
if _do_sig(prec(self)): sig_on()
acb_pow_si(res.value, self.value, PyInt_AS_LONG(expo), prec(self))
acb_pow_si(res.value, self.value, PyLong_AsLong(expo), prec(self))
if _do_sig(prec(self)): sig_off()
elif isinstance(expo, Integer):
if _do_sig(prec(self)): sig_on()
Expand Down
10 changes: 5 additions & 5 deletions src/sage/rings/finite_rings/integer_mod.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ TESTS::

from cysignals.signals cimport sig_on, sig_off, sig_check

from cpython.int cimport *
from cpython.long cimport *
from cpython.list cimport *
from cpython.ref cimport *

Expand Down Expand Up @@ -2793,8 +2793,8 @@ cdef class IntegerMod_int(IntegerMod_abstract):
cdef long long_exp
cdef int_fast32_t res
cdef mpz_t res_mpz
if type(exp) is int and -100000 < PyInt_AS_LONG(exp) < 100000:
long_exp = PyInt_AS_LONG(exp)
if type(exp) is int and -100000 < PyLong_AsLong(exp) < 100000:
long_exp = PyLong_AsLong(exp)
elif type(exp) is Integer and mpz_cmpabs_ui((<Integer>exp).value, 100000) == -1:
long_exp = mpz_get_si((<Integer>exp).value)
else:
Expand Down Expand Up @@ -3624,8 +3624,8 @@ cdef class IntegerMod_int64(IntegerMod_abstract):
cdef long long_exp
cdef int_fast64_t res
cdef mpz_t res_mpz
if type(exp) is int and -100000 < PyInt_AS_LONG(exp) < 100000:
long_exp = PyInt_AS_LONG(exp)
if type(exp) is int and -100000 < PyLong_AsLong(exp) < 100000:
long_exp = PyLong_AsLong(exp)
elif type(exp) is Integer and mpz_cmpabs_ui((<Integer>exp).value, 100000) == -1:
long_exp = mpz_get_si((<Integer>exp).value)
else:
Expand Down
11 changes: 6 additions & 5 deletions src/sage/rings/integer.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ from sage.arith.long cimport (integer_check_long,

from cpython.list cimport *
from cpython.number cimport *
from cpython.int cimport *
from cpython.long cimport *
from cpython.object cimport *
from libc.stdint cimport uint64_t
cimport sage.structure.element
Expand Down Expand Up @@ -3451,7 +3451,7 @@ cdef class Integer(sage.structure.element.EuclideanDomainElement):
cdef long d, res

if is_small_python_int(other):
d = PyInt_AS_LONG(other)
d = PyLong_AsLong(other)
if d > 0:
mpz_fdiv_qr_ui(q.value, r.value, self.value, d)
elif d == 0:
Expand Down Expand Up @@ -6664,7 +6664,7 @@ cdef class Integer(sage.structure.element.EuclideanDomainElement):

if type(y) is int:
# For a Python int, we can just use the Python/C API.
n = PyInt_AS_LONG(y)
n = PyLong_AsLong(y)
else:
# If it's not already an Integer, try to convert it.
if not isinstance(y, Integer):
Expand Down Expand Up @@ -7407,7 +7407,7 @@ cdef class int_to_Z(Morphism):
if type(a) is not int:
raise TypeError("must be a Python int object")

return smallInteger(PyInt_AS_LONG(a))
return smallInteger(PyLong_AsLong(a))

def _repr_type(self):
"""
Expand Down Expand Up @@ -7680,7 +7680,8 @@ cdef Integer zero = the_integer_ring._zero_element
cdef Integer one = the_integer_ring._one_element

# pool of small integer for fast sign computation
# Use the same defaults as Python, documented at https://docs.python.org/2/c-api/int.html#PyInt_FromLong
# Use the same defaults as Python3 documented at
# https://docs.python.org/3/c-api/long.html#c.PyLong_FromLong
DEF small_pool_min = -5
DEF small_pool_max = 256
# we could use the above zero and one here
Expand Down
4 changes: 2 additions & 2 deletions src/sage/rings/integer_ring.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ other types will also coerce to the integers, when it makes sense.
# http://www.gnu.org/licenses/
#*****************************************************************************

from cpython.int cimport *
from cpython.long cimport *
from cpython.list cimport *
from cpython.object cimport Py_NE

Expand Down Expand Up @@ -478,7 +478,7 @@ cdef class IntegerRing_class(PrincipalIdealDomain):

L = []
if type(step) is int:
istep = PyInt_AS_LONG(step)
istep = PyLong_AsLong(step)
step_sign = istep
else:
zstep = <Integer>step
Expand Down
2 changes: 1 addition & 1 deletion src/sage/rings/number_field/number_field_element.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ AUTHORS:

import operator

from cpython.int cimport *
from cpython.long cimport *

from cysignals.signals cimport sig_on, sig_off
from sage.ext.stdsage cimport PY_NEW
Expand Down
2 changes: 1 addition & 1 deletion src/sage/rings/padics/FM_template.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ AUTHORS:
#*****************************************************************************

include "padic_template_element.pxi"
from cpython.int cimport *
from cpython.long cimport *

from sage.structure.element cimport Element
from sage.rings.padics.common_conversion cimport comb_prec, _process_args_and_kwds
Expand Down
2 changes: 1 addition & 1 deletion src/sage/rings/padics/FP_template.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ AUTHORS:

from sage.ext.stdsage cimport PY_NEW
include "padic_template_element.pxi"
from cpython.int cimport *
from cpython.long cimport *

from sage.structure.element cimport Element
from sage.rings.padics.common_conversion cimport comb_prec, _process_args_and_kwds
Expand Down
8 changes: 4 additions & 4 deletions src/sage/rings/padics/common_conversion.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ AUTHORS:
# https://www.gnu.org/licenses/
# ****************************************************************************

from cpython.int cimport *
from cpython.long cimport *
from sage.ext.stdsage cimport PY_NEW
from sage.libs.gmp.all cimport *
from sage.arith.rational_reconstruction cimport mpq_rational_reconstruction
Expand Down Expand Up @@ -84,7 +84,7 @@ cdef long get_ordp(x, PowComputer_class prime_pow) except? -10000:
if x == 0:
return maxordp
try:
n = PyInt_AsLong(x)
n = PyLong_AsLong(x)
except OverflowError:
return get_ordp(Integer(x), prime_pow)
else:
Expand Down Expand Up @@ -250,7 +250,7 @@ cdef long comb_prec(iprec, long prec) except? -10000:
raise OverflowError("precision overflow")
return mpz_get_si(intprec.value)
if isinstance(iprec, int):
return min(PyInt_AS_LONG(iprec), prec)
return min(PyLong_AsLong(iprec), prec)
return comb_prec(Integer(iprec), prec)

cdef int _process_args_and_kwds(long *aprec, long *rprec, args, kwds, bint absolute, PowComputer_class prime_pow) except -1:
Expand Down Expand Up @@ -405,7 +405,7 @@ cdef inline int cconv_shared(mpz_t out, x, long prec, long valshift, PowComputer
- ``prime_pow`` -- a PowComputer for the ring.

"""
if PyInt_Check(x):
if PyLong_Check(x):
x = Integer(x)
elif isinstance(x, pari_gen):
x = x.sage()
Expand Down
6 changes: 3 additions & 3 deletions src/sage/rings/padics/padic_template_element.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ AUTHORS:
# https://www.gnu.org/licenses/
# ****************************************************************************

from cpython.int cimport *
from cpython.long cimport *

from sage.libs.gmp.all cimport *
import sage.rings.finite_rings.integer_mod
Expand Down Expand Up @@ -253,7 +253,7 @@ cdef class pAdicTemplateElement(pAdicGenericElement):
# The "verify that shift is an integer" part could be shared
cdef long s
if isinstance(shift, int):
s = PyInt_AS_LONG(shift)
s = PyLong_AsLong(shift)
else:
if not isinstance(shift, Integer):
shift = Integer(shift)
Expand Down Expand Up @@ -301,7 +301,7 @@ cdef class pAdicTemplateElement(pAdicGenericElement):
"""
cdef long s
if isinstance(shift, int):
s = PyInt_AS_LONG(shift)
s = PyLong_AsLong(shift)
else:
if not isinstance(shift, Integer):
shift = Integer(shift)
Expand Down
Loading

0 comments on commit 88ad3a1

Please sign in to comment.