| 1 |
// Collects code that was copied in from cpython, for a couple of different reasons: |
| 2 |
// * We wanted to modify it to produce a more efficient version for our uses |
| 3 |
// * We needed to call it and it was static :( |
| 4 |
// * We wanted to call it and needed to backport it |
| 5 |
|
| 6 |
#include "pythonsupport.h" |
| 7 |
|
| 8 |
///////////////////////////////////////// |
| 9 |
// Adapted from bltinmodule.c in Python 3.7.0 |
| 10 |
PyObject* |
| 11 |
update_bases(PyObject *bases) |
| 12 |
{ |
| 13 |
Py_ssize_t i, j; |
| 14 |
PyObject *base, *meth, *new_base, *result, *new_bases = NULL; |
| 15 |
PyObject *stack[1] = {bases}; |
| 16 |
assert(PyTuple_Check(bases)); |
| 17 |
|
| 18 |
Py_ssize_t nargs = PyTuple_GET_SIZE(bases); |
| 19 |
for (i = 0; i < nargs; i++) { |
| 20 |
base = PyTuple_GET_ITEM(bases, i); |
| 21 |
if (PyType_Check(base)) { |
| 22 |
if (new_bases) { |
| 23 |
/* If we already have made a replacement, then we append every normal base, |
| 24 |
otherwise just skip it. */ |
| 25 |
if (PyList_Append(new_bases, base) < 0) { |
| 26 |
goto error; |
| 27 |
} |
| 28 |
} |
| 29 |
continue; |
| 30 |
} |
| 31 |
if (PyObject_GetOptionalAttr(base, mypyc_interned_str.__mro_entries__, &meth) < 0) { |
| 32 |
goto error; |
| 33 |
} |
| 34 |
if (!meth) { |
| 35 |
if (new_bases) { |
| 36 |
if (PyList_Append(new_bases, base) < 0) { |
| 37 |
goto error; |
| 38 |
} |
| 39 |
} |
| 40 |
continue; |
| 41 |
} |
| 42 |
new_base = PyObject_Vectorcall(meth, stack, 1, NULL); |
| 43 |
Py_DECREF(meth); |
| 44 |
if (!new_base) { |
| 45 |
goto error; |
| 46 |
} |
| 47 |
if (!PyTuple_Check(new_base)) { |
| 48 |
PyErr_SetString(PyExc_TypeError, |
| 49 |
"__mro_entries__ must return a tuple"); |
| 50 |
Py_DECREF(new_base); |
| 51 |
goto error; |
| 52 |
} |
| 53 |
if (!new_bases) { |
| 54 |
/* If this is a first successful replacement, create new_bases list and |
| 55 |
copy previously encountered bases. */ |
| 56 |
if (!(new_bases = PyList_New(i))) { |
| 57 |
goto error; |
| 58 |
} |
| 59 |
for (j = 0; j < i; j++) { |
| 60 |
base = PyTuple_GET_ITEM(bases, j); |
| 61 |
PyList_SET_ITEM(new_bases, j, base); |
| 62 |
Py_INCREF(base); |
| 63 |
} |
| 64 |
} |
| 65 |
j = PyList_GET_SIZE(new_bases); |
| 66 |
if (PyList_SetSlice(new_bases, j, j, new_base) < 0) { |
| 67 |
goto error; |
| 68 |
} |
| 69 |
Py_DECREF(new_base); |
| 70 |
} |
| 71 |
if (!new_bases) { |
| 72 |
return bases; |
| 73 |
} |
| 74 |
result = PyList_AsTuple(new_bases); |
| 75 |
Py_DECREF(new_bases); |
| 76 |
return result; |
| 77 |
|
| 78 |
error: |
| 79 |
Py_XDECREF(new_bases); |
| 80 |
return NULL; |
| 81 |
} |
| 82 |
|
| 83 |
// From Python 3.7's typeobject.c |
| 84 |
int |
| 85 |
init_subclass(PyTypeObject *type, PyObject *kwds) |
| 86 |
{ |
| 87 |
PyObject *super, *func, *result; |
| 88 |
PyObject *args[2] = {(PyObject *)type, (PyObject *)type}; |
| 89 |
|
| 90 |
super = PyObject_Vectorcall((PyObject *)&PySuper_Type, args, 2, NULL); |
| 91 |
if (super == NULL) { |
| 92 |
return -1; |
| 93 |
} |
| 94 |
|
| 95 |
func = PyObject_GetAttr(super, mypyc_interned_str.__init_subclass__); |
| 96 |
Py_DECREF(super); |
| 97 |
if (func == NULL) { |
| 98 |
return -1; |
| 99 |
} |
| 100 |
|
| 101 |
result = _PyObject_FastCallDict(func, NULL, 0, kwds); |
| 102 |
Py_DECREF(func); |
| 103 |
if (result == NULL) { |
| 104 |
return -1; |
| 105 |
} |
| 106 |
|
| 107 |
Py_DECREF(result); |
| 108 |
return 0; |
| 109 |
} |
| 110 |
|
| 111 |
#if CPY_3_12_FEATURES |
| 112 |
|
| 113 |
// Slow path of CPyLong_AsSsize_tAndOverflow (non-inlined) |
| 114 |
Py_ssize_t |
| 115 |
CPyLong_AsSsize_tAndOverflow_(PyObject *vv, int *overflow) |
| 116 |
{ |
| 117 |
PyLongObject *v = (PyLongObject *)vv; |
| 118 |
size_t x, prev; |
| 119 |
Py_ssize_t res; |
| 120 |
Py_ssize_t i; |
| 121 |
int sign; |
| 122 |
|
| 123 |
*overflow = 0; |
| 124 |
|
| 125 |
res = -1; |
| 126 |
i = CPY_LONG_TAG(v); |
| 127 |
|
| 128 |
sign = 1; |
| 129 |
x = 0; |
| 130 |
if (i & CPY_SIGN_NEGATIVE) { |
| 131 |
sign = -1; |
| 132 |
} |
| 133 |
i >>= CPY_NON_SIZE_BITS; |
| 134 |
while (--i >= 0) { |
| 135 |
prev = x; |
| 136 |
x = (x << PyLong_SHIFT) + CPY_LONG_DIGIT(v, i); |
| 137 |
if ((x >> PyLong_SHIFT) != prev) { |
| 138 |
*overflow = sign; |
| 139 |
goto exit; |
| 140 |
} |
| 141 |
} |
| 142 |
/* Haven't lost any bits, but casting to long requires extra |
| 143 |
* care. |
| 144 |
*/ |
| 145 |
if (x <= (size_t)CPY_TAGGED_MAX) { |
| 146 |
res = (Py_ssize_t)x * sign; |
| 147 |
} |
| 148 |
else if (sign < 0 && x == CPY_TAGGED_ABS_MIN) { |
| 149 |
res = CPY_TAGGED_MIN; |
| 150 |
} |
| 151 |
else { |
| 152 |
*overflow = sign; |
| 153 |
/* res is already set to -1 */ |
| 154 |
} |
| 155 |
exit: |
| 156 |
return res; |
| 157 |
} |
| 158 |
|
| 159 |
#else |
| 160 |
|
| 161 |
// Slow path of CPyLong_AsSsize_tAndOverflow (non-inlined, Python 3.11 and earlier) |
| 162 |
Py_ssize_t |
| 163 |
CPyLong_AsSsize_tAndOverflow_(PyObject *vv, int *overflow) |
| 164 |
{ |
| 165 |
/* This version by Tim Peters */ |
| 166 |
PyLongObject *v = (PyLongObject *)vv; |
| 167 |
size_t x, prev; |
| 168 |
Py_ssize_t res; |
| 169 |
Py_ssize_t i; |
| 170 |
int sign; |
| 171 |
|
| 172 |
*overflow = 0; |
| 173 |
|
| 174 |
res = -1; |
| 175 |
i = Py_SIZE(v); |
| 176 |
|
| 177 |
sign = 1; |
| 178 |
x = 0; |
| 179 |
if (i < 0) { |
| 180 |
sign = -1; |
| 181 |
i = -(i); |
| 182 |
} |
| 183 |
while (--i >= 0) { |
| 184 |
prev = x; |
| 185 |
x = (x << PyLong_SHIFT) + CPY_LONG_DIGIT(v, i); |
| 186 |
if ((x >> PyLong_SHIFT) != prev) { |
| 187 |
*overflow = sign; |
| 188 |
goto exit; |
| 189 |
} |
| 190 |
} |
| 191 |
/* Haven't lost any bits, but casting to long requires extra |
| 192 |
* care. |
| 193 |
*/ |
| 194 |
if (x <= (size_t)CPY_TAGGED_MAX) { |
| 195 |
res = (Py_ssize_t)x * sign; |
| 196 |
} |
| 197 |
else if (sign < 0 && x == CPY_TAGGED_ABS_MIN) { |
| 198 |
res = CPY_TAGGED_MIN; |
| 199 |
} |
| 200 |
else { |
| 201 |
*overflow = sign; |
| 202 |
/* res is already set to -1 */ |
| 203 |
} |
| 204 |
exit: |
| 205 |
return res; |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
#endif |