[Python-Dev] Adding insint() function (original) (raw)

Vladimir Marangozov Vladimir.Marangozov@inrialpes.fr
Sat, 19 Aug 2000 05:27:20 +0200 (CEST)


Fred L. Drake, Jr. wrote:

Vladimir Marangozov writes: > So name it PyModuleAddConstant(module, name, constant), > which fails with "can't add constant to module" err msg. Even better! I expect there should be at least a couple of these; one for ints, one for strings.

What about something like this (untested):


int PyModule_AddObject(PyObject *m, char *name, PyObject *o) { if (!PyModule_Check(m) || o == NULL) return -1; if (PyDict_SetItemString(((PyModuleObject *)m)->md_dict, name, o)) return -1; Py_DECREF(o); return 0; }

#define PyModule_AddConstant(m, x)
PyModule_AddObject(m, #x, PyInt_FromLong(x))

#define PyModule_AddString(m, x) \
PyModule_AddObject(m, x, PyString_FromString(x))


void initmymodule(void) { int CONSTANT = 123456; char *STR__doc__ = "Vlad";

    PyObject *m = Py_InitModule4("mymodule"...);


    if (PyModule_AddString(m, STR__doc__) ||
        PyModule_AddConstant(m, CONSTANT) ||
        ... 
    {
        Py_FatalError("can't init mymodule");
    }

}

-- Vladimir MARANGOZOV | Vladimir.Marangozov@inrialpes.fr http://sirac.inrialpes.fr/~marangoz | tel:(+33-4)76615277 fax:76615252