X-Git-Url: https://git.distorted.org.uk/~mdw/pyke/blobdiff_plain/87aa2e3c42d96790852436f8f108fc70bd87de9c..aa34cb72a4f0a502cb6bb55235c8f3186f46a28c:/util.c diff --git a/util.c b/util.c index 5abd7c9..03a39af 100644 --- a/util.c +++ b/util.c @@ -32,7 +32,7 @@ /*----- External values ---------------------------------------------------*/ -static PyObject *modname = 0; +PyObject *modname = 0; PyObject *home_module = 0; /*----- Conversions -------------------------------------------------------*/ @@ -68,8 +68,8 @@ 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 } @@ -185,7 +185,7 @@ int convk64(PyObject *o, void *pp) rc = 1; end: - if (i) Py_DECREF(i); + Py_XDECREF(i); return (rc); } @@ -256,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); } @@ -280,38 +280,45 @@ PyTypeObject *inittype(PyTypeObject *tyskel, PyTypeObject *meta) 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 --------------------------------------------------------*/ @@ -324,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) { @@ -344,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); @@ -503,7 +511,7 @@ static PyTypeObject itemiter_pytype_skel = { Py_TPFLAGS_BASETYPE, /* @tp_doc@ */ -"Iterates over the keys of a mapping.", + "Iterates over the items of a mapping.", 0, /* @tp_traverse@ */ 0, /* @tp_clear@ */ @@ -561,7 +569,7 @@ static PyTypeObject valiter_pytype_skel = { Py_TPFLAGS_BASETYPE, /* @tp_doc@ */ -"Iterates over the values of a mapping.", + "Iterates over the values of a mapping.", 0, /* @tp_traverse@ */ 0, /* @tp_clear@ */ @@ -736,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(); @@ -753,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(); @@ -766,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) @@ -803,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"); @@ -837,14 +852,13 @@ static PyObject *meth__set_home_module(PyObject *me, PyObject *arg) static const PyMethodDef methods[] = { #define METHNAME(func) meth_##func - METH (_set_home_module, "_set_home_module(MOD)") + 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);