Skip to content

Commit 943a431

Browse files
committed
fix: _PyArg_CheckPositional function became an internal API since Python 3.13
1 parent 3368d3b commit 943a431

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

include/pyshim.hh

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,35 @@ typedef struct {
3838
} _PyDictViewObject;
3939
#endif
4040

41+
/**
42+
* @brief Shim for `_PyArg_CheckPositional`.
43+
* Since Python 3.13, `_PyArg_CheckPositional` function became an internal API.
44+
* @see Modified from https://github.com/python/cpython/blob/v3.13.0rc1/Python/getargs.c#L2738-L2780
45+
*/
46+
#if PY_VERSION_HEX >= 0x030d0000 // Python version is greater than 3.13
47+
inline int _PyArg_CheckPositional(const char *name, Py_ssize_t nargs, Py_ssize_t min, Py_ssize_t max) {
48+
if (nargs < min) {
49+
PyErr_Format(
50+
PyExc_TypeError,
51+
"%.200s expected %s%zd argument%s, got %zd",
52+
name, (min == max ? "" : "at least "), min, min == 1 ? "" : "s", nargs);
53+
return 0;
54+
}
55+
56+
if (nargs == 0) {
57+
return 1;
58+
}
59+
60+
if (nargs > max) {
61+
PyErr_Format(
62+
PyExc_TypeError,
63+
"%.200s expected %s%zd argument%s, got %zd",
64+
name, (min == max ? "" : "at most "), max, max == 1 ? "" : "s", nargs);
65+
return 0;
66+
}
67+
68+
return 1;
69+
}
70+
#endif
71+
4172
#endif // #ifndef PythonMonkey_py_version_shim_

src/JSArrayProxy.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <jsfriendapi.h>
2424

2525
#include <Python.h>
26+
#include "include/pyshim.hh"
2627

2728

2829
void JSArrayProxyMethodDefinitions::JSArrayProxy_dealloc(JSArrayProxy *self)

0 commit comments

Comments
 (0)