Skip to content

Commit

Permalink
refactor: add shim for Py_SET_SIZE
Browse files Browse the repository at this point in the history
  • Loading branch information
Xmader committed Sep 25, 2024
1 parent 56b6409 commit 3f7f722
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
11 changes: 11 additions & 0 deletions include/pyshim.hh
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,15 @@ inline void PyErr_SetKeyError(PyObject *key) {
#endif
}

/**
* @brief Shim for `Py_SET_SIZE`.
* `Py_SET_SIZE` is not available in Python < 3.9
*/
#ifndef Py_SET_SIZE
static inline void _Py_SET_SIZE(PyVarObject *ob, Py_ssize_t size) {
ob->ob_size = size;
}
#define Py_SET_SIZE(ob, size) _Py_SET_SIZE((PyVarObject *)(ob), size)
#endif

#endif // #ifndef PythonMonkey_py_version_shim_
4 changes: 0 additions & 4 deletions src/IntType.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,7 @@ static inline void PythonLong_SetSign(PyLongObject *op, int sign) {
#else // Python version is less than 3.12
// see https://github.com/python/cpython/blob/v3.9.16/Objects/longobject.c#L956
Py_ssize_t pyDigitCount = Py_SIZE(op);
#if PY_VERSION_HEX >= 0x03090000
Py_SET_SIZE(op, sign * std::abs(pyDigitCount));
#else
((PyVarObject *)op)->ob_size = sign * std::abs(pyDigitCount); // Py_SET_SIZE is not available in Python < 3.9
#endif
#endif
}

Expand Down

0 comments on commit 3f7f722

Please sign in to comment.