Adding insint() function
Four modules define insint() functions to insert an integer into a dictionary in order to initialize constants in their module dictionaries: kronos Modules>grep -l insint *.c pcremodule.c shamodule.c socketmodule.c zlibmodule.c kronos Modules> (Hm... I was involved with 3 of them...) Other modules don't use a helper function, but just do PyDict_SetItemString(d, "foo", PyInt_FromLong(...)) directly. This duplication bugs me. Shall I submit a patch to add an API convenience function to do this, and change the modules to use it? Suggested prototype and name: PyDict_InsertInteger( dict *, string, long) --amk
"AK" == Andrew Kuchling <akuchlin@mems-exchange.org> writes:
AK> Four modules define insint() functions to insert an integer AK> into a dictionary in order to initialize constants in their AK> module dictionaries: | kronos Modules>grep -l insint *.c | pcremodule.c | shamodule.c | socketmodule.c | zlibmodule.c | kronos Modules> AK> (Hm... I was involved with 3 of them...) Other modules don't AK> use a helper function, but just do PyDict_SetItemString(d, AK> "foo", PyInt_FromLong(...)) directly. AK> This duplication bugs me. Shall I submit a patch to add an AK> API convenience function to do this, and change the modules to AK> use it? Suggested prototype and name: PyDict_InsertInteger( AK> dict *, string, long) +0, but it should probably be called PyDict_SetItemSomething(). It seems more related to the other PyDict_SetItem*() functions, even though in those cases the `*' refers to the type of the key, not the value. -Barry
On Fri, Aug 18, 2000 at 11:27:41AM -0400, Barry A. Warsaw wrote:
+0, but it should probably be called PyDict_SetItemSomething(). It seems more related to the other PyDict_SetItem*() functions, even though in those cases the `*' refers to the type of the key, not the value.
PyDict_SetItemInteger seems misleading; PyDict_SetItemStringToInteger is simply long. PyDict_SetIntegerItem, maybe? :) Anyway, I'll start working on a patch and change the name later once there's a good suggestion. --amk
Barry A. Warsaw writes:
+0, but it should probably be called PyDict_SetItemSomething(). It seems more related to the other PyDict_SetItem*() functions, even though in those cases the `*' refers to the type of the key, not the value.
Hmm... How about PyDict_SetItemStringInt() ? It's still long, but I don't think that's actually a problem. -Fred -- Fred L. Drake, Jr. <fdrake at beopen.com> BeOpen PythonLabs Team Member
[Andrew Kuchling]
Four modules define insint() functions to insert an integer into a
Five or macro
dictionary in order to initialize constants in their module dictionaries:
kronos Modules>grep -l insint *.c pcremodule.c shamodule.c socketmodule.c zlibmodule.c kronos Modules>
It's actually a macro in shamodule. Missing is _winreg.c (in the PC directory). The perils of duplication manifest in subtle differences among these guys (like _winreg's inserts a long while the others insert an int -- and _winreg is certainly more correct here because a Python int *is* a C long; and they differ in treatment of errors, and it's not at all clear that's intentional).
... This duplication bugs me. Shall I submit a patch to add an API convenience function to do this, and change the modules to use it? Suggested prototype and name: PyDict_InsertInteger( dict *, string, long)
+1, provided the treatment of errors is clearly documented.
On Fri, Aug 18, 2000 at 01:52:20PM -0400, Tim Peters wrote:
+1, provided the treatment of errors is clearly documented.
The treatment of errors in module init functions seems to be simply charge ahead and do the inserts, and then do 'if (PyErr_Occurred()) Py_FatalError())'. The new function will probably return NULL if there's an error, but I doubt anyone will check it; it's too ungainly to write if ( (PyDict_SetItemStringInt(d, "foo", FOO)) == NULL || (PyDict_SetItemStringInt(d, "bar", BAR)) == NULL || ... repeat for 187 more constants ... --amk
Andrew Kuchling wrote:
On Fri, Aug 18, 2000 at 01:52:20PM -0400, Tim Peters wrote:
+1, provided the treatment of errors is clearly documented.
The treatment of errors in module init functions seems to be simply charge ahead and do the inserts, and then do 'if (PyErr_Occurred()) Py_FatalError())'. The new function will probably return NULL if there's an error, but I doubt anyone will check it; it's too ungainly to write if ( (PyDict_SetItemStringInt(d, "foo", FOO)) == NULL || (PyDict_SetItemStringInt(d, "bar", BAR)) == NULL || ... repeat for 187 more constants ...
:-) So name it PyModule_AddConstant(module, name, constant), which fails with "can't add constant to module" err msg. -- Vladimir MARANGOZOV | Vladimir.Marangozov@inrialpes.fr http://sirac.inrialpes.fr/~marangoz | tel:(+33-4)76615277 fax:76615252
Vladimir Marangozov writes:
So name it PyModule_AddConstant(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. -Fred -- Fred L. Drake, Jr. <fdrake at beopen.com> BeOpen PythonLabs Team Member
Fred L. Drake, Jr. wrote:
Vladimir Marangozov writes:
So name it PyModule_AddConstant(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
Andrew Kuchling <akuchlin@mems-exchange.org> wrote,
This duplication bugs me. Shall I submit a patch to add an API convenience function to do this, and change the modules to use it? Suggested prototype and name: PyDict_InsertInteger( dict *, string, long)
+0 but... ...why not: PyDict_SetValueString(PyObject* dict, char* key, char* fmt, ...) and PyDict_SetValue(PyObject* dict, PyObject* key, char* fmt, ...) where the fmt is Py_BuildValue() format string and the ... is, of course, the argument list. -- Donald Beaudry Ab Initio Software Corp. 201 Spring Street donb@init.com Lexington, MA 02421 ...Will hack for sushi...
participants (6)
-
Andrew Kuchling -
bwarsaw@beopen.com -
Donald Beaudry -
Fred L. Drake, Jr. -
Tim Peters -
Vladimir.Marangozov@inrialpes.fr