Debian Codegraph @

src/c/compat/backports_zstd_edits.h

raw
1 #ifndef BACKPORTS_ZSTD_EDITS_H
2 #define BACKPORTS_ZSTD_EDITS_H
3
4 #include "backports_zstd_edits_orig.h"
5
6 #define BACKPORTSZSTD__PyArg_BadArgument(fname, displayname, expected, args) \
7 _PyArg_BadArgument(fname, displayname, expected, args)
8
9 #define BACKPORTSZSTD__PyArg_CheckPositional(funcname, nargs, min, max) \
10 _PyArg_CheckPositional(funcname, nargs, min, max)
11
12 static inline PyObject *const *
13 BACKPORTSZSTD__PyArg_UnpackKeywords(
14 PyObject *const *args,
15 Py_ssize_t nargs,
16 PyObject *kwargs,
17 PyObject *kwnames,
18 struct _PyArg_Parser *parser,
19 int minpos,
20 int maxpos,
21 int minkw,
22 int varpos, // introduced in Python 3.14
23 PyObject **buf)
24 {
25 if (varpos)
26 {
27 /*
28 All calls of BACKPORTSZSTD__PyArg_UnpackKeywords have varpos set to 0
29 This will catch future code evolutions that may change this assumption
30 */
31 Py_FatalError("Not implemented");
32 }
33 return _PyArg_UnpackKeywords(
34 args,
35 nargs,
36 kwargs,
37 kwnames,
38 parser,
39 minpos,
40 maxpos,
41 minkw,
42 buf);
43 }
44
45 #define BACKPORTSZSTD__PyNumber_Index(o) _PyNumber_Index(o)
46
47 #if PY_VERSION_HEX < 0x030D0000 // Python 3.12 and below
48 #define BACKPORTSZSTD_PyErr_Format_AppendPT(t, s, o) PyErr_Format(t, (s "%s"), Py_TYPE(o)->tp_name)
49 #else
50 #define BACKPORTSZSTD_PyErr_Format_AppendPT(t, s, o) PyErr_Format(t, (s "%T"), o)
51 #endif
52
53 /*
54 Backporting PyMutex is a lot of work
55 Instead, we fallback on PyThread_type_lock for Python 3.12 and below
56 We introduced some functions of our own to compensate API differences
57 */
58 #if PY_VERSION_HEX < 0x030D0000 // Python 3.12 and below
59
60 #define BACKPORTSZSTD_LOCK PyThread_type_lock
61 #define BACKPORTSZSTD_LOCK_allocate PyThread_allocate_lock
62 #define BACKPORTSZSTD_LOCK_isError(l) (l == NULL)
63 static inline void BACKPORTSZSTD_LOCK_lock(PyThread_type_lock *mp)
64 {
65 Py_BEGIN_ALLOW_THREADS
66 PyThread_acquire_lock(*mp, WAIT_LOCK);
67 Py_END_ALLOW_THREADS
68 }
69 static inline void BACKPORTSZSTD_LOCK_unlock(PyThread_type_lock *mp)
70 {
71 PyThread_release_lock(*mp);
72 }
73 static inline void BACKPORTSZSTD_LOCK_free(PyThread_type_lock mp)
74 {
75 if (mp)
76 {
77 PyThread_free_lock(mp);
78 }
79 }
80 static inline int BACKPORTSZSTD_LOCK_isLocked(PyThread_type_lock *mp)
81 {
82 // note: this function is only used in asserts
83 PyLockStatus status;
84 Py_BEGIN_ALLOW_THREADS
85 status = PyThread_acquire_lock_timed(*mp, 0, 0);
86 Py_END_ALLOW_THREADS
87 if (status == PY_LOCK_ACQUIRED)
88 {
89 PyThread_release_lock(*mp);
90 return 0;
91 }
92 return 1;
93 }
94
95 #else // Python 3.13 and above
96
97 #define BACKPORTSZSTD_LOCK PyMutex
98 #define BACKPORTSZSTD_LOCK_allocate() ((PyMutex){0})
99 #define BACKPORTSZSTD_LOCK_isError(l) (0)
100 #define BACKPORTSZSTD_LOCK_lock PyMutex_Lock
101 #define BACKPORTSZSTD_LOCK_unlock PyMutex_Unlock
102 #define BACKPORTSZSTD_LOCK_free(l)
103 #if PY_VERSION_HEX < 0x030E0000 // Python 3.13 and below
104 static inline int BACKPORTSZSTD_LOCK_isLocked(PyMutex *lp)
105 {
106 return (_Py_atomic_load_uint8(&lp->_bits) & _Py_LOCKED) != 0;
107 }
108 #else // Python 3.14 and above
109 #define BACKPORTSZSTD_LOCK_isLocked PyMutex_IsLocked
110 #endif
111
112 #endif /* !BACKPORTSZSTD_LOCK */
113
114 #endif /* !BACKPORTS_ZSTD_EDITS_H */