Skip to content

Commit 3f7f722

Browse files
committed
refactor: add shim for Py_SET_SIZE
1 parent 56b6409 commit 3f7f722

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

include/pyshim.hh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,15 @@ inline void PyErr_SetKeyError(PyObject *key) {
107107
#endif
108108
}
109109

110+
/**
111+
* @brief Shim for `Py_SET_SIZE`.
112+
* `Py_SET_SIZE` is not available in Python < 3.9
113+
*/
114+
#ifndef Py_SET_SIZE
115+
static inline void _Py_SET_SIZE(PyVarObject *ob, Py_ssize_t size) {
116+
ob->ob_size = size;
117+
}
118+
#define Py_SET_SIZE(ob, size) _Py_SET_SIZE((PyVarObject *)(ob), size)
119+
#endif
120+
110121
#endif // #ifndef PythonMonkey_py_version_shim_

src/IntType.cc

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,7 @@ static inline void PythonLong_SetSign(PyLongObject *op, int sign) {
4444
#else // Python version is less than 3.12
4545
// see https://github.com/python/cpython/blob/v3.9.16/Objects/longobject.c#L956
4646
Py_ssize_t pyDigitCount = Py_SIZE(op);
47-
#if PY_VERSION_HEX >= 0x03090000
4847
Py_SET_SIZE(op, sign * std::abs(pyDigitCount));
49-
#else
50-
((PyVarObject *)op)->ob_size = sign * std::abs(pyDigitCount); // Py_SET_SIZE is not available in Python < 3.9
51-
#endif
5248
#endif
5349
}
5450

0 commit comments

Comments
 (0)