| 1 |
#define PY_SSIZE_T_CLEAN |
| 2 |
#include "CPy.h" |
| 3 |
#include <stdint.h> |
| 4 |
|
| 5 |
#define CPyFunction_weakreflist(f) (((PyCFunctionObject *)f)->m_weakreflist) |
| 6 |
#define CPyFunction_class(f) ((PyObject*) ((PyCMethodObject *) (f))->mm_class) |
| 7 |
#define CPyFunction_func_vectorcall(f) (((PyCFunctionObject *)f)->vectorcall) |
| 8 |
|
| 9 |
static int |
| 10 |
CPyFunction_clear(CPyFunction *m) { |
| 11 |
Py_CLEAR(((PyCFunctionObject*)m)->m_module); |
| 12 |
PyObject_ClearManagedDict((PyObject*)m); |
| 13 |
Py_CLEAR(m->func_name); |
| 14 |
Py_CLEAR(m->func_code); |
| 15 |
PyObject *cls = CPyFunction_class(m); |
| 16 |
((PyCMethodObject *)m)->mm_class = NULL; |
| 17 |
Py_XDECREF(cls); |
| 18 |
|
| 19 |
return 0; |
| 20 |
} |
| 21 |
|
| 22 |
static void CPyFunction_dealloc(CPyFunction *m) { |
| 23 |
PyObject_GC_UnTrack(m); |
| 24 |
if (CPyFunction_weakreflist(m) != NULL) |
| 25 |
PyObject_ClearWeakRefs((PyObject *) m); |
| 26 |
CPyFunction_clear(m); |
| 27 |
PyMem_Free(m->func.func.m_ml); |
| 28 |
PyObject_GC_Del(m); |
| 29 |
} |
| 30 |
|
| 31 |
static PyObject* CPyFunction_repr(CPyFunction *op) { |
| 32 |
// Use helper to get name for free threading safety. |
| 33 |
PyObject *name = CPyFunction_get_name((PyObject *)op, NULL); |
| 34 |
if (unlikely(name == NULL)) { |
| 35 |
return NULL; |
| 36 |
} |
| 37 |
PyObject *result = PyUnicode_FromFormat("<function %U at %p>", name, (void *)op); |
| 38 |
Py_DECREF(name); |
| 39 |
return result; |
| 40 |
} |
| 41 |
|
| 42 |
static PyObject* CPyFunction_call(PyObject *func, PyObject *args, PyObject *kw) { |
| 43 |
CPyFunction *f = (CPyFunction *)func; |
| 44 |
vectorcallfunc vc = CPyFunction_func_vectorcall(f); |
| 45 |
assert(vc); |
| 46 |
return PyVectorcall_Call(func, args, kw); |
| 47 |
} |
| 48 |
|
| 49 |
static int CPyFunction_traverse(CPyFunction *m, visitproc visit, void *arg) { |
| 50 |
Py_VISIT(((PyCFunctionObject *)m)->m_module); |
| 51 |
int e = PyObject_VisitManagedDict((PyObject*)m, visit, arg); |
| 52 |
if (e != 0) return e; |
| 53 |
Py_VISIT(m->func_name); |
| 54 |
Py_VISIT(m->func_code); |
| 55 |
Py_VISIT(CPyFunction_class(m)); |
| 56 |
return 0; |
| 57 |
} |
| 58 |
|
| 59 |
static PyMemberDef CPyFunction_members[] = { |
| 60 |
{"__module__", T_OBJECT, offsetof(PyCFunctionObject, m_module), 0, 0}, |
| 61 |
{"__vectorcalloffset__", T_PYSSIZET, offsetof(PyCFunctionObject, vectorcall), READONLY, 0}, |
| 62 |
{"__weaklistoffset__", T_PYSSIZET, offsetof(PyCFunctionObject, m_weakreflist), READONLY, 0}, |
| 63 |
{0, 0, 0, 0, 0} |
| 64 |
}; |
| 65 |
|
| 66 |
PyObject* CPyFunction_get_name(PyObject *op, void *context) { |
| 67 |
(void)context; |
| 68 |
CPyFunction *func = (CPyFunction *)op; |
| 69 |
PyObject *result; |
| 70 |
Py_BEGIN_CRITICAL_SECTION(op); |
| 71 |
if (unlikely(func->func_name == NULL)) { |
| 72 |
func->func_name = PyUnicode_InternFromString(((PyCFunctionObject *)func)->m_ml->ml_name); |
| 73 |
} |
| 74 |
result = func->func_name; |
| 75 |
Py_XINCREF(result); |
| 76 |
Py_END_CRITICAL_SECTION(); |
| 77 |
return result; |
| 78 |
} |
| 79 |
|
| 80 |
int CPyFunction_set_name(PyObject *op, PyObject *value, void *context) { |
| 81 |
(void)context; |
| 82 |
CPyFunction *func = (CPyFunction *)op; |
| 83 |
if (unlikely(!value || !PyUnicode_Check(value))) { |
| 84 |
PyErr_SetString(PyExc_TypeError, "__name__ must be set to a string object"); |
| 85 |
return -1; |
| 86 |
} |
| 87 |
|
| 88 |
Py_INCREF(value); |
| 89 |
// Decref outside critical section, since it could run arbitrary code. |
| 90 |
PyObject *old; |
| 91 |
Py_BEGIN_CRITICAL_SECTION(op); |
| 92 |
old = func->func_name; |
| 93 |
func->func_name = value; |
| 94 |
Py_END_CRITICAL_SECTION(); |
| 95 |
Py_XDECREF(old); |
| 96 |
return 0; |
| 97 |
} |
| 98 |
|
| 99 |
PyObject* CPyFunction_get_code(PyObject *op, void *context) { |
| 100 |
(void)context; |
| 101 |
CPyFunction *func = (CPyFunction *)op; |
| 102 |
PyObject* result = (func->func_code) ? func->func_code : Py_None; |
| 103 |
Py_INCREF(result); |
| 104 |
return result; |
| 105 |
} |
| 106 |
|
| 107 |
static PyObject* CPyFunction_get_none(PyObject *op, void *context) { |
| 108 |
(void)op; |
| 109 |
(void)context; |
| 110 |
PyObject* result = Py_None; |
| 111 |
Py_INCREF(result); |
| 112 |
return result; |
| 113 |
} |
| 114 |
|
| 115 |
int CPyFunction_set_none(PyObject *op, PyObject *value, void *context) { |
| 116 |
(void)op; |
| 117 |
(void)value; |
| 118 |
(void)context; |
| 119 |
return 0; |
| 120 |
} |
| 121 |
|
| 122 |
PyObject* CPyFunction_get_defaults(PyObject *op, void *context) { |
| 123 |
return CPyFunction_get_none(op, context); |
| 124 |
} |
| 125 |
|
| 126 |
PyObject* CPyFunction_get_kwdefaults(PyObject *op, void *context) { |
| 127 |
return CPyFunction_get_none(op, context); |
| 128 |
} |
| 129 |
|
| 130 |
PyObject* CPyFunction_get_annotations(PyObject *op, void *context) { |
| 131 |
return CPyFunction_get_none(op, context); |
| 132 |
} |
| 133 |
|
| 134 |
int CPyFunction_set_annotations(PyObject *op, PyObject *value, void *context) { |
| 135 |
return CPyFunction_set_none(op, value, context); |
| 136 |
} |
| 137 |
|
| 138 |
static PyGetSetDef CPyFunction_getsets[] = { |
| 139 |
{"__dict__", (getter)PyObject_GenericGetDict, (setter)PyObject_GenericSetDict, 0, 0}, |
| 140 |
{"__name__", (getter)CPyFunction_get_name, (setter)CPyFunction_set_name, 0, 0}, |
| 141 |
{"__code__", (getter)CPyFunction_get_code, 0, 0, 0}, |
| 142 |
{"__defaults__", (getter)CPyFunction_get_defaults, 0, 0, 0}, |
| 143 |
{"__kwdefaults__", (getter)CPyFunction_get_kwdefaults, 0, 0, 0}, |
| 144 |
{"__annotations__", (getter)CPyFunction_get_annotations, CPyFunction_set_annotations, 0, 0}, |
| 145 |
{0, 0, 0, 0, 0} |
| 146 |
}; |
| 147 |
|
| 148 |
static PyObject* CPy_PyMethod_New(PyObject *func, PyObject *self, PyObject *typ) { |
| 149 |
(void)typ; |
| 150 |
if (!self) { |
| 151 |
Py_INCREF(func); |
| 152 |
return func; |
| 153 |
} |
| 154 |
return PyMethod_New(func, self); |
| 155 |
} |
| 156 |
|
| 157 |
static PyType_Slot CPyFunction_slots[] = { |
| 158 |
{Py_tp_dealloc, (void *)CPyFunction_dealloc}, |
| 159 |
{Py_tp_repr, (void *)CPyFunction_repr}, |
| 160 |
{Py_tp_call, (void *)CPyFunction_call}, |
| 161 |
{Py_tp_traverse, (void *)CPyFunction_traverse}, |
| 162 |
{Py_tp_clear, (void *)CPyFunction_clear}, |
| 163 |
{Py_tp_members, (void *)CPyFunction_members}, |
| 164 |
{Py_tp_getset, (void *)CPyFunction_getsets}, |
| 165 |
{Py_tp_descr_get, (void *)CPy_PyMethod_New}, |
| 166 |
{0, 0}, |
| 167 |
}; |
| 168 |
|
| 169 |
static PyType_Spec CPyFunction_spec = { |
| 170 |
.name = "Function compiled with mypyc", |
| 171 |
.basicsize = sizeof(CPyFunction), |
| 172 |
.itemsize = 0, |
| 173 |
.flags = Py_TPFLAGS_IMMUTABLETYPE | |
| 174 |
#if PY_VERSION_HEX >= 0x030C0000 |
| 175 |
Py_TPFLAGS_MANAGED_DICT | |
| 176 |
#endif |
| 177 |
Py_TPFLAGS_HAVE_VECTORCALL | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE, |
| 178 |
.slots = CPyFunction_slots, |
| 179 |
}; |
| 180 |
|
| 181 |
static PyTypeObject *CPyFunctionType = NULL; |
| 182 |
|
| 183 |
static PyObject* CPyFunction_Vectorcall(PyObject *func, PyObject *const *args, size_t nargsf, PyObject *kwnames) { |
| 184 |
CPyFunction *f = (CPyFunction *)func; |
| 185 |
Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); |
| 186 |
PyObject *self; |
| 187 |
PyCFunction meth = ((PyCFunctionObject *)f)->m_ml->ml_meth; |
| 188 |
|
| 189 |
self = ((PyCFunctionObject *)f)->m_self; |
| 190 |
if (!self) { |
| 191 |
self = args[0]; |
| 192 |
args += 1; |
| 193 |
nargs -= 1; |
| 194 |
} |
| 195 |
return ((_PyCFunctionFastWithKeywords)(void(*)(void))meth)(self, args, nargs, kwnames); |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
static CPyFunction* CPyFunction_Init(CPyFunction *op, PyMethodDef *ml, PyObject* name, |
| 200 |
PyObject *module, PyObject* code, bool set_self) { |
| 201 |
PyCFunctionObject *cf = (PyCFunctionObject *)op; |
| 202 |
CPyFunction_weakreflist(op) = NULL; |
| 203 |
cf->m_ml = ml; |
| 204 |
cf->m_self = set_self ? (PyObject *) op : NULL; |
| 205 |
|
| 206 |
Py_XINCREF(module); |
| 207 |
cf->m_module = module; |
| 208 |
|
| 209 |
Py_INCREF(name); |
| 210 |
op->func_name = name; |
| 211 |
|
| 212 |
((PyCMethodObject *)op)->mm_class = NULL; |
| 213 |
|
| 214 |
Py_XINCREF(code); |
| 215 |
op->func_code = code; |
| 216 |
|
| 217 |
CPyFunction_func_vectorcall(op) = CPyFunction_Vectorcall; |
| 218 |
return op; |
| 219 |
} |
| 220 |
|
| 221 |
static PyObject* CPyCode_New(const char *filename, const char *funcname, int first_line, int flags) { |
| 222 |
PyCodeObject *code_obj = PyCode_NewEmpty(filename, funcname, first_line); |
| 223 |
if (unlikely(!code_obj)) { |
| 224 |
return NULL; |
| 225 |
} |
| 226 |
code_obj->co_flags = flags; |
| 227 |
return (PyObject *)code_obj; |
| 228 |
} |
| 229 |
|
| 230 |
static PyMethodDef* CPyMethodDef_New(const char *name, PyCFunction func, int flags, const char *doc) { |
| 231 |
PyMethodDef *method = (PyMethodDef *)PyMem_Malloc(sizeof(PyMethodDef)); |
| 232 |
if (unlikely(!method)) { |
| 233 |
return NULL; |
| 234 |
} |
| 235 |
method->ml_name = name; |
| 236 |
method->ml_meth = func; |
| 237 |
method->ml_flags = flags; |
| 238 |
method->ml_doc = doc; |
| 239 |
return method; |
| 240 |
} |
| 241 |
|
| 242 |
PyObject* CPyFunction_New(PyObject *module, const char *filename, const char *funcname, |
| 243 |
PyCFunction func, int func_flags, const char *func_doc, |
| 244 |
int first_line, int code_flags, bool has_self_arg) { |
| 245 |
PyMethodDef *method = NULL; |
| 246 |
PyObject *code = NULL, *op = NULL; |
| 247 |
bool set_self = false; |
| 248 |
|
| 249 |
#ifdef Py_GIL_DISABLED |
| 250 |
// Double-checked locking: the common case (type already created) is a |
| 251 |
// lock-free atomic load. Only the first-time initialization takes the |
| 252 |
// mutex, which serializes concurrent creators. |
| 253 |
if (!_Py_atomic_load_ptr_acquire(&CPyFunctionType)) { |
| 254 |
static PyMutex type_init_mutex = {0}; |
| 255 |
PyMutex_Lock(&type_init_mutex); |
| 256 |
if (!CPyFunctionType) { |
| 257 |
PyTypeObject *type = (PyTypeObject *)PyType_FromSpec(&CPyFunction_spec); |
| 258 |
if (unlikely(!type)) { |
| 259 |
PyMutex_Unlock(&type_init_mutex); |
| 260 |
goto err; |
| 261 |
} |
| 262 |
_Py_atomic_store_ptr_release(&CPyFunctionType, type); |
| 263 |
} |
| 264 |
PyMutex_Unlock(&type_init_mutex); |
| 265 |
} |
| 266 |
#else |
| 267 |
if (!CPyFunctionType) { |
| 268 |
CPyFunctionType = (PyTypeObject *)PyType_FromSpec(&CPyFunction_spec); |
| 269 |
if (unlikely(!CPyFunctionType)) { |
| 270 |
goto err; |
| 271 |
} |
| 272 |
} |
| 273 |
#endif |
| 274 |
|
| 275 |
method = CPyMethodDef_New(funcname, func, func_flags, func_doc); |
| 276 |
if (unlikely(!method)) { |
| 277 |
goto err; |
| 278 |
} |
| 279 |
code = CPyCode_New(filename, funcname, first_line, code_flags); |
| 280 |
if (unlikely(!code)) { |
| 281 |
goto err; |
| 282 |
} |
| 283 |
|
| 284 |
// Set m_self inside the function wrapper only if the wrapped function has no self arg |
| 285 |
// to pass m_self as the self arg when the function is called. |
| 286 |
// When the function has a self arg, it will come in the args vector passed to the |
| 287 |
// vectorcall handler. |
| 288 |
set_self = !has_self_arg; |
| 289 |
op = (PyObject *)CPyFunction_Init(PyObject_GC_New(CPyFunction, CPyFunctionType), |
| 290 |
method, PyUnicode_FromString(funcname), module, |
| 291 |
code, set_self); |
| 292 |
if (unlikely(!op)) { |
| 293 |
goto err; |
| 294 |
} |
| 295 |
PyObject_GC_Track(op); |
| 296 |
return op; |
| 297 |
|
| 298 |
err: |
| 299 |
CPyError_OutOfMemory(); |
| 300 |
if (method) { |
| 301 |
PyMem_Free(method); |
| 302 |
} |
| 303 |
return NULL; |
| 304 |
} |