Coverage Report

Created: 2025-10-10 06:33

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/cpython/Python/modsupport.c
Line
Count
Source
1
2
/* Module support implementation */
3
4
#include "Python.h"
5
#include "pycore_abstract.h"   // _PyIndex_Check()
6
#include "pycore_object.h"     // _PyType_IsReady()
7
#include "pycore_unicodeobject.h"     // _PyUnicodeWriter_FormatV()
8
9
typedef double va_double;
10
11
static PyObject *va_build_value(const char *, va_list);
12
13
14
int
15
_Py_convert_optional_to_ssize_t(PyObject *obj, void *result)
16
196k
{
17
196k
    Py_ssize_t limit;
18
196k
    if (obj == Py_None) {
19
0
        return 1;
20
0
    }
21
196k
    else if (_PyIndex_Check(obj)) {
22
196k
        limit = PyNumber_AsSsize_t(obj, PyExc_OverflowError);
23
196k
        if (limit == -1 && PyErr_Occurred()) {
24
0
            return 0;
25
0
        }
26
196k
    }
27
0
    else {
28
0
        PyErr_Format(PyExc_TypeError,
29
0
                     "argument should be integer or None, not '%.200s'",
30
0
                     Py_TYPE(obj)->tp_name);
31
0
        return 0;
32
0
    }
33
196k
    *((Py_ssize_t *)result) = limit;
34
196k
    return 1;
35
196k
}
36
37
int
38
_Py_convert_optional_to_non_negative_ssize_t(PyObject *obj, void *result)
39
0
{
40
0
    if (!_Py_convert_optional_to_ssize_t(obj, result)) {
41
0
        return 0;
42
0
    }
43
0
    if (obj != Py_None && *((Py_ssize_t *)result) < 0) {
44
0
        PyErr_SetString(PyExc_ValueError, "argument cannot be negative");
45
0
        return 0;
46
0
    }
47
0
    return 1;
48
0
}
49
50
51
/* Helper for mkvalue() to scan the length of a format */
52
53
static Py_ssize_t
54
countformat(const char *format, char endchar)
55
1.76M
{
56
1.76M
    Py_ssize_t count = 0;
57
1.76M
    int level = 0;
58
6.98M
    while (level > 0 || *format != endchar) {
59
5.22M
        switch (*format) {
60
0
        case '\0':
61
            /* Premature end */
62
0
            PyErr_SetString(PyExc_SystemError,
63
0
                            "unmatched paren in format");
64
0
            return -1;
65
273k
        case '(':
66
273k
        case '[':
67
274k
        case '{':
68
274k
            if (level == 0) {
69
272k
                count++;
70
272k
            }
71
274k
            level++;
72
274k
            break;
73
273k
        case ')':
74
273k
        case ']':
75
274k
        case '}':
76
274k
            level--;
77
274k
            break;
78
77.4k
        case '#':
79
77.4k
        case '&':
80
77.4k
        case ',':
81
77.4k
        case ':':
82
77.4k
        case ' ':
83
77.4k
        case '\t':
84
77.4k
            break;
85
4.59M
        default:
86
4.59M
            if (level == 0) {
87
3.94M
                count++;
88
3.94M
            }
89
5.22M
        }
90
5.22M
        format++;
91
5.22M
    }
92
1.76M
    return count;
93
1.76M
}
94
95
96
/* Generic function to create a value -- the inverse of getargs() */
97
/* After an original idea and first implementation by Steven Miale */
98
99
static PyObject *do_mktuple(const char**, va_list *, char, Py_ssize_t);
100
static int do_mkstack(PyObject **, const char**, va_list *, char, Py_ssize_t);
101
static PyObject *do_mklist(const char**, va_list *, char, Py_ssize_t);
102
static PyObject *do_mkdict(const char**, va_list *, char, Py_ssize_t);
103
static PyObject *do_mkvalue(const char**, va_list *);
104
105
static int
106
check_end(const char **p_format, char endchar)
107
1.49M
{
108
1.49M
    const char *f = *p_format;
109
1.49M
    while (*f != endchar) {
110
0
        if (*f != ' ' && *f != '\t' && *f != ',' && *f != ':') {
111
0
            PyErr_SetString(PyExc_SystemError,
112
0
                            "Unmatched paren in format");
113
0
            return 0;
114
0
        }
115
0
        f++;
116
0
    }
117
1.49M
    if (endchar) {
118
272k
        f++;
119
272k
    }
120
1.49M
    *p_format = f;
121
1.49M
    return 1;
122
1.49M
}
123
124
static void
125
do_ignore(const char **p_format, va_list *p_va, char endchar, Py_ssize_t n)
126
0
{
127
0
    assert(PyErr_Occurred());
128
0
    PyObject *v = PyTuple_New(n);
129
0
    for (Py_ssize_t i = 0; i < n; i++) {
130
0
        PyObject *exc = PyErr_GetRaisedException();
131
0
        PyObject *w = do_mkvalue(p_format, p_va);
132
0
        PyErr_SetRaisedException(exc);
133
0
        if (w != NULL) {
134
0
            if (v != NULL) {
135
0
                PyTuple_SET_ITEM(v, i, w);
136
0
            }
137
0
            else {
138
0
                Py_DECREF(w);
139
0
            }
140
0
        }
141
0
    }
142
0
    Py_XDECREF(v);
143
0
    if (!check_end(p_format, endchar)) {
144
0
        return;
145
0
    }
146
0
}
147
148
static PyObject *
149
do_mkdict(const char **p_format, va_list *p_va, char endchar, Py_ssize_t n)
150
1.40k
{
151
1.40k
    PyObject *d;
152
1.40k
    Py_ssize_t i;
153
1.40k
    if (n < 0)
154
0
        return NULL;
155
1.40k
    if (n % 2) {
156
0
        PyErr_SetString(PyExc_SystemError,
157
0
                        "Bad dict format");
158
0
        do_ignore(p_format, p_va, endchar, n);
159
0
        return NULL;
160
0
    }
161
    /* Note that we can't bail immediately on error as this will leak
162
       refcounts on any 'N' arguments. */
163
1.40k
    if ((d = PyDict_New()) == NULL) {
164
0
        do_ignore(p_format, p_va, endchar, n);
165
0
        return NULL;
166
0
    }
167
6.92k
    for (i = 0; i < n; i+= 2) {
168
5.51k
        PyObject *k, *v;
169
170
5.51k
        k = do_mkvalue(p_format, p_va);
171
5.51k
        if (k == NULL) {
172
0
            do_ignore(p_format, p_va, endchar, n - i - 1);
173
0
            Py_DECREF(d);
174
0
            return NULL;
175
0
        }
176
5.51k
        v = do_mkvalue(p_format, p_va);
177
5.51k
        if (v == NULL || PyDict_SetItem(d, k, v) < 0) {
178
0
            do_ignore(p_format, p_va, endchar, n - i - 2);
179
0
            Py_DECREF(k);
180
0
            Py_XDECREF(v);
181
0
            Py_DECREF(d);
182
0
            return NULL;
183
0
        }
184
5.51k
        Py_DECREF(k);
185
5.51k
        Py_DECREF(v);
186
5.51k
    }
187
1.40k
    if (!check_end(p_format, endchar)) {
188
0
        Py_DECREF(d);
189
0
        return NULL;
190
0
    }
191
1.40k
    return d;
192
1.40k
}
193
194
static PyObject *
195
do_mklist(const char **p_format, va_list *p_va, char endchar, Py_ssize_t n)
196
0
{
197
0
    PyObject *v;
198
0
    Py_ssize_t i;
199
0
    if (n < 0)
200
0
        return NULL;
201
    /* Note that we can't bail immediately on error as this will leak
202
       refcounts on any 'N' arguments. */
203
0
    v = PyList_New(n);
204
0
    if (v == NULL) {
205
0
        do_ignore(p_format, p_va, endchar, n);
206
0
        return NULL;
207
0
    }
208
0
    for (i = 0; i < n; i++) {
209
0
        PyObject *w = do_mkvalue(p_format, p_va);
210
0
        if (w == NULL) {
211
0
            do_ignore(p_format, p_va, endchar, n - i - 1);
212
0
            Py_DECREF(v);
213
0
            return NULL;
214
0
        }
215
0
        PyList_SET_ITEM(v, i, w);
216
0
    }
217
0
    if (!check_end(p_format, endchar)) {
218
0
        Py_DECREF(v);
219
0
        return NULL;
220
0
    }
221
0
    return v;
222
0
}
223
224
static int
225
do_mkstack(PyObject **stack, const char **p_format, va_list *p_va,
226
           char endchar, Py_ssize_t n)
227
292k
{
228
292k
    Py_ssize_t i;
229
230
292k
    if (n < 0) {
231
0
        return -1;
232
0
    }
233
    /* Note that we can't bail immediately on error as this will leak
234
       refcounts on any 'N' arguments. */
235
1.74M
    for (i = 0; i < n; i++) {
236
1.44M
        PyObject *w = do_mkvalue(p_format, p_va);
237
1.44M
        if (w == NULL) {
238
0
            do_ignore(p_format, p_va, endchar, n - i - 1);
239
0
            goto error;
240
0
        }
241
1.44M
        stack[i] = w;
242
1.44M
    }
243
292k
    if (!check_end(p_format, endchar)) {
244
0
        goto error;
245
0
    }
246
292k
    return 0;
247
248
0
error:
249
0
    n = i;
250
0
    for (i=0; i < n; i++) {
251
0
        Py_DECREF(stack[i]);
252
0
    }
253
0
    return -1;
254
292k
}
255
256
static PyObject *
257
do_mktuple(const char **p_format, va_list *p_va, char endchar, Py_ssize_t n)
258
1.20M
{
259
1.20M
    PyObject *v;
260
1.20M
    Py_ssize_t i;
261
1.20M
    if (n < 0)
262
0
        return NULL;
263
    /* Note that we can't bail immediately on error as this will leak
264
       refcounts on any 'N' arguments. */
265
1.20M
    if ((v = PyTuple_New(n)) == NULL) {
266
0
        do_ignore(p_format, p_va, endchar, n);
267
0
        return NULL;
268
0
    }
269
3.69M
    for (i = 0; i < n; i++) {
270
2.49M
        PyObject *w = do_mkvalue(p_format, p_va);
271
2.49M
        if (w == NULL) {
272
0
            do_ignore(p_format, p_va, endchar, n - i - 1);
273
0
            Py_DECREF(v);
274
0
            return NULL;
275
0
        }
276
2.49M
        PyTuple_SET_ITEM(v, i, w);
277
2.49M
    }
278
1.20M
    if (!check_end(p_format, endchar)) {
279
0
        Py_DECREF(v);
280
0
        return NULL;
281
0
    }
282
1.20M
    return v;
283
1.20M
}
284
285
static PyObject *
286
do_mkvalue(const char **p_format, va_list *p_va)
287
4.21M
{
288
4.21M
    for (;;) {
289
4.21M
        switch (*(*p_format)++) {
290
270k
        case '(':
291
270k
            return do_mktuple(p_format, p_va, ')',
292
270k
                              countformat(*p_format, ')'));
293
294
0
        case '[':
295
0
            return do_mklist(p_format, p_va, ']',
296
0
                             countformat(*p_format, ']'));
297
298
1.40k
        case '{':
299
1.40k
            return do_mkdict(p_format, p_va, '}',
300
1.40k
                             countformat(*p_format, '}'));
301
302
0
        case 'b':
303
0
        case 'B':
304
0
        case 'h':
305
69.0k
        case 'i':
306
69.0k
            return PyLong_FromLong((long)va_arg(*p_va, int));
307
308
0
        case 'H':
309
0
            return PyLong_FromLong((long)va_arg(*p_va, unsigned int));
310
311
40
        case 'I':
312
40
        {
313
40
            unsigned int n;
314
40
            n = va_arg(*p_va, unsigned int);
315
40
            return PyLong_FromUnsignedLong(n);
316
0
        }
317
318
1.76M
        case 'n':
319
#if SIZEOF_SIZE_T!=SIZEOF_LONG
320
            return PyLong_FromSsize_t(va_arg(*p_va, Py_ssize_t));
321
#endif
322
            /* Fall through from 'n' to 'l' if Py_ssize_t is long */
323
1.76M
            _Py_FALLTHROUGH;
324
1.76M
        case 'l':
325
1.76M
            return PyLong_FromLong(va_arg(*p_va, long));
326
327
0
        case 'k':
328
0
        {
329
0
            unsigned long n;
330
0
            n = va_arg(*p_va, unsigned long);
331
0
            return PyLong_FromUnsignedLong(n);
332
1.76M
        }
333
334
0
        case 'L':
335
0
            return PyLong_FromLongLong((long long)va_arg(*p_va, long long));
336
337
0
        case 'K':
338
0
            return PyLong_FromUnsignedLongLong(
339
0
                va_arg(*p_va, unsigned long long));
340
341
0
        case 'u':
342
0
        {
343
0
            PyObject *v;
344
0
            const wchar_t *u = va_arg(*p_va, wchar_t*);
345
0
            Py_ssize_t n;
346
0
            if (**p_format == '#') {
347
0
                ++*p_format;
348
0
                n = va_arg(*p_va, Py_ssize_t);
349
0
            }
350
0
            else
351
0
                n = -1;
352
0
            if (u == NULL) {
353
0
                v = Py_NewRef(Py_None);
354
0
            }
355
0
            else {
356
0
                if (n < 0)
357
0
                    n = wcslen(u);
358
0
                v = PyUnicode_FromWideChar(u, n);
359
0
            }
360
0
            return v;
361
1.76M
        }
362
0
        case 'f':
363
0
        case 'd':
364
0
            return PyFloat_FromDouble(
365
0
                (double)va_arg(*p_va, va_double));
366
367
0
        case 'D':
368
0
            return PyComplex_FromCComplex(
369
0
                *((Py_complex *)va_arg(*p_va, Py_complex *)));
370
371
146
        case 'c':
372
146
        {
373
146
            char p[1];
374
146
            p[0] = (char)va_arg(*p_va, int);
375
146
            return PyBytes_FromStringAndSize(p, 1);
376
0
        }
377
0
        case 'C':
378
0
        {
379
0
            int i = va_arg(*p_va, int);
380
0
            return PyUnicode_FromOrdinal(i);
381
0
        }
382
0
        case 'p':
383
0
        {
384
0
            int i = va_arg(*p_va, int);
385
0
            return PyBool_FromLong(i);
386
0
        }
387
388
565k
        case 's':
389
568k
        case 'z':
390
568k
        case 'U':   /* XXX deprecated alias */
391
568k
        {
392
568k
            PyObject *v;
393
568k
            const char *str = va_arg(*p_va, const char *);
394
568k
            Py_ssize_t n;
395
568k
            if (**p_format == '#') {
396
0
                ++*p_format;
397
0
                n = va_arg(*p_va, Py_ssize_t);
398
0
            }
399
568k
            else
400
568k
                n = -1;
401
568k
            if (str == NULL) {
402
0
                v = Py_NewRef(Py_None);
403
0
            }
404
568k
            else {
405
568k
                if (n < 0) {
406
568k
                    size_t m = strlen(str);
407
568k
                    if (m > PY_SSIZE_T_MAX) {
408
0
                        PyErr_SetString(PyExc_OverflowError,
409
0
                            "string too long for Python string");
410
0
                        return NULL;
411
0
                    }
412
568k
                    n = (Py_ssize_t)m;
413
568k
                }
414
568k
                v = PyUnicode_FromStringAndSize(str, n);
415
568k
            }
416
568k
            return v;
417
568k
        }
418
419
77.4k
        case 'y':
420
77.4k
        {
421
77.4k
            PyObject *v;
422
77.4k
            const char *str = va_arg(*p_va, const char *);
423
77.4k
            Py_ssize_t n;
424
77.4k
            if (**p_format == '#') {
425
77.4k
                ++*p_format;
426
77.4k
                n = va_arg(*p_va, Py_ssize_t);
427
77.4k
            }
428
0
            else
429
0
                n = -1;
430
77.4k
            if (str == NULL) {
431
0
                v = Py_NewRef(Py_None);
432
0
            }
433
77.4k
            else {
434
77.4k
                if (n < 0) {
435
0
                    size_t m = strlen(str);
436
0
                    if (m > PY_SSIZE_T_MAX) {
437
0
                        PyErr_SetString(PyExc_OverflowError,
438
0
                            "string too long for Python bytes");
439
0
                        return NULL;
440
0
                    }
441
0
                    n = (Py_ssize_t)m;
442
0
                }
443
77.4k
                v = PyBytes_FromStringAndSize(str, n);
444
77.4k
            }
445
77.4k
            return v;
446
77.4k
        }
447
448
1.18M
        case 'N':
449
1.18M
        case 'S':
450
1.46M
        case 'O':
451
1.46M
        if (**p_format == '&') {
452
0
            typedef PyObject *(*converter)(void *);
453
0
            converter func = va_arg(*p_va, converter);
454
0
            void *arg = va_arg(*p_va, void *);
455
0
            ++*p_format;
456
0
            return (*func)(arg);
457
0
        }
458
1.46M
        else {
459
1.46M
            PyObject *v;
460
1.46M
            v = va_arg(*p_va, PyObject *);
461
1.46M
            if (v != NULL) {
462
1.46M
                if (*(*p_format - 1) != 'N')
463
286k
                    Py_INCREF(v);
464
1.46M
            }
465
0
            else if (!PyErr_Occurred())
466
                /* If a NULL was passed
467
                 * because a call that should
468
                 * have constructed a value
469
                 * failed, that's OK, and we
470
                 * pass the error on; but if
471
                 * no error occurred it's not
472
                 * clear that the caller knew
473
                 * what she was doing. */
474
0
                PyErr_SetString(PyExc_SystemError,
475
0
                    "NULL object passed to Py_BuildValue");
476
1.46M
            return v;
477
1.46M
        }
478
479
0
        case ':':
480
0
        case ',':
481
0
        case ' ':
482
0
        case '\t':
483
0
            break;
484
485
0
        default:
486
0
            PyErr_SetString(PyExc_SystemError,
487
0
                "bad format char passed to Py_BuildValue");
488
0
            return NULL;
489
490
4.21M
        }
491
4.21M
    }
492
4.21M
}
493
494
495
PyObject *
496
Py_BuildValue(const char *format, ...)
497
1.19M
{
498
1.19M
    va_list va;
499
1.19M
    PyObject* retval;
500
1.19M
    va_start(va, format);
501
1.19M
    retval = va_build_value(format, va);
502
1.19M
    va_end(va);
503
1.19M
    return retval;
504
1.19M
}
505
506
PyAPI_FUNC(PyObject *) /* abi only */
507
_Py_BuildValue_SizeT(const char *format, ...)
508
0
{
509
0
    va_list va;
510
0
    PyObject* retval;
511
0
    va_start(va, format);
512
0
    retval = va_build_value(format, va);
513
0
    va_end(va);
514
0
    return retval;
515
0
}
516
517
PyObject *
518
Py_VaBuildValue(const char *format, va_list va)
519
0
{
520
0
    return va_build_value(format, va);
521
0
}
522
523
PyAPI_FUNC(PyObject *) /* abi only */
524
_Py_VaBuildValue_SizeT(const char *format, va_list va)
525
0
{
526
0
    return va_build_value(format, va);
527
0
}
528
529
static PyObject *
530
va_build_value(const char *format, va_list va)
531
1.19M
{
532
1.19M
    const char *f = format;
533
1.19M
    Py_ssize_t n = countformat(f, '\0');
534
1.19M
    va_list lva;
535
1.19M
    PyObject *retval;
536
537
1.19M
    if (n < 0)
538
0
        return NULL;
539
1.19M
    if (n == 0) {
540
0
        Py_RETURN_NONE;
541
0
    }
542
1.19M
    va_copy(lva, va);
543
1.19M
    if (n == 1) {
544
266k
        retval = do_mkvalue(&f, &lva);
545
933k
    } else {
546
933k
        retval = do_mktuple(&f, &lva, '\0', n);
547
933k
    }
548
1.19M
    va_end(lva);
549
1.19M
    return retval;
550
1.19M
}
551
552
PyObject **
553
_Py_VaBuildStack(PyObject **small_stack, Py_ssize_t small_stack_len,
554
                const char *format, va_list va, Py_ssize_t *p_nargs)
555
292k
{
556
292k
    const char *f;
557
292k
    Py_ssize_t n;
558
292k
    va_list lva;
559
292k
    PyObject **stack;
560
292k
    int res;
561
562
292k
    n = countformat(format, '\0');
563
292k
    if (n < 0) {
564
0
        *p_nargs = 0;
565
0
        return NULL;
566
0
    }
567
568
292k
    if (n == 0) {
569
0
        *p_nargs = 0;
570
0
        return small_stack;
571
0
    }
572
573
292k
    if (n <= small_stack_len) {
574
292k
        stack = small_stack;
575
292k
    }
576
96
    else {
577
96
        stack = PyMem_Malloc(n * sizeof(stack[0]));
578
96
        if (stack == NULL) {
579
0
            PyErr_NoMemory();
580
0
            return NULL;
581
0
        }
582
96
    }
583
584
292k
    va_copy(lva, va);
585
292k
    f = format;
586
292k
    res = do_mkstack(stack, &f, &lva, '\0', n);
587
292k
    va_end(lva);
588
589
292k
    if (res < 0) {
590
0
        if (stack != small_stack) {
591
0
            PyMem_Free(stack);
592
0
        }
593
0
        return NULL;
594
0
    }
595
596
292k
    *p_nargs = n;
597
292k
    return stack;
598
292k
}
599
600
601
int
602
PyModule_AddObjectRef(PyObject *mod, const char *name, PyObject *value)
603
8.97k
{
604
8.97k
    if (!PyModule_Check(mod)) {
605
0
        PyErr_SetString(PyExc_TypeError,
606
0
                        "PyModule_AddObjectRef() first argument "
607
0
                        "must be a module");
608
0
        return -1;
609
0
    }
610
8.97k
    if (!value) {
611
0
        if (!PyErr_Occurred()) {
612
0
            PyErr_SetString(PyExc_SystemError,
613
0
                            "PyModule_AddObjectRef() must be called "
614
0
                            "with an exception raised if value is NULL");
615
0
        }
616
0
        return -1;
617
0
    }
618
619
8.97k
    PyObject *dict = PyModule_GetDict(mod);
620
8.97k
    if (dict == NULL) {
621
        /* Internal error -- modules must have a dict! */
622
0
        PyErr_Format(PyExc_SystemError, "module '%s' has no __dict__",
623
0
                     PyModule_GetName(mod));
624
0
        return -1;
625
0
    }
626
8.97k
    return PyDict_SetItemString(dict, name, value);
627
8.97k
}
628
629
int
630
PyModule_Add(PyObject *mod, const char *name, PyObject *value)
631
6.74k
{
632
6.74k
    int res = PyModule_AddObjectRef(mod, name, value);
633
6.74k
    Py_XDECREF(value);
634
6.74k
    return res;
635
6.74k
}
636
637
int
638
PyModule_AddObject(PyObject *mod, const char *name, PyObject *value)
639
0
{
640
0
    int res = PyModule_AddObjectRef(mod, name, value);
641
0
    if (res == 0) {
642
0
        Py_DECREF(value);
643
0
    }
644
0
    return res;
645
0
}
646
647
int
648
PyModule_AddIntConstant(PyObject *m, const char *name, long value)
649
6.33k
{
650
6.33k
    return PyModule_Add(m, name, PyLong_FromLong(value));
651
6.33k
}
652
653
int
654
PyModule_AddStringConstant(PyObject *m, const char *name, const char *value)
655
40
{
656
40
    return PyModule_Add(m, name, PyUnicode_FromString(value));
657
40
}
658
659
int
660
PyModule_AddType(PyObject *module, PyTypeObject *type)
661
821
{
662
821
    if (!_PyType_IsReady(type) && PyType_Ready(type) < 0) {
663
0
        return -1;
664
0
    }
665
666
821
    const char *name = _PyType_Name(type);
667
821
    assert(name != NULL);
668
669
821
    return PyModule_AddObjectRef(module, name, (PyObject *)type);
670
821
}
671
672
static int _abiinfo_raise(const char *module_name, const char *format, ...)
673
0
{
674
0
    PyUnicodeWriter *writer = PyUnicodeWriter_Create(0);
675
0
    if (!writer) {
676
0
        return -1;
677
0
    }
678
0
    if (module_name) {
679
0
        if (PyUnicodeWriter_WriteUTF8(writer, module_name, -1) < 0) {
680
0
            PyUnicodeWriter_Discard(writer);
681
0
            return -1;
682
0
        }
683
0
        if (PyUnicodeWriter_WriteASCII(writer, ": ", 2) < 0) {
684
0
            PyUnicodeWriter_Discard(writer);
685
0
            return -1;
686
0
        }
687
0
    }
688
0
    va_list vargs;
689
0
    va_start(vargs, format);
690
0
    if (_PyUnicodeWriter_FormatV(writer, format, vargs) < 0) {
691
0
        PyUnicodeWriter_Discard(writer);
692
0
        return -1;
693
0
    }
694
0
    PyObject *message = PyUnicodeWriter_Finish(writer);
695
0
    if (!message) {
696
0
        return -1;
697
0
    }
698
0
    PyErr_SetObject(PyExc_ImportError, message);
699
0
    Py_DECREF(message);
700
0
    return -1;
701
0
}
702
703
int PyABIInfo_Check(PyABIInfo *info, const char *module_name)
704
6
{
705
6
    if (!info) {
706
0
        return _abiinfo_raise(module_name, "NULL PyABIInfo");
707
0
    }
708
709
    /* abiinfo_major_version */
710
6
    if (info->abiinfo_major_version == 0) {
711
0
        return 0;
712
0
    }
713
6
    if (info->abiinfo_major_version > 1) {
714
0
        return _abiinfo_raise(module_name, "PyABIInfo version too high");
715
0
    }
716
717
    /* Internal ABI */
718
6
    if (info->flags & PyABIInfo_INTERNAL) {
719
6
        if (info->abi_version && (info->abi_version != PY_VERSION_HEX)) {
720
0
            return _abiinfo_raise(
721
0
                module_name,
722
0
                "incompatible internal ABI (0x%x != 0x%x)",
723
0
                info->abi_version, PY_VERSION_HEX);
724
0
        }
725
6
    }
726
727
12
#define XY_MASK 0xffff0000
728
6
    if (info->flags & PyABIInfo_STABLE) {
729
        /* Greater-than major.minor version check */
730
0
        if (info->abi_version) {
731
0
            if ((info->abi_version & XY_MASK) > (PY_VERSION_HEX & XY_MASK)) {
732
0
                return _abiinfo_raise(
733
0
                    module_name,
734
0
                    "incompatible future stable ABI version (%d.%d)",
735
0
                    ((info->abi_version) >> 24) % 0xff,
736
0
                    ((info->abi_version) >> 16) % 0xff);
737
0
            }
738
0
            if (info->abi_version < Py_PACK_VERSION(3, 2)) {
739
0
                return _abiinfo_raise(
740
0
                    module_name,
741
0
                    "invalid stable ABI version (%d.%d)",
742
0
                    ((info->abi_version) >> 24) % 0xff,
743
0
                    ((info->abi_version) >> 16) % 0xff);
744
0
            }
745
0
        }
746
0
        if (info->flags & PyABIInfo_INTERNAL) {
747
0
            return _abiinfo_raise(module_name,
748
0
                                  "cannot use both internal and stable ABI");
749
0
        }
750
0
    }
751
6
    else {
752
        /* Exact major.minor version check */
753
6
        if (info->abi_version) {
754
6
            if ((info->abi_version & XY_MASK) != (PY_VERSION_HEX & XY_MASK)) {
755
0
                return _abiinfo_raise(
756
0
                    module_name,
757
0
                    "incompatible ABI version (%d.%d)",
758
0
                    ((info->abi_version) >> 24) % 0xff,
759
0
                    ((info->abi_version) >> 16) % 0xff);
760
0
            }
761
6
        }
762
6
    }
763
6
#undef XY_MASK
764
765
    /* Free-threading/GIL */
766
6
    uint16_t gilflags = info->flags & (PyABIInfo_GIL | PyABIInfo_FREETHREADED);
767
#if Py_GIL_DISABLED
768
    if (gilflags == PyABIInfo_GIL) {
769
        return _abiinfo_raise(module_name,
770
                              "incompatible with free-threaded CPython");
771
    }
772
#else
773
6
    if (gilflags == PyABIInfo_FREETHREADED) {
774
0
        return _abiinfo_raise(module_name,
775
0
                              "only compatible with free-threaded CPython");
776
0
    }
777
6
#endif
778
779
6
    return 0;
780
6
}
781
782
783
/* Exported functions for version helper macros */
784
785
#undef Py_PACK_FULL_VERSION
786
uint32_t
787
Py_PACK_FULL_VERSION(int x, int y, int z, int level, int serial)
788
0
{
789
0
    return _Py_PACK_FULL_VERSION(x, y, z, level, serial);
790
0
}
791
792
#undef Py_PACK_VERSION
793
uint32_t
794
Py_PACK_VERSION(int x, int y)
795
0
{
796
0
    return _Py_PACK_VERSION(x, y);
797
0
}