Skip to content

Commit

Permalink
Merge pull request #441 from skirpichev/fix-for-cpython3.13
Browse files Browse the repository at this point in the history
CPython 3.13: add _PyLong_New() and import some macros from pycore_hash.h
  • Loading branch information
casevh authored Oct 25, 2023
2 parents e094479 + 1edb036 commit ff2ec3d
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/pip_install_gmpy2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: [3.11, 3.12]
python-version: [3.11, 3.12, 3.13]
os: [macos-latest]
runs-on: ${{ matrix.os }}
steps:
Expand Down
38 changes: 38 additions & 0 deletions src/gmpy2_convert.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,44 @@ extern "C" {
# define _PyLong_DigitCount(obj) (_PyLong_IsNegative(obj)? -Py_SIZE(obj):Py_SIZE(obj))
#endif

#if PY_VERSION_HEX >= 0x030D0000

#define MAX_LONG_DIGITS \
((PY_SSIZE_T_MAX - offsetof(PyLongObject, long_value.ob_digit))/sizeof(digit))

PyLongObject *
_PyLong_New(Py_ssize_t size)
{
assert(size >= 0);
PyLongObject *result;
if (size > (Py_ssize_t)MAX_LONG_DIGITS) {
PyErr_SetString(PyExc_OverflowError,
"too many digits in integer");
return NULL;
}
/* Fast operations for single digit integers (including zero)
* assume that there is always at least one digit present. */
Py_ssize_t ndigits = size ? size : 1;
/* Number of bytes needed is: offsetof(PyLongObject, ob_digit) +
sizeof(digit)*size. Previous incarnations of this code used
sizeof() instead of the offsetof, but this risks being
incorrect in the presence of padding between the header
and the digits. */
result = PyObject_Malloc(offsetof(PyLongObject, long_value.ob_digit) +
ndigits*sizeof(digit));
if (!result) {
PyErr_NoMemory();
return NULL;
}
_PyLong_SetSignAndDigitCount(result, size != 0, size);
PyObject_Init((PyObject*)result, &PyLong_Type);
/* The digit has to be initialized explicitly to avoid
* use-of-uninitialized-value. */
result->long_value.ob_digit[0] = 0;
return result;
}
#endif

/* Since the macros are used in gmpy2's codebase, these functions are skipped
* until they are needed for the C API in the future.
*/
Expand Down
5 changes: 5 additions & 0 deletions src/gmpy2_hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@
* License along with GMPY2; if not, see <http://www.gnu.org/licenses/> *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

#if PY_VERSION_HEX >= 0x030D0000
# define Py_BUILD_CORE
# include <internal/pycore_pyhash.h>
#endif

static Py_hash_t
GMPy_MPZ_Hash_Slot(MPZ_Object *self)
{
Expand Down

0 comments on commit ff2ec3d

Please sign in to comment.