Coverage Report

Created: 2022-07-08 09:39

/home/mdboom/Work/builds/cpython/Python/modsupport.c
Line
Count
Source (jump to first uncovered line)
1
2
/* Module support implementation */
3
4
#include "Python.h"
5
#include "pycore_abstract.h"   // _PyIndex_Check()
6
7
#define FLAG_SIZE_T 1
8
typedef double va_double;
9
10
static PyObject *va_build_value(const char *, va_list, int);
11
static PyObject **va_build_stack(PyObject **small_stack, Py_ssize_t small_stack_len, const char *, va_list, int, Py_ssize_t*);
12
13
/* Package context -- the full module name for package imports */
14
const char *_Py_PackageContext = NULL;
15
16
17
int
18
_Py_convert_optional_to_ssize_t(PyObject *obj, void *result)
19
{
20
    Py_ssize_t limit;
21
    if (obj == Py_None) {
  Branch (21:9): [True: 107, False: 11.8M]
22
        return 1;
23
    }
24
    else if (_PyIndex_Check(obj)) {
  Branch (24:14): [True: 11.8M, False: 23]
25
        limit = PyNumber_AsSsize_t(obj, PyExc_OverflowError);
26
        if (limit == -1 && 
PyErr_Occurred()2.04k
) {
  Branch (26:13): [True: 2.04k, False: 11.8M]
  Branch (26:28): [True: 5, False: 2.03k]
27
            return 0;
28
        }
29
    }
30
    else {
31
        PyErr_Format(PyExc_TypeError,
32
                     "argument should be integer or None, not '%.200s'",
33
                     Py_TYPE(obj)->tp_name);
34
        return 0;
35
    }
36
    *((Py_ssize_t *)result) = limit;
37
    return 1;
38
}
39
40
41
/* Helper for mkvalue() to scan the length of a format */
42
43
static Py_ssize_t
44
countformat(const char *format, char endchar)
45
{
46
    Py_ssize_t count = 0;
47
    int level = 0;
48
    while (level > 0 || 
*format != endchar29.6M
) {
  Branch (48:12): [True: 9.01M, False: 29.6M]
  Branch (48:25): [True: 20.3M, False: 9.31M]
49
        switch (*format) {
50
        case '\0':
  Branch (50:9): [True: 0, False: 29.3M]
51
            /* Premature end */
52
            PyErr_SetString(PyExc_SystemError,
53
                            "unmatched paren in format");
54
            return -1;
55
        case '(':
  Branch (55:9): [True: 1.57M, False: 27.8M]
56
        case '[':
  Branch (56:9): [True: 5, False: 29.3M]
57
        case '{':
  Branch (57:9): [True: 6.06k, False: 29.3M]
58
            if (level == 0) {
  Branch (58:17): [True: 1.55M, False: 19.8k]
59
                count++;
60
            }
61
            level++;
62
            break;
63
        case ')':
  Branch (63:9): [True: 1.57M, False: 27.8M]
64
        case ']':
  Branch (64:9): [True: 5, False: 29.3M]
65
        case '}':
  Branch (65:9): [True: 6.06k, False: 29.3M]
66
            level--;
67
            break;
68
        case '#':
  Branch (68:9): [True: 26.7k, False: 29.3M]
69
        case '&':
  Branch (69:9): [True: 5.36M, False: 24.0M]
70
        case ',':
  Branch (70:9): [True: 236, False: 29.3M]
71
        case ':':
  Branch (71:9): [True: 472, False: 29.3M]
72
        case ' ':
  Branch (72:9): [True: 0, False: 29.3M]
73
        case '\t':
  Branch (73:9): [True: 0, False: 29.3M]
74
            break;
75
        default:
  Branch (75:9): [True: 20.8M, False: 8.54M]
76
            if (level == 0) {
  Branch (76:17): [True: 16.1M, False: 4.73M]
77
                count++;
78
            }
79
        }
80
        format++;
81
    }
82
    return count;
83
}
84
85
86
/* Generic function to create a value -- the inverse of getargs() */
87
/* After an original idea and first implementation by Steven Miale */
88
89
static PyObject *do_mktuple(const char**, va_list *, char, Py_ssize_t, int);
90
static int do_mkstack(PyObject **, const char**, va_list *, char, Py_ssize_t, int);
91
static PyObject *do_mklist(const char**, va_list *, char, Py_ssize_t, int);
92
static PyObject *do_mkdict(const char**, va_list *, char, Py_ssize_t, int);
93
static PyObject *do_mkvalue(const char**, va_list *, int);
94
95
96
static void
97
do_ignore(const char **p_format, va_list *p_va, char endchar, Py_ssize_t n, int flags)
98
{
99
    PyObject *v;
100
    Py_ssize_t i;
101
    assert(PyErr_Occurred());
102
    v = PyTuple_New(n);
103
    for (i = 0; i < n; 
i++26
) {
  Branch (103:17): [True: 26, False: 24]
104
        PyObject *exception, *value, *tb, *w;
105
106
        PyErr_Fetch(&exception, &value, &tb);
107
        w = do_mkvalue(p_format, p_va, flags);
108
        PyErr_Restore(exception, value, tb);
109
        if (w != NULL) {
  Branch (109:13): [True: 26, False: 0]
110
            if (v != NULL) {
  Branch (110:17): [True: 26, False: 0]
111
                PyTuple_SET_ITEM(v, i, w);
112
            }
113
            else {
114
                Py_DECREF(w);
115
            }
116
        }
117
    }
118
    Py_XDECREF(v);
119
    if (**p_format != endchar) {
  Branch (119:9): [True: 0, False: 24]
120
        PyErr_SetString(PyExc_SystemError,
121
                        "Unmatched paren in format");
122
        return;
123
    }
124
    if (endchar) {
  Branch (124:9): [True: 12, False: 12]
125
        ++*p_format;
126
    }
127
}
128
129
static PyObject *
130
do_mkdict(const char **p_format, va_list *p_va, char endchar, Py_ssize_t n, int flags)
131
{
132
    PyObject *d;
133
    Py_ssize_t i;
134
    if (n < 0)
  Branch (134:9): [True: 0, False: 6.06k]
135
        return NULL;
136
    if (n % 2) {
  Branch (136:9): [True: 0, False: 6.06k]
137
        PyErr_SetString(PyExc_SystemError,
138
                        "Bad dict format");
139
        do_ignore(p_format, p_va, endchar, n, flags);
140
        return NULL;
141
    }
142
    /* Note that we can't bail immediately on error as this will leak
143
       refcounts on any 'N' arguments. */
144
    if ((d = PyDict_New()) == NULL) {
  Branch (144:9): [True: 0, False: 6.06k]
145
        do_ignore(p_format, p_va, endchar, n, flags);
146
        return NULL;
147
    }
148
    
for (i = 0; 6.06k
i < n;
i+= 213.3k
) {
  Branch (148:17): [True: 13.3k, False: 6.06k]
149
        PyObject *k, *v;
150
151
        k = do_mkvalue(p_format, p_va, flags);
152
        if (k == NULL) {
  Branch (152:13): [True: 2, False: 13.3k]
153
            do_ignore(p_format, p_va, endchar, n - i - 1, flags);
154
            Py_DECREF(d);
155
            return NULL;
156
        }
157
        v = do_mkvalue(p_format, p_va, flags);
158
        if (v == NULL || 
PyDict_SetItem(d, k, v) < 013.3k
) {
  Branch (158:13): [True: 2, False: 13.3k]
  Branch (158:26): [True: 0, False: 13.3k]
159
            do_ignore(p_format, p_va, endchar, n - i - 2, flags);
160
            Py_DECREF(k);
161
            Py_XDECREF(v);
162
            Py_DECREF(d);
163
            return NULL;
164
        }
165
        Py_DECREF(k);
166
        Py_DECREF(v);
167
    }
168
    if (**p_format != endchar) {
  Branch (168:9): [True: 0, False: 6.06k]
169
        Py_DECREF(d);
170
        PyErr_SetString(PyExc_SystemError,
171
                        "Unmatched paren in format");
172
        return NULL;
173
    }
174
    if (endchar)
  Branch (174:9): [True: 6.06k, False: 0]
175
        ++*p_format;
176
    return d;
177
}
178
179
static PyObject *
180
do_mklist(const char **p_format, va_list *p_va, char endchar, Py_ssize_t n, int flags)
181
{
182
    PyObject *v;
183
    Py_ssize_t i;
184
    if (n < 0)
  Branch (184:9): [True: 0, False: 5]
185
        return NULL;
186
    /* Note that we can't bail immediately on error as this will leak
187
       refcounts on any 'N' arguments. */
188
    v = PyList_New(n);
189
    if (v == NULL) {
  Branch (189:9): [True: 0, False: 5]
190
        do_ignore(p_format, p_va, endchar, n, flags);
191
        return NULL;
192
    }
193
    
for (i = 0; 5
i < n;
i++8
) {
  Branch (193:17): [True: 10, False: 3]
194
        PyObject *w = do_mkvalue(p_format, p_va, flags);
195
        if (w == NULL) {
  Branch (195:13): [True: 2, False: 8]
196
            do_ignore(p_format, p_va, endchar, n - i - 1, flags);
197
            Py_DECREF(v);
198
            return NULL;
199
        }
200
        PyList_SET_ITEM(v, i, w);
201
    }
202
    if (**p_format != endchar) {
  Branch (202:9): [True: 0, False: 3]
203
        Py_DECREF(v);
204
        PyErr_SetString(PyExc_SystemError,
205
                        "Unmatched paren in format");
206
        return NULL;
207
    }
208
    if (endchar)
  Branch (208:9): [True: 3, False: 0]
209
        ++*p_format;
210
    return v;
211
}
212
213
static int
214
do_mkstack(PyObject **stack, const char **p_format, va_list *p_va,
215
           char endchar, Py_ssize_t n, int flags)
216
{
217
    Py_ssize_t i;
218
219
    if (n < 0) {
  Branch (219:9): [True: 0, False: 1.04M]
220
        return -1;
221
    }
222
    /* Note that we can't bail immediately on error as this will leak
223
       refcounts on any 'N' arguments. */
224
    
for (i = 0; 1.04M
i < n;
i++2.96M
) {
  Branch (224:17): [True: 2.96M, False: 1.04M]
225
        PyObject *w = do_mkvalue(p_format, p_va, flags);
226
        if (w == NULL) {
  Branch (226:13): [True: 0, False: 2.96M]
227
            do_ignore(p_format, p_va, endchar, n - i - 1, flags);
228
            goto error;
229
        }
230
        stack[i] = w;
231
    }
232
    if (**p_format != endchar) {
  Branch (232:9): [True: 0, False: 1.04M]
233
        PyErr_SetString(PyExc_SystemError,
234
                        "Unmatched paren in format");
235
        goto error;
236
    }
237
    if (endchar) {
  Branch (237:9): [True: 0, False: 1.04M]
238
        ++*p_format;
239
    }
240
    return 0;
241
242
error:
243
    n = i;
244
    for (i=0; i < n; i++) {
  Branch (244:15): [True: 0, False: 0]
245
        Py_DECREF(stack[i]);
246
    }
247
    return -1;
248
}
249
250
static PyObject *
251
do_mktuple(const char **p_format, va_list *p_va, char endchar, Py_ssize_t n, int flags)
252
{
253
    PyObject *v;
254
    Py_ssize_t i;
255
    if (n < 0)
  Branch (255:9): [True: 0, False: 4.20M]
256
        return NULL;
257
    /* Note that we can't bail immediately on error as this will leak
258
       refcounts on any 'N' arguments. */
259
    if ((v = PyTuple_New(n)) == NULL) {
  Branch (259:9): [True: 0, False: 4.20M]
260
        do_ignore(p_format, p_va, endchar, n, flags);
261
        return NULL;
262
    }
263
    
for (i = 0; 4.20M
i < n;
i++10.6M
) {
  Branch (263:17): [True: 10.6M, False: 4.20M]
264
        PyObject *w = do_mkvalue(p_format, p_va, flags);
265
        if (w == NULL) {
  Branch (265:13): [True: 18, False: 10.6M]
266
            do_ignore(p_format, p_va, endchar, n - i - 1, flags);
267
            Py_DECREF(v);
268
            return NULL;
269
        }
270
        PyTuple_SET_ITEM(v, i, w);
271
    }
272
    if (**p_format != endchar) {
  Branch (272:9): [True: 0, False: 4.20M]
273
        Py_DECREF(v);
274
        PyErr_SetString(PyExc_SystemError,
275
                        "Unmatched paren in format");
276
        return NULL;
277
    }
278
    if (endchar)
  Branch (278:9): [True: 1.55M, False: 2.65M]
279
        ++*p_format;
280
    return v;
281
}
282
283
static PyObject *
284
do_mkvalue(const char **p_format, va_list *p_va, int flags)
285
{
286
#define ERROR_NEED_PY_SSIZE_T_CLEAN \
287
    { \
288
        PyErr_SetString(PyExc_SystemError, \
289
                        "PY_SSIZE_T_CLEAN macro must be defined for '#' formats"); \
290
        return NULL; \
291
    }
292
293
    for (;;) {
294
        switch (*(*p_format)++) {
295
        case '(':
  Branch (295:9): [True: 1.55M, False: 16.1M]
296
            return do_mktuple(p_format, p_va, ')',
297
                              countformat(*p_format, ')'), flags);
298
299
        case '[':
  Branch (299:9): [True: 5, False: 17.6M]
300
            return do_mklist(p_format, p_va, ']',
301
                             countformat(*p_format, ']'), flags);
302
303
        case '{':
  Branch (303:9): [True: 6.06k, False: 17.6M]
304
            return do_mkdict(p_format, p_va, '}',
305
                             countformat(*p_format, '}'), flags);
306
307
        case 'b':
  Branch (307:9): [True: 0, False: 17.6M]
308
        case 'B':
  Branch (308:9): [True: 36, False: 17.6M]
309
        case 'h':
  Branch (309:9): [True: 0, False: 17.6M]
310
        case 'i':
  Branch (310:9): [True: 2.39M, False: 15.2M]
311
            return PyLong_FromLong((long)va_arg(*p_va, int));
312
313
        case 'H':
  Branch (313:9): [True: 0, False: 17.6M]
314
            return PyLong_FromLong((long)va_arg(*p_va, unsigned int));
315
316
        case 'I':
  Branch (316:9): [True: 38.9k, False: 17.6M]
317
        {
318
            unsigned int n;
319
            n = va_arg(*p_va, unsigned int);
320
            return PyLong_FromUnsignedLong(n);
321
        }
322
323
        case 'n':
  Branch (323:9): [True: 696k, False: 16.9M]
324
#if SIZEOF_SIZE_T!=SIZEOF_LONG
325
            return PyLong_FromSsize_t(va_arg(*p_va, Py_ssize_t));
326
#endif
327
            /* Fall through from 'n' to 'l' if Py_ssize_t is long */
328
        case 'l':
  Branch (328:9): [True: 6.69k, False: 17.6M]
329
            return PyLong_FromLong(va_arg(*p_va, long));
330
331
        case 'k':
  Branch (331:9): [True: 669, False: 17.6M]
332
        {
333
            unsigned long n;
334
            n = va_arg(*p_va, unsigned long);
335
            return PyLong_FromUnsignedLong(n);
336
        }
337
338
        case 'L':
  Branch (338:9): [True: 2, False: 17.6M]
339
            return PyLong_FromLongLong((long long)va_arg(*p_va, long long));
340
341
        case 'K':
  Branch (341:9): [True: 37.1k, False: 17.6M]
342
            return PyLong_FromUnsignedLongLong((long long)va_arg(*p_va, unsigned long long));
343
344
        case 'u':
  Branch (344:9): [True: 101, False: 17.6M]
345
        {
346
            PyObject *v;
347
            Py_UNICODE *u = va_arg(*p_va, Py_UNICODE *);
348
            Py_ssize_t n;
349
            if (**p_format == '#') {
  Branch (349:17): [True: 1, False: 100]
350
                ++*p_format;
351
                if (flags & FLAG_SIZE_T) {
  Branch (351:21): [True: 0, False: 1]
352
                    n = va_arg(*p_va, Py_ssize_t);
353
                }
354
                else {
355
                    n = va_arg(*p_va, int);
356
                    ERROR_NEED_PY_SSIZE_T_CLEAN;
357
                }
358
            }
359
            else
360
                n = -1;
361
            if (u == NULL) {
  Branch (361:17): [True: 0, False: 100]
362
                v = Py_None;
363
                Py_INCREF(v);
364
            }
365
            else {
366
                if (n < 0)
  Branch (366:21): [True: 100, False: 0]
367
                    n = wcslen(u);
368
                v = PyUnicode_FromWideChar(u, n);
369
            }
370
            return v;
371
        }
372
        case 'f':
  Branch (372:9): [True: 5, False: 17.6M]
373
        case 'd':
  Branch (373:9): [True: 532k, False: 17.1M]
374
            return PyFloat_FromDouble(
375
                (double)va_arg(*p_va, va_double));
376
377
        case 'D':
  Branch (377:9): [True: 0, False: 17.6M]
378
            return PyComplex_FromCComplex(
379
                *((Py_complex *)va_arg(*p_va, Py_complex *)));
380
381
        case 'c':
  Branch (381:9): [True: 598, False: 17.6M]
382
        {
383
            char p[1];
384
            p[0] = (char)va_arg(*p_va, int);
385
            return PyBytes_FromStringAndSize(p, 1);
386
        }
387
        case 'C':
  Branch (387:9): [True: 72.7k, False: 17.5M]
388
        {
389
            int i = va_arg(*p_va, int);
390
            return PyUnicode_FromOrdinal(i);
391
        }
392
393
        case 's':
  Branch (393:9): [True: 1.50M, False: 16.1M]
394
        case 'z':
  Branch (394:9): [True: 1.45k, False: 17.6M]
395
        case 'U':   /* XXX deprecated alias */
  Branch (395:9): [True: 0, False: 17.6M]
396
        {
397
            PyObject *v;
398
            const char *str = va_arg(*p_va, const char *);
399
            Py_ssize_t n;
400
            if (**p_format == '#') {
  Branch (400:17): [True: 539, False: 1.51M]
401
                ++*p_format;
402
                if (flags & FLAG_SIZE_T) {
  Branch (402:21): [True: 537, False: 2]
403
                    n = va_arg(*p_va, Py_ssize_t);
404
                }
405
                else {
406
                    n = va_arg(*p_va, int);
407
                    ERROR_NEED_PY_SSIZE_T_CLEAN;
408
                }
409
            }
410
            else
411
                n = -1;
412
            if (str == NULL) {
  Branch (412:17): [True: 18.9k, False: 1.49M]
413
                v = Py_None;
414
                Py_INCREF(v);
415
            }
416
            else {
417
                if (n < 0) {
  Branch (417:21): [True: 1.49M, False: 537]
418
                    size_t m = strlen(str);
419
                    if (m > PY_SSIZE_T_MAX) {
  Branch (419:25): [True: 0, False: 1.49M]
420
                        PyErr_SetString(PyExc_OverflowError,
421
                            "string too long for Python string");
422
                        return NULL;
423
                    }
424
                    n = (Py_ssize_t)m;
425
                }
426
                v = PyUnicode_FromStringAndSize(str, n);
427
            }
428
            return v;
429
        }
430
431
        case 'y':
  Branch (431:9): [True: 87.8k, False: 17.5M]
432
        {
433
            PyObject *v;
434
            const char *str = va_arg(*p_va, const char *);
435
            Py_ssize_t n;
436
            if (**p_format == '#') {
  Branch (436:17): [True: 26.1k, False: 61.6k]
437
                ++*p_format;
438
                if (flags & FLAG_SIZE_T) {
  Branch (438:21): [True: 26.1k, False: 1]
439
                    n = va_arg(*p_va, Py_ssize_t);
440
                }
441
                else {
442
                    n = va_arg(*p_va, int);
443
                    ERROR_NEED_PY_SSIZE_T_CLEAN;
444
                }
445
            }
446
            else
447
                n = -1;
448
            if (str == NULL) {
  Branch (448:17): [True: 0, False: 87.8k]
449
                v = Py_None;
450
                Py_INCREF(v);
451
            }
452
            else {
453
                if (n < 0) {
  Branch (453:21): [True: 61.6k, False: 26.1k]
454
                    size_t m = strlen(str);
455
                    if (m > PY_SSIZE_T_MAX) {
  Branch (455:25): [True: 0, False: 61.6k]
456
                        PyErr_SetString(PyExc_OverflowError,
457
                            "string too long for Python bytes");
458
                        return NULL;
459
                    }
460
                    n = (Py_ssize_t)m;
461
                }
462
                v = PyBytes_FromStringAndSize(str, n);
463
            }
464
            return v;
465
        }
466
467
        case 'N':
  Branch (467:9): [True: 942k, False: 16.7M]
468
        case 'S':
  Branch (468:9): [True: 0, False: 17.6M]
469
        case 'O':
  Branch (469:9): [True: 9.78M, False: 7.88M]
470
        if (**p_format == '&') {
  Branch (470:13): [True: 2.68M, False: 8.03M]
471
            typedef PyObject *(*converter)(void *);
472
            converter func = va_arg(*p_va, converter);
473
            void *arg = va_arg(*p_va, void *);
474
            ++*p_format;
475
            return (*func)(arg);
476
        }
477
        else {
478
            PyObject *v;
479
            v = va_arg(*p_va, PyObject *);
480
            if (v != NULL) {
  Branch (480:17): [True: 8.03M, False: 10]
481
                if (*(*p_format - 1) != 'N')
  Branch (481:21): [True: 7.09M, False: 942k]
482
                    Py_INCREF(v);
483
            }
484
            else if (!PyErr_Occurred())
  Branch (484:22): [True: 0, False: 10]
485
                /* If a NULL was passed
486
                 * because a call that should
487
                 * have constructed a value
488
                 * failed, that's OK, and we
489
                 * pass the error on; but if
490
                 * no error occurred it's not
491
                 * clear that the caller knew
492
                 * what she was doing. */
493
                PyErr_SetString(PyExc_SystemError,
494
                    "NULL object passed to Py_BuildValue");
495
            return v;
496
        }
497
498
        case ':':
  Branch (498:9): [True: 236, False: 17.6M]
499
        case ',':
  Branch (499:9): [True: 118, False: 17.6M]
500
        case ' ':
  Branch (500:9): [True: 0, False: 17.6M]
501
        case '\t':
  Branch (501:9): [True: 0, False: 17.6M]
502
            break;
503
504
        default:
  Branch (504:9): [True: 0, False: 17.6M]
505
            PyErr_SetString(PyExc_SystemError,
506
                "bad format char passed to Py_BuildValue");
507
            return NULL;
508
509
        }
510
    }
511
512
#undef ERROR_NEED_PY_SSIZE_T_CLEAN
513
}
514
515
516
PyObject *
517
Py_BuildValue(const char *format, ...)
518
{
519
    va_list va;
520
    PyObject* retval;
521
    va_start(va, format);
522
    retval = va_build_value(format, va, 0);
523
    va_end(va);
524
    return retval;
525
}
526
527
PyObject *
528
_Py_BuildValue_SizeT(const char *format, ...)
529
{
530
    va_list va;
531
    PyObject* retval;
532
    va_start(va, format);
533
    retval = va_build_value(format, va, FLAG_SIZE_T);
534
    va_end(va);
535
    return retval;
536
}
537
538
PyObject *
539
Py_VaBuildValue(const char *format, va_list va)
540
{
541
    return va_build_value(format, va, 0);
542
}
543
544
PyObject *
545
_Py_VaBuildValue_SizeT(const char *format, va_list va)
546
{
547
    return va_build_value(format, va, FLAG_SIZE_T);
548
}
549
550
static PyObject *
551
va_build_value(const char *format, va_list va, int flags)
552
{
553
    const char *f = format;
554
    Py_ssize_t n = countformat(f, '\0');
555
    va_list lva;
556
    PyObject *retval;
557
558
    if (n < 0)
  Branch (558:9): [True: 0, False: 6.71M]
559
        return NULL;
560
    if (n == 0) {
  Branch (560:9): [True: 0, False: 6.71M]
561
        Py_RETURN_NONE;
562
    }
563
    va_copy(lva, va);
564
    if (n == 1) {
  Branch (564:9): [True: 4.06M, False: 2.65M]
565
        retval = do_mkvalue(&f, &lva, flags);
566
    } else {
567
        retval = do_mktuple(&f, &lva, '\0', n, flags);
568
    }
569
    va_end(lva);
570
    return retval;
571
}
572
573
PyObject **
574
_Py_VaBuildStack(PyObject **small_stack, Py_ssize_t small_stack_len,
575
                const char *format, va_list va, Py_ssize_t *p_nargs)
576
{
577
    return va_build_stack(small_stack, small_stack_len, format, va, 0, p_nargs);
578
}
579
580
PyObject **
581
_Py_VaBuildStack_SizeT(PyObject **small_stack, Py_ssize_t small_stack_len,
582
                       const char *format, va_list va, Py_ssize_t *p_nargs)
583
{
584
    return va_build_stack(small_stack, small_stack_len, format, va, FLAG_SIZE_T, p_nargs);
585
}
586
587
static PyObject **
588
va_build_stack(PyObject **small_stack, Py_ssize_t small_stack_len,
589
               const char *format, va_list va, int flags, Py_ssize_t *p_nargs)
590
{
591
    const char *f;
592
    Py_ssize_t n;
593
    va_list lva;
594
    PyObject **stack;
595
    int res;
596
597
    n = countformat(format, '\0');
598
    if (n < 0) {
  Branch (598:9): [True: 0, False: 1.04M]
599
        *p_nargs = 0;
600
        return NULL;
601
    }
602
603
    if (n == 0) {
  Branch (603:9): [True: 0, False: 1.04M]
604
        *p_nargs = 0;
605
        return small_stack;
606
    }
607
608
    if (n <= small_stack_len) {
  Branch (608:9): [True: 1.03M, False: 4.14k]
609
        stack = small_stack;
610
    }
611
    else {
612
        stack = PyMem_Malloc(n * sizeof(stack[0]));
613
        if (stack == NULL) {
  Branch (613:13): [True: 0, False: 4.14k]
614
            PyErr_NoMemory();
615
            return NULL;
616
        }
617
    }
618
619
    va_copy(lva, va);
620
    f = format;
621
    res = do_mkstack(stack, &f, &lva, '\0', n, flags);
622
    va_end(lva);
623
624
    if (res < 0) {
  Branch (624:9): [True: 0, False: 1.04M]
625
        if (stack != small_stack) {
  Branch (625:13): [True: 0, False: 0]
626
            PyMem_Free(stack);
627
        }
628
        return NULL;
629
    }
630
631
    *p_nargs = n;
632
    return stack;
633
}
634
635
636
int
637
PyModule_AddObjectRef(PyObject *mod, const char *name, PyObject *value)
638
{
639
    if (!PyModule_Check(mod)) {
  Branch (639:9): [True: 0, False: 82.2k]
640
        PyErr_SetString(PyExc_TypeError,
641
                        "PyModule_AddObjectRef() first argument "
642
                        "must be a module");
643
        return -1;
644
    }
645
    if (!value) {
  Branch (645:9): [True: 0, False: 82.2k]
646
        if (!PyErr_Occurred()) {
  Branch (646:13): [True: 0, False: 0]
647
            PyErr_SetString(PyExc_SystemError,
648
                            "PyModule_AddObjectRef() must be called "
649
                            "with an exception raised if value is NULL");
650
        }
651
        return -1;
652
    }
653
654
    PyObject *dict = PyModule_GetDict(mod);
655
    if (dict == NULL) {
  Branch (655:9): [True: 0, False: 82.2k]
656
        /* Internal error -- modules must have a dict! */
657
        PyErr_Format(PyExc_SystemError, "module '%s' has no __dict__",
658
                     PyModule_GetName(mod));
659
        return -1;
660
    }
661
662
    if (PyDict_SetItemString(dict, name, value)) {
  Branch (662:9): [True: 0, False: 82.2k]
663
        return -1;
664
    }
665
    return 0;
666
}
667
668
669
int
670
PyModule_AddObject(PyObject *mod, const char *name, PyObject *value)
671
{
672
    int res = PyModule_AddObjectRef(mod, name, value);
673
    if (res == 0) {
  Branch (673:9): [True: 6.74k, False: 0]
674
        Py_DECREF(value);
675
    }
676
    return res;
677
}
678
679
int
680
PyModule_AddIntConstant(PyObject *m, const char *name, long value)
681
{
682
    PyObject *obj = PyLong_FromLong(value);
683
    if (!obj) {
  Branch (683:9): [True: 0, False: 64.0k]
684
        return -1;
685
    }
686
    int res = PyModule_AddObjectRef(m, name, obj);
687
    Py_DECREF(obj);
688
    return res;
689
}
690
691
int
692
PyModule_AddStringConstant(PyObject *m, const char *name, const char *value)
693
{
694
    PyObject *obj = PyUnicode_FromString(value);
695
    if (!obj) {
  Branch (695:9): [True: 0, False: 148]
696
        return -1;
697
    }
698
    int res = PyModule_AddObjectRef(m, name, obj);
699
    Py_DECREF(obj);
700
    return res;
701
}
702
703
int
704
PyModule_AddType(PyObject *module, PyTypeObject *type)
705
{
706
    if (PyType_Ready(type) < 0) {
  Branch (706:9): [True: 0, False: 7.90k]
707
        return -1;
708
    }
709
710
    const char *name = _PyType_Name(type);
711
    assert(name != NULL);
712
713
    return PyModule_AddObjectRef(module, name, (PyObject *)type);
714
}