| 1 |
#include "Python.h" |
| 2 |
#include <signal.h> |
| 3 |
|
| 4 |
#define _PyTuple_ITEMS PySequence_Fast_ITEMS |
| 5 |
|
| 6 |
PyObject* |
| 7 |
_Py_CheckFunctionResult(PyObject *callable, PyObject *result, const char *where) |
| 8 |
{ |
| 9 |
int err_occurred = (PyErr_Occurred() != NULL); |
| 10 |
|
| 11 |
assert((callable != NULL) ^ (where != NULL)); |
| 12 |
|
| 13 |
if (result == NULL) { |
| 14 |
if (!err_occurred) { |
| 15 |
if (callable) |
| 16 |
PyErr_Format(PyExc_SystemError, |
| 17 |
"%R returned NULL without setting an error", |
| 18 |
callable); |
| 19 |
else |
| 20 |
PyErr_Format(PyExc_SystemError, |
| 21 |
"%s returned NULL without setting an error", |
| 22 |
where); |
| 23 |
#ifdef Py_DEBUG |
| 24 |
|
| 25 |
Py_FatalError("a function returned NULL without setting an error"); |
| 26 |
#endif |
| 27 |
return NULL; |
| 28 |
} |
| 29 |
} |
| 30 |
else { |
| 31 |
if (err_occurred) { |
| 32 |
Py_DECREF(result); |
| 33 |
|
| 34 |
if (callable) { |
| 35 |
_PyErr_FormatFromCause(PyExc_SystemError, |
| 36 |
"%R returned a result with an error set", |
| 37 |
callable); |
| 38 |
} |
| 39 |
else { |
| 40 |
_PyErr_FormatFromCause(PyExc_SystemError, |
| 41 |
"%s returned a result with an error set", |
| 42 |
where); |
| 43 |
} |
| 44 |
#ifdef Py_DEBUG |
| 45 |
|
| 46 |
Py_FatalError("a function returned a result with an error set"); |
| 47 |
#endif |
| 48 |
return NULL; |
| 49 |
} |
| 50 |
} |
| 51 |
return result; |
| 52 |
} |
| 53 |
|
| 54 |
int |
| 55 |
_PyStack_UnpackDict(PyObject *const *args, Py_ssize_t nargs, PyObject *kwargs, |
| 56 |
PyObject *const **p_stack, PyObject **p_kwnames) |
| 57 |
{ |
| 58 |
PyObject **stack, **kwstack; |
| 59 |
Py_ssize_t nkwargs; |
| 60 |
Py_ssize_t pos, i; |
| 61 |
PyObject *key, *value; |
| 62 |
PyObject *kwnames; |
| 63 |
|
| 64 |
assert(nargs >= 0); |
| 65 |
assert(kwargs == NULL || PyDict_CheckExact(kwargs)); |
| 66 |
|
| 67 |
if (kwargs == NULL || (nkwargs = PyDict_GET_SIZE(kwargs)) == 0) { |
| 68 |
*p_stack = args; |
| 69 |
*p_kwnames = NULL; |
| 70 |
return 0; |
| 71 |
} |
| 72 |
|
| 73 |
if ((size_t)nargs > PY_SSIZE_T_MAX / sizeof(stack[0]) - (size_t)nkwargs) { |
| 74 |
PyErr_NoMemory(); |
| 75 |
return -1; |
| 76 |
} |
| 77 |
|
| 78 |
stack = PyMem_Malloc((nargs + nkwargs) * sizeof(stack[0])); |
| 79 |
if (stack == NULL) { |
| 80 |
PyErr_NoMemory(); |
| 81 |
return -1; |
| 82 |
} |
| 83 |
|
| 84 |
kwnames = PyTuple_New(nkwargs); |
| 85 |
if (kwnames == NULL) { |
| 86 |
PyMem_Free(stack); |
| 87 |
return -1; |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
for (i = 0; i < nargs; i++) { |
| 92 |
Py_INCREF(args[i]); |
| 93 |
stack[i] = args[i]; |
| 94 |
} |
| 95 |
|
| 96 |
kwstack = stack + nargs; |
| 97 |
pos = i = 0; |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
while (PyDict_Next(kwargs, &pos, &key, &value)) { |
| 102 |
Py_INCREF(key); |
| 103 |
Py_INCREF(value); |
| 104 |
PyTuple_SET_ITEM(kwnames, i, key); |
| 105 |
kwstack[i] = value; |
| 106 |
i++; |
| 107 |
} |
| 108 |
|
| 109 |
*p_stack = stack; |
| 110 |
*p_kwnames = kwnames; |
| 111 |
return 0; |
| 112 |
} |
| 113 |
|
| 114 |
PyObject * |
| 115 |
PyVectorcall_Call(PyObject *callable, PyObject *tuple, PyObject *kwargs) |
| 116 |
{ |
| 117 |
|
| 118 |
|
| 119 |
vectorcallfunc func; |
| 120 |
Py_ssize_t offset = Py_TYPE(callable)->tp_vectorcall_offset; |
| 121 |
if (offset == 0) { |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
if (Py_TYPE(callable)->tp_call) { |
| 127 |
PyObject *result = Py_TYPE(callable)->tp_call(callable, tuple, kwargs); |
| 128 |
return _Py_CheckFunctionResult(callable, result, NULL); |
| 129 |
} |
| 130 |
} |
| 131 |
if (offset <= 0) { |
| 132 |
PyErr_Format(PyExc_TypeError, "'%.200s' object does not support vectorcall", |
| 133 |
Py_TYPE(callable)->tp_name); |
| 134 |
return NULL; |
| 135 |
} |
| 136 |
memcpy(&func, (char *) callable + offset, sizeof(func)); |
| 137 |
if (func == NULL) { |
| 138 |
PyErr_Format(PyExc_TypeError, "'%.200s' object does not support vectorcall", |
| 139 |
Py_TYPE(callable)->tp_name); |
| 140 |
return NULL; |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
PyObject *const *args; |
| 145 |
Py_ssize_t nargs = PyTuple_GET_SIZE(tuple); |
| 146 |
PyObject *kwnames; |
| 147 |
if (_PyStack_UnpackDict(_PyTuple_ITEMS(tuple), nargs, |
| 148 |
kwargs, &args, &kwnames) < 0) { |
| 149 |
return NULL; |
| 150 |
} |
| 151 |
PyObject *result = func(callable, args, nargs, kwnames); |
| 152 |
if (kwnames != NULL) { |
| 153 |
Py_ssize_t i, n = PyTuple_GET_SIZE(kwnames) + nargs; |
| 154 |
for (i = 0; i < n; i++) { |
| 155 |
Py_DECREF(args[i]); |
| 156 |
} |
| 157 |
PyMem_Free((PyObject **)args); |
| 158 |
Py_DECREF(kwnames); |
| 159 |
} |
| 160 |
|
| 161 |
return _Py_CheckFunctionResult(callable, result, NULL); |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
PyObject * |
| 168 |
PyEval_CallFunction(PyObject *obj, const char *format, ...) |
| 169 |
{ |
| 170 |
va_list vargs; |
| 171 |
PyObject *args; |
| 172 |
PyObject *res; |
| 173 |
|
| 174 |
va_start(vargs, format); |
| 175 |
|
| 176 |
args = Py_VaBuildValue(format, vargs); |
| 177 |
va_end(vargs); |
| 178 |
|
| 179 |
if (args == NULL) |
| 180 |
return NULL; |
| 181 |
|
| 182 |
res = PyEval_CallObject(obj, args); |
| 183 |
Py_DECREF(args); |
| 184 |
|
| 185 |
return res; |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
PyObject * |
| 192 |
PyEval_CallMethod(PyObject *obj, const char *methodname, const char *format, ...) |
| 193 |
{ |
| 194 |
va_list vargs; |
| 195 |
PyObject *meth; |
| 196 |
PyObject *args; |
| 197 |
PyObject *res; |
| 198 |
|
| 199 |
meth = PyObject_GetAttrString(obj, methodname); |
| 200 |
if (meth == NULL) |
| 201 |
return NULL; |
| 202 |
|
| 203 |
va_start(vargs, format); |
| 204 |
|
| 205 |
args = Py_VaBuildValue(format, vargs); |
| 206 |
va_end(vargs); |
| 207 |
|
| 208 |
if (args == NULL) { |
| 209 |
Py_DECREF(meth); |
| 210 |
return NULL; |
| 211 |
} |
| 212 |
|
| 213 |
res = PyEval_CallObject(meth, args); |
| 214 |
Py_DECREF(meth); |
| 215 |
Py_DECREF(args); |
| 216 |
return res; |
| 217 |
} |