No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 

385 líneas
10 KiB

  1. // Header file providing new functions of the Python C API to old Python
  2. // versions.
  3. //
  4. // File distributed under the MIT license.
  5. //
  6. // Copyright Contributors to the pythoncapi_compat project.
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining a copy
  9. // of this software and associated documentation files (the "Software"), to deal
  10. // in the Software without restriction, including without limitation the rights
  11. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. // copies of the Software, and to permit persons to whom the Software is
  13. // furnished to do so, subject to the following conditions:
  14. //
  15. // The above copyright notice and this permission notice shall be included in all
  16. // copies or substantial portions of the Software.
  17. //
  18. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  24. // SOFTWARE.
  25. //
  26. // Homepage:
  27. // https://github.com/pythoncapi/pythoncapi_compat
  28. //
  29. // Latest version:
  30. // https://raw.githubusercontent.com/pythoncapi/pythoncapi_compat/master/pythoncapi_compat.h
  31. //
  32. // SPDX-License-Identifier: MIT
  33. #ifndef PYTHONCAPI_COMPAT
  34. #define PYTHONCAPI_COMPAT
  35. #ifdef __cplusplus
  36. extern "C" {
  37. #endif
  38. #include <Python.h>
  39. #include "frameobject.h" // PyFrameObject, PyFrame_GetBack()
  40. // Compatibility with Visual Studio 2013 and older which don't support
  41. // the inline keyword in C (only in C++): use __inline instead.
  42. #if (defined(_MSC_VER) && _MSC_VER < 1900 \
  43. && !defined(__cplusplus) && !defined(inline))
  44. # define inline __inline
  45. # define PYTHONCAPI_COMPAT_MSC_INLINE
  46. // These two macros are undefined at the end of this file
  47. #endif
  48. // Cast argument to PyObject* type.
  49. #ifndef _PyObject_CAST
  50. # define _PyObject_CAST(op) ((PyObject*)(op))
  51. #endif
  52. #ifndef _PyObject_CAST_CONST
  53. # define _PyObject_CAST_CONST(op) ((const PyObject*)(op))
  54. #endif
  55. // bpo-42262 added Py_NewRef() to Python 3.10.0a3
  56. #if PY_VERSION_HEX < 0x030A00A3 && !defined(Py_NewRef)
  57. static inline PyObject* _Py_NewRef(PyObject *obj)
  58. {
  59. Py_INCREF(obj);
  60. return obj;
  61. }
  62. #define Py_NewRef(obj) _Py_NewRef(_PyObject_CAST(obj))
  63. #endif
  64. // bpo-42262 added Py_XNewRef() to Python 3.10.0a3
  65. #if PY_VERSION_HEX < 0x030A00A3 && !defined(Py_XNewRef)
  66. static inline PyObject* _Py_XNewRef(PyObject *obj)
  67. {
  68. Py_XINCREF(obj);
  69. return obj;
  70. }
  71. #define Py_XNewRef(obj) _Py_XNewRef(_PyObject_CAST(obj))
  72. #endif
  73. // See https://bugs.python.org/issue42522
  74. #if !defined(_Py_StealRef)
  75. static inline PyObject* __Py_StealRef(PyObject *obj)
  76. {
  77. Py_DECREF(obj);
  78. return obj;
  79. }
  80. #define _Py_StealRef(obj) __Py_StealRef(_PyObject_CAST(obj))
  81. #endif
  82. // See https://bugs.python.org/issue42522
  83. #if !defined(_Py_XStealRef)
  84. static inline PyObject* __Py_XStealRef(PyObject *obj)
  85. {
  86. Py_XDECREF(obj);
  87. return obj;
  88. }
  89. #define _Py_XStealRef(obj) __Py_XStealRef(_PyObject_CAST(obj))
  90. #endif
  91. // bpo-39573 added Py_SET_REFCNT() to Python 3.9.0a4
  92. #if PY_VERSION_HEX < 0x030900A4 && !defined(Py_SET_REFCNT)
  93. static inline void _Py_SET_REFCNT(PyObject *ob, Py_ssize_t refcnt)
  94. {
  95. ob->ob_refcnt = refcnt;
  96. }
  97. #define Py_SET_REFCNT(ob, refcnt) _Py_SET_REFCNT(_PyObject_CAST(ob), refcnt)
  98. #endif
  99. // Py_SETREF() and Py_XSETREF() were added to Python 3.5.2.
  100. // It is excluded from the limited C API.
  101. #if (PY_VERSION_HEX < 0x03050200 && !defined(Py_SETREF)) && !defined(Py_LIMITED_API)
  102. #define Py_SETREF(op, op2) \
  103. do { \
  104. PyObject *_py_tmp = _PyObject_CAST(op); \
  105. (op) = (op2); \
  106. Py_DECREF(_py_tmp); \
  107. } while (0)
  108. #define Py_XSETREF(op, op2) \
  109. do { \
  110. PyObject *_py_tmp = _PyObject_CAST(op); \
  111. (op) = (op2); \
  112. Py_XDECREF(_py_tmp); \
  113. } while (0)
  114. #endif
  115. // bpo-43753 added Py_Is(), Py_IsNone(), Py_IsTrue() and Py_IsFalse()
  116. // to Python 3.10.0b1.
  117. #if PY_VERSION_HEX < 0x030A00B1 && !defined(Py_Is)
  118. # define Py_Is(x, y) ((x) == (y))
  119. #endif
  120. #if PY_VERSION_HEX < 0x030A00B1 && !defined(Py_IsNone)
  121. # define Py_IsNone(x) Py_Is(x, Py_None)
  122. #endif
  123. #if PY_VERSION_HEX < 0x030A00B1 && !defined(Py_IsTrue)
  124. # define Py_IsTrue(x) Py_Is(x, Py_True)
  125. #endif
  126. #if PY_VERSION_HEX < 0x030A00B1 && !defined(Py_IsFalse)
  127. # define Py_IsFalse(x) Py_Is(x, Py_False)
  128. #endif
  129. // bpo-39573 added Py_SET_TYPE() to Python 3.9.0a4
  130. #if PY_VERSION_HEX < 0x030900A4 && !defined(Py_SET_TYPE)
  131. static inline void
  132. _Py_SET_TYPE(PyObject *ob, PyTypeObject *type)
  133. {
  134. ob->ob_type = type;
  135. }
  136. #define Py_SET_TYPE(ob, type) _Py_SET_TYPE(_PyObject_CAST(ob), type)
  137. #endif
  138. // bpo-39573 added Py_SET_SIZE() to Python 3.9.0a4
  139. #if PY_VERSION_HEX < 0x030900A4 && !defined(Py_SET_SIZE)
  140. static inline void
  141. _Py_SET_SIZE(PyVarObject *ob, Py_ssize_t size)
  142. {
  143. ob->ob_size = size;
  144. }
  145. #define Py_SET_SIZE(ob, size) _Py_SET_SIZE((PyVarObject*)(ob), size)
  146. #endif
  147. // bpo-40421 added PyFrame_GetCode() to Python 3.9.0b1
  148. #if PY_VERSION_HEX < 0x030900B1
  149. static inline PyCodeObject*
  150. PyFrame_GetCode(PyFrameObject *frame)
  151. {
  152. assert(frame != NULL);
  153. assert(frame->f_code != NULL);
  154. return (PyCodeObject*)Py_NewRef(frame->f_code);
  155. }
  156. #endif
  157. static inline PyCodeObject*
  158. _PyFrame_GetCodeBorrow(PyFrameObject *frame)
  159. {
  160. return (PyCodeObject *)_Py_StealRef(PyFrame_GetCode(frame));
  161. }
  162. // bpo-40421 added PyFrame_GetCode() to Python 3.9.0b1
  163. #if PY_VERSION_HEX < 0x030900B1 && !defined(PYPY_VERSION)
  164. static inline PyFrameObject*
  165. PyFrame_GetBack(PyFrameObject *frame)
  166. {
  167. assert(frame != NULL);
  168. return (PyFrameObject*)Py_XNewRef(frame->f_back);
  169. }
  170. #endif
  171. #if !defined(PYPY_VERSION)
  172. static inline PyFrameObject*
  173. _PyFrame_GetBackBorrow(PyFrameObject *frame)
  174. {
  175. return (PyFrameObject *)_Py_XStealRef(PyFrame_GetBack(frame));
  176. }
  177. #endif
  178. // bpo-39947 added PyThreadState_GetInterpreter() to Python 3.9.0a5
  179. #if PY_VERSION_HEX < 0x030900A5
  180. static inline PyInterpreterState *
  181. PyThreadState_GetInterpreter(PyThreadState *tstate)
  182. {
  183. assert(tstate != NULL);
  184. return tstate->interp;
  185. }
  186. #endif
  187. // bpo-40429 added PyThreadState_GetFrame() to Python 3.9.0b1
  188. #if PY_VERSION_HEX < 0x030900B1 && !defined(PYPY_VERSION)
  189. static inline PyFrameObject*
  190. PyThreadState_GetFrame(PyThreadState *tstate)
  191. {
  192. assert(tstate != NULL);
  193. return (PyFrameObject *)Py_XNewRef(tstate->frame);
  194. }
  195. #endif
  196. #if !defined(PYPY_VERSION)
  197. static inline PyFrameObject*
  198. _PyThreadState_GetFrameBorrow(PyThreadState *tstate)
  199. {
  200. return (PyFrameObject *)_Py_XStealRef(PyThreadState_GetFrame(tstate));
  201. }
  202. #endif
  203. // bpo-39947 added PyInterpreterState_Get() to Python 3.9.0a5
  204. #if PY_VERSION_HEX < 0x030900A5
  205. static inline PyInterpreterState *
  206. PyInterpreterState_Get(void)
  207. {
  208. PyThreadState *tstate;
  209. PyInterpreterState *interp;
  210. tstate = PyThreadState_GET();
  211. if (tstate == NULL) {
  212. Py_FatalError("GIL released (tstate is NULL)");
  213. }
  214. interp = tstate->interp;
  215. if (interp == NULL) {
  216. Py_FatalError("no current interpreter");
  217. }
  218. return interp;
  219. }
  220. #endif
  221. // bpo-39947 added PyInterpreterState_Get() to Python 3.9.0a6
  222. #if 0x030700A1 <= PY_VERSION_HEX && PY_VERSION_HEX < 0x030900A6 && !defined(PYPY_VERSION)
  223. static inline uint64_t
  224. PyThreadState_GetID(PyThreadState *tstate)
  225. {
  226. assert(tstate != NULL);
  227. return tstate->id;
  228. }
  229. #endif
  230. // bpo-37194 added PyObject_CallNoArgs() to Python 3.9.0a1
  231. #if PY_VERSION_HEX < 0x030900A1
  232. static inline PyObject*
  233. PyObject_CallNoArgs(PyObject *func)
  234. {
  235. return PyObject_CallFunctionObjArgs(func, NULL);
  236. }
  237. #endif
  238. // bpo-39245 made PyObject_CallOneArg() public (previously called
  239. // _PyObject_CallOneArg) in Python 3.9.0a4
  240. #if PY_VERSION_HEX < 0x030900A4
  241. static inline PyObject*
  242. PyObject_CallOneArg(PyObject *func, PyObject *arg)
  243. {
  244. return PyObject_CallFunctionObjArgs(func, arg, NULL);
  245. }
  246. #endif
  247. // bpo-1635741 added PyModule_AddObjectRef() to Python 3.10.0a3
  248. #if PY_VERSION_HEX < 0x030A00A3
  249. static inline int
  250. PyModule_AddObjectRef(PyObject *module, const char *name, PyObject *value)
  251. {
  252. int res;
  253. Py_XINCREF(value);
  254. res = PyModule_AddObject(module, name, value);
  255. if (res < 0) {
  256. Py_XDECREF(value);
  257. }
  258. return res;
  259. }
  260. #endif
  261. // bpo-40024 added PyModule_AddType() to Python 3.9.0a5
  262. #if PY_VERSION_HEX < 0x030900A5
  263. static inline int
  264. PyModule_AddType(PyObject *module, PyTypeObject *type)
  265. {
  266. const char *name, *dot;
  267. if (PyType_Ready(type) < 0) {
  268. return -1;
  269. }
  270. // inline _PyType_Name()
  271. name = type->tp_name;
  272. assert(name != NULL);
  273. dot = strrchr(name, '.');
  274. if (dot != NULL) {
  275. name = dot + 1;
  276. }
  277. return PyModule_AddObjectRef(module, name, (PyObject *)type);
  278. }
  279. #endif
  280. // bpo-40241 added PyObject_GC_IsTracked() to Python 3.9.0a6.
  281. // bpo-4688 added _PyObject_GC_IS_TRACKED() to Python 2.7.0a2.
  282. #if PY_VERSION_HEX < 0x030900A6 && !defined(PYPY_VERSION)
  283. static inline int
  284. PyObject_GC_IsTracked(PyObject* obj)
  285. {
  286. return (PyObject_IS_GC(obj) && _PyObject_GC_IS_TRACKED(obj));
  287. }
  288. #endif
  289. // bpo-40241 added PyObject_GC_IsFinalized() to Python 3.9.0a6.
  290. // bpo-18112 added _PyGCHead_FINALIZED() to Python 3.4.0 final.
  291. #if PY_VERSION_HEX < 0x030900A6 && PY_VERSION_HEX >= 0x030400F0 && !defined(PYPY_VERSION)
  292. static inline int
  293. PyObject_GC_IsFinalized(PyObject *obj)
  294. {
  295. return (PyObject_IS_GC(obj) && _PyGCHead_FINALIZED((PyGC_Head *)(obj)-1));
  296. }
  297. #endif
  298. // bpo-39573 added Py_IS_TYPE() to Python 3.9.0a4
  299. #if PY_VERSION_HEX < 0x030900A4 && !defined(Py_IS_TYPE)
  300. static inline int
  301. _Py_IS_TYPE(const PyObject *ob, const PyTypeObject *type) {
  302. return ob->ob_type == type;
  303. }
  304. #define Py_IS_TYPE(ob, type) _Py_IS_TYPE(_PyObject_CAST_CONST(ob), type)
  305. #endif
  306. // Py_UNUSED() was added to Python 3.4.0b2.
  307. #if PY_VERSION_HEX < 0x030400B2 && !defined(Py_UNUSED)
  308. # if defined(__GNUC__) || defined(__clang__)
  309. # define Py_UNUSED(name) _unused_ ## name __attribute__((unused))
  310. # else
  311. # define Py_UNUSED(name) _unused_ ## name
  312. # endif
  313. #endif
  314. #ifdef PYTHONCAPI_COMPAT_MSC_INLINE
  315. # undef inline
  316. # undef PYTHONCAPI_COMPAT_MSC_INLINE
  317. #endif
  318. #ifdef __cplusplus
  319. }
  320. #endif
  321. #endif // PYTHONCAPI_COMPAT