Fix building FreeStyle with Python 3.12

Add helper functions for creating PyLong sup-types.
In Python 3.12 it's no longer practical to manipulate PyLongObject
values directly. Resolve by creating PyLongObject and copying the
values to a newly created sub-type.
This commit is contained in:
Campbell Barton
2024-02-08 23:10:04 +11:00
parent 8887955def
commit 252ae7029d
7 changed files with 67 additions and 100 deletions

View File

@@ -59,6 +59,32 @@ PyObject *PyBool_from_bool(bool b)
return PyBool_FromLong(b ? 1 : 0);
}
PyObject *PyLong_subtype_new(PyTypeObject *ty, long value)
{
BLI_assert(ty->tp_basicsize == sizeof(PyLongObject));
PyLongObject *result = PyObject_NewVar(PyLongObject, ty, 1);
#if PY_VERSION_HEX >= 0x030c0000
{
/* Account for change in `PyLongObject` in Python 3.12+.
* The values of longs are no longer accessible via public API's, copy the value instead. */
PyLongObject *value_py = (PyLongObject *)PyLong_FromLong(value);
memcpy(&result->long_value, &value_py->long_value, sizeof(result->long_value));
Py_DECREF(value_py);
}
#else
result->ob_digit[0] = value;
#endif
return (PyObject *)result;
}
void PyLong_subtype_add_to_dict(PyObject *dict, PyTypeObject *ty, const char *attr, long value)
{
PyObject *result = PyLong_subtype_new(ty, value);
PyDict_SetItemString(dict, attr, result);
/* Owned by the dictionary. */
Py_DECREF(result);
}
PyObject *Vector_from_Vec2f(Vec2f &vec)
{
float vec_data[2]; // because vec->_coord is protected