greenlet_msvc_compat.hpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #ifndef GREENLET_MSVC_COMPAT_HPP
  2. #define GREENLET_MSVC_COMPAT_HPP
  3. /*
  4. * Support for MSVC on Windows.
  5. *
  6. * Beginning with Python 3.14, some of the internal
  7. * include files we need are not compatible with MSVC
  8. * in C++ mode:
  9. *
  10. * internal\pycore_stackref.h(253): error C4576: a parenthesized type
  11. * followed by an initializer list is a non-standard explicit type conversion syntax
  12. *
  13. * This file is included from ``internal/pycore_interpframe.h``, which
  14. * we need for the ``_PyFrame_IsIncomplete`` API.
  15. *
  16. * Unfortunately, that API is a ``static inline`` function, as are a
  17. * bunch of the functions it calls. The only solution seems to be to
  18. * copy those definitions and the supporting inline functions here.
  19. *
  20. * Now, this makes us VERY fragile to changes in those functions. Because
  21. * they're internal and static, the CPython devs might feel free to change
  22. * them in even minor versions, meaning that we could runtime link and load,
  23. * but still crash. We have that problem on all platforms though. It's just worse
  24. * here because we have to keep copying the updated definitions.
  25. */
  26. #include <Python.h>
  27. #include "greenlet_cpython_compat.hpp"
  28. // This file is only included on 3.14+
  29. extern "C" {
  30. // pycore_code.h ----------------
  31. #define _PyCode_CODE(CO) _Py_RVALUE((_Py_CODEUNIT *)(CO)->co_code_adaptive)
  32. // End pycore_code.h ----------
  33. // pycore_interpframe.h ----------
  34. #if !defined(Py_GIL_DISABLED) && defined(Py_STACKREF_DEBUG)
  35. #define Py_TAG_BITS 0
  36. #else
  37. #define Py_TAG_BITS ((uintptr_t)1)
  38. #define Py_TAG_DEFERRED (1)
  39. #endif
  40. static const _PyStackRef PyStackRef_NULL = { .bits = Py_TAG_DEFERRED};
  41. #define PyStackRef_IsNull(stackref) ((stackref).bits == PyStackRef_NULL.bits)
  42. static inline PyObject *
  43. PyStackRef_AsPyObjectBorrow(_PyStackRef stackref)
  44. {
  45. PyObject *cleared = ((PyObject *)((stackref).bits & (~Py_TAG_BITS)));
  46. return cleared;
  47. }
  48. static inline PyCodeObject *_PyFrame_GetCode(_PyInterpreterFrame *f) {
  49. assert(!PyStackRef_IsNull(f->f_executable));
  50. PyObject *executable = PyStackRef_AsPyObjectBorrow(f->f_executable);
  51. assert(PyCode_Check(executable));
  52. return (PyCodeObject *)executable;
  53. }
  54. static inline _Py_CODEUNIT *
  55. _PyFrame_GetBytecode(_PyInterpreterFrame *f)
  56. {
  57. #ifdef Py_GIL_DISABLED
  58. PyCodeObject *co = _PyFrame_GetCode(f);
  59. _PyCodeArray *tlbc = _PyCode_GetTLBCArray(co);
  60. assert(f->tlbc_index >= 0 && f->tlbc_index < tlbc->size);
  61. return (_Py_CODEUNIT *)tlbc->entries[f->tlbc_index];
  62. #else
  63. return _PyCode_CODE(_PyFrame_GetCode(f));
  64. #endif
  65. }
  66. static inline bool //_Py_NO_SANITIZE_THREAD
  67. _PyFrame_IsIncomplete(_PyInterpreterFrame *frame)
  68. {
  69. if (frame->owner >= FRAME_OWNED_BY_INTERPRETER) {
  70. return true;
  71. }
  72. return frame->owner != FRAME_OWNED_BY_GENERATOR &&
  73. frame->instr_ptr < _PyFrame_GetBytecode(frame) +
  74. _PyFrame_GetCode(frame)->_co_firsttraceable;
  75. }
  76. // pycore_interpframe.h ----------
  77. }
  78. #endif // GREENLET_MSVC_COMPAT_HPP