greenlet_msvc_compat.hpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. #ifdef Py_GIL_DISABLED
  33. static inline _PyCodeArray *
  34. _PyCode_GetTLBCArray(PyCodeObject *co)
  35. {
  36. return _Py_STATIC_CAST(_PyCodeArray *,
  37. _Py_atomic_load_ptr_acquire(&co->co_tlbc));
  38. }
  39. #endif
  40. // End pycore_code.h ----------
  41. // pycore_interpframe.h ----------
  42. #if !defined(Py_GIL_DISABLED) && defined(Py_STACKREF_DEBUG)
  43. #define Py_TAG_BITS 0
  44. #else
  45. #define Py_TAG_BITS ((uintptr_t)1)
  46. #define Py_TAG_DEFERRED (1)
  47. #endif
  48. static const _PyStackRef PyStackRef_NULL = { .bits = Py_TAG_DEFERRED};
  49. #define PyStackRef_IsNull(stackref) ((stackref).bits == PyStackRef_NULL.bits)
  50. static inline PyObject *
  51. PyStackRef_AsPyObjectBorrow(_PyStackRef stackref)
  52. {
  53. PyObject *cleared = ((PyObject *)((stackref).bits & (~Py_TAG_BITS)));
  54. return cleared;
  55. }
  56. static inline PyCodeObject *_PyFrame_GetCode(_PyInterpreterFrame *f) {
  57. assert(!PyStackRef_IsNull(f->f_executable));
  58. PyObject *executable = PyStackRef_AsPyObjectBorrow(f->f_executable);
  59. assert(PyCode_Check(executable));
  60. return (PyCodeObject *)executable;
  61. }
  62. static inline _Py_CODEUNIT *
  63. _PyFrame_GetBytecode(_PyInterpreterFrame *f)
  64. {
  65. #ifdef Py_GIL_DISABLED
  66. PyCodeObject *co = _PyFrame_GetCode(f);
  67. _PyCodeArray *tlbc = _PyCode_GetTLBCArray(co);
  68. assert(f->tlbc_index >= 0 && f->tlbc_index < tlbc->size);
  69. return (_Py_CODEUNIT *)tlbc->entries[f->tlbc_index];
  70. #else
  71. return _PyCode_CODE(_PyFrame_GetCode(f));
  72. #endif
  73. }
  74. static inline bool //_Py_NO_SANITIZE_THREAD
  75. _PyFrame_IsIncomplete(_PyInterpreterFrame *frame)
  76. {
  77. if (frame->owner >= FRAME_OWNED_BY_INTERPRETER) {
  78. return true;
  79. }
  80. return frame->owner != FRAME_OWNED_BY_GENERATOR &&
  81. frame->instr_ptr < _PyFrame_GetBytecode(frame) +
  82. _PyFrame_GetCode(frame)->_co_firsttraceable;
  83. }
  84. // pycore_interpframe.h ----------
  85. }
  86. #endif // GREENLET_MSVC_COMPAT_HPP