| 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 |
#ifdef Py_GIL_DISABLED |
| 9 |
// Cold slow path of CPy_GetAttrRef (declared in pythonsupport.h). Reached only |
| 10 |
// when the inline fast-path try-incref fails: the value is owned by another |
| 11 |
// thread, so taking a reference requires an atomic shared-refcount operation. |
| 12 |
// Kept out-of-line so the inline fast path stays small. |
| 13 |
// |
| 14 |
// First try the lock-free shared-refcount CAS. If the value has not had |
| 15 |
// maybe-weakref set yet (for example, it was published by CPy_InitAttrRef), force |
| 16 |
// a cross-thread reference via _Py_NewRefWithLock, which cannot fail and sets |
| 17 |
// maybe-weakref so subsequent reads take the fast path. The value was already |
| 18 |
// observed in the field by CPy_GetAttrRef; CPy_SetAttrRef's QSBR-delayed decref |
| 19 |
// keeps any replaced value alive long enough for this reader. |
| 20 |
CPy_NOINLINE |
| 21 |
PyObject *CPy_GetAttrRefSlow(PyObject *v) { |
| 22 |
if (_Py_TryIncRefShared(v)) { |
| 23 |
return v; |
| 24 |
} |
| 25 |
return _Py_NewRefWithLock(v); // sets maybe-weakref; cannot fail |
| 26 |
} |
| 27 |
#endif |
| 28 |
|
| 29 |
///////////////////////////////////////// |
| 30 |
// Adapted from bltinmodule.c in Python 3.7.0 |
| 31 |
PyObject* |
| 32 |
update_bases(PyObject *bases) |
| 33 |
{ |
| 34 |
Py_ssize_t i, j; |
| 35 |
PyObject *base, *meth, *new_base, *result, *new_bases = NULL; |
| 36 |
PyObject *stack[1] = {bases}; |
| 37 |
assert(PyTuple_Check(bases)); |
| 38 |
|
| 39 |
Py_ssize_t nargs = PyTuple_GET_SIZE(bases); |
| 40 |
for (i = 0; i < nargs; i++) { |
| 41 |
base = PyTuple_GET_ITEM(bases, i); |
| 42 |
if (PyType_Check(base)) { |
| 43 |
if (new_bases) { |
| 44 |
/* If we already have made a replacement, then we append every normal base, |
| 45 |
otherwise just skip it. */ |
| 46 |
if (PyList_Append(new_bases, base) < 0) { |
| 47 |
goto error; |
| 48 |
} |
| 49 |
} |
| 50 |
continue; |
| 51 |
} |
| 52 |
if (PyObject_GetOptionalAttr(base, mypyc_interned_str.__mro_entries__, &meth) < 0) { |
| 53 |
goto error; |
| 54 |
} |
| 55 |
if (!meth) { |
| 56 |
if (new_bases) { |
| 57 |
if (PyList_Append(new_bases, base) < 0) { |
| 58 |
goto error; |
| 59 |
} |
| 60 |
} |
| 61 |
continue; |
| 62 |
} |
| 63 |
new_base = PyObject_Vectorcall(meth, stack, 1, NULL); |
| 64 |
Py_DECREF(meth); |
| 65 |
if (!new_base) { |
| 66 |
goto error; |
| 67 |
} |
| 68 |
if (!PyTuple_Check(new_base)) { |
| 69 |
PyErr_SetString(PyExc_TypeError, |
| 70 |
"__mro_entries__ must return a tuple"); |
| 71 |
Py_DECREF(new_base); |
| 72 |
goto error; |
| 73 |
} |
| 74 |
if (!new_bases) { |
| 75 |
/* If this is a first successful replacement, create new_bases list and |
| 76 |
copy previously encountered bases. */ |
| 77 |
if (!(new_bases = PyList_New(i))) { |
| 78 |
goto error; |
| 79 |
} |
| 80 |
for (j = 0; j < i; j++) { |
| 81 |
base = PyTuple_GET_ITEM(bases, j); |
| 82 |
PyList_SET_ITEM(new_bases, j, base); |
| 83 |
Py_INCREF(base); |
| 84 |
} |
| 85 |
} |
| 86 |
j = PyList_GET_SIZE(new_bases); |
| 87 |
if (PyList_SetSlice(new_bases, j, j, new_base) < 0) { |
| 88 |
goto error; |
| 89 |
} |
| 90 |
Py_DECREF(new_base); |
| 91 |
} |
| 92 |
if (!new_bases) { |
| 93 |
return bases; |
| 94 |
} |
| 95 |
result = PyList_AsTuple(new_bases); |
| 96 |
Py_DECREF(new_bases); |
| 97 |
return result; |
| 98 |
|
| 99 |
error: |
| 100 |
Py_XDECREF(new_bases); |
| 101 |
return NULL; |
| 102 |
} |
| 103 |
|
| 104 |
// From Python 3.7's typeobject.c |
| 105 |
int |
| 106 |
init_subclass(PyTypeObject *type, PyObject *kwds) |
| 107 |
{ |
| 108 |
PyObject *super, *func, *result; |
| 109 |
PyObject *args[2] = {(PyObject *)type, (PyObject *)type}; |
| 110 |
|
| 111 |
super = PyObject_Vectorcall((PyObject *)&PySuper_Type, args, 2, NULL); |
| 112 |
if (super == NULL) { |
| 113 |
return -1; |
| 114 |
} |
| 115 |
|
| 116 |
func = PyObject_GetAttr(super, mypyc_interned_str.__init_subclass__); |
| 117 |
Py_DECREF(super); |
| 118 |
if (func == NULL) { |
| 119 |
return -1; |
| 120 |
} |
| 121 |
|
| 122 |
result = _PyObject_FastCallDict(func, NULL, 0, kwds); |
| 123 |
Py_DECREF(func); |
| 124 |
if (result == NULL) { |
| 125 |
return -1; |
| 126 |
} |
| 127 |
|
| 128 |
Py_DECREF(result); |
| 129 |
return 0; |
| 130 |
} |
| 131 |
|
| 132 |
#if CPY_3_12_FEATURES |
| 133 |
|
| 134 |
// Slow path of CPyLong_AsSsize_tAndOverflow (non-inlined) |
| 135 |
Py_ssize_t |
| 136 |
CPyLong_AsSsize_tAndOverflow_(PyObject *vv, int *overflow) |
| 137 |
{ |
| 138 |
PyLongObject *v = (PyLongObject *)vv; |
| 139 |
size_t x, prev; |
| 140 |
Py_ssize_t res; |
| 141 |
Py_ssize_t i; |
| 142 |
int sign; |
| 143 |
|
| 144 |
*overflow = 0; |
| 145 |
|
| 146 |
res = -1; |
| 147 |
i = CPY_LONG_TAG(v); |
| 148 |
|
| 149 |
sign = 1; |
| 150 |
x = 0; |
| 151 |
if (i & CPY_SIGN_NEGATIVE) { |
| 152 |
sign = -1; |
| 153 |
} |
| 154 |
i >>= CPY_NON_SIZE_BITS; |
| 155 |
while (--i >= 0) { |
| 156 |
prev = x; |
| 157 |
x = (x << PyLong_SHIFT) + CPY_LONG_DIGIT(v, i); |
| 158 |
if ((x >> PyLong_SHIFT) != prev) { |
| 159 |
*overflow = sign; |
| 160 |
goto exit; |
| 161 |
} |
| 162 |
} |
| 163 |
/* Haven't lost any bits, but casting to long requires extra |
| 164 |
* care. |
| 165 |
*/ |
| 166 |
if (x <= (size_t)CPY_TAGGED_MAX) { |
| 167 |
res = (Py_ssize_t)x * sign; |
| 168 |
} |
| 169 |
else if (sign < 0 && x == CPY_TAGGED_ABS_MIN) { |
| 170 |
res = CPY_TAGGED_MIN; |
| 171 |
} |
| 172 |
else { |
| 173 |
*overflow = sign; |
| 174 |
/* res is already set to -1 */ |
| 175 |
} |
| 176 |
exit: |
| 177 |
return res; |
| 178 |
} |
| 179 |
|
| 180 |
#else |
| 181 |
|
| 182 |
// Slow path of CPyLong_AsSsize_tAndOverflow (non-inlined, Python 3.11 and earlier) |
| 183 |
Py_ssize_t |
| 184 |
CPyLong_AsSsize_tAndOverflow_(PyObject *vv, int *overflow) |
| 185 |
{ |
| 186 |
/* This version by Tim Peters */ |
| 187 |
PyLongObject *v = (PyLongObject *)vv; |
| 188 |
size_t x, prev; |
| 189 |
Py_ssize_t res; |
| 190 |
Py_ssize_t i; |
| 191 |
int sign; |
| 192 |
|
| 193 |
*overflow = 0; |
| 194 |
|
| 195 |
res = -1; |
| 196 |
i = Py_SIZE(v); |
| 197 |
|
| 198 |
sign = 1; |
| 199 |
x = 0; |
| 200 |
if (i < 0) { |
| 201 |
sign = -1; |
| 202 |
i = -(i); |
| 203 |
} |
| 204 |
while (--i >= 0) { |
| 205 |
prev = x; |
| 206 |
x = (x << PyLong_SHIFT) + CPY_LONG_DIGIT(v, i); |
| 207 |
if ((x >> PyLong_SHIFT) != prev) { |
| 208 |
*overflow = sign; |
| 209 |
goto exit; |
| 210 |
} |
| 211 |
} |
| 212 |
/* Haven't lost any bits, but casting to long requires extra |
| 213 |
* care. |
| 214 |
*/ |
| 215 |
if (x <= (size_t)CPY_TAGGED_MAX) { |
| 216 |
res = (Py_ssize_t)x * sign; |
| 217 |
} |
| 218 |
else if (sign < 0 && x == CPY_TAGGED_ABS_MIN) { |
| 219 |
res = CPY_TAGGED_MIN; |
| 220 |
} |
| 221 |
else { |
| 222 |
*overflow = sign; |
| 223 |
/* res is already set to -1 */ |
| 224 |
} |
| 225 |
exit: |
| 226 |
return res; |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
#endif |