pythoncapi_compat.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. // Header file providing new C API functions to old Python versions.
  2. //
  3. // File distributed under the Zero Clause BSD (0BSD) license.
  4. // Copyright Contributors to the pythoncapi_compat project.
  5. //
  6. // Homepage:
  7. // https://github.com/python/pythoncapi_compat
  8. //
  9. // Latest version:
  10. // https://raw.githubusercontent.com/python/pythoncapi_compat/master/pythoncapi_compat.h
  11. //
  12. // SPDX-License-Identifier: 0BSD
  13. #ifndef PYTHONCAPI_COMPAT
  14. #define PYTHONCAPI_COMPAT
  15. #ifdef __cplusplus
  16. extern "C" {
  17. #endif
  18. #include <Python.h>
  19. #include "frameobject.h" // PyFrameObject, PyFrame_GetBack()
  20. // Compatibility with Visual Studio 2013 and older which don't support
  21. // the inline keyword in C (only in C++): use __inline instead.
  22. #if (defined(_MSC_VER) && _MSC_VER < 1900 \
  23. && !defined(__cplusplus) && !defined(inline))
  24. # define PYCAPI_COMPAT_STATIC_INLINE(TYPE) static __inline TYPE
  25. #else
  26. # define PYCAPI_COMPAT_STATIC_INLINE(TYPE) static inline TYPE
  27. #endif
  28. // C++ compatibility: _Py_CAST() and _Py_NULL
  29. #ifndef _Py_CAST
  30. # ifdef __cplusplus
  31. # define _Py_CAST(type, expr) \
  32. const_cast<type>(reinterpret_cast<const type>(expr))
  33. # else
  34. # define _Py_CAST(type, expr) ((type)(expr))
  35. # endif
  36. #endif
  37. #ifndef _Py_NULL
  38. # ifdef __cplusplus
  39. # define _Py_NULL nullptr
  40. # else
  41. # define _Py_NULL NULL
  42. # endif
  43. #endif
  44. // Cast argument to PyObject* type.
  45. #ifndef _PyObject_CAST
  46. # define _PyObject_CAST(op) _Py_CAST(PyObject*, op)
  47. #endif
  48. // bpo-42262 added Py_NewRef() to Python 3.10.0a3
  49. #if PY_VERSION_HEX < 0x030A00A3 && !defined(Py_NewRef)
  50. PYCAPI_COMPAT_STATIC_INLINE(PyObject*)
  51. _Py_NewRef(PyObject *obj)
  52. {
  53. Py_INCREF(obj);
  54. return obj;
  55. }
  56. #define Py_NewRef(obj) _Py_NewRef(_PyObject_CAST(obj))
  57. #endif
  58. // bpo-42262 added Py_XNewRef() to Python 3.10.0a3
  59. #if PY_VERSION_HEX < 0x030A00A3 && !defined(Py_XNewRef)
  60. PYCAPI_COMPAT_STATIC_INLINE(PyObject*)
  61. _Py_XNewRef(PyObject *obj)
  62. {
  63. Py_XINCREF(obj);
  64. return obj;
  65. }
  66. #define Py_XNewRef(obj) _Py_XNewRef(_PyObject_CAST(obj))
  67. #endif
  68. // bpo-39573 added Py_SET_REFCNT() to Python 3.9.0a4
  69. #if PY_VERSION_HEX < 0x030900A4 && !defined(Py_SET_REFCNT)
  70. PYCAPI_COMPAT_STATIC_INLINE(void)
  71. _Py_SET_REFCNT(PyObject *ob, Py_ssize_t refcnt)
  72. {
  73. ob->ob_refcnt = refcnt;
  74. }
  75. #define Py_SET_REFCNT(ob, refcnt) _Py_SET_REFCNT(_PyObject_CAST(ob), refcnt)
  76. #endif
  77. // Py_SETREF() and Py_XSETREF() were added to Python 3.5.2.
  78. // It is excluded from the limited C API.
  79. #if (PY_VERSION_HEX < 0x03050200 && !defined(Py_SETREF)) && !defined(Py_LIMITED_API)
  80. #define Py_SETREF(op, op2) \
  81. do { \
  82. PyObject *_py_tmp = _PyObject_CAST(op); \
  83. (op) = (op2); \
  84. Py_DECREF(_py_tmp); \
  85. } while (0)
  86. #define Py_XSETREF(op, op2) \
  87. do { \
  88. PyObject *_py_tmp = _PyObject_CAST(op); \
  89. (op) = (op2); \
  90. Py_XDECREF(_py_tmp); \
  91. } while (0)
  92. #endif
  93. // bpo-43753 added Py_Is(), Py_IsNone(), Py_IsTrue() and Py_IsFalse()
  94. // to Python 3.10.0b1.
  95. #if PY_VERSION_HEX < 0x030A00B1 && !defined(Py_Is)
  96. # define Py_Is(x, y) ((x) == (y))
  97. #endif
  98. #if PY_VERSION_HEX < 0x030A00B1 && !defined(Py_IsNone)
  99. # define Py_IsNone(x) Py_Is(x, Py_None)
  100. #endif
  101. #if PY_VERSION_HEX < 0x030A00B1 && !defined(Py_IsTrue)
  102. # define Py_IsTrue(x) Py_Is(x, Py_True)
  103. #endif
  104. #if PY_VERSION_HEX < 0x030A00B1 && !defined(Py_IsFalse)
  105. # define Py_IsFalse(x) Py_Is(x, Py_False)
  106. #endif
  107. // bpo-39573 added Py_SET_TYPE() to Python 3.9.0a4
  108. #if PY_VERSION_HEX < 0x030900A4 && !defined(Py_SET_TYPE)
  109. PYCAPI_COMPAT_STATIC_INLINE(void)
  110. _Py_SET_TYPE(PyObject *ob, PyTypeObject *type)
  111. {
  112. ob->ob_type = type;
  113. }
  114. #define Py_SET_TYPE(ob, type) _Py_SET_TYPE(_PyObject_CAST(ob), type)
  115. #endif
  116. // bpo-39573 added Py_SET_SIZE() to Python 3.9.0a4
  117. #if PY_VERSION_HEX < 0x030900A4 && !defined(Py_SET_SIZE)
  118. PYCAPI_COMPAT_STATIC_INLINE(void)
  119. _Py_SET_SIZE(PyVarObject *ob, Py_ssize_t size)
  120. {
  121. ob->ob_size = size;
  122. }
  123. #define Py_SET_SIZE(ob, size) _Py_SET_SIZE((PyVarObject*)(ob), size)
  124. #endif
  125. // bpo-40421 added PyFrame_GetCode() to Python 3.9.0b1
  126. #if PY_VERSION_HEX < 0x030900B1
  127. PYCAPI_COMPAT_STATIC_INLINE(PyCodeObject*)
  128. PyFrame_GetCode(PyFrameObject *frame)
  129. {
  130. assert(frame != _Py_NULL);
  131. assert(frame->f_code != _Py_NULL);
  132. return _Py_CAST(PyCodeObject*, Py_NewRef(frame->f_code));
  133. }
  134. #endif
  135. PYCAPI_COMPAT_STATIC_INLINE(PyCodeObject*)
  136. _PyFrame_GetCodeBorrow(PyFrameObject *frame)
  137. {
  138. PyCodeObject *code = PyFrame_GetCode(frame);
  139. Py_DECREF(code);
  140. return code;
  141. }
  142. // bpo-40421 added PyFrame_GetBack() to Python 3.9.0b1
  143. #if PY_VERSION_HEX < 0x030900B1 && !defined(PYPY_VERSION)
  144. PYCAPI_COMPAT_STATIC_INLINE(PyFrameObject*)
  145. PyFrame_GetBack(PyFrameObject *frame)
  146. {
  147. assert(frame != _Py_NULL);
  148. return _Py_CAST(PyFrameObject*, Py_XNewRef(frame->f_back));
  149. }
  150. #endif
  151. #if !defined(PYPY_VERSION)
  152. PYCAPI_COMPAT_STATIC_INLINE(PyFrameObject*)
  153. _PyFrame_GetBackBorrow(PyFrameObject *frame)
  154. {
  155. PyFrameObject *back = PyFrame_GetBack(frame);
  156. Py_XDECREF(back);
  157. return back;
  158. }
  159. #endif
  160. // bpo-40421 added PyFrame_GetLocals() to Python 3.11.0a7
  161. #if PY_VERSION_HEX < 0x030B00A7 && !defined(PYPY_VERSION)
  162. PYCAPI_COMPAT_STATIC_INLINE(PyObject*)
  163. PyFrame_GetLocals(PyFrameObject *frame)
  164. {
  165. #if PY_VERSION_HEX >= 0x030400B1
  166. if (PyFrame_FastToLocalsWithError(frame) < 0) {
  167. return NULL;
  168. }
  169. #else
  170. PyFrame_FastToLocals(frame);
  171. #endif
  172. return Py_NewRef(frame->f_locals);
  173. }
  174. #endif
  175. // bpo-40421 added PyFrame_GetGlobals() to Python 3.11.0a7
  176. #if PY_VERSION_HEX < 0x030B00A7 && !defined(PYPY_VERSION)
  177. PYCAPI_COMPAT_STATIC_INLINE(PyObject*)
  178. PyFrame_GetGlobals(PyFrameObject *frame)
  179. {
  180. return Py_NewRef(frame->f_globals);
  181. }
  182. #endif
  183. // bpo-40421 added PyFrame_GetBuiltins() to Python 3.11.0a7
  184. #if PY_VERSION_HEX < 0x030B00A7 && !defined(PYPY_VERSION)
  185. PYCAPI_COMPAT_STATIC_INLINE(PyObject*)
  186. PyFrame_GetBuiltins(PyFrameObject *frame)
  187. {
  188. return Py_NewRef(frame->f_builtins);
  189. }
  190. #endif
  191. // bpo-40421 added PyFrame_GetLasti() to Python 3.11.0b1
  192. #if PY_VERSION_HEX < 0x030B00B1 && !defined(PYPY_VERSION)
  193. PYCAPI_COMPAT_STATIC_INLINE(int)
  194. PyFrame_GetLasti(PyFrameObject *frame)
  195. {
  196. #if PY_VERSION_HEX >= 0x030A00A7
  197. // bpo-27129: Since Python 3.10.0a7, f_lasti is an instruction offset,
  198. // not a bytes offset anymore. Python uses 16-bit "wordcode" (2 bytes)
  199. // instructions.
  200. if (frame->f_lasti < 0) {
  201. return -1;
  202. }
  203. return frame->f_lasti * 2;
  204. #else
  205. return frame->f_lasti;
  206. #endif
  207. }
  208. #endif
  209. // bpo-39947 added PyThreadState_GetInterpreter() to Python 3.9.0a5
  210. #if PY_VERSION_HEX < 0x030900A5
  211. PYCAPI_COMPAT_STATIC_INLINE(PyInterpreterState *)
  212. PyThreadState_GetInterpreter(PyThreadState *tstate)
  213. {
  214. assert(tstate != _Py_NULL);
  215. return tstate->interp;
  216. }
  217. #endif
  218. // bpo-40429 added PyThreadState_GetFrame() to Python 3.9.0b1
  219. #if PY_VERSION_HEX < 0x030900B1 && !defined(PYPY_VERSION)
  220. PYCAPI_COMPAT_STATIC_INLINE(PyFrameObject*)
  221. PyThreadState_GetFrame(PyThreadState *tstate)
  222. {
  223. assert(tstate != _Py_NULL);
  224. return _Py_CAST(PyFrameObject *, Py_XNewRef(tstate->frame));
  225. }
  226. #endif
  227. #if !defined(PYPY_VERSION)
  228. PYCAPI_COMPAT_STATIC_INLINE(PyFrameObject*)
  229. _PyThreadState_GetFrameBorrow(PyThreadState *tstate)
  230. {
  231. PyFrameObject *frame = PyThreadState_GetFrame(tstate);
  232. Py_XDECREF(frame);
  233. return frame;
  234. }
  235. #endif
  236. // bpo-39947 added PyInterpreterState_Get() to Python 3.9.0a5
  237. #if PY_VERSION_HEX < 0x030900A5
  238. PYCAPI_COMPAT_STATIC_INLINE(PyInterpreterState*)
  239. PyInterpreterState_Get(void)
  240. {
  241. PyThreadState *tstate;
  242. PyInterpreterState *interp;
  243. tstate = PyThreadState_GET();
  244. if (tstate == _Py_NULL) {
  245. Py_FatalError("GIL released (tstate is NULL)");
  246. }
  247. interp = tstate->interp;
  248. if (interp == _Py_NULL) {
  249. Py_FatalError("no current interpreter");
  250. }
  251. return interp;
  252. }
  253. #endif
  254. // bpo-39947 added PyInterpreterState_Get() to Python 3.9.0a6
  255. #if 0x030700A1 <= PY_VERSION_HEX && PY_VERSION_HEX < 0x030900A6 && !defined(PYPY_VERSION)
  256. PYCAPI_COMPAT_STATIC_INLINE(uint64_t)
  257. PyThreadState_GetID(PyThreadState *tstate)
  258. {
  259. assert(tstate != _Py_NULL);
  260. return tstate->id;
  261. }
  262. #endif
  263. // bpo-43760 added PyThreadState_EnterTracing() to Python 3.11.0a2
  264. #if PY_VERSION_HEX < 0x030B00A2 && !defined(PYPY_VERSION)
  265. PYCAPI_COMPAT_STATIC_INLINE(void)
  266. PyThreadState_EnterTracing(PyThreadState *tstate)
  267. {
  268. tstate->tracing++;
  269. #if PY_VERSION_HEX >= 0x030A00A1
  270. tstate->cframe->use_tracing = 0;
  271. #else
  272. tstate->use_tracing = 0;
  273. #endif
  274. }
  275. #endif
  276. // bpo-43760 added PyThreadState_LeaveTracing() to Python 3.11.0a2
  277. #if PY_VERSION_HEX < 0x030B00A2 && !defined(PYPY_VERSION)
  278. PYCAPI_COMPAT_STATIC_INLINE(void)
  279. PyThreadState_LeaveTracing(PyThreadState *tstate)
  280. {
  281. int use_tracing = (tstate->c_tracefunc != _Py_NULL
  282. || tstate->c_profilefunc != _Py_NULL);
  283. tstate->tracing--;
  284. #if PY_VERSION_HEX >= 0x030A00A1
  285. tstate->cframe->use_tracing = use_tracing;
  286. #else
  287. tstate->use_tracing = use_tracing;
  288. #endif
  289. }
  290. #endif
  291. // bpo-37194 added PyObject_CallNoArgs() to Python 3.9.0a1
  292. #if PY_VERSION_HEX < 0x030900A1
  293. PYCAPI_COMPAT_STATIC_INLINE(PyObject*)
  294. PyObject_CallNoArgs(PyObject *func)
  295. {
  296. return PyObject_CallFunctionObjArgs(func, NULL);
  297. }
  298. #endif
  299. // bpo-39245 made PyObject_CallOneArg() public (previously called
  300. // _PyObject_CallOneArg) in Python 3.9.0a4
  301. #if PY_VERSION_HEX < 0x030900A4
  302. PYCAPI_COMPAT_STATIC_INLINE(PyObject*)
  303. PyObject_CallOneArg(PyObject *func, PyObject *arg)
  304. {
  305. return PyObject_CallFunctionObjArgs(func, arg, NULL);
  306. }
  307. #endif
  308. // bpo-1635741 added PyModule_AddObjectRef() to Python 3.10.0a3
  309. #if PY_VERSION_HEX < 0x030A00A3
  310. PYCAPI_COMPAT_STATIC_INLINE(int)
  311. PyModule_AddObjectRef(PyObject *module, const char *name, PyObject *value)
  312. {
  313. int res;
  314. Py_XINCREF(value);
  315. res = PyModule_AddObject(module, name, value);
  316. if (res < 0) {
  317. Py_XDECREF(value);
  318. }
  319. return res;
  320. }
  321. #endif
  322. // bpo-40024 added PyModule_AddType() to Python 3.9.0a5
  323. #if PY_VERSION_HEX < 0x030900A5
  324. PYCAPI_COMPAT_STATIC_INLINE(int)
  325. PyModule_AddType(PyObject *module, PyTypeObject *type)
  326. {
  327. const char *name, *dot;
  328. if (PyType_Ready(type) < 0) {
  329. return -1;
  330. }
  331. // inline _PyType_Name()
  332. name = type->tp_name;
  333. assert(name != _Py_NULL);
  334. dot = strrchr(name, '.');
  335. if (dot != _Py_NULL) {
  336. name = dot + 1;
  337. }
  338. return PyModule_AddObjectRef(module, name, _PyObject_CAST(type));
  339. }
  340. #endif
  341. // bpo-40241 added PyObject_GC_IsTracked() to Python 3.9.0a6.
  342. // bpo-4688 added _PyObject_GC_IS_TRACKED() to Python 2.7.0a2.
  343. #if PY_VERSION_HEX < 0x030900A6 && !defined(PYPY_VERSION)
  344. PYCAPI_COMPAT_STATIC_INLINE(int)
  345. PyObject_GC_IsTracked(PyObject* obj)
  346. {
  347. return (PyObject_IS_GC(obj) && _PyObject_GC_IS_TRACKED(obj));
  348. }
  349. #endif
  350. // bpo-40241 added PyObject_GC_IsFinalized() to Python 3.9.0a6.
  351. // bpo-18112 added _PyGCHead_FINALIZED() to Python 3.4.0 final.
  352. #if PY_VERSION_HEX < 0x030900A6 && PY_VERSION_HEX >= 0x030400F0 && !defined(PYPY_VERSION)
  353. PYCAPI_COMPAT_STATIC_INLINE(int)
  354. PyObject_GC_IsFinalized(PyObject *obj)
  355. {
  356. PyGC_Head *gc = _Py_CAST(PyGC_Head*, obj) - 1;
  357. return (PyObject_IS_GC(obj) && _PyGCHead_FINALIZED(gc));
  358. }
  359. #endif
  360. // bpo-39573 added Py_IS_TYPE() to Python 3.9.0a4
  361. #if PY_VERSION_HEX < 0x030900A4 && !defined(Py_IS_TYPE)
  362. PYCAPI_COMPAT_STATIC_INLINE(int)
  363. _Py_IS_TYPE(PyObject *ob, PyTypeObject *type) {
  364. return Py_TYPE(ob) == type;
  365. }
  366. #define Py_IS_TYPE(ob, type) _Py_IS_TYPE(_PyObject_CAST(ob), type)
  367. #endif
  368. // bpo-46906 added PyFloat_Pack2() and PyFloat_Unpack2() to Python 3.11a7.
  369. // bpo-11734 added _PyFloat_Pack2() and _PyFloat_Unpack2() to Python 3.6.0b1.
  370. // Python 3.11a2 moved _PyFloat_Pack2() and _PyFloat_Unpack2() to the internal
  371. // C API: Python 3.11a2-3.11a6 versions are not supported.
  372. #if 0x030600B1 <= PY_VERSION_HEX && PY_VERSION_HEX <= 0x030B00A1 && !defined(PYPY_VERSION)
  373. PYCAPI_COMPAT_STATIC_INLINE(int)
  374. PyFloat_Pack2(double x, char *p, int le)
  375. { return _PyFloat_Pack2(x, (unsigned char*)p, le); }
  376. PYCAPI_COMPAT_STATIC_INLINE(double)
  377. PyFloat_Unpack2(const char *p, int le)
  378. { return _PyFloat_Unpack2((const unsigned char *)p, le); }
  379. #endif
  380. // bpo-46906 added PyFloat_Pack4(), PyFloat_Pack8(), PyFloat_Unpack4() and
  381. // PyFloat_Unpack8() to Python 3.11a7.
  382. // Python 3.11a2 moved _PyFloat_Pack4(), _PyFloat_Pack8(), _PyFloat_Unpack4()
  383. // and _PyFloat_Unpack8() to the internal C API: Python 3.11a2-3.11a6 versions
  384. // are not supported.
  385. #if PY_VERSION_HEX <= 0x030B00A1 && !defined(PYPY_VERSION)
  386. PYCAPI_COMPAT_STATIC_INLINE(int)
  387. PyFloat_Pack4(double x, char *p, int le)
  388. { return _PyFloat_Pack4(x, (unsigned char*)p, le); }
  389. PYCAPI_COMPAT_STATIC_INLINE(int)
  390. PyFloat_Pack8(double x, char *p, int le)
  391. { return _PyFloat_Pack8(x, (unsigned char*)p, le); }
  392. PYCAPI_COMPAT_STATIC_INLINE(double)
  393. PyFloat_Unpack4(const char *p, int le)
  394. { return _PyFloat_Unpack4((const unsigned char *)p, le); }
  395. PYCAPI_COMPAT_STATIC_INLINE(double)
  396. PyFloat_Unpack8(const char *p, int le)
  397. { return _PyFloat_Unpack8((const unsigned char *)p, le); }
  398. #endif
  399. // gh-92154 added PyCode_GetCode() to Python 3.11.0b1
  400. #if PY_VERSION_HEX < 0x030B00B1 && !defined(PYPY_VERSION)
  401. PYCAPI_COMPAT_STATIC_INLINE(PyObject*)
  402. PyCode_GetCode(PyCodeObject *code)
  403. {
  404. return Py_NewRef(code->co_code);
  405. }
  406. #endif
  407. // Py_UNUSED() was added to Python 3.4.0b2.
  408. #if PY_VERSION_HEX < 0x030400B2 && !defined(Py_UNUSED)
  409. # if defined(__GNUC__) || defined(__clang__)
  410. # define Py_UNUSED(name) _unused_ ## name __attribute__((unused))
  411. # else
  412. # define Py_UNUSED(name) _unused_ ## name
  413. # endif
  414. #endif
  415. #ifdef __cplusplus
  416. }
  417. #endif
  418. #endif // PYTHONCAPI_COMPAT