X-Git-Url: https://git.distorted.org.uk/~mdw/pyke/blobdiff_plain/d920a806df51ec41ffef2f8fb66a54235c2506eb..a144ac2dc016d261f8e8b1d9bf1f95419a9bb2e8:/util.c diff --git a/util.c b/util.c index 723c819..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); } @@ -293,25 +293,32 @@ void setconstants(PyObject *mod, const struct nameval *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 --------------------------------------------------------*/ @@ -504,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@ */ @@ -562,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@ */ @@ -845,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);