| 1 |
#include "pythoncapi_compat.h" |
| 2 |
|
| 3 |
// Exception related primitive operations |
| 4 |
// |
| 5 |
// These are registered in mypyc.primitives.exc_ops. |
| 6 |
|
| 7 |
#include <Python.h> |
| 8 |
#include "CPy.h" |
| 9 |
|
| 10 |
void CPy_Raise(PyObject *exc) { |
| 11 |
if (PyObject_IsInstance(exc, (PyObject *)&PyType_Type)) { |
| 12 |
PyObject *obj = PyObject_CallNoArgs(exc); |
| 13 |
if (!obj) |
| 14 |
return; |
| 15 |
PyErr_SetObject(exc, obj); |
| 16 |
Py_DECREF(obj); |
| 17 |
} else { |
| 18 |
PyErr_SetObject((PyObject *)Py_TYPE(exc), exc); |
| 19 |
} |
| 20 |
} |
| 21 |
|
| 22 |
void CPy_Reraise(void) { |
| 23 |
PyObject *p_type, *p_value, *p_traceback; |
| 24 |
PyErr_GetExcInfo(&p_type, &p_value, &p_traceback); |
| 25 |
PyErr_Restore(p_type, p_value, p_traceback); |
| 26 |
} |
| 27 |
|
| 28 |
void CPyErr_SetObjectAndTraceback(PyObject *type, PyObject *value, PyObject *traceback) { |
| 29 |
if (!PyType_Check(type) && Py_IsNone(value)) { |
| 30 |
// The first argument must be an exception instance |
| 31 |
value = type; |
| 32 |
type = (PyObject *)Py_TYPE(value); |
| 33 |
} |
| 34 |
|
| 35 |
// Set the value and traceback of an error. Because calling |
| 36 |
// PyErr_Restore takes away a reference to each object passed in |
| 37 |
// as an argument, we manually increase the reference count of |
| 38 |
// each argument before calling it. |
| 39 |
Py_INCREF(type); |
| 40 |
Py_INCREF(value); |
| 41 |
Py_INCREF(traceback); |
| 42 |
PyErr_Restore(type, value, traceback); |
| 43 |
} |
| 44 |
|
| 45 |
tuple_T3OOO CPy_CatchError(void) { |
| 46 |
// We need to return the existing sys.exc_info() information, so |
| 47 |
// that it can be restored when we finish handling the error we |
| 48 |
// are catching now. Grab that triple and convert NULL values to |
| 49 |
// the ExcDummy object in order to simplify refcount handling in |
| 50 |
// generated code. |
| 51 |
tuple_T3OOO ret; |
| 52 |
PyErr_GetExcInfo(&ret.f0, &ret.f1, &ret.f2); |
| 53 |
_CPy_ToDummy(&ret.f0); |
| 54 |
_CPy_ToDummy(&ret.f1); |
| 55 |
_CPy_ToDummy(&ret.f2); |
| 56 |
|
| 57 |
if (!PyErr_Occurred()) { |
| 58 |
PyErr_SetString(PyExc_RuntimeError, "CPy_CatchError called with no error!"); |
| 59 |
} |
| 60 |
|
| 61 |
// Retrieve the error info and normalize it so that it looks like |
| 62 |
// what python code needs it to be. |
| 63 |
PyObject *type, *value, *traceback; |
| 64 |
PyErr_Fetch(&type, &value, &traceback); |
| 65 |
// Could we avoid always normalizing? |
| 66 |
PyErr_NormalizeException(&type, &value, &traceback); |
| 67 |
if (traceback != NULL) { |
| 68 |
PyException_SetTraceback(value, traceback); |
| 69 |
} |
| 70 |
// Indicate that we are now handling this exception by stashing it |
| 71 |
// in sys.exc_info(). mypyc routines that need access to the |
| 72 |
// exception will read it out of there. |
| 73 |
PyErr_SetExcInfo(type, value, traceback); |
| 74 |
// Clear the error indicator, since the exception isn't |
| 75 |
// propagating anymore. |
| 76 |
PyErr_Clear(); |
| 77 |
|
| 78 |
return ret; |
| 79 |
} |
| 80 |
|
| 81 |
void CPy_RestoreExcInfo(tuple_T3OOO info) { |
| 82 |
PyErr_SetExcInfo(_CPy_FromDummy(info.f0), _CPy_FromDummy(info.f1), _CPy_FromDummy(info.f2)); |
| 83 |
} |
| 84 |
|
| 85 |
bool CPy_ExceptionMatches(PyObject *type) { |
| 86 |
return PyErr_GivenExceptionMatches((PyObject *)Py_TYPE(CPy_ExcState()->exc_value), type); |
| 87 |
} |
| 88 |
|
| 89 |
PyObject *CPy_GetExcValue(void) { |
| 90 |
PyObject *exc = CPy_ExcState()->exc_value; |
| 91 |
Py_INCREF(exc); |
| 92 |
return exc; |
| 93 |
} |
| 94 |
|
| 95 |
static inline void _CPy_ToNone(PyObject **p) { |
| 96 |
if (*p == NULL) { |
| 97 |
Py_INCREF(Py_None); |
| 98 |
*p = Py_None; |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
void _CPy_GetExcInfo(PyObject **p_type, PyObject **p_value, PyObject **p_traceback) { |
| 103 |
PyErr_GetExcInfo(p_type, p_value, p_traceback); |
| 104 |
_CPy_ToNone(p_type); |
| 105 |
_CPy_ToNone(p_value); |
| 106 |
_CPy_ToNone(p_traceback); |
| 107 |
} |
| 108 |
|
| 109 |
tuple_T3OOO CPy_GetExcInfo(void) { |
| 110 |
tuple_T3OOO ret; |
| 111 |
_CPy_GetExcInfo(&ret.f0, &ret.f1, &ret.f2); |
| 112 |
return ret; |
| 113 |
} |
| 114 |
|
| 115 |
void CPyError_OutOfMemory(void) { |
| 116 |
fprintf(stderr, "fatal: out of memory\n"); |
| 117 |
fflush(stderr); |
| 118 |
abort(); |
| 119 |
} |
| 120 |
|
| 121 |
// Construct a nicely formatted type name based on __module__ and __name__. |
| 122 |
static PyObject *CPy_GetTypeName(PyObject *type) { |
| 123 |
PyObject *module = NULL, *name = NULL; |
| 124 |
PyObject *full = NULL; |
| 125 |
|
| 126 |
module = PyObject_GetAttr(type, mypyc_interned_str.__module__); |
| 127 |
if (!module || !PyUnicode_Check(module)) { |
| 128 |
goto out; |
| 129 |
} |
| 130 |
name = PyObject_GetAttr(type, mypyc_interned_str.__qualname__); |
| 131 |
if (!name || !PyUnicode_Check(name)) { |
| 132 |
goto out; |
| 133 |
} |
| 134 |
|
| 135 |
if (PyUnicode_CompareWithASCIIString(module, "builtins") == 0) { |
| 136 |
Py_INCREF(name); |
| 137 |
full = name; |
| 138 |
} else { |
| 139 |
full = PyUnicode_FromFormat("%U.%U", module, name); |
| 140 |
} |
| 141 |
|
| 142 |
out: |
| 143 |
Py_XDECREF(module); |
| 144 |
Py_XDECREF(name); |
| 145 |
return full; |
| 146 |
} |
| 147 |
|
| 148 |
// Get the type of a value as a string, expanding tuples to include |
| 149 |
// all the element types. |
| 150 |
static PyObject *CPy_FormatTypeName(PyObject *value) { |
| 151 |
if (Py_IsNone(value)) { |
| 152 |
return PyUnicode_FromString("None"); |
| 153 |
} |
| 154 |
|
| 155 |
if (!PyTuple_CheckExact(value)) { |
| 156 |
return CPy_GetTypeName((PyObject *)Py_TYPE(value)); |
| 157 |
} |
| 158 |
|
| 159 |
if (PyTuple_GET_SIZE(value) > 10) { |
| 160 |
return PyUnicode_FromFormat("tuple[<%d items>]", PyTuple_GET_SIZE(value)); |
| 161 |
} |
| 162 |
|
| 163 |
// Most of the logic is all for tuples, which is the only interesting case |
| 164 |
PyObject *output = PyUnicode_FromString("tuple["); |
| 165 |
if (!output) { |
| 166 |
return NULL; |
| 167 |
} |
| 168 |
/* This is quadratic but if that ever matters something is really weird. */ |
| 169 |
int i; |
| 170 |
for (i = 0; i < PyTuple_GET_SIZE(value); i++) { |
| 171 |
PyObject *s = CPy_FormatTypeName(PyTuple_GET_ITEM(value, i)); |
| 172 |
if (!s) { |
| 173 |
Py_DECREF(output); |
| 174 |
return NULL; |
| 175 |
} |
| 176 |
PyObject *next = PyUnicode_FromFormat("%U%U%s", output, s, |
| 177 |
i + 1 == PyTuple_GET_SIZE(value) ? "]" : ", "); |
| 178 |
Py_DECREF(output); |
| 179 |
Py_DECREF(s); |
| 180 |
if (!next) { |
| 181 |
return NULL; |
| 182 |
} |
| 183 |
output = next; |
| 184 |
} |
| 185 |
return output; |
| 186 |
} |
| 187 |
|
| 188 |
CPy_NOINLINE |
| 189 |
void CPy_TypeError(const char *expected, PyObject *value) { |
| 190 |
PyObject *out = CPy_FormatTypeName(value); |
| 191 |
if (out) { |
| 192 |
PyErr_Format(PyExc_TypeError, "%s object expected; got %U", expected, out); |
| 193 |
Py_DECREF(out); |
| 194 |
} else { |
| 195 |
PyErr_Format(PyExc_TypeError, "%s object expected; and errored formatting real type!", |
| 196 |
expected); |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
// The PyFrameObject type definition (struct _frame) has been moved |
| 201 |
// to the internal C API: to the pycore_frame.h header file. |
| 202 |
// https://github.com/python/cpython/pull/31530 |
| 203 |
#if PY_VERSION_HEX >= 0x030b00a6 |
| 204 |
#include "internal/pycore_frame.h" |
| 205 |
#endif |
| 206 |
|
| 207 |
// This function is basically exactly the same with _PyTraceback_Add |
| 208 |
// which is available in all the versions we support. |
| 209 |
// We're continuing to use this because we'll probably optimize this later. |
| 210 |
void CPy_AddTraceback(const char *filename, const char *funcname, int line, PyObject *globals) { |
| 211 |
PyObject *exc, *val, *tb; |
| 212 |
PyThreadState *thread_state = PyThreadState_GET(); |
| 213 |
PyFrameObject *frame_obj; |
| 214 |
|
| 215 |
// We need to save off the exception state because in 3.8, |
| 216 |
// PyFrame_New fails if there is an error set and it fails to look |
| 217 |
// up builtins in the globals. (_PyTraceback_Add documents that it |
| 218 |
// needs to do it because it decodes the filename according to the |
| 219 |
// FS encoding, which could have a decoder in Python. We don't do |
| 220 |
// that so *that* doesn't apply to us.) |
| 221 |
PyErr_Fetch(&exc, &val, &tb); |
| 222 |
PyCodeObject *code_obj = PyCode_NewEmpty(filename, funcname, line); |
| 223 |
if (code_obj == NULL) { |
| 224 |
goto error; |
| 225 |
} |
| 226 |
|
| 227 |
frame_obj = PyFrame_New(thread_state, code_obj, globals, 0); |
| 228 |
if (frame_obj == NULL) { |
| 229 |
Py_DECREF(code_obj); |
| 230 |
goto error; |
| 231 |
} |
| 232 |
frame_obj->f_lineno = line; |
| 233 |
PyErr_Restore(exc, val, tb); |
| 234 |
PyTraceBack_Here(frame_obj); |
| 235 |
Py_DECREF(code_obj); |
| 236 |
Py_DECREF(frame_obj); |
| 237 |
|
| 238 |
return; |
| 239 |
|
| 240 |
error: |
| 241 |
#if CPY_3_12_FEATURES |
| 242 |
_PyErr_ChainExceptions1(exc); |
| 243 |
#else |
| 244 |
_PyErr_ChainExceptions(exc, val, tb); |
| 245 |
#endif |
| 246 |
} |
| 247 |
|
| 248 |
CPy_NOINLINE |
| 249 |
void CPy_TypeErrorTraceback(const char *filename, const char *funcname, int line, |
| 250 |
PyObject *globals, const char *expected, PyObject *value) { |
| 251 |
CPy_TypeError(expected, value); |
| 252 |
CPy_AddTraceback(filename, funcname, line, globals); |
| 253 |
} |
| 254 |
|
| 255 |
void CPy_AttributeError(const char *filename, const char *funcname, const char *classname, |
| 256 |
const char *attrname, int line, PyObject *globals) { |
| 257 |
char buf[500]; |
| 258 |
snprintf(buf, sizeof(buf), "attribute '%.200s' of '%.200s' undefined", attrname, classname); |
| 259 |
PyErr_SetString(PyExc_AttributeError, buf); |
| 260 |
CPy_AddTraceback(filename, funcname, line, globals); |
| 261 |
} |