X-Git-Url: https://git.distorted.org.uk/~mdw/pyke/blobdiff_plain/1b6734b68d788a57b9491360a3ca8dfebeae60bc..aa34cb72a4f0a502cb6bb55235c8f3186f46a28c:/util.c diff --git a/util.c b/util.c index 4e82ed6..03a39af 100644 --- a/util.c +++ b/util.c @@ -28,9 +28,12 @@ #include "catacomb-python.h" +/* #undef HAVE_LONG_LONG */ + /*----- External values ---------------------------------------------------*/ -static PyObject *modname = 0; +PyObject *modname = 0; +PyObject *home_module = 0; /*----- Conversions -------------------------------------------------------*/ @@ -42,12 +45,17 @@ PyObject *getulong(unsigned long w) return (PyLong_FromUnsignedLong(w)); } +#ifndef HAVE_LONG_LONG static PyObject *i32 = 0; static int init_i32(void) { if (!i32 && (i32 = PyInt_FromLong(32)) == 0) return (-1); return (0); } +#endif PyObject *getk64(kludge64 u) { +#ifdef HAVE_LONG_LONG + return (PyLong_FromUnsignedLongLong(GET64(unsigned PY_LONG_LONG, u))); +#else PyObject *i = 0, *j = 0, *t; PyObject *rc = 0; @@ -60,9 +68,10 @@ PyObject *getk64(kludge64 u) Py_DECREF(i); i = t; if ((rc = PyNumber_Int(i)) == 0) goto end; end: - if (i) Py_DECREF(i); - if (j) Py_DECREF(j); + Py_XDECREF(i); + Py_XDECREF(j); return (rc); +#endif } PyObject *getbool(int b) @@ -83,6 +92,7 @@ int convulong(PyObject *o, void *pp) unsigned long *p = pp; PyObject *t; + if (!o) VALERR("can't delete"); if (PyInt_Check(o)) { i = PyInt_AS_LONG(o); if (i < 0) VALERR("must be nonnegative"); @@ -98,15 +108,32 @@ end: return (0); } +#ifdef HAVE_UINT64 +# define CONVu64(n) do { \ + kludge64 k; \ + uint64 t; \ + if (!convk64(o, &k)) goto end; \ + t = GET64(uint64, k); \ + if (t > MASK##n) VALERR("out of range"); \ + *p = t; \ + } while (0) +#else +# define CONVu64(n) assert(!"shouldn't be possible") +#endif + #define CONVU_(n) \ int convu##n(PyObject *o, void *pp) \ { \ unsigned long u; \ uint##n *p = pp; \ \ - if (!convulong(o, &u)) goto end; \ - if (u > MASK##n) VALERR("out of range"); \ - *p = u; \ + if (MASK##n > ULONG_MAX) \ + CONVu64(n); \ + else { \ + if (!convulong(o, &u)) goto end; \ + if (u > MASK##n) VALERR("out of range"); \ + *p = u; \ + } \ return (1); \ end: \ return (0); \ @@ -128,10 +155,22 @@ end: int convk64(PyObject *o, void *pp) { - PyObject *i = 0, *t; + PyObject *i = 0; int rc = 0; +#if HAVE_LONG_LONG + unsigned PY_LONG_LONG t; +#else + PyObject *t; uint32 lo, hi; - +#endif + + if (!o) VALERR("can't delete"); +#if HAVE_LONG_LONG + if ((i = PyNumber_Long(o)) == 0) goto end; + t = PyLong_AsUnsignedLongLong(i); + if (t == (unsigned PY_LONG_LONG)-1 && PyErr_Occurred()) goto end; + ASSIGN64(*(kludge64 *)pp, t); +#else if (init_i32()) goto end; if ((i = PyNumber_Int(o)) == 0) goto end; lo = PyInt_AsUnsignedLongMask(i); @@ -142,9 +181,11 @@ int convk64(PyObject *o, void *pp) Py_DECREF(i); i = t; if (PyObject_IsTrue(i)) VALERR("out of range"); SET64(*(kludge64 *)pp, hi, lo); +#endif rc = 1; + end: - if (i) Py_DECREF(i); + Py_XDECREF(i); return (rc); } @@ -176,8 +217,11 @@ end: int convbool(PyObject *o, void *pp) { + if (!o) VALERR("can't delete"); *(int *)pp = PyObject_IsTrue(o); return (1); +end: + return (0); } /*----- Type messing ------------------------------------------------------*/ @@ -212,7 +256,7 @@ void *newtype(PyTypeObject *metaty, ty->ht_name = PyString_FromString(ty->ht_type.tp_name); if (ty->ht_name) ty->ht_type.tp_name = PyString_AS_STRING(ty->ht_name); - DISCARD(PyObject_INIT(&ty->ht_type, metaty)); + (void)PyObject_INIT(&ty->ht_type, metaty); Py_INCREF(metaty); return (ty); } @@ -223,9 +267,9 @@ void typeready(PyTypeObject *ty) PyDict_SetItemString(ty->tp_dict, "__module__", modname); } -PyTypeObject *inittype(PyTypeObject *tyskel) +PyTypeObject *inittype(PyTypeObject *tyskel, PyTypeObject *meta) { - PyTypeObject *ty = newtype(&PyType_Type, tyskel, 0); + PyTypeObject *ty = newtype(meta, tyskel, 0); ty->tp_flags |= Py_TPFLAGS_HEAPTYPE; typeready(ty); return (ty); @@ -236,38 +280,45 @@ PyTypeObject *inittype(PyTypeObject *tyskel) void setconstants(PyObject *mod, const struct nameval *c) { PyObject *x; + unsigned long u; while (c->name) { - if (c->value > LONG_MAX) - x = PyLong_FromUnsignedLong(c->value); - else - x = PyInt_FromLong(c->value); - PyModule_AddObject(mod, (/*unconst*/ char *)c->name, x); - c++; + u = c->value; + if (u <= LONG_MAX) x = PyInt_FromLong(u); + else if (c->f&CF_SIGNED) x = PyInt_FromLong(-1 - (long)(ULONG_MAX - u)); + else x = PyLong_FromUnsignedLong(u); + PyModule_AddObject(mod, (/*unconst*/ char *)c->name, x); c++; } } /*----- Building method tables --------------------------------------------*/ -DA_DECL(method_v, PyMethodDef); -static method_v global_pymethods = DA_INIT; +static PyMethodDef *global_methods; +static size_t nmethods, methodsz; + void addmethods(const PyMethodDef *m) { - size_t n; + size_t n, want, newsz; for (n = 0; m[n].ml_name; n++); - DA_ENSURE(&global_pymethods, n); - memcpy(DA(&global_pymethods) + DA_LEN(&global_pymethods), - m, n * sizeof(*m)); - DA_EXTEND(&global_pymethods, n); + want = nmethods + n + 1; + if (want > methodsz) { + newsz = methodsz ? 2*methodsz : 16; + while (want > newsz) newsz *= 2; + if (!global_methods) + global_methods = PyObject_Malloc(newsz*sizeof(PyMethodDef)); + else + global_methods = PyObject_Realloc(global_methods, + newsz*sizeof(PyMethodDef)); + assert(global_methods); + methodsz = newsz; + } + memcpy(global_methods + nmethods, m, n*sizeof(PyMethodDef)); + nmethods += n; + global_methods[nmethods].ml_name = 0; } -PyMethodDef *donemethods(void) -{ - static const PyMethodDef mzero = { 0 }; - DA_PUSH(&global_pymethods, mzero); - return (DA(&global_pymethods)); -} +PyMethodDef *donemethods(void) { return (global_methods); } /*----- Exceptions --------------------------------------------------------*/ @@ -280,13 +331,7 @@ PyObject *mkexc(PyObject *mod, PyObject *base, PyObject *func = 0; PyObject *meth = 0; - if ((nameobj = PyString_FromFormat("%s.%s", - PyModule_GetName(mod), - name)) == 0 || - (dict = PyDict_New()) == 0 || - (exc = PyErr_NewException(PyString_AS_STRING(nameobj), - base, dict)) == 0) - goto fail; + if ((dict = PyDict_New()) == 0) goto fail; if (mm) { while (mm->ml_name) { @@ -300,6 +345,13 @@ PyObject *mkexc(PyObject *mod, PyObject *base, } } + if ((nameobj = PyString_FromFormat("%s.%s", + PyModule_GetName(mod), + name)) == 0 || + (exc = PyErr_NewException(PyString_AS_STRING(nameobj), + base, dict)) == 0) + goto fail; + done: Py_XDECREF(nameobj); Py_XDECREF(dict); @@ -313,6 +365,101 @@ fail: goto done; } +void report_lost_exception_v(struct excinfo *exc, + const char *why, va_list ap) +{ + PyObject *hookfn = 0; + PyObject *whyobj = 0; + PyObject *obj = 0; + + /* Make sure we start out without a pending exception, or this will get + * really confusing. + */ + assert(!PyErr_Occurred()); + + /* Format the explanation. */ + if (why) whyobj = PyString_FromFormatV(why, ap); + else { whyobj = Py_None; Py_INCREF(whyobj); } + + /* Find our home module's `lostexchook' function. This won't work if + * there's no module, or the function isn't defined, or it's `None'. + */ + if (!home_module) goto sys; + hookfn = PyObject_GetAttrString(home_module, "lostexchook"); + if (hookfn == Py_None) goto sys; + else if (hookfn) ; + else if (!PyErr_ExceptionMatches(PyExc_AttributeError)) goto ouch; + else { PyErr_Clear(); goto sys; } + + /* Call the hook function. */ + obj = PyObject_CallFunction(hookfn, "(OOOO)", + whyobj, exc->ty, exc->val, exc->tb); + if (!obj) goto ouch; + goto end; + + /* Something went wrong reporting the problem. */ +ouch: + PySys_WriteStderr("\n!!! FAILURE REPORTING LOST EXCEPTION\n"); + PyErr_Print(); + /* drop through... */ + + /* There was no hook, so try to do something sensible using + * `sys.excepthook'. + */ +sys: + PySys_WriteStderr("\n!!! LOST EXCEPTION: %s\n", + PyString_AS_STRING(whyobj)); + RESTORE_EXCINFO(exc); + PyErr_Print(); + /* drop through... */ + + /* Clean up afterwards. */ +end: + Py_XDECREF(hookfn); + Py_XDECREF(whyobj); + Py_XDECREF(obj); +} + +void report_lost_exception(struct excinfo *exc, const char *why, ...) +{ + va_list ap; + + va_start(ap, why); + report_lost_exception_v(exc, why, ap); + va_end(ap); +} + +void stash_exception(struct excinfo *exc, const char *why, ...) +{ + va_list ap; + struct excinfo stash; + + if (!exc->ty) + STASH_EXCINFO(exc); + else { + va_start(ap, why); + STASH_EXCINFO(&stash); + report_lost_exception_v(&stash, why, ap); + va_end(ap); + } +} + +void restore_exception(struct excinfo *exc, const char *why, ...) +{ + va_list ap; + struct excinfo stash; + + if (!PyErr_Occurred()) + RESTORE_EXCINFO(exc); + else { + va_start(ap, why); + STASH_EXCINFO(&stash); + report_lost_exception_v(exc, why, ap); + RESTORE_EXCINFO(&stash); + va_end(ap); + } +} + /*----- Generic dictionary methods ----------------------------------------*/ static PyTypeObject *itemiter_pytype, *valiter_pytype; @@ -364,7 +511,7 @@ static PyTypeObject itemiter_pytype_skel = { Py_TPFLAGS_BASETYPE, /* @tp_doc@ */ -"Iterates over the items of a mapping.", + "Iterates over the items of a mapping.", 0, /* @tp_traverse@ */ 0, /* @tp_clear@ */ @@ -422,7 +569,7 @@ static PyTypeObject valiter_pytype_skel = { Py_TPFLAGS_BASETYPE, /* @tp_doc@ */ -"Iterates over the items of a mapping.", + "Iterates over the values of a mapping.", 0, /* @tp_traverse@ */ 0, /* @tp_clear@ */ @@ -461,8 +608,7 @@ PySequenceMethods gmap_pysequence = { Py_ssize_t gmap_pysize(PyObject *me) { PyObject *i = 0, *x = 0; - int rc = -1; - int n = 0; + Py_ssize_t rc = -1, n = 0; if ((i = PyObject_GetIter(me)) == 0) goto done; while ((x = PyIter_Next(i)) != 0) { n++; Py_DECREF(x); x = 0; } @@ -598,13 +744,15 @@ end: return (rc); } -static char *def_kwlist[] = { "key", "default", 0 }; +static const char *const def_kwlist[] = { "key", "default", 0 }; PyObject *gmapmeth_get(PyObject *me, PyObject *arg, PyObject *kw) { PyObject *k, *def = Py_None, *v; - if (!PyArg_ParseTupleAndKeywords(arg, kw, "OO:get", def_kwlist, &k, &def)) + if (!PyArg_ParseTupleAndKeywords(arg, kw, "O|O:get", + (/*unconst*/ char **)def_kwlist, + &k, &def)) return (0); if ((v = PyObject_GetItem(me, k)) != 0) return (v); PyErr_Clear(); @@ -615,8 +763,9 @@ PyObject *gmapmeth_setdefault(PyObject *me, PyObject *arg, PyObject *kw) { PyObject *k, *def = Py_None, *v; - if (!PyArg_ParseTupleAndKeywords(arg, kw, "OO:setdefault", - def_kwlist, &k, &def)) + if (!PyArg_ParseTupleAndKeywords(arg, kw, "O|O:setdefault", + (/*unconst*/ char **)def_kwlist, + &k, &def)) return (0); if ((v = PyObject_GetItem(me, k)) != 0) return (v); PyErr_Clear(); @@ -628,14 +777,18 @@ PyObject *gmapmeth_pop(PyObject *me, PyObject *arg, PyObject *kw) { PyObject *k, *def = 0, *v; - if (!PyArg_ParseTupleAndKeywords(arg, kw, "OO:pop", def_kwlist, &k, &def)) + if (!PyArg_ParseTupleAndKeywords(arg, kw, "O|O:pop", + (/*unconst*/ char **)def_kwlist, + &k, &def)) return (0); if ((v = PyObject_GetItem(me, k)) != 0) { PyObject_DelItem(me, k); return (v); - } - PyErr_Clear(); - RETURN_OBJ(def); + } else if (def) { + PyErr_Clear(); + RETURN_OBJ(def); + } else + return (0); } PyObject *gmapmeth_update(PyObject *me, PyObject *arg) @@ -665,7 +818,7 @@ PyObject *gmapmeth_popitem(PyObject *me, PyObject *arg) PyObject *i = 0, *k = 0, *v = 0, *rc = 0; if (!PyArg_ParseTuple(arg, ":popitem") || - (i = PyObject_GetIter(me))) + (i = PyObject_GetIter(me)) == 0) goto end; if ((k = PyIter_Next(i)) == 0) { if (!PyErr_Occurred()) VALERR("popitem(): mapping is empty"); @@ -687,11 +840,28 @@ PyMethodDef gmap_pymethods[] = { /*----- Initialization ----------------------------------------------------*/ +static PyObject *meth__set_home_module(PyObject *me, PyObject *arg) +{ + PyObject *mod; + + if (!PyArg_ParseTuple(arg, "O!:_set_home_module", &PyModule_Type, &mod)) + return (0); + Py_XDECREF(home_module); home_module = mod; Py_INCREF(home_module); + RETURN_NONE; +} + +static const PyMethodDef methods[] = { +#define METHNAME(func) meth_##func + METH (_set_home_module, "_set_home_module(MOD)") +#undef METHNAME + { 0 } +}; + void util_pyinit(void) { - modname = PyString_FromString("catacomb"); INITTYPE(itemiter, root); INITTYPE(valiter, root); + addmethods(methods); } void util_pyinsert(PyObject *mod)