Qucs-S S-parameter Viewer & RF Synthesis Tools
Loading...
Searching...
No Matches
greenlet_msvc_compat.hpp
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
29// This file is only included on 3.14+
30
31extern "C" {
32
33// pycore_code.h ----------------
34#define _PyCode_CODE(CO) _Py_RVALUE((_Py_CODEUNIT *)(CO)->co_code_adaptive)
35// End pycore_code.h ----------
36
37// pycore_interpframe.h ----------
38#if !defined(Py_GIL_DISABLED) && defined(Py_STACKREF_DEBUG)
39
40#define Py_TAG_BITS 0
41#else
42#define Py_TAG_BITS ((uintptr_t)1)
43#define Py_TAG_DEFERRED (1)
44#endif
45
46
47static const _PyStackRef PyStackRef_NULL = { .bits = Py_TAG_DEFERRED};
48#define PyStackRef_IsNull(stackref) ((stackref).bits == PyStackRef_NULL.bits)
49
50static inline PyObject *
51PyStackRef_AsPyObjectBorrow(_PyStackRef stackref)
52{
53 PyObject *cleared = ((PyObject *)((stackref).bits & (~Py_TAG_BITS)));
54 return cleared;
55}
56
57static inline PyCodeObject *_PyFrame_GetCode(_PyInterpreterFrame *f) {
58 assert(!PyStackRef_IsNull(f->f_executable));
59 PyObject *executable = PyStackRef_AsPyObjectBorrow(f->f_executable);
60 assert(PyCode_Check(executable));
61 return (PyCodeObject *)executable;
62}
63
64
65static inline _Py_CODEUNIT *
66_PyFrame_GetBytecode(_PyInterpreterFrame *f)
67{
68#ifdef Py_GIL_DISABLED
69 PyCodeObject *co = _PyFrame_GetCode(f);
70 _PyCodeArray *tlbc = _PyCode_GetTLBCArray(co);
71 assert(f->tlbc_index >= 0 && f->tlbc_index < tlbc->size);
72 return (_Py_CODEUNIT *)tlbc->entries[f->tlbc_index];
73#else
74 return _PyCode_CODE(_PyFrame_GetCode(f));
75#endif
76}
77
78static inline bool //_Py_NO_SANITIZE_THREAD
79_PyFrame_IsIncomplete(_PyInterpreterFrame *frame)
80{
81 if (frame->owner >= FRAME_OWNED_BY_INTERPRETER) {
82 return true;
83 }
84 return frame->owner != FRAME_OWNED_BY_GENERATOR &&
85 frame->instr_ptr < _PyFrame_GetBytecode(frame) +
86 _PyFrame_GetCode(frame)->_co_firsttraceable;
87}
88// pycore_interpframe.h ----------
89
90}
91#endif // GREENLET_MSVC_COMPAT_HPP