/* Demo API */ PyObject * func(const PyObject *a, const PyObject *b) { /* thread local context */ PyObject *context = NULL; PyObject *workcontext = NULL; PyObject *x, *y, *result; /* borrowed reference */ context = PyDec_GetCurrentContext(); if (context == NULL) return NULL; workcontext = PyDec_WorkContext(context); if (workcontext == NULL) return NULL; /* PyDec_SetPrec(context, prec); ??? PyDecContext_SetPrec(context, prec); ??? */ CTX(workcontext)->prec = 100; CTX(workcontext)->emax = 999; CTX(workcontext)->emin = -999; x = PyDec_Add(a, b, workcontext); if (x == NULL) goto error; y = PyDec_Sqrt(x, workcontext); Py_CLEAR(x); if (y == NULL) goto error; /* Continue with decref and repeated error checking. */ /* PyDecContext_GetStatus(workcontext) & MPD_Underflow ??? */ if (CTX(workcontext)->status & MPD_Underflow) /* ... */ else /* ... */ result = /* ... */ /* finally raise if respective traps are set */ if (PyDec_AddStatus(context, workcontext)) { Py_DECREF(workcontext); Py_DECREF(result); return NULL; } Py_DECREF(workcontext); return result; error: /* ... */ } PyObject * func_mpd(const PyObject *a, const PyObject *b) { /* thread local context */ PyObject *context = NULL; PyObject *result = NULL; mpd_context_t ctx; uint32_t status = 0; mpd_t *x; /* borrowed reference */ context = PyDec_GetCurrentContext(); if (context == NULL) return NULL; result = PyDec_New(); if (result == NULL) goto error; x = pympd_new(); if (x == NULL) goto error; pympd_workcontext(&ctx, CTX(context)); ctx.prec = 100; ctx.emax = 999; ctx.emin = -999; pympd_add(x, MPD(a), MPD(b), &ctx, &status); pympd_sqrt(x, x, &ctx, &status); /* Continue without error checking, NaNs are propagated, even Malloc_error is handled gracefully (result is also NaN). */ if (status & MPD_Underflow) /* ... */ else /* ... */ pympd_func(MPD(result), ...) /* finally raise if respective traps are set */ if (dec_addstatus(context, status)) { Py_DECREF(result); return NULL; } return result; error: /* ... */ }