Blender V5.0
python_compat.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10
11#include <Python.h> /* IWYU pragma: keep. */
12
13#include "BLI_utildefines.h" /* IWYU pragma: keep. */
14#include "python_compat.hh" /* IWYU pragma: keep. */
15
16#if PY_VERSION_HEX >= 0x030d0000 /* >=3.14 */
17
18/* Removed in Python 3.13. */
19int _PyArg_CheckPositional(const char *name, Py_ssize_t nargs, Py_ssize_t min, Py_ssize_t max)
20{
21 BLI_assert(min >= 0);
22 BLI_assert(min <= max);
23
24 if (nargs < min) {
25 if (name != nullptr) {
26 PyErr_Format(PyExc_TypeError,
27 "%.200s expected %s%zd argument%s, got %zd",
28 name,
29 (min == max ? "" : "at least "),
30 min,
31 min == 1 ? "" : "s",
32 nargs);
33 }
34 else {
35 PyErr_Format(PyExc_TypeError,
36 "unpacked tuple should have %s%zd element%s,"
37 " but has %zd",
38 (min == max ? "" : "at least "),
39 min,
40 min == 1 ? "" : "s",
41 nargs);
42 }
43 return 0;
44 }
45
46 if (nargs == 0) {
47 return 1;
48 }
49
50 if (nargs > max) {
51 if (name != nullptr) {
52 PyErr_Format(PyExc_TypeError,
53 "%.200s expected %s%zd argument%s, got %zd",
54 name,
55 (min == max ? "" : "at most "),
56 max,
57 max == 1 ? "" : "s",
58 nargs);
59 }
60 else {
61 PyErr_Format(PyExc_TypeError,
62 "unpacked tuple should have %s%zd element%s,"
63 " but has %zd",
64 (min == max ? "" : "at most "),
65 max,
66 max == 1 ? "" : "s",
67 nargs);
68 }
69 return 0;
70 }
71
72 return 1;
73}
74
75#endif
#define BLI_assert(a)
Definition BLI_assert.h:46
header-only compatibility defines.
const char * name
#define min(a, b)
Definition sort.cc:36
max
Definition text_draw.cc:251