Skip to content

Commit 9e28515

Browse files
committed
Andrew Kuchling <akuchlin@mems-exchange.org>:
Add three new convenience functions to the PyModule_*() family: PyModule_AddObject(), PyModule_AddIntConstant(), PyModule_AddStringConstant(). This closes SourceForge patch #101233.
1 parent f84fb66 commit 9e28515

2 files changed

Lines changed: 31 additions & 0 deletions

File tree

Include/modsupport.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ extern DL_IMPORT(PyObject *) Py_BuildValue(char *, ...);
1818
extern DL_IMPORT(int) PyArg_VaParse(PyObject *, char *, va_list);
1919
extern DL_IMPORT(PyObject *) Py_VaBuildValue(char *, va_list);
2020

21+
extern DL_IMPORT(int) PyModule_AddObject(PyObject *, char *, PyObject *);
22+
extern DL_IMPORT(int) PyModule_AddIntConstant(PyObject *, char *, long);
23+
extern DL_IMPORT(int) PyModule_AddStringConstant(PyObject *, char *, char *);
24+
2125
#define PYTHON_API_VERSION 1009
2226
#define PYTHON_API_STRING "1009"
2327
/* The API version is maintained (independently from the Python version)

Python/modsupport.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,3 +459,30 @@ PyEval_CallMethod(PyObject *obj, char *methodname, char *format, ...)
459459

460460
return res;
461461
}
462+
463+
int
464+
PyModule_AddObject(PyObject *m, char *name, PyObject *o)
465+
{
466+
PyObject *dict;
467+
if (!PyModule_Check(m) || o == NULL)
468+
return -1;
469+
dict = PyModule_GetDict(m);
470+
if (dict == NULL)
471+
return -1;
472+
if (PyDict_SetItemString(dict, name, o))
473+
return -1;
474+
Py_DECREF(o);
475+
return 0;
476+
}
477+
478+
int
479+
PyModule_AddIntConstant(PyObject *m, char *name, long value)
480+
{
481+
return PyModule_AddObject(m, name, PyInt_FromLong(value));
482+
}
483+
484+
int
485+
PyModule_AddStringConstant(PyObject *m, char *name, char *value)
486+
{
487+
return PyModule_AddObject(m, name, PyString_FromString(value));
488+
}

0 commit comments

Comments
 (0)