theKernel 10.0
Loading...
Searching...
No Matches
MkExceptionC_py.c
Go to the documentation of this file.
1
9/* LABEL-NO */
10
11#define META_FILE_NAME "MkExceptionC_py.c"
12
14
15/* LABEL-END */
16
20
23void NS(MkExceptionC_Raise) (
25 MK_MNGN const expobj,
26 MK_STRN const doc,
27 MK_STRN const callfunc,
28 MK_I32 callline
29)
30{
31 PyErr_Clear();
32 MK_ERR err = MkErrorFORMAT_1M(expobj);
33 MkErrorStackFormat(err,doc,callfunc,callline);
34 PyObject *stackO = NULL; // buffer hold all the current stack frame
35 for (int lvl = 0; MK(Get_Call_Stack)(MK_RT_CALL err, lvl, &stackO); lvl++) {
36 //printI(lvl);
37 }
38 Py_CLEAR(stackO);
39 MK_ERR newerr = MkErrCheck(expobj) && expobj != err ? (MK_ERR)expobj : MkErrorDup(err);
40 PyObject* errO = OT_TMP_ERR_OBJ(newerr);
41 PyErr_SetObject(NS(MkKernelThreadState).MkExceptionC, errO);
42 Py_CLEAR(errO);
43 // TODO difference between python and tcl MkErrorReset_1M(expobj) versa MkErrorReset_1(err)
44 if (expobj) MkErrorReset_1M(expobj);
45}
46
48MK_ERR NS(MkExceptionC_Catch)(
50 MK_MNG const expobj,
51 MK_EXP const exception,
52 MK_STRN const callfunc
53) {
54 //MQ_CTX context = MqCtx(fmtObj);
55
56 PyObject *expO = (PyObject*) exception;
57
58 MK_ERR err = MkErrorFORMAT_1M(expobj);
59 PyObject *typeO = NULL, *valueO=NULL;
60
61 if (expO == Py_None || expO == NULL) {
62 valueO = PyErr_GetRaisedException();
63 if (valueO == NULL) {
64 MkErrorSetC (err, "No active exception to reraise", callfunc, -1);
65 return err;
66 }
67 typeO = Py_NewRef(Py_TYPE(valueO));
68 } else {
69 typeO = Py_NewRef(Py_TYPE(expO));
70 valueO = Py_NewRef(expO);
71 }
72
73//printLngObj(typeO)
74//printLngObj(valueO)
75 {
76 // step 1. - is it an error?
77 if (typeO == Py_None || typeO == NULL) {
78 MkErrorSetC (err, "No active exception to reraise", callfunc, -1);
79
80 // step 2. - is it an MQ error?
81 } else if (PyErr_GivenExceptionMatches(typeO,NS(MkKernelThreadState).MkExceptionC)) {
82 MK_ERR newerr = NULL;
83 while (true) {
84 if (PyObject_TypeCheck(valueO,&NS(MkErrorCR))) {
85 newerr = MkErr(((MkErrorC_Obj*)expO)->hdl);
86 } else if (PyObject_TypeCheck(valueO,(PyTypeObject*)PyExc_BaseException)) {
87 PyObject *tupO = ((PyBaseExceptionObject*)valueO)->args;
88 if (tupO == NULL || !PyTuple_Check(tupO)) break;
89 PyObject *expO = PyTuple_GetItem(tupO, 0);
90 if (expO == NULL) break;
91 if (PyObject_TypeCheck(expO,&NS(MkErrorCR))) {
92 newerr = MkErr(((MkErrorC_Obj*)expO)->hdl);
93 }
94 }
95 break;
96 }
97 if (newerr) {
98 MkErrorSetE (err, newerr);
99 } else {
100 MkErrorSetC (err, "No active exception value object to reraise", callfunc, -1);
101 }
102
103 // step 3. - is it an PYTHON error?
104 } else if (valueO) {
105 OT_OBJ_T errO = OT_TMP_ERR_OBJ(err);
106 OT_OBJ_T tstO = PyObject_CallFunction(NS(MkErrorC_FormatException), "OOs", errO, valueO, callfunc);
107 if (tstO == NULL) {
108 if (PyErr_Occurred()) PyErr_Print();
109 } else {
110 Py_CLEAR(tstO);
111 }
113 MkErrorSetV(err, callfunc, -1, "%s: python error", PyExceptionClass_Name(typeO));
114 }
115 }
116
117 Py_CLEAR(typeO);
118 Py_CLEAR(valueO);
119 if (PyErr_Occurred()) PyErr_Clear();
120 return err;
121}
122
124
125// vim: nowrap
tag: nhi1-release-250425
#define MK(n)
#define NS(n)
#define OT_TMP_ERR_OBJ(val)
PyObject * OT_OBJ_T
#define OT_Prefix_ARGS
struct MkErrorS * MK_ERR
class-shortcut for struct MkErrorS *, all shortcut using the XX_YYY syntax (only for public API) …
static bool MkErrCheck(MK_MNGN mng)
check MkErrorS -> MkObjectS::signature …
static MK_ERR MkErr(MK_MNG mng)
cast a unknown-object into an MkErrorS pointer or NULL if not possible
#define MkErrorGetCode_0E()
#define MkErrorStackFormat(...)
#define MkErrorReset_1M(err)
#define MkErrorSetC(...)
#define MkErrorSetV(...)
#define MkErrorSetE(...)
#define MkErrorFORMAT_1M(m)
#define MkErrorDup(...)
@ MK_ERROR
(persistent) raise an error-event, the calling-fucntion is interrupted.
const MK_PTRB * MK_MNGN
const - a managed object pointer, datatype will be checked at runtime
const MK_STRB * MK_STRN
constant string pointer data-type
MK_PTRB * MK_MNG
managed object pointer, datatype will be checked at runtime
MK_PTRB const * MK_EXP
exception object pointer
signed int MK_I32
4 byte integer data-type
#define MK_RT_CALL
The data-type to store and handle the error-condition …