Skip to content
Snippets Groups Projects
Commit 5e9d9943 authored by Ben Boeckel's avatar Ben Boeckel
Browse files

Wrapping/PythonCore: use slots for number access

parent 3644d8c0
No related branches found
No related tags found
Loading
......@@ -92,12 +92,18 @@ static PyObject* PyVTKReference_CompatibleObject(PyObject* self, PyObject* opn)
}
// check if it has number protocol and suitable methods
#if PY_VERSION_HEX < 0x030A0000
PyNumberMethods* nb = Py_TYPE(opn)->tp_as_number;
if (nb)
#endif
{
if (nb->nb_index)
#if PY_VERSION_HEX >= 0x030A0000
if (unaryfunc nb_index = (unaryfunc)PyType_GetSlot(Py_TYPE(opn), Py_nb_index))
#else
if (unaryfunc nb_index = nb->nb_index)
#endif
{
opn = nb->nb_index(opn);
opn = nb_index(opn);
if (opn == nullptr || !PyLong_Check(opn))
{
PyErr_SetString(PyExc_TypeError, "nb_index should return integer object");
......@@ -105,9 +111,13 @@ static PyObject* PyVTKReference_CompatibleObject(PyObject* self, PyObject* opn)
}
return opn;
}
else if (nb->nb_float)
#if PY_VERSION_HEX >= 0x030A0000
else if (unaryfunc nb_float = (unaryfunc)PyType_GetSlot(Py_TYPE(opn), Py_nb_float))
#else
else if (unaryfunc nb_float = nb->nb_float)
#endif
{
opn = nb->nb_float(opn);
opn = nb_float(opn);
if (opn == nullptr || !PyFloat_Check(opn))
{
PyErr_SetString(PyExc_TypeError, "nb_float should return float object");
......
......@@ -249,6 +249,11 @@ static int vtkPythonIntPenalty(PY_LONG_LONG tmpi, int penalty, char format)
static bool vtkPythonCanConvertToInt(PyObject* arg)
{
#if PY_VERSION_HEX >= 0x030A0000
unaryfunc asint = (unaryfunc)PyType_GetSlot(Py_TYPE(arg), Py_nb_int);
unaryfunc asindex = (unaryfunc)PyType_GetSlot(Py_TYPE(arg), Py_nb_index);
return (asint || asindex);
#else
// Python 3.8 deprecated implicit conversions via __int__, so we must
// check for the existence of the __int__ and __index__ slots ourselves
// instead of simply attempting a conversion.
......@@ -258,6 +263,7 @@ static bool vtkPythonCanConvertToInt(PyObject* arg)
#else
return (nb && nb->nb_int);
#endif
#endif
}
//------------------------------------------------------------------------------
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment