Skip to content

Commit

Permalink
Changes to accommodate Cython 3.0 and Python 3.12 -- breaks Python 3.11
Browse files Browse the repository at this point in the history
  • Loading branch information
culler committed Sep 4, 2023
1 parent 71b17e4 commit 4ccd122
Show file tree
Hide file tree
Showing 12 changed files with 256 additions and 109 deletions.
8 changes: 0 additions & 8 deletions cypari/Py_SET_SIZE.h

This file was deleted.

10 changes: 4 additions & 6 deletions cypari/_pari.pxd
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
from .types cimport *
cimport cython

include "sage.pxi"

cdef class RingElement:
pass

Expand Down Expand Up @@ -52,9 +50,9 @@ cdef int _pari_err_handle(GEN E) except 0
cdef void _pari_err_recover(long errnum)

cdef extern from *:
int sig_on() nogil except 0
int sig_str(char*) nogil except 0
int sig_check() nogil except 0
int sig_on() except 0 nogil
int sig_str(char*) except 0 nogil
int sig_check() except 0 nogil
void sig_off() nogil
void sig_retry() nogil # Does not return
void sig_error() nogil # Does not return
Expand Down Expand Up @@ -82,7 +80,7 @@ cdef extern from *:
# can be used to make Cython check whether there is a pending exception
# (PyErr_Occurred() is non-NULL). To Cython, it will look like
# cython_check_exception() actually raised the exception.
cdef inline void cython_check_exception() nogil except *:
cdef inline void cython_check_exception() except * nogil:
pass


Expand Down
7 changes: 2 additions & 5 deletions cypari/_pari.pyx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
cython: c_api_binop_methods=False

# Use sys.getdefaultencoding() to convert Unicode strings to <char*>
#
# cython: c_string_encoding=default
Expand Down Expand Up @@ -53,11 +55,6 @@ AUTHORS:
# the License, or (at your option) any later version.
# http://www.gnu.org/licenses/
#*****************************************************************************
#from __future__ import print_function

# Define the conditional compilation variable SAGE
include "sage.pxi"

import sys, types
if sys.version_info.major > 2:
iterable_types = (list, tuple, types.GeneratorType)
Expand Down
43 changes: 15 additions & 28 deletions cypari/convert.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -47,29 +47,22 @@ include "cysignals/signals.pxi"
from .paridecl cimport *
from .stack cimport new_gen
"""
IF UNAME_SYSNAME == "Windows":
cdef int LONG_MAX = 2147483647
cdef int LONG_MIN = -2147483648
ELSE:
from libc.limits cimport LONG_MIN, LONG_MAX

from cpython.version cimport PY_MAJOR_VERSION
from cpython.ref cimport PyObject
from cpython.object cimport Py_SIZE
from cpython.int cimport PyInt_AS_LONG, PyInt_FromLong
from cpython.longintrepr cimport (_PyLong_New, digit, PyLong_SHIFT, PyLong_MASK, py_long)

cdef extern from *:
ctypedef struct PyLongObject:
digit* ob_digit

cdef extern from "Py_SET_SIZE.h":
void Py_SET_SIZE(py_long o, Py_ssize_t size)
cdef extern from "pylong_support.h":
digit* OB_DIGIT(py_long o)
void _PyLong_SetSignAndDigitCount(py_long o, int sign, Py_ssize_t size)
Py_ssize_t _PyLong_DigitCount(py_long op)
Py_ssize_t _PyLong_Sign(py_long op)
cdef int LONG_MAX, LONG_MIN

####################################
# Integers
####################################

cpdef integer_to_gen(x):
"""
Convert a Python ``int`` or ``long`` to a PARI ``gen`` of type
Expand Down Expand Up @@ -115,7 +108,7 @@ cdef PyLong_FromINT(GEN g):
cdef Py_ssize_t sizedigits_final = 0

cdef py_long x = _PyLong_New(sizedigits)
cdef digit* D = x.ob_digit
cdef digit* D = OB_DIGIT(x)

cdef digit d
cdef ulong w
Expand All @@ -142,11 +135,7 @@ cdef PyLong_FromINT(GEN g):
if d:
sizedigits_final = i+1

if signe(g) > 0:
Py_SET_SIZE(x, sizedigits_final)
else:
Py_SET_SIZE(x, -sizedigits_final)

_PyLong_SetSignAndDigitCount(x, signe(g), sizedigits_final)
return x

cpdef gen_to_integer(Gen x):
Expand Down Expand Up @@ -271,21 +260,19 @@ cdef GEN gtoi(GEN g0) except NULL:


cdef GEN PyLong_AsGEN(py_long x):
cdef const digit* D = x.ob_digit
cdef const digit* D = OB_DIGIT(x)

# Size of the input
cdef Py_ssize_t sizedigits
cdef Py_ssize_t sign
cdef pari_longword sgn

if Py_SIZE(x) == 0:
sizedigits = _PyLong_DigitCount(x)
sign = _PyLong_Sign(x)
if sign == 0:
return gen_0
elif Py_SIZE(x) > 0:
sizedigits = Py_SIZE(x)
sgn = evalsigne(1)
else:
sizedigits = -Py_SIZE(x)
sgn = evalsigne(-1)

sgn = evalsigne(sign)

# Size of the output, in bits and in words
cdef size_t sizebits = sizedigits * PyLong_SHIFT
cdef size_t sizewords = (sizebits + BITS_IN_LONG - 1) // BITS_IN_LONG
Expand Down
Loading

0 comments on commit 4ccd122

Please sign in to comment.