PythonMonkey   v1.1.1 (dev)
Loading...
Searching...
No Matches
pyshim.hh
Go to the documentation of this file.
1
13#ifndef PythonMonkey_py_version_shim_
14#define PythonMonkey_py_version_shim_
15
16#include <Python.h>
17
22#if PY_VERSION_HEX >= 0x030d0000 // Python version is greater than 3.13
23 #define Py_IsFinalizing Py_IsFinalizing
24#else
25 #define Py_IsFinalizing _Py_IsFinalizing
26#endif
27
34#if PY_VERSION_HEX >= 0x030d0000 // Python version is greater than 3.13
35typedef struct {
36 PyObject_HEAD
37 PyDictObject *dv_dict;
38} _PyDictViewObject;
39#endif
40
46#if PY_VERSION_HEX >= 0x030d0000 // Python version is greater than 3.13
47inline int _PyArg_CheckPositional(const char *name, Py_ssize_t nargs, Py_ssize_t min, Py_ssize_t max) {
48 if (!name) { // _PyArg_CheckPositional may also be when unpacking a tuple
49 name = "unpacked tuple"; // https://github.com/python/cpython/blob/v3.13.0rc1/Python/getargs.c#L2746
50 }
51
52 if (nargs < min) {
53 PyErr_Format(
54 PyExc_TypeError,
55 "%.200s expected %s%zd argument%s, got %zd",
56 name, (min == max ? "" : "at least "), min, min == 1 ? "" : "s", nargs);
57 return 0;
58 }
59
60 if (nargs == 0) {
61 return 1;
62 }
63
64 if (nargs > max) {
65 PyErr_Format(
66 PyExc_TypeError,
67 "%.200s expected %s%zd argument%s, got %zd",
68 name, (min == max ? "" : "at most "), max, max == 1 ? "" : "s", nargs);
69 return 0;
70 }
71
72 return 1;
73}
74#endif
75
81inline PyObject *PyDictView_New(PyObject *dict, PyTypeObject *type) {
82#if PY_VERSION_HEX < 0x030d0000 // Python version is lower than 3.13
83 return _PyDictView_New(dict, type);
84#else
85 _PyDictViewObject *dv;
86 dv = PyObject_GC_New(_PyDictViewObject, type);
87 if (dv == NULL)
88 return NULL;
89 Py_INCREF(dict);
90 dv->dv_dict = (PyDictObject *)dict;
91 PyObject_GC_Track(dv);
92 return (PyObject *)dv;
93#endif
94}
95
100inline void PyErr_SetKeyError(PyObject *key) {
101 // Use the provided API when possible, as `PyErr_SetObject`'s behaviour is more complex than originally thought
102 // see also: https://github.com/python/cpython/issues/101578
103#if PY_VERSION_HEX < 0x030d0000 // Python version is lower than 3.13
104 return _PyErr_SetKeyError(key);
105#else
106 return PyErr_SetObject(PyExc_KeyError, key);
107#endif
108}
109
114#ifndef Py_SET_SIZE
115static inline void _Py_SET_SIZE(PyVarObject *ob, Py_ssize_t size) {
116 ob->ob_size = size;
117}
118#define Py_SET_SIZE(ob, size) _Py_SET_SIZE((PyVarObject *)(ob), size)
119#endif
120
125#if PY_VERSION_HEX < 0x03090000 // Python version is less than 3.9
126inline PyObject *PyObject_CallOneArg(PyObject *func, PyObject *arg) {
127 return PyObject_CallFunction(func, "O", arg);
128}
129#endif
130
137inline int PyLong_AsByteArray(PyLongObject *v, unsigned char *bytes, size_t n, bool little_endian, bool is_signed) {
138#if PY_VERSION_HEX >= 0x030d0000 // Python version is 3.13 or higher
139 return _PyLong_AsByteArray(v, bytes, n, little_endian, is_signed, /*with_exceptions*/ false);
140#else
141 return _PyLong_AsByteArray(v, bytes, n, little_endian, is_signed);
142#endif
143}
144
145#endif // #ifndef PythonMonkey_py_version_shim_
readonly name
Definition dom-exception.d.ts:24
int PyLong_AsByteArray(PyLongObject *v, unsigned char *bytes, size_t n, bool little_endian, bool is_signed)
Shim for _PyLong_AsByteArray. Python 3.13.0a4 added a new public API PyLong_AsNativeBytes() to replac...
Definition pyshim.hh:137
void PyErr_SetKeyError(PyObject *key)
Shim for _PyErr_SetKeyError. Since Python 3.13, _PyErr_SetKeyError function became an internal API.
Definition pyshim.hh:100
PyObject * PyDictView_New(PyObject *dict, PyTypeObject *type)
_PyDictViewObject type definition moved from Python's public API to the internal header file internal...
Definition pyshim.hh:81
PyObject * PyObject_CallOneArg(PyObject *func, PyObject *arg)
Shim for PyObject_CallOneArg. PyObject_CallOneArg is not available in Python < 3.9.
Definition pyshim.hh:126