blob: 6c938ddd797e549260f2318d582f1a1421439b7d [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* Module support implementation */
3
Guido van Rossum79f25d91997-04-29 20:08:16 +00004#include "Python.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00006#define FLAG_SIZE_T 1
Guido van Rossum1d5735e1994-08-30 08:27:36 +00007typedef double va_double;
Guido van Rossum1d5735e1994-08-30 08:27:36 +00008
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00009static PyObject *va_build_value(const char *, va_list, int);
10
Guido van Rossum2e58ff31997-11-19 18:53:33 +000011/* Package context -- the full module name for package imports */
12char *_Py_PackageContext = NULL;
13
Guido van Rossumbf80e541993-02-08 15:49:17 +000014/* Helper for mkvalue() to scan the length of a format */
Guido van Rossumfc61adb1992-04-13 15:53:41 +000015
Fred Drakeceead6d2003-01-30 15:08:25 +000016static int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +000017countformat(const char *format, int endchar)
Guido van Rossumfc61adb1992-04-13 15:53:41 +000018{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000019 int count = 0;
20 int level = 0;
21 while (level > 0 || *format != endchar) {
22 switch (*format) {
23 case '\0':
24 /* Premature end */
25 PyErr_SetString(PyExc_SystemError,
26 "unmatched paren in format");
27 return -1;
28 case '(':
29 case '[':
30 case '{':
31 if (level == 0)
32 count++;
33 level++;
34 break;
35 case ')':
36 case ']':
37 case '}':
38 level--;
39 break;
40 case '#':
41 case '&':
42 case ',':
43 case ':':
44 case ' ':
45 case '\t':
46 break;
47 default:
48 if (level == 0)
49 count++;
50 }
51 format++;
52 }
53 return count;
Guido van Rossumfc61adb1992-04-13 15:53:41 +000054}
55
56
Guido van Rossumfc61adb1992-04-13 15:53:41 +000057/* Generic function to create a value -- the inverse of getargs() */
58/* After an original idea and first implementation by Steven Miale */
59
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000060static PyObject *do_mktuple(const char**, va_list *, int, int, int);
61static PyObject *do_mklist(const char**, va_list *, int, int, int);
62static PyObject *do_mkdict(const char**, va_list *, int, int, int);
63static PyObject *do_mkvalue(const char**, va_list *, int);
Guido van Rossumfc61adb1992-04-13 15:53:41 +000064
Guido van Rossum1ae940a1995-01-02 19:04:15 +000065
Guido van Rossum79f25d91997-04-29 20:08:16 +000066static PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000067do_mkdict(const char **p_format, va_list *p_va, int endchar, int n, int flags)
Guido van Rossum1ae940a1995-01-02 19:04:15 +000068{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000069 PyObject *d;
70 int i;
71 int itemfailed = 0;
72 if (n < 0)
73 return NULL;
74 if ((d = PyDict_New()) == NULL)
75 return NULL;
76 /* Note that we can't bail immediately on error as this will leak
77 refcounts on any 'N' arguments. */
78 for (i = 0; i < n; i+= 2) {
79 PyObject *k, *v;
80 int err;
81 k = do_mkvalue(p_format, p_va, flags);
82 if (k == NULL) {
83 itemfailed = 1;
84 Py_INCREF(Py_None);
85 k = Py_None;
86 }
87 v = do_mkvalue(p_format, p_va, flags);
88 if (v == NULL) {
89 itemfailed = 1;
90 Py_INCREF(Py_None);
91 v = Py_None;
92 }
93 err = PyDict_SetItem(d, k, v);
94 Py_DECREF(k);
95 Py_DECREF(v);
96 if (err < 0 || itemfailed) {
97 Py_DECREF(d);
98 return NULL;
99 }
100 }
101 if (d != NULL && **p_format != endchar) {
102 Py_DECREF(d);
103 d = NULL;
104 PyErr_SetString(PyExc_SystemError,
105 "Unmatched paren in format");
106 }
107 else if (endchar)
108 ++*p_format;
109 return d;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000110}
111
Guido van Rossum79f25d91997-04-29 20:08:16 +0000112static PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000113do_mklist(const char **p_format, va_list *p_va, int endchar, int n, int flags)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000114{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000115 PyObject *v;
116 int i;
117 int itemfailed = 0;
118 if (n < 0)
119 return NULL;
120 v = PyList_New(n);
121 if (v == NULL)
122 return NULL;
123 /* Note that we can't bail immediately on error as this will leak
124 refcounts on any 'N' arguments. */
125 for (i = 0; i < n; i++) {
126 PyObject *w = do_mkvalue(p_format, p_va, flags);
127 if (w == NULL) {
128 itemfailed = 1;
129 Py_INCREF(Py_None);
130 w = Py_None;
131 }
132 PyList_SET_ITEM(v, i, w);
133 }
Neal Norwitz3e90fa52006-03-06 23:07:34 +0000134
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000135 if (itemfailed) {
136 /* do_mkvalue() should have already set an error */
137 Py_DECREF(v);
138 return NULL;
139 }
140 if (**p_format != endchar) {
141 Py_DECREF(v);
142 PyErr_SetString(PyExc_SystemError,
143 "Unmatched paren in format");
144 return NULL;
145 }
146 if (endchar)
147 ++*p_format;
148 return v;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000149}
150
Guido van Rossum79f25d91997-04-29 20:08:16 +0000151static PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000152do_mktuple(const char **p_format, va_list *p_va, int endchar, int n, int flags)
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000153{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000154 PyObject *v;
155 int i;
156 int itemfailed = 0;
157 if (n < 0)
158 return NULL;
159 if ((v = PyTuple_New(n)) == NULL)
160 return NULL;
161 /* Note that we can't bail immediately on error as this will leak
162 refcounts on any 'N' arguments. */
163 for (i = 0; i < n; i++) {
Victor Stinner2d854c82014-01-21 20:52:17 +0100164 PyObject *w;
165
166 if (itemfailed) {
167 PyObject *exception, *value, *tb;
168 PyErr_Fetch(&exception, &value, &tb);
169 w = do_mkvalue(p_format, p_va, flags);
170 PyErr_Restore(exception, value, tb);
171 }
172 else {
173 w = do_mkvalue(p_format, p_va, flags);
174 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000175 if (w == NULL) {
176 itemfailed = 1;
177 Py_INCREF(Py_None);
178 w = Py_None;
179 }
180 PyTuple_SET_ITEM(v, i, w);
181 }
182 if (itemfailed) {
183 /* do_mkvalue() should have already set an error */
184 Py_DECREF(v);
185 return NULL;
186 }
187 if (**p_format != endchar) {
188 Py_DECREF(v);
189 PyErr_SetString(PyExc_SystemError,
190 "Unmatched paren in format");
191 return NULL;
192 }
193 if (endchar)
194 ++*p_format;
195 return v;
Guido van Rossumfc61adb1992-04-13 15:53:41 +0000196}
197
Guido van Rossum79f25d91997-04-29 20:08:16 +0000198static PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000199do_mkvalue(const char **p_format, va_list *p_va, int flags)
Guido van Rossum899dcf31992-05-15 11:04:59 +0000200{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000201 for (;;) {
202 switch (*(*p_format)++) {
203 case '(':
204 return do_mktuple(p_format, p_va, ')',
205 countformat(*p_format, ')'), flags);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000206
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000207 case '[':
208 return do_mklist(p_format, p_va, ']',
209 countformat(*p_format, ']'), flags);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000210
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000211 case '{':
212 return do_mkdict(p_format, p_va, '}',
213 countformat(*p_format, '}'), flags);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000214
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000215 case 'b':
216 case 'B':
217 case 'h':
218 case 'i':
219 return PyLong_FromLong((long)va_arg(*p_va, int));
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000220
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000221 case 'H':
222 return PyLong_FromLong((long)va_arg(*p_va, unsigned int));
223
224 case 'I':
225 {
226 unsigned int n;
227 n = va_arg(*p_va, unsigned int);
228 return PyLong_FromUnsignedLong(n);
229 }
230
231 case 'n':
Martin v. Löwis18e16552006-02-15 17:27:45 +0000232#if SIZEOF_SIZE_T!=SIZEOF_LONG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000233 return PyLong_FromSsize_t(va_arg(*p_va, Py_ssize_t));
Martin v. Löwis18e16552006-02-15 17:27:45 +0000234#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000235 /* Fall through from 'n' to 'l' if Py_ssize_t is long */
236 case 'l':
237 return PyLong_FromLong(va_arg(*p_va, long));
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000238
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000239 case 'k':
240 {
241 unsigned long n;
242 n = va_arg(*p_va, unsigned long);
243 return PyLong_FromUnsignedLong(n);
244 }
Jack Jansendbd65032003-04-17 22:01:10 +0000245
Guido van Rossum3dbba6e1999-01-25 21:48:56 +0000246#ifdef HAVE_LONG_LONG
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000247 case 'L':
248 return PyLong_FromLongLong((PY_LONG_LONG)va_arg(*p_va, PY_LONG_LONG));
Jack Jansendbd65032003-04-17 22:01:10 +0000249
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000250 case 'K':
251 return PyLong_FromUnsignedLongLong((PY_LONG_LONG)va_arg(*p_va, unsigned PY_LONG_LONG));
Guido van Rossum1a8791e1998-08-04 22:46:29 +0000252#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000253 case 'u':
254 {
255 PyObject *v;
256 Py_UNICODE *u = va_arg(*p_va, Py_UNICODE *);
257 Py_ssize_t n;
258 if (**p_format == '#') {
259 ++*p_format;
260 if (flags & FLAG_SIZE_T)
261 n = va_arg(*p_va, Py_ssize_t);
262 else
263 n = va_arg(*p_va, int);
264 }
265 else
266 n = -1;
267 if (u == NULL) {
268 v = Py_None;
269 Py_INCREF(v);
270 }
271 else {
272 if (n < 0)
Victor Stinnerd9c563e2011-09-29 04:01:43 +0200273 n = Py_UNICODE_strlen(u);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000274 v = PyUnicode_FromUnicode(u, n);
275 }
276 return v;
277 }
278 case 'f':
279 case 'd':
280 return PyFloat_FromDouble(
281 (double)va_arg(*p_va, va_double));
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000282
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000283 case 'D':
284 return PyComplex_FromCComplex(
285 *((Py_complex *)va_arg(*p_va, Py_complex *)));
Fred Drakeaec79242001-03-12 21:03:26 +0000286
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000287 case 'c':
288 {
289 char p[1];
290 p[0] = (char)va_arg(*p_va, int);
291 return PyBytes_FromStringAndSize(p, 1);
292 }
293 case 'C':
294 {
295 int i = va_arg(*p_va, int);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000296 return PyUnicode_FromOrdinal(i);
297 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000298
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000299 case 's':
300 case 'z':
Victor Stinner7eeb5b52010-06-07 19:57:46 +0000301 case 'U': /* XXX deprecated alias */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000302 {
303 PyObject *v;
304 char *str = va_arg(*p_va, char *);
305 Py_ssize_t n;
306 if (**p_format == '#') {
307 ++*p_format;
308 if (flags & FLAG_SIZE_T)
309 n = va_arg(*p_va, Py_ssize_t);
310 else
311 n = va_arg(*p_va, int);
312 }
313 else
314 n = -1;
315 if (str == NULL) {
316 v = Py_None;
317 Py_INCREF(v);
318 }
319 else {
320 if (n < 0) {
321 size_t m = strlen(str);
322 if (m > PY_SSIZE_T_MAX) {
323 PyErr_SetString(PyExc_OverflowError,
324 "string too long for Python string");
325 return NULL;
326 }
327 n = (Py_ssize_t)m;
328 }
329 v = PyUnicode_FromStringAndSize(str, n);
330 }
331 return v;
332 }
Walter Dörwaldd2034312007-05-18 16:29:38 +0000333
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000334 case 'y':
335 {
336 PyObject *v;
337 char *str = va_arg(*p_va, char *);
338 Py_ssize_t n;
339 if (**p_format == '#') {
340 ++*p_format;
341 if (flags & FLAG_SIZE_T)
342 n = va_arg(*p_va, Py_ssize_t);
343 else
344 n = va_arg(*p_va, int);
345 }
346 else
347 n = -1;
348 if (str == NULL) {
349 v = Py_None;
350 Py_INCREF(v);
351 }
352 else {
353 if (n < 0) {
354 size_t m = strlen(str);
355 if (m > PY_SSIZE_T_MAX) {
356 PyErr_SetString(PyExc_OverflowError,
357 "string too long for Python bytes");
358 return NULL;
359 }
360 n = (Py_ssize_t)m;
361 }
362 v = PyBytes_FromStringAndSize(str, n);
363 }
364 return v;
365 }
Walter Dörwald612344f2007-05-04 19:28:21 +0000366
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000367 case 'N':
368 case 'S':
369 case 'O':
370 if (**p_format == '&') {
371 typedef PyObject *(*converter)(void *);
372 converter func = va_arg(*p_va, converter);
373 void *arg = va_arg(*p_va, void *);
374 ++*p_format;
375 return (*func)(arg);
376 }
377 else {
378 PyObject *v;
379 v = va_arg(*p_va, PyObject *);
380 if (v != NULL) {
381 if (*(*p_format - 1) != 'N')
382 Py_INCREF(v);
383 }
384 else if (!PyErr_Occurred())
385 /* If a NULL was passed
386 * because a call that should
387 * have constructed a value
388 * failed, that's OK, and we
389 * pass the error on; but if
390 * no error occurred it's not
391 * clear that the caller knew
392 * what she was doing. */
393 PyErr_SetString(PyExc_SystemError,
394 "NULL object passed to Py_BuildValue");
395 return v;
396 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000397
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000398 case ':':
399 case ',':
400 case ' ':
401 case '\t':
402 break;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000403
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000404 default:
405 PyErr_SetString(PyExc_SystemError,
406 "bad format char passed to Py_BuildValue");
407 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000408
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000409 }
410 }
Guido van Rossum3cfe6fa1992-04-13 10:48:55 +0000411}
412
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000413
Fred Drakeceead6d2003-01-30 15:08:25 +0000414PyObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000415Py_BuildValue(const char *format, ...)
Guido van Rossum3cfe6fa1992-04-13 10:48:55 +0000416{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000417 va_list va;
418 PyObject* retval;
419 va_start(va, format);
420 retval = va_build_value(format, va, 0);
421 va_end(va);
422 return retval;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000423}
424
425PyObject *
426_Py_BuildValue_SizeT(const char *format, ...)
427{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000428 va_list va;
429 PyObject* retval;
430 va_start(va, format);
431 retval = va_build_value(format, va, FLAG_SIZE_T);
432 va_end(va);
433 return retval;
Guido van Rossum3cfe6fa1992-04-13 10:48:55 +0000434}
Guido van Rossume5372401993-03-16 12:15:04 +0000435
Guido van Rossum79f25d91997-04-29 20:08:16 +0000436PyObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000437Py_VaBuildValue(const char *format, va_list va)
Guido van Rossume5372401993-03-16 12:15:04 +0000438{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000439 return va_build_value(format, va, 0);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000440}
441
442PyObject *
443_Py_VaBuildValue_SizeT(const char *format, va_list va)
444{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000445 return va_build_value(format, va, FLAG_SIZE_T);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000446}
447
448static PyObject *
449va_build_value(const char *format, va_list va, int flags)
450{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000451 const char *f = format;
452 int n = countformat(f, '\0');
453 va_list lva;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000454
Alexander Belopolskyf0f45142010-08-11 17:31:17 +0000455 Py_VA_COPY(lva, va);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000456
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000457 if (n < 0)
458 return NULL;
459 if (n == 0) {
460 Py_INCREF(Py_None);
461 return Py_None;
462 }
463 if (n == 1)
464 return do_mkvalue(&f, &lva, flags);
465 return do_mktuple(&f, &lva, '\0', n, flags);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000466}
467
468
Guido van Rossum79f25d91997-04-29 20:08:16 +0000469PyObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000470PyEval_CallFunction(PyObject *obj, const char *format, ...)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000471{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000472 va_list vargs;
473 PyObject *args;
474 PyObject *res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000475
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000476 va_start(vargs, format);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000477
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000478 args = Py_VaBuildValue(format, vargs);
479 va_end(vargs);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000480
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000481 if (args == NULL)
482 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000483
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000484 res = PyEval_CallObject(obj, args);
485 Py_DECREF(args);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000486
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000487 return res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000488}
489
490
Guido van Rossum79f25d91997-04-29 20:08:16 +0000491PyObject *
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000492PyEval_CallMethod(PyObject *obj, const char *methodname, const char *format, ...)
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000493{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000494 va_list vargs;
495 PyObject *meth;
496 PyObject *args;
497 PyObject *res;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000498
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000499 meth = PyObject_GetAttrString(obj, methodname);
500 if (meth == NULL)
501 return NULL;
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000502
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000503 va_start(vargs, format);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000504
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000505 args = Py_VaBuildValue(format, vargs);
506 va_end(vargs);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000507
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000508 if (args == NULL) {
509 Py_DECREF(meth);
510 return NULL;
511 }
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000512
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000513 res = PyEval_CallObject(meth, args);
514 Py_DECREF(meth);
515 Py_DECREF(args);
Guido van Rossum1ae940a1995-01-02 19:04:15 +0000516
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000517 return res;
Guido van Rossume5372401993-03-16 12:15:04 +0000518}
Fred Drake9e285152000-09-23 03:24:27 +0000519
520int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000521PyModule_AddObject(PyObject *m, const char *name, PyObject *o)
Fred Drake9e285152000-09-23 03:24:27 +0000522{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000523 PyObject *dict;
524 if (!PyModule_Check(m)) {
525 PyErr_SetString(PyExc_TypeError,
526 "PyModule_AddObject() needs module as first arg");
527 return -1;
528 }
529 if (!o) {
530 if (!PyErr_Occurred())
531 PyErr_SetString(PyExc_TypeError,
532 "PyModule_AddObject() needs non-NULL value");
533 return -1;
534 }
Jeremy Hyltonc44dbc42003-06-21 21:35:25 +0000535
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000536 dict = PyModule_GetDict(m);
537 if (dict == NULL) {
538 /* Internal error -- modules must have a dict! */
539 PyErr_Format(PyExc_SystemError, "module '%s' has no __dict__",
540 PyModule_GetName(m));
541 return -1;
542 }
543 if (PyDict_SetItemString(dict, name, o))
544 return -1;
545 Py_DECREF(o);
546 return 0;
Fred Drake9e285152000-09-23 03:24:27 +0000547}
548
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000549int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000550PyModule_AddIntConstant(PyObject *m, const char *name, long value)
Fred Drake9e285152000-09-23 03:24:27 +0000551{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000552 PyObject *o = PyLong_FromLong(value);
553 if (!o)
554 return -1;
555 if (PyModule_AddObject(m, name, o) == 0)
556 return 0;
557 Py_DECREF(o);
558 return -1;
Fred Drake9e285152000-09-23 03:24:27 +0000559}
560
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000561int
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000562PyModule_AddStringConstant(PyObject *m, const char *name, const char *value)
Fred Drake9e285152000-09-23 03:24:27 +0000563{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000564 PyObject *o = PyUnicode_FromString(value);
565 if (!o)
566 return -1;
567 if (PyModule_AddObject(m, name, o) == 0)
568 return 0;
569 Py_DECREF(o);
570 return -1;
Fred Drake9e285152000-09-23 03:24:27 +0000571}