jonghanko commited on
Commit
6881eaa
·
verified ·
1 Parent(s): 05a19c0

Add files using upload-large-folder tool

Browse files
This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/_yaml/__init__.py +33 -0
  2. Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/CObjects.cpp +157 -0
  3. Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/PyGreenlet.cpp +738 -0
  4. Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/PyGreenlet.hpp +35 -0
  5. Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/PyGreenletUnswitchable.cpp +147 -0
  6. Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/PyModule.cpp +292 -0
  7. Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/TBrokenGreenlet.cpp +45 -0
  8. Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/TExceptionState.cpp +62 -0
  9. Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/TGreenlet.cpp +718 -0
  10. Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/TGreenlet.hpp +824 -0
  11. Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/TGreenletGlobals.cpp +94 -0
  12. Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/TMainGreenlet.cpp +153 -0
  13. Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/TPythonState.cpp +402 -0
  14. Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/TStackState.cpp +265 -0
  15. Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/TThreadState.hpp +497 -0
  16. Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/TThreadStateCreator.hpp +102 -0
  17. Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/TThreadStateDestroy.cpp +217 -0
  18. Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/TUserGreenlet.cpp +662 -0
  19. Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/__init__.py +71 -0
  20. Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/greenlet.cpp +320 -0
  21. Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/greenlet.h +164 -0
  22. Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/greenlet_allocator.hpp +63 -0
  23. Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/greenlet_compiler_compat.hpp +98 -0
  24. Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/greenlet_cpython_compat.hpp +148 -0
  25. Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/greenlet_exceptions.hpp +171 -0
  26. Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/greenlet_internal.hpp +107 -0
  27. Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/greenlet_msvc_compat.hpp +91 -0
  28. Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/greenlet_refs.hpp +1118 -0
  29. Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/greenlet_slp_switch.hpp +99 -0
  30. Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/greenlet_thread_support.hpp +31 -0
  31. Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/platform/__init__.py +0 -0
  32. Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/platform/setup_switch_x64_masm.cmd +2 -0
  33. Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/platform/switch_aarch64_gcc.h +124 -0
  34. Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/platform/switch_alpha_unix.h +30 -0
  35. Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/platform/switch_amd64_unix.h +87 -0
  36. Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/platform/switch_arm32_gcc.h +79 -0
  37. Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/platform/switch_arm32_ios.h +67 -0
  38. Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/platform/switch_arm64_masm.asm +53 -0
  39. Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/platform/switch_arm64_masm.obj +0 -0
  40. Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/platform/switch_arm64_msvc.h +17 -0
  41. Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/platform/switch_csky_gcc.h +48 -0
  42. Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/platform/switch_loongarch64_linux.h +31 -0
  43. Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/platform/switch_m68k_gcc.h +38 -0
  44. Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/platform/switch_mips_unix.h +64 -0
  45. Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/platform/switch_ppc64_aix.h +103 -0
  46. Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/platform/switch_ppc64_linux.h +105 -0
  47. Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/platform/switch_ppc_aix.h +87 -0
  48. Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/platform/switch_ppc_linux.h +84 -0
  49. Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/platform/switch_ppc_macosx.h +82 -0
  50. Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/platform/switch_ppc_unix.h +82 -0
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/_yaml/__init__.py ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # This is a stub package designed to roughly emulate the _yaml
2
+ # extension module, which previously existed as a standalone module
3
+ # and has been moved into the `yaml` package namespace.
4
+ # It does not perfectly mimic its old counterpart, but should get
5
+ # close enough for anyone who's relying on it even when they shouldn't.
6
+ import yaml
7
+
8
+ # in some circumstances, the yaml module we imoprted may be from a different version, so we need
9
+ # to tread carefully when poking at it here (it may not have the attributes we expect)
10
+ if not getattr(yaml, '__with_libyaml__', False):
11
+ from sys import version_info
12
+
13
+ exc = ModuleNotFoundError if version_info >= (3, 6) else ImportError
14
+ raise exc("No module named '_yaml'")
15
+ else:
16
+ from yaml._yaml import *
17
+ import warnings
18
+ warnings.warn(
19
+ 'The _yaml extension module is now located at yaml._yaml'
20
+ ' and its location is subject to change. To use the'
21
+ ' LibYAML-based parser and emitter, import from `yaml`:'
22
+ ' `from yaml import CLoader as Loader, CDumper as Dumper`.',
23
+ DeprecationWarning
24
+ )
25
+ del warnings
26
+ # Don't `del yaml` here because yaml is actually an existing
27
+ # namespace member of _yaml.
28
+
29
+ __name__ = '_yaml'
30
+ # If the module is top-level (i.e. not a part of any specific package)
31
+ # then the attribute should be set to ''.
32
+ # https://docs.python.org/3.8/library/types.html
33
+ __package__ = ''
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/CObjects.cpp ADDED
@@ -0,0 +1,157 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #ifndef COBJECTS_CPP
2
+ #define COBJECTS_CPP
3
+ /*****************************************************************************
4
+ * C interface
5
+ *
6
+ * These are exported using the CObject API
7
+ */
8
+ #ifdef __clang__
9
+ # pragma clang diagnostic push
10
+ # pragma clang diagnostic ignored "-Wunused-function"
11
+ #endif
12
+
13
+ #include "greenlet_exceptions.hpp"
14
+
15
+ #include "greenlet_internal.hpp"
16
+ #include "greenlet_refs.hpp"
17
+
18
+
19
+ #include "TThreadStateDestroy.cpp"
20
+
21
+ #include "PyGreenlet.hpp"
22
+
23
+ using greenlet::PyErrOccurred;
24
+ using greenlet::Require;
25
+
26
+
27
+
28
+ extern "C" {
29
+ static PyGreenlet*
30
+ PyGreenlet_GetCurrent(void)
31
+ {
32
+ return GET_THREAD_STATE().state().get_current().relinquish_ownership();
33
+ }
34
+
35
+ static int
36
+ PyGreenlet_SetParent(PyGreenlet* g, PyGreenlet* nparent)
37
+ {
38
+ return green_setparent((PyGreenlet*)g, (PyObject*)nparent, NULL);
39
+ }
40
+
41
+ static PyGreenlet*
42
+ PyGreenlet_New(PyObject* run, PyGreenlet* parent)
43
+ {
44
+ using greenlet::refs::NewDictReference;
45
+ // In the past, we didn't use green_new and green_init, but that
46
+ // was a maintenance issue because we duplicated code. This way is
47
+ // much safer, but slightly slower. If that's a problem, we could
48
+ // refactor green_init to separate argument parsing from initialization.
49
+ OwnedGreenlet g = OwnedGreenlet::consuming(green_new(&PyGreenlet_Type, nullptr, nullptr));
50
+ if (!g) {
51
+ return NULL;
52
+ }
53
+
54
+ try {
55
+ NewDictReference kwargs;
56
+ if (run) {
57
+ kwargs.SetItem(mod_globs->str_run, run);
58
+ }
59
+ if (parent) {
60
+ kwargs.SetItem("parent", (PyObject*)parent);
61
+ }
62
+
63
+ Require(green_init(g.borrow(), mod_globs->empty_tuple, kwargs.borrow()));
64
+ }
65
+ catch (const PyErrOccurred&) {
66
+ return nullptr;
67
+ }
68
+
69
+ return g.relinquish_ownership();
70
+ }
71
+
72
+ static PyObject*
73
+ PyGreenlet_Switch(PyGreenlet* self, PyObject* args, PyObject* kwargs)
74
+ {
75
+ if (!PyGreenlet_Check(self)) {
76
+ PyErr_BadArgument();
77
+ return NULL;
78
+ }
79
+
80
+ if (args == NULL) {
81
+ args = mod_globs->empty_tuple;
82
+ }
83
+
84
+ if (kwargs == NULL || !PyDict_Check(kwargs)) {
85
+ kwargs = NULL;
86
+ }
87
+
88
+ return green_switch(self, args, kwargs);
89
+ }
90
+
91
+ static PyObject*
92
+ PyGreenlet_Throw(PyGreenlet* self, PyObject* typ, PyObject* val, PyObject* tb)
93
+ {
94
+ if (!PyGreenlet_Check(self)) {
95
+ PyErr_BadArgument();
96
+ return nullptr;
97
+ }
98
+ try {
99
+ PyErrPieces err_pieces(typ, val, tb);
100
+ return internal_green_throw(self, err_pieces).relinquish_ownership();
101
+ }
102
+ catch (const PyErrOccurred&) {
103
+ return nullptr;
104
+ }
105
+ }
106
+
107
+
108
+
109
+ static int
110
+ Extern_PyGreenlet_MAIN(PyGreenlet* self)
111
+ {
112
+ if (!PyGreenlet_Check(self)) {
113
+ PyErr_BadArgument();
114
+ return -1;
115
+ }
116
+ return self->pimpl->main();
117
+ }
118
+
119
+ static int
120
+ Extern_PyGreenlet_ACTIVE(PyGreenlet* self)
121
+ {
122
+ if (!PyGreenlet_Check(self)) {
123
+ PyErr_BadArgument();
124
+ return -1;
125
+ }
126
+ return self->pimpl->active();
127
+ }
128
+
129
+ static int
130
+ Extern_PyGreenlet_STARTED(PyGreenlet* self)
131
+ {
132
+ if (!PyGreenlet_Check(self)) {
133
+ PyErr_BadArgument();
134
+ return -1;
135
+ }
136
+ return self->pimpl->started();
137
+ }
138
+
139
+ static PyGreenlet*
140
+ Extern_PyGreenlet_GET_PARENT(PyGreenlet* self)
141
+ {
142
+ if (!PyGreenlet_Check(self)) {
143
+ PyErr_BadArgument();
144
+ return NULL;
145
+ }
146
+ // This can return NULL even if there is no exception
147
+ return self->pimpl->parent().acquire();
148
+ }
149
+ } // extern C.
150
+
151
+ /** End C API ****************************************************************/
152
+ #ifdef __clang__
153
+ # pragma clang diagnostic pop
154
+ #endif
155
+
156
+
157
+ #endif
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/PyGreenlet.cpp ADDED
@@ -0,0 +1,738 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* -*- indent-tabs-mode: nil; tab-width: 4; -*- */
2
+ #ifndef PYGREENLET_CPP
3
+ #define PYGREENLET_CPP
4
+ /*****************
5
+ The Python slot functions for TGreenlet.
6
+ */
7
+
8
+
9
+ #define PY_SSIZE_T_CLEAN
10
+ #include <Python.h>
11
+ #include "structmember.h" // PyMemberDef
12
+
13
+ #include "greenlet_internal.hpp"
14
+ #include "TThreadStateDestroy.cpp"
15
+ #include "TGreenlet.hpp"
16
+ // #include "TUserGreenlet.cpp"
17
+ // #include "TMainGreenlet.cpp"
18
+ // #include "TBrokenGreenlet.cpp"
19
+
20
+
21
+ #include "greenlet_refs.hpp"
22
+ #include "greenlet_slp_switch.hpp"
23
+
24
+ #include "greenlet_thread_support.hpp"
25
+ #include "TGreenlet.hpp"
26
+
27
+ #include "TGreenletGlobals.cpp"
28
+ #include "TThreadStateDestroy.cpp"
29
+ #include "PyGreenlet.hpp"
30
+ // #include "TGreenlet.cpp"
31
+
32
+ // #include "TExceptionState.cpp"
33
+ // #include "TPythonState.cpp"
34
+ // #include "TStackState.cpp"
35
+
36
+ using greenlet::LockGuard;
37
+ using greenlet::LockInitError;
38
+ using greenlet::PyErrOccurred;
39
+ using greenlet::Require;
40
+
41
+ using greenlet::g_handle_exit;
42
+ using greenlet::single_result;
43
+
44
+ using greenlet::Greenlet;
45
+ using greenlet::UserGreenlet;
46
+ using greenlet::MainGreenlet;
47
+ using greenlet::BrokenGreenlet;
48
+ using greenlet::ThreadState;
49
+ using greenlet::PythonState;
50
+
51
+
52
+
53
+ static PyGreenlet*
54
+ green_new(PyTypeObject* type, PyObject* UNUSED(args), PyObject* UNUSED(kwds))
55
+ {
56
+ PyGreenlet* o =
57
+ (PyGreenlet*)PyBaseObject_Type.tp_new(type, mod_globs->empty_tuple, mod_globs->empty_dict);
58
+ if (o) {
59
+ new UserGreenlet(o, GET_THREAD_STATE().state().borrow_current());
60
+ assert(Py_REFCNT(o) == 1);
61
+ }
62
+ return o;
63
+ }
64
+
65
+
66
+ // green_init is used in the tp_init slot. So it's important that
67
+ // it can be called directly from CPython. Thus, we don't use
68
+ // BorrowedGreenlet and BorrowedObject --- although in theory
69
+ // these should be binary layout compatible, that may not be
70
+ // guaranteed to be the case (32-bit linux ppc possibly).
71
+ static int
72
+ green_init(PyGreenlet* self, PyObject* args, PyObject* kwargs)
73
+ {
74
+ PyArgParseParam run;
75
+ PyArgParseParam nparent;
76
+ static const char* kwlist[] = {
77
+ "run",
78
+ "parent",
79
+ NULL
80
+ };
81
+
82
+ // recall: The O specifier does NOT increase the reference count.
83
+ if (!PyArg_ParseTupleAndKeywords(
84
+ args, kwargs, "|OO:green", (char**)kwlist, &run, &nparent)) {
85
+ return -1;
86
+ }
87
+
88
+ if (run) {
89
+ if (green_setrun(self, run, NULL)) {
90
+ return -1;
91
+ }
92
+ }
93
+ if (nparent && !nparent.is_None()) {
94
+ return green_setparent(self, nparent, NULL);
95
+ }
96
+ return 0;
97
+ }
98
+
99
+
100
+
101
+ static int
102
+ green_traverse(PyGreenlet* self, visitproc visit, void* arg)
103
+ {
104
+ // We must only visit referenced objects, i.e. only objects
105
+ // Py_INCREF'ed by this greenlet (directly or indirectly):
106
+ //
107
+ // - stack_prev is not visited: holds previous stack pointer, but it's not
108
+ // referenced
109
+ // - frames are not visited as we don't strongly reference them;
110
+ // alive greenlets are not garbage collected
111
+ // anyway. This can be a problem, however, if this greenlet is
112
+ // never allowed to finish, and is referenced from the frame: we
113
+ // have an uncollectible cycle in that case. Note that the
114
+ // frame object itself is also frequently not even tracked by the GC
115
+ // starting with Python 3.7 (frames are allocated by the
116
+ // interpreter untracked, and only become tracked when their
117
+ // evaluation is finished if they have a refcount > 1). All of
118
+ // this is to say that we should probably strongly reference
119
+ // the frame object. Doing so, while always allowing GC on a
120
+ // greenlet, solves several leaks for us.
121
+
122
+ Py_VISIT(self->dict);
123
+ if (!self->pimpl) {
124
+ // Hmm. I have seen this at interpreter shutdown time,
125
+ // I think. That's very odd because this doesn't go away until
126
+ // we're ``green_dealloc()``, at which point we shouldn't be
127
+ // traversed anymore.
128
+ return 0;
129
+ }
130
+
131
+ return self->pimpl->tp_traverse(visit, arg);
132
+ }
133
+
134
+ static int
135
+ green_is_gc(PyObject* _self)
136
+ {
137
+ BorrowedGreenlet self(_self);
138
+ int result = 0;
139
+ /* Main greenlet can be garbage collected since it can only
140
+ become unreachable if the underlying thread exited.
141
+ Active greenlets --- including those that are suspended ---
142
+ cannot be garbage collected, however.
143
+ */
144
+ if (self->main() || !self->active()) {
145
+ result = 1;
146
+ }
147
+ // The main greenlet pointer will eventually go away after the thread dies.
148
+ if (self->was_running_in_dead_thread()) {
149
+ // Our thread is dead! We can never run again. Might as well
150
+ // GC us. Note that if a tuple containing only us and other
151
+ // immutable objects had been scanned before this, when we
152
+ // would have returned 0, the tuple will take itself out of GC
153
+ // tracking and never be investigated again. So that could
154
+ // result in both us and the tuple leaking due to an
155
+ // unreachable/uncollectible reference. The same goes for
156
+ // dictionaries.
157
+ //
158
+ // It's not a great idea to be changing our GC state on the
159
+ // fly.
160
+ result = 1;
161
+ }
162
+ return result;
163
+ }
164
+
165
+
166
+ static int
167
+ green_clear(PyGreenlet* self)
168
+ {
169
+ /* Greenlet is only cleared if it is about to be collected.
170
+ Since active greenlets are not garbage collectable, we can
171
+ be sure that, even if they are deallocated during clear,
172
+ nothing they reference is in unreachable or finalizers,
173
+ so even if it switches we are relatively safe. */
174
+ // XXX: Are we responsible for clearing weakrefs here?
175
+ Py_CLEAR(self->dict);
176
+ return self->pimpl->tp_clear();
177
+ }
178
+
179
+ /**
180
+ * Returns 0 on failure (the object was resurrected) or 1 on success.
181
+ **/
182
+ static int
183
+ _green_dealloc_kill_started_non_main_greenlet(BorrowedGreenlet self)
184
+ {
185
+ /* Hacks hacks hacks copied from instance_dealloc() */
186
+ /* Temporarily resurrect the greenlet. */
187
+ assert(self.REFCNT() == 0);
188
+ Py_SET_REFCNT(self.borrow(), 1);
189
+ /* Save the current exception, if any. */
190
+ PyErrPieces saved_err;
191
+ try {
192
+ // BY THE TIME WE GET HERE, the state may actually be going
193
+ // away
194
+ // if we're shutting down the interpreter and freeing thread
195
+ // entries,
196
+ // this could result in freeing greenlets that were leaked. So
197
+ // we can't try to read the state.
198
+ self->deallocing_greenlet_in_thread(
199
+ self->thread_state()
200
+ ? static_cast<ThreadState*>(GET_THREAD_STATE())
201
+ : nullptr);
202
+ }
203
+ catch (const PyErrOccurred&) {
204
+ PyErr_WriteUnraisable(self.borrow_o());
205
+ /* XXX what else should we do? */
206
+ }
207
+ /* Check for no resurrection must be done while we keep
208
+ * our internal reference, otherwise PyFile_WriteObject
209
+ * causes recursion if using Py_INCREF/Py_DECREF
210
+ */
211
+ if (self.REFCNT() == 1 && self->active()) {
212
+ /* Not resurrected, but still not dead!
213
+ XXX what else should we do? we complain. */
214
+ PyObject* f = PySys_GetObject("stderr");
215
+ Py_INCREF(self.borrow_o()); /* leak! */
216
+ if (f != NULL) {
217
+ PyFile_WriteString("GreenletExit did not kill ", f);
218
+ PyFile_WriteObject(self.borrow_o(), f, 0);
219
+ PyFile_WriteString("\n", f);
220
+ }
221
+ }
222
+ /* Restore the saved exception. */
223
+ saved_err.PyErrRestore();
224
+ /* Undo the temporary resurrection; can't use DECREF here,
225
+ * it would cause a recursive call.
226
+ */
227
+ assert(self.REFCNT() > 0);
228
+
229
+ Py_ssize_t refcnt = self.REFCNT() - 1;
230
+ Py_SET_REFCNT(self.borrow_o(), refcnt);
231
+ if (refcnt != 0) {
232
+ /* Resurrected! */
233
+ _Py_NewReference(self.borrow_o());
234
+ Py_SET_REFCNT(self.borrow_o(), refcnt);
235
+ /* Better to use tp_finalizer slot (PEP 442)
236
+ * and call ``PyObject_CallFinalizerFromDealloc``,
237
+ * but that's only supported in Python 3.4+; see
238
+ * Modules/_io/iobase.c for an example.
239
+ *
240
+ * The following approach is copied from iobase.c in CPython 2.7.
241
+ * (along with much of this function in general). Here's their
242
+ * comment:
243
+ *
244
+ * When called from a heap type's dealloc, the type will be
245
+ * decref'ed on return (see e.g. subtype_dealloc in typeobject.c). */
246
+ if (PyType_HasFeature(self.TYPE(), Py_TPFLAGS_HEAPTYPE)) {
247
+ Py_INCREF(self.TYPE());
248
+ }
249
+
250
+ PyObject_GC_Track((PyObject*)self);
251
+
252
+ _Py_DEC_REFTOTAL;
253
+ #ifdef COUNT_ALLOCS
254
+ --Py_TYPE(self)->tp_frees;
255
+ --Py_TYPE(self)->tp_allocs;
256
+ #endif /* COUNT_ALLOCS */
257
+ return 0;
258
+ }
259
+ return 1;
260
+ }
261
+
262
+
263
+ static void
264
+ green_dealloc(PyGreenlet* self)
265
+ {
266
+ PyObject_GC_UnTrack(self);
267
+ BorrowedGreenlet me(self);
268
+ if (me->active()
269
+ && me->started()
270
+ && !me->main()) {
271
+ if (!_green_dealloc_kill_started_non_main_greenlet(me)) {
272
+ return;
273
+ }
274
+ }
275
+
276
+ if (self->weakreflist != NULL) {
277
+ PyObject_ClearWeakRefs((PyObject*)self);
278
+ }
279
+ Py_CLEAR(self->dict);
280
+
281
+ if (self->pimpl) {
282
+ // In case deleting this, which frees some memory,
283
+ // somehow winds up calling back into us. That's usually a
284
+ //bug in our code.
285
+ Greenlet* p = self->pimpl;
286
+ self->pimpl = nullptr;
287
+ delete p;
288
+ }
289
+ // and finally we're done. self is now invalid.
290
+ Py_TYPE(self)->tp_free((PyObject*)self);
291
+ }
292
+
293
+
294
+
295
+ static OwnedObject
296
+ internal_green_throw(BorrowedGreenlet self, PyErrPieces& err_pieces)
297
+ {
298
+ PyObject* result = nullptr;
299
+ err_pieces.PyErrRestore();
300
+ assert(PyErr_Occurred());
301
+ if (self->started() && !self->active()) {
302
+ /* dead greenlet: turn GreenletExit into a regular return */
303
+ result = g_handle_exit(OwnedObject()).relinquish_ownership();
304
+ }
305
+ self->args() <<= result;
306
+
307
+ return single_result(self->g_switch());
308
+ }
309
+
310
+
311
+
312
+ PyDoc_STRVAR(
313
+ green_switch_doc,
314
+ "switch(*args, **kwargs)\n"
315
+ "\n"
316
+ "Switch execution to this greenlet.\n"
317
+ "\n"
318
+ "If this greenlet has never been run, then this greenlet\n"
319
+ "will be switched to using the body of ``self.run(*args, **kwargs)``.\n"
320
+ "\n"
321
+ "If the greenlet is active (has been run, but was switch()'ed\n"
322
+ "out before leaving its run function), then this greenlet will\n"
323
+ "be resumed and the return value to its switch call will be\n"
324
+ "None if no arguments are given, the given argument if one\n"
325
+ "argument is given, or the args tuple and keyword args dict if\n"
326
+ "multiple arguments are given.\n"
327
+ "\n"
328
+ "If the greenlet is dead, or is the current greenlet then this\n"
329
+ "function will simply return the arguments using the same rules as\n"
330
+ "above.\n");
331
+
332
+ static PyObject*
333
+ green_switch(PyGreenlet* self, PyObject* args, PyObject* kwargs)
334
+ {
335
+ using greenlet::SwitchingArgs;
336
+ SwitchingArgs switch_args(OwnedObject::owning(args), OwnedObject::owning(kwargs));
337
+ self->pimpl->may_switch_away();
338
+ self->pimpl->args() <<= switch_args;
339
+
340
+ // If we're switching out of a greenlet, and that switch is the
341
+ // last thing the greenlet does, the greenlet ought to be able to
342
+ // go ahead and die at that point. Currently, someone else must
343
+ // manually switch back to the greenlet so that we "fall off the
344
+ // end" and can perform cleanup. You'd think we'd be able to
345
+ // figure out that this is happening using the frame's ``f_lasti``
346
+ // member, which is supposed to be an index into
347
+ // ``frame->f_code->co_code``, the bytecode string. However, in
348
+ // recent interpreters, ``f_lasti`` tends not to be updated thanks
349
+ // to things like the PREDICT() macros in ceval.c. So it doesn't
350
+ // really work to do that in many cases. For example, the Python
351
+ // code:
352
+ // def run():
353
+ // greenlet.getcurrent().parent.switch()
354
+ // produces bytecode of len 16, with the actual call to switch()
355
+ // being at index 10 (in Python 3.10). However, the reported
356
+ // ``f_lasti`` we actually see is...5! (Which happens to be the
357
+ // second byte of the CALL_METHOD op for ``getcurrent()``).
358
+
359
+ try {
360
+ //OwnedObject result = single_result(self->pimpl->g_switch());
361
+ OwnedObject result(single_result(self->pimpl->g_switch()));
362
+ #ifndef NDEBUG
363
+ // Note that the current greenlet isn't necessarily self. If self
364
+ // finished, we went to one of its parents.
365
+ assert(!self->pimpl->args());
366
+
367
+ const BorrowedGreenlet& current = GET_THREAD_STATE().state().borrow_current();
368
+ // It's possible it's never been switched to.
369
+ assert(!current->args());
370
+ #endif
371
+ PyObject* p = result.relinquish_ownership();
372
+
373
+ if (!p && !PyErr_Occurred()) {
374
+ // This shouldn't be happening anymore, so the asserts
375
+ // are there for debug builds. Non-debug builds
376
+ // crash "gracefully" in this case, although there is an
377
+ // argument to be made for killing the process in all
378
+ // cases --- for this to be the case, our switches
379
+ // probably nested in an incorrect way, so the state is
380
+ // suspicious. Nothing should be corrupt though, just
381
+ // confused at the Python level. Letting this propagate is
382
+ // probably good enough.
383
+ assert(p || PyErr_Occurred());
384
+ throw PyErrOccurred(
385
+ mod_globs->PyExc_GreenletError,
386
+ "Greenlet.switch() returned NULL without an exception set."
387
+ );
388
+ }
389
+ return p;
390
+ }
391
+ catch(const PyErrOccurred&) {
392
+ return nullptr;
393
+ }
394
+ }
395
+
396
+ PyDoc_STRVAR(
397
+ green_throw_doc,
398
+ "Switches execution to this greenlet, but immediately raises the\n"
399
+ "given exception in this greenlet. If no argument is provided, the "
400
+ "exception\n"
401
+ "defaults to `greenlet.GreenletExit`. The normal exception\n"
402
+ "propagation rules apply, as described for `switch`. Note that calling "
403
+ "this\n"
404
+ "method is almost equivalent to the following::\n"
405
+ "\n"
406
+ " def raiser():\n"
407
+ " raise typ, val, tb\n"
408
+ " g_raiser = greenlet(raiser, parent=g)\n"
409
+ " g_raiser.switch()\n"
410
+ "\n"
411
+ "except that this trick does not work for the\n"
412
+ "`greenlet.GreenletExit` exception, which would not propagate\n"
413
+ "from ``g_raiser`` to ``g``.\n");
414
+
415
+ static PyObject*
416
+ green_throw(PyGreenlet* self, PyObject* args)
417
+ {
418
+ PyArgParseParam typ(mod_globs->PyExc_GreenletExit);
419
+ PyArgParseParam val;
420
+ PyArgParseParam tb;
421
+
422
+ if (!PyArg_ParseTuple(args, "|OOO:throw", &typ, &val, &tb)) {
423
+ return nullptr;
424
+ }
425
+
426
+ assert(typ.borrow() || val.borrow());
427
+
428
+ self->pimpl->may_switch_away();
429
+ try {
430
+ // Both normalizing the error and the actual throw_greenlet
431
+ // could throw PyErrOccurred.
432
+ PyErrPieces err_pieces(typ.borrow(), val.borrow(), tb.borrow());
433
+
434
+ return internal_green_throw(self, err_pieces).relinquish_ownership();
435
+ }
436
+ catch (const PyErrOccurred&) {
437
+ return nullptr;
438
+ }
439
+ }
440
+
441
+ static int
442
+ green_bool(PyGreenlet* self)
443
+ {
444
+ return self->pimpl->active();
445
+ }
446
+
447
+ /**
448
+ * CAUTION: Allocates memory, may run GC and arbitrary Python code.
449
+ */
450
+ static PyObject*
451
+ green_getdict(PyGreenlet* self, void* UNUSED(context))
452
+ {
453
+ if (self->dict == NULL) {
454
+ self->dict = PyDict_New();
455
+ if (self->dict == NULL) {
456
+ return NULL;
457
+ }
458
+ }
459
+ Py_INCREF(self->dict);
460
+ return self->dict;
461
+ }
462
+
463
+ static int
464
+ green_setdict(PyGreenlet* self, PyObject* val, void* UNUSED(context))
465
+ {
466
+ PyObject* tmp;
467
+
468
+ if (val == NULL) {
469
+ PyErr_SetString(PyExc_TypeError, "__dict__ may not be deleted");
470
+ return -1;
471
+ }
472
+ if (!PyDict_Check(val)) {
473
+ PyErr_SetString(PyExc_TypeError, "__dict__ must be a dictionary");
474
+ return -1;
475
+ }
476
+ tmp = self->dict;
477
+ Py_INCREF(val);
478
+ self->dict = val;
479
+ Py_XDECREF(tmp);
480
+ return 0;
481
+ }
482
+
483
+ static bool
484
+ _green_not_dead(BorrowedGreenlet self)
485
+ {
486
+ // XXX: Where else should we do this?
487
+ // Probably on entry to most Python-facing functions?
488
+ if (self->was_running_in_dead_thread()) {
489
+ self->deactivate_and_free();
490
+ return false;
491
+ }
492
+ return self->active() || !self->started();
493
+ }
494
+
495
+
496
+ static PyObject*
497
+ green_getdead(PyGreenlet* self, void* UNUSED(context))
498
+ {
499
+ if (_green_not_dead(self)) {
500
+ Py_RETURN_FALSE;
501
+ }
502
+ else {
503
+ Py_RETURN_TRUE;
504
+ }
505
+ }
506
+
507
+ static PyObject*
508
+ green_get_stack_saved(PyGreenlet* self, void* UNUSED(context))
509
+ {
510
+ return PyLong_FromSsize_t(self->pimpl->stack_saved());
511
+ }
512
+
513
+
514
+ static PyObject*
515
+ green_getrun(PyGreenlet* self, void* UNUSED(context))
516
+ {
517
+ try {
518
+ OwnedObject result(BorrowedGreenlet(self)->run());
519
+ return result.relinquish_ownership();
520
+ }
521
+ catch(const PyErrOccurred&) {
522
+ return nullptr;
523
+ }
524
+ }
525
+
526
+
527
+ static int
528
+ green_setrun(PyGreenlet* self, PyObject* nrun, void* UNUSED(context))
529
+ {
530
+ try {
531
+ BorrowedGreenlet(self)->run(nrun);
532
+ return 0;
533
+ }
534
+ catch(const PyErrOccurred&) {
535
+ return -1;
536
+ }
537
+ }
538
+
539
+ static PyObject*
540
+ green_getparent(PyGreenlet* self, void* UNUSED(context))
541
+ {
542
+ return BorrowedGreenlet(self)->parent().acquire_or_None();
543
+ }
544
+
545
+
546
+ static int
547
+ green_setparent(PyGreenlet* self, PyObject* nparent, void* UNUSED(context))
548
+ {
549
+ try {
550
+ BorrowedGreenlet(self)->parent(nparent);
551
+ }
552
+ catch(const PyErrOccurred&) {
553
+ return -1;
554
+ }
555
+ return 0;
556
+ }
557
+
558
+
559
+ static PyObject*
560
+ green_getcontext(const PyGreenlet* self, void* UNUSED(context))
561
+ {
562
+ const Greenlet *const g = self->pimpl;
563
+ try {
564
+ OwnedObject result(g->context());
565
+ return result.relinquish_ownership();
566
+ }
567
+ catch(const PyErrOccurred&) {
568
+ return nullptr;
569
+ }
570
+ }
571
+
572
+ static int
573
+ green_setcontext(PyGreenlet* self, PyObject* nctx, void* UNUSED(context))
574
+ {
575
+ try {
576
+ BorrowedGreenlet(self)->context(nctx);
577
+ return 0;
578
+ }
579
+ catch(const PyErrOccurred&) {
580
+ return -1;
581
+ }
582
+ }
583
+
584
+
585
+ static PyObject*
586
+ green_getframe(PyGreenlet* self, void* UNUSED(context))
587
+ {
588
+ const PythonState::OwnedFrame& top_frame = BorrowedGreenlet(self)->top_frame();
589
+ return top_frame.acquire_or_None();
590
+ }
591
+
592
+
593
+ static PyObject*
594
+ green_getstate(PyGreenlet* self)
595
+ {
596
+ PyErr_Format(PyExc_TypeError,
597
+ "cannot serialize '%s' object",
598
+ Py_TYPE(self)->tp_name);
599
+ return nullptr;
600
+ }
601
+
602
+ static PyObject*
603
+ green_repr(PyGreenlet* _self)
604
+ {
605
+ BorrowedGreenlet self(_self);
606
+ /*
607
+ Return a string like
608
+ <greenlet.greenlet at 0xdeadbeef [current][active started]|dead main>
609
+
610
+ The handling of greenlets across threads is not super good.
611
+ We mostly use the internal definitions of these terms, but they
612
+ generally should make sense to users as well.
613
+ */
614
+ PyObject* result;
615
+ int never_started = !self->started() && !self->active();
616
+
617
+ const char* const tp_name = Py_TYPE(self)->tp_name;
618
+
619
+ if (_green_not_dead(self)) {
620
+ /* XXX: The otid= is almost useless because you can't correlate it to
621
+ any thread identifier exposed to Python. We could use
622
+ PyThreadState_GET()->thread_id, but we'd need to save that in the
623
+ greenlet, or save the whole PyThreadState object itself.
624
+
625
+ As it stands, its only useful for identifying greenlets from the same thread.
626
+ */
627
+ const char* state_in_thread;
628
+ if (self->was_running_in_dead_thread()) {
629
+ // The thread it was running in is dead!
630
+ // This can happen, especially at interpreter shut down.
631
+ // It complicates debugging output because it may be
632
+ // impossible to access the current thread state at that
633
+ // time. Thus, don't access the current thread state.
634
+ state_in_thread = " (thread exited)";
635
+ }
636
+ else {
637
+ state_in_thread = GET_THREAD_STATE().state().is_current(self)
638
+ ? " current"
639
+ : (self->started() ? " suspended" : "");
640
+ }
641
+ result = PyUnicode_FromFormat(
642
+ "<%s object at %p (otid=%p)%s%s%s%s>",
643
+ tp_name,
644
+ self.borrow_o(),
645
+ self->thread_state(),
646
+ state_in_thread,
647
+ self->active() ? " active" : "",
648
+ never_started ? " pending" : " started",
649
+ self->main() ? " main" : ""
650
+ );
651
+ }
652
+ else {
653
+ result = PyUnicode_FromFormat(
654
+ "<%s object at %p (otid=%p) %sdead>",
655
+ tp_name,
656
+ self.borrow_o(),
657
+ self->thread_state(),
658
+ self->was_running_in_dead_thread()
659
+ ? "(thread exited) "
660
+ : ""
661
+ );
662
+ }
663
+
664
+ return result;
665
+ }
666
+
667
+
668
+ static PyMethodDef green_methods[] = {
669
+ {
670
+ .ml_name="switch",
671
+ .ml_meth=reinterpret_cast<PyCFunction>(green_switch),
672
+ .ml_flags=METH_VARARGS | METH_KEYWORDS,
673
+ .ml_doc=green_switch_doc
674
+ },
675
+ {.ml_name="throw", .ml_meth=(PyCFunction)green_throw, .ml_flags=METH_VARARGS, .ml_doc=green_throw_doc},
676
+ {.ml_name="__getstate__", .ml_meth=(PyCFunction)green_getstate, .ml_flags=METH_NOARGS, .ml_doc=NULL},
677
+ {.ml_name=NULL, .ml_meth=NULL} /* sentinel */
678
+ };
679
+
680
+ static PyGetSetDef green_getsets[] = {
681
+ /* name, getter, setter, doc, context pointer */
682
+ {.name="__dict__", .get=(getter)green_getdict, .set=(setter)green_setdict},
683
+ {.name="run", .get=(getter)green_getrun, .set=(setter)green_setrun},
684
+ {.name="parent", .get=(getter)green_getparent, .set=(setter)green_setparent},
685
+ {.name="gr_frame", .get=(getter)green_getframe },
686
+ {
687
+ .name="gr_context",
688
+ .get=(getter)green_getcontext,
689
+ .set=(setter)green_setcontext
690
+ },
691
+ {.name="dead", .get=(getter)green_getdead},
692
+ {.name="_stack_saved", .get=(getter)green_get_stack_saved},
693
+ {.name=NULL}
694
+ };
695
+
696
+ static PyMemberDef green_members[] = {
697
+ {.name=NULL}
698
+ };
699
+
700
+ static PyNumberMethods green_as_number = {
701
+ .nb_bool=(inquiry)green_bool,
702
+ };
703
+
704
+
705
+ PyTypeObject PyGreenlet_Type = {
706
+ .ob_base=PyVarObject_HEAD_INIT(NULL, 0)
707
+ .tp_name="greenlet.greenlet", /* tp_name */
708
+ .tp_basicsize=sizeof(PyGreenlet), /* tp_basicsize */
709
+ /* methods */
710
+ .tp_dealloc=(destructor)green_dealloc, /* tp_dealloc */
711
+ .tp_repr=(reprfunc)green_repr, /* tp_repr */
712
+ .tp_as_number=&green_as_number, /* tp_as _number*/
713
+ .tp_flags=G_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
714
+ .tp_doc="greenlet(run=None, parent=None) -> greenlet\n\n"
715
+ "Creates a new greenlet object (without running it).\n\n"
716
+ " - *run* -- The callable to invoke.\n"
717
+ " - *parent* -- The parent greenlet. The default is the current "
718
+ "greenlet.", /* tp_doc */
719
+ .tp_traverse=(traverseproc)green_traverse, /* tp_traverse */
720
+ .tp_clear=(inquiry)green_clear, /* tp_clear */
721
+ .tp_weaklistoffset=offsetof(PyGreenlet, weakreflist), /* tp_weaklistoffset */
722
+
723
+ .tp_methods=green_methods, /* tp_methods */
724
+ .tp_members=green_members, /* tp_members */
725
+ .tp_getset=green_getsets, /* tp_getset */
726
+ .tp_dictoffset=offsetof(PyGreenlet, dict), /* tp_dictoffset */
727
+ .tp_init=(initproc)green_init, /* tp_init */
728
+ .tp_alloc=PyType_GenericAlloc, /* tp_alloc */
729
+ .tp_new=(newfunc)green_new, /* tp_new */
730
+ .tp_free=PyObject_GC_Del, /* tp_free */
731
+ .tp_is_gc=(inquiry)green_is_gc, /* tp_is_gc */
732
+ };
733
+
734
+ #endif
735
+
736
+ // Local Variables:
737
+ // flycheck-clang-include-path: ("/opt/local/Library/Frameworks/Python.framework/Versions/3.8/include/python3.8")
738
+ // End:
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/PyGreenlet.hpp ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #ifndef PYGREENLET_HPP
2
+ #define PYGREENLET_HPP
3
+
4
+
5
+ #include "greenlet.h"
6
+ #include "greenlet_compiler_compat.hpp"
7
+ #include "greenlet_refs.hpp"
8
+
9
+
10
+ using greenlet::refs::OwnedGreenlet;
11
+ using greenlet::refs::BorrowedGreenlet;
12
+ using greenlet::refs::BorrowedObject;;
13
+ using greenlet::refs::OwnedObject;
14
+ using greenlet::refs::PyErrPieces;
15
+
16
+
17
+ // XXX: These doesn't really belong here, it's not a Python slot.
18
+ static OwnedObject internal_green_throw(BorrowedGreenlet self, PyErrPieces& err_pieces);
19
+
20
+ static PyGreenlet* green_new(PyTypeObject* type, PyObject* UNUSED(args), PyObject* UNUSED(kwds));
21
+ static int green_clear(PyGreenlet* self);
22
+ static int green_init(PyGreenlet* self, PyObject* args, PyObject* kwargs);
23
+ static int green_setparent(PyGreenlet* self, PyObject* nparent, void* UNUSED(context));
24
+ static int green_setrun(PyGreenlet* self, PyObject* nrun, void* UNUSED(context));
25
+ static int green_traverse(PyGreenlet* self, visitproc visit, void* arg);
26
+ static void green_dealloc(PyGreenlet* self);
27
+ static PyObject* green_getparent(PyGreenlet* self, void* UNUSED(context));
28
+
29
+ static int green_is_gc(PyObject* self);
30
+ static PyObject* green_getdead(PyGreenlet* self, void* UNUSED(context));
31
+ static PyObject* green_getrun(PyGreenlet* self, void* UNUSED(context));
32
+ static int green_setcontext(PyGreenlet* self, PyObject* nctx, void* UNUSED(context));
33
+ static PyObject* green_getframe(PyGreenlet* self, void* UNUSED(context));
34
+ static PyObject* green_repr(PyGreenlet* self);
35
+ #endif
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/PyGreenletUnswitchable.cpp ADDED
@@ -0,0 +1,147 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* -*- indent-tabs-mode: nil; tab-width: 4; -*- */
2
+ /**
3
+ Implementation of the Python slots for PyGreenletUnswitchable_Type
4
+ */
5
+ #ifndef PY_GREENLET_UNSWITCHABLE_CPP
6
+ #define PY_GREENLET_UNSWITCHABLE_CPP
7
+
8
+
9
+
10
+ #define PY_SSIZE_T_CLEAN
11
+ #include <Python.h>
12
+ #include "structmember.h" // PyMemberDef
13
+
14
+ #include "greenlet_internal.hpp"
15
+ // Code after this point can assume access to things declared in stdint.h,
16
+ // including the fixed-width types. This goes for the platform-specific switch functions
17
+ // as well.
18
+ #include "greenlet_refs.hpp"
19
+ #include "greenlet_slp_switch.hpp"
20
+
21
+ #include "greenlet_thread_support.hpp"
22
+ #include "TGreenlet.hpp"
23
+
24
+ #include "TGreenlet.cpp"
25
+ #include "TGreenletGlobals.cpp"
26
+ #include "TThreadStateDestroy.cpp"
27
+
28
+
29
+ using greenlet::LockGuard;
30
+ using greenlet::LockInitError;
31
+ using greenlet::PyErrOccurred;
32
+ using greenlet::Require;
33
+
34
+ using greenlet::g_handle_exit;
35
+ using greenlet::single_result;
36
+
37
+ using greenlet::Greenlet;
38
+ using greenlet::UserGreenlet;
39
+ using greenlet::MainGreenlet;
40
+ using greenlet::BrokenGreenlet;
41
+ using greenlet::ThreadState;
42
+ using greenlet::PythonState;
43
+
44
+
45
+ #include "PyGreenlet.hpp"
46
+
47
+ static PyGreenlet*
48
+ green_unswitchable_new(PyTypeObject* type, PyObject* UNUSED(args), PyObject* UNUSED(kwds))
49
+ {
50
+ PyGreenlet* o =
51
+ (PyGreenlet*)PyBaseObject_Type.tp_new(type, mod_globs->empty_tuple, mod_globs->empty_dict);
52
+ if (o) {
53
+ new BrokenGreenlet(o, GET_THREAD_STATE().state().borrow_current());
54
+ assert(Py_REFCNT(o) == 1);
55
+ }
56
+ return o;
57
+ }
58
+
59
+ static PyObject*
60
+ green_unswitchable_getforce(PyGreenlet* self, void* UNUSED(context))
61
+ {
62
+ BrokenGreenlet* broken = dynamic_cast<BrokenGreenlet*>(self->pimpl);
63
+ return PyBool_FromLong(broken->_force_switch_error);
64
+ }
65
+
66
+ static int
67
+ green_unswitchable_setforce(PyGreenlet* self, PyObject* nforce, void* UNUSED(context))
68
+ {
69
+ if (!nforce) {
70
+ PyErr_SetString(
71
+ PyExc_AttributeError,
72
+ "Cannot delete force_switch_error"
73
+ );
74
+ return -1;
75
+ }
76
+ BrokenGreenlet* broken = dynamic_cast<BrokenGreenlet*>(self->pimpl);
77
+ int is_true = PyObject_IsTrue(nforce);
78
+ if (is_true == -1) {
79
+ return -1;
80
+ }
81
+ broken->_force_switch_error = is_true;
82
+ return 0;
83
+ }
84
+
85
+ static PyObject*
86
+ green_unswitchable_getforceslp(PyGreenlet* self, void* UNUSED(context))
87
+ {
88
+ BrokenGreenlet* broken = dynamic_cast<BrokenGreenlet*>(self->pimpl);
89
+ return PyBool_FromLong(broken->_force_slp_switch_error);
90
+ }
91
+
92
+ static int
93
+ green_unswitchable_setforceslp(PyGreenlet* self, PyObject* nforce, void* UNUSED(context))
94
+ {
95
+ if (!nforce) {
96
+ PyErr_SetString(
97
+ PyExc_AttributeError,
98
+ "Cannot delete force_slp_switch_error"
99
+ );
100
+ return -1;
101
+ }
102
+ BrokenGreenlet* broken = dynamic_cast<BrokenGreenlet*>(self->pimpl);
103
+ int is_true = PyObject_IsTrue(nforce);
104
+ if (is_true == -1) {
105
+ return -1;
106
+ }
107
+ broken->_force_slp_switch_error = is_true;
108
+ return 0;
109
+ }
110
+
111
+ static PyGetSetDef green_unswitchable_getsets[] = {
112
+ /* name, getter, setter, doc, closure (context pointer) */
113
+ {
114
+ .name="force_switch_error",
115
+ .get=(getter)green_unswitchable_getforce,
116
+ .set=(setter)green_unswitchable_setforce,
117
+ .doc=NULL
118
+ },
119
+ {
120
+ .name="force_slp_switch_error",
121
+ .get=(getter)green_unswitchable_getforceslp,
122
+ .set=(setter)green_unswitchable_setforceslp,
123
+ .doc=nullptr
124
+ },
125
+ {.name=nullptr}
126
+ };
127
+
128
+ PyTypeObject PyGreenletUnswitchable_Type = {
129
+ .ob_base=PyVarObject_HEAD_INIT(NULL, 0)
130
+ .tp_name="greenlet._greenlet.UnswitchableGreenlet",
131
+ .tp_dealloc= (destructor)green_dealloc, /* tp_dealloc */
132
+ .tp_flags=G_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
133
+ .tp_doc="Undocumented internal class", /* tp_doc */
134
+ .tp_traverse=(traverseproc)green_traverse, /* tp_traverse */
135
+ .tp_clear=(inquiry)green_clear, /* tp_clear */
136
+
137
+ .tp_getset=green_unswitchable_getsets, /* tp_getset */
138
+ .tp_base=&PyGreenlet_Type, /* tp_base */
139
+ .tp_init=(initproc)green_init, /* tp_init */
140
+ .tp_alloc=PyType_GenericAlloc, /* tp_alloc */
141
+ .tp_new=(newfunc)green_unswitchable_new, /* tp_new */
142
+ .tp_free=PyObject_GC_Del, /* tp_free */
143
+ .tp_is_gc=(inquiry)green_is_gc, /* tp_is_gc */
144
+ };
145
+
146
+
147
+ #endif
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/PyModule.cpp ADDED
@@ -0,0 +1,292 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #ifndef PY_MODULE_CPP
2
+ #define PY_MODULE_CPP
3
+
4
+ #include "greenlet_internal.hpp"
5
+
6
+
7
+ #include "TGreenletGlobals.cpp"
8
+ #include "TMainGreenlet.cpp"
9
+ #include "TThreadStateDestroy.cpp"
10
+
11
+ using greenlet::LockGuard;
12
+ using greenlet::ThreadState;
13
+
14
+ #ifdef __clang__
15
+ # pragma clang diagnostic push
16
+ # pragma clang diagnostic ignored "-Wunused-function"
17
+ # pragma clang diagnostic ignored "-Wunused-variable"
18
+ #endif
19
+
20
+ PyDoc_STRVAR(mod_getcurrent_doc,
21
+ "getcurrent() -> greenlet\n"
22
+ "\n"
23
+ "Returns the current greenlet (i.e. the one which called this "
24
+ "function).\n");
25
+
26
+ static PyObject*
27
+ mod_getcurrent(PyObject* UNUSED(module))
28
+ {
29
+ return GET_THREAD_STATE().state().get_current().relinquish_ownership_o();
30
+ }
31
+
32
+ PyDoc_STRVAR(mod_settrace_doc,
33
+ "settrace(callback) -> object\n"
34
+ "\n"
35
+ "Sets a new tracing function and returns the previous one.\n");
36
+ static PyObject*
37
+ mod_settrace(PyObject* UNUSED(module), PyObject* args)
38
+ {
39
+ PyArgParseParam tracefunc;
40
+ if (!PyArg_ParseTuple(args, "O", &tracefunc)) {
41
+ return NULL;
42
+ }
43
+ ThreadState& state = GET_THREAD_STATE();
44
+ OwnedObject previous = state.get_tracefunc();
45
+ if (!previous) {
46
+ previous = Py_None;
47
+ }
48
+
49
+ state.set_tracefunc(tracefunc);
50
+
51
+ return previous.relinquish_ownership();
52
+ }
53
+
54
+ PyDoc_STRVAR(mod_gettrace_doc,
55
+ "gettrace() -> object\n"
56
+ "\n"
57
+ "Returns the currently set tracing function, or None.\n");
58
+
59
+ static PyObject*
60
+ mod_gettrace(PyObject* UNUSED(module))
61
+ {
62
+ OwnedObject tracefunc = GET_THREAD_STATE().state().get_tracefunc();
63
+ if (!tracefunc) {
64
+ tracefunc = Py_None;
65
+ }
66
+ return tracefunc.relinquish_ownership();
67
+ }
68
+
69
+
70
+
71
+ PyDoc_STRVAR(mod_set_thread_local_doc,
72
+ "set_thread_local(key, value) -> None\n"
73
+ "\n"
74
+ "Set a value in the current thread-local dictionary. Debugging only.\n");
75
+
76
+ static PyObject*
77
+ mod_set_thread_local(PyObject* UNUSED(module), PyObject* args)
78
+ {
79
+ PyArgParseParam key;
80
+ PyArgParseParam value;
81
+ PyObject* result = NULL;
82
+
83
+ if (PyArg_UnpackTuple(args, "set_thread_local", 2, 2, &key, &value)) {
84
+ if(PyDict_SetItem(
85
+ PyThreadState_GetDict(), // borrow
86
+ key,
87
+ value) == 0 ) {
88
+ // success
89
+ Py_INCREF(Py_None);
90
+ result = Py_None;
91
+ }
92
+ }
93
+ return result;
94
+ }
95
+
96
+ PyDoc_STRVAR(mod_get_pending_cleanup_count_doc,
97
+ "get_pending_cleanup_count() -> Integer\n"
98
+ "\n"
99
+ "Get the number of greenlet cleanup operations pending. Testing only.\n");
100
+
101
+
102
+ static PyObject*
103
+ mod_get_pending_cleanup_count(PyObject* UNUSED(module))
104
+ {
105
+ LockGuard cleanup_lock(*mod_globs->thread_states_to_destroy_lock);
106
+ return PyLong_FromSize_t(mod_globs->thread_states_to_destroy.size());
107
+ }
108
+
109
+ PyDoc_STRVAR(mod_get_total_main_greenlets_doc,
110
+ "get_total_main_greenlets() -> Integer\n"
111
+ "\n"
112
+ "Quickly return the number of main greenlets that exist. Testing only.\n");
113
+
114
+ static PyObject*
115
+ mod_get_total_main_greenlets(PyObject* UNUSED(module))
116
+ {
117
+ return PyLong_FromSize_t(G_TOTAL_MAIN_GREENLETS);
118
+ }
119
+
120
+
121
+
122
+ PyDoc_STRVAR(mod_get_clocks_used_doing_optional_cleanup_doc,
123
+ "get_clocks_used_doing_optional_cleanup() -> Integer\n"
124
+ "\n"
125
+ "Get the number of clock ticks the program has used doing optional "
126
+ "greenlet cleanup.\n"
127
+ "Beginning in greenlet 2.0, greenlet tries to find and dispose of greenlets\n"
128
+ "that leaked after a thread exited. This requires invoking Python's garbage collector,\n"
129
+ "which may have a performance cost proportional to the number of live objects.\n"
130
+ "This function returns the amount of processor time\n"
131
+ "greenlet has used to do this. In programs that run with very large amounts of live\n"
132
+ "objects, this metric can be used to decide whether the cost of doing this cleanup\n"
133
+ "is worth the memory leak being corrected. If not, you can disable the cleanup\n"
134
+ "using ``enable_optional_cleanup(False)``.\n"
135
+ "The units are arbitrary and can only be compared to themselves (similarly to ``time.clock()``);\n"
136
+ "for example, to see how it scales with your heap. You can attempt to convert them into seconds\n"
137
+ "by dividing by the value of CLOCKS_PER_SEC."
138
+ "If cleanup has been disabled, returns None."
139
+ "\n"
140
+ "This is an implementation specific, provisional API. It may be changed or removed\n"
141
+ "in the future.\n"
142
+ ".. versionadded:: 2.0"
143
+ );
144
+ static PyObject*
145
+ mod_get_clocks_used_doing_optional_cleanup(PyObject* UNUSED(module))
146
+ {
147
+ std::clock_t& clocks = ThreadState::clocks_used_doing_gc();
148
+
149
+ if (clocks == std::clock_t(-1)) {
150
+ Py_RETURN_NONE;
151
+ }
152
+ // This might not actually work on some implementations; clock_t
153
+ // is an opaque type.
154
+ return PyLong_FromSsize_t(clocks);
155
+ }
156
+
157
+ PyDoc_STRVAR(mod_enable_optional_cleanup_doc,
158
+ "mod_enable_optional_cleanup(bool) -> None\n"
159
+ "\n"
160
+ "Enable or disable optional cleanup operations.\n"
161
+ "See ``get_clocks_used_doing_optional_cleanup()`` for details.\n"
162
+ );
163
+ static PyObject*
164
+ mod_enable_optional_cleanup(PyObject* UNUSED(module), PyObject* flag)
165
+ {
166
+ int is_true = PyObject_IsTrue(flag);
167
+ if (is_true == -1) {
168
+ return nullptr;
169
+ }
170
+
171
+ std::clock_t& clocks = ThreadState::clocks_used_doing_gc();
172
+ if (is_true) {
173
+ // If we already have a value, we don't want to lose it.
174
+ if (clocks == std::clock_t(-1)) {
175
+ clocks = 0;
176
+ }
177
+ }
178
+ else {
179
+ clocks = std::clock_t(-1);
180
+ }
181
+ Py_RETURN_NONE;
182
+ }
183
+
184
+
185
+
186
+
187
+ #if !GREENLET_PY313
188
+ PyDoc_STRVAR(mod_get_tstate_trash_delete_nesting_doc,
189
+ "get_tstate_trash_delete_nesting() -> Integer\n"
190
+ "\n"
191
+ "Return the 'trash can' nesting level. Testing only.\n");
192
+ static PyObject*
193
+ mod_get_tstate_trash_delete_nesting(PyObject* UNUSED(module))
194
+ {
195
+ PyThreadState* tstate = PyThreadState_GET();
196
+
197
+ #if GREENLET_PY312
198
+ return PyLong_FromLong(tstate->trash.delete_nesting);
199
+ #else
200
+ return PyLong_FromLong(tstate->trash_delete_nesting);
201
+ #endif
202
+ }
203
+ #endif
204
+
205
+
206
+
207
+
208
+ static PyMethodDef GreenMethods[] = {
209
+ {
210
+ .ml_name="getcurrent",
211
+ .ml_meth=(PyCFunction)mod_getcurrent,
212
+ .ml_flags=METH_NOARGS,
213
+ .ml_doc=mod_getcurrent_doc
214
+ },
215
+ {
216
+ .ml_name="settrace",
217
+ .ml_meth=(PyCFunction)mod_settrace,
218
+ .ml_flags=METH_VARARGS,
219
+ .ml_doc=mod_settrace_doc
220
+ },
221
+ {
222
+ .ml_name="gettrace",
223
+ .ml_meth=(PyCFunction)mod_gettrace,
224
+ .ml_flags=METH_NOARGS,
225
+ .ml_doc=mod_gettrace_doc
226
+ },
227
+ {
228
+ .ml_name="set_thread_local",
229
+ .ml_meth=(PyCFunction)mod_set_thread_local,
230
+ .ml_flags=METH_VARARGS,
231
+ .ml_doc=mod_set_thread_local_doc
232
+ },
233
+ {
234
+ .ml_name="get_pending_cleanup_count",
235
+ .ml_meth=(PyCFunction)mod_get_pending_cleanup_count,
236
+ .ml_flags=METH_NOARGS,
237
+ .ml_doc=mod_get_pending_cleanup_count_doc
238
+ },
239
+ {
240
+ .ml_name="get_total_main_greenlets",
241
+ .ml_meth=(PyCFunction)mod_get_total_main_greenlets,
242
+ .ml_flags=METH_NOARGS,
243
+ .ml_doc=mod_get_total_main_greenlets_doc
244
+ },
245
+ {
246
+ .ml_name="get_clocks_used_doing_optional_cleanup",
247
+ .ml_meth=(PyCFunction)mod_get_clocks_used_doing_optional_cleanup,
248
+ .ml_flags=METH_NOARGS,
249
+ .ml_doc=mod_get_clocks_used_doing_optional_cleanup_doc
250
+ },
251
+ {
252
+ .ml_name="enable_optional_cleanup",
253
+ .ml_meth=(PyCFunction)mod_enable_optional_cleanup,
254
+ .ml_flags=METH_O,
255
+ .ml_doc=mod_enable_optional_cleanup_doc
256
+ },
257
+ #if !GREENLET_PY313
258
+ {
259
+ .ml_name="get_tstate_trash_delete_nesting",
260
+ .ml_meth=(PyCFunction)mod_get_tstate_trash_delete_nesting,
261
+ .ml_flags=METH_NOARGS,
262
+ .ml_doc=mod_get_tstate_trash_delete_nesting_doc
263
+ },
264
+ #endif
265
+ {.ml_name=NULL, .ml_meth=NULL} /* Sentinel */
266
+ };
267
+
268
+ static const char* const copy_on_greentype[] = {
269
+ "getcurrent",
270
+ "error",
271
+ "GreenletExit",
272
+ "settrace",
273
+ "gettrace",
274
+ NULL
275
+ };
276
+
277
+ static struct PyModuleDef greenlet_module_def = {
278
+ .m_base=PyModuleDef_HEAD_INIT,
279
+ .m_name="greenlet._greenlet",
280
+ .m_doc=NULL,
281
+ .m_size=-1,
282
+ .m_methods=GreenMethods,
283
+ };
284
+
285
+
286
+ #endif
287
+
288
+ #ifdef __clang__
289
+ # pragma clang diagnostic pop
290
+ #elif defined(__GNUC__)
291
+ # pragma GCC diagnostic pop
292
+ #endif
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/TBrokenGreenlet.cpp ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* -*- indent-tabs-mode: nil; tab-width: 4; -*- */
2
+ /**
3
+ * Implementation of greenlet::UserGreenlet.
4
+ *
5
+ * Format with:
6
+ * clang-format -i --style=file src/greenlet/greenlet.c
7
+ *
8
+ *
9
+ * Fix missing braces with:
10
+ * clang-tidy src/greenlet/greenlet.c -fix -checks="readability-braces-around-statements"
11
+ */
12
+
13
+ #include "TGreenlet.hpp"
14
+
15
+ namespace greenlet {
16
+
17
+ void* BrokenGreenlet::operator new(size_t UNUSED(count))
18
+ {
19
+ return allocator.allocate(1);
20
+ }
21
+
22
+
23
+ void BrokenGreenlet::operator delete(void* ptr)
24
+ {
25
+ return allocator.deallocate(static_cast<BrokenGreenlet*>(ptr),
26
+ 1);
27
+ }
28
+
29
+ greenlet::PythonAllocator<greenlet::BrokenGreenlet> greenlet::BrokenGreenlet::allocator;
30
+
31
+ bool
32
+ BrokenGreenlet::force_slp_switch_error() const noexcept
33
+ {
34
+ return this->_force_slp_switch_error;
35
+ }
36
+
37
+ UserGreenlet::switchstack_result_t BrokenGreenlet::g_switchstack(void)
38
+ {
39
+ if (this->_force_switch_error) {
40
+ return switchstack_result_t(-1);
41
+ }
42
+ return UserGreenlet::g_switchstack();
43
+ }
44
+
45
+ }; //namespace greenlet
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/TExceptionState.cpp ADDED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #ifndef GREENLET_EXCEPTION_STATE_CPP
2
+ #define GREENLET_EXCEPTION_STATE_CPP
3
+
4
+ #include <Python.h>
5
+ #include "TGreenlet.hpp"
6
+
7
+ namespace greenlet {
8
+
9
+
10
+ ExceptionState::ExceptionState()
11
+ {
12
+ this->clear();
13
+ }
14
+
15
+ void ExceptionState::operator<<(const PyThreadState *const tstate) noexcept
16
+ {
17
+ this->exc_info = tstate->exc_info;
18
+ this->exc_state = tstate->exc_state;
19
+ }
20
+
21
+ void ExceptionState::operator>>(PyThreadState *const tstate) noexcept
22
+ {
23
+ tstate->exc_state = this->exc_state;
24
+ tstate->exc_info =
25
+ this->exc_info ? this->exc_info : &tstate->exc_state;
26
+ this->clear();
27
+ }
28
+
29
+ void ExceptionState::clear() noexcept
30
+ {
31
+ this->exc_info = nullptr;
32
+ this->exc_state.exc_value = nullptr;
33
+ #if !GREENLET_PY311
34
+ this->exc_state.exc_type = nullptr;
35
+ this->exc_state.exc_traceback = nullptr;
36
+ #endif
37
+ this->exc_state.previous_item = nullptr;
38
+ }
39
+
40
+ int ExceptionState::tp_traverse(visitproc visit, void* arg) noexcept
41
+ {
42
+ Py_VISIT(this->exc_state.exc_value);
43
+ #if !GREENLET_PY311
44
+ Py_VISIT(this->exc_state.exc_type);
45
+ Py_VISIT(this->exc_state.exc_traceback);
46
+ #endif
47
+ return 0;
48
+ }
49
+
50
+ void ExceptionState::tp_clear() noexcept
51
+ {
52
+ Py_CLEAR(this->exc_state.exc_value);
53
+ #if !GREENLET_PY311
54
+ Py_CLEAR(this->exc_state.exc_type);
55
+ Py_CLEAR(this->exc_state.exc_traceback);
56
+ #endif
57
+ }
58
+
59
+
60
+ }; // namespace greenlet
61
+
62
+ #endif // GREENLET_EXCEPTION_STATE_CPP
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/TGreenlet.cpp ADDED
@@ -0,0 +1,718 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* -*- indent-tabs-mode: nil; tab-width: 4; -*- */
2
+ /**
3
+ * Implementation of greenlet::Greenlet.
4
+ *
5
+ * Format with:
6
+ * clang-format -i --style=file src/greenlet/greenlet.c
7
+ *
8
+ *
9
+ * Fix missing braces with:
10
+ * clang-tidy src/greenlet/greenlet.c -fix -checks="readability-braces-around-statements"
11
+ */
12
+ #ifndef TGREENLET_CPP
13
+ #define TGREENLET_CPP
14
+ #include "greenlet_internal.hpp"
15
+ #include "TGreenlet.hpp"
16
+
17
+
18
+ #include "TGreenletGlobals.cpp"
19
+ #include "TThreadStateDestroy.cpp"
20
+
21
+ namespace greenlet {
22
+
23
+ Greenlet::Greenlet(PyGreenlet* p)
24
+ : Greenlet(p, StackState())
25
+ {
26
+ }
27
+
28
+ Greenlet::Greenlet(PyGreenlet* p, const StackState& initial_stack)
29
+ : _self(p), stack_state(initial_stack)
30
+ {
31
+ assert(p->pimpl == nullptr);
32
+ p->pimpl = this;
33
+ }
34
+
35
+ Greenlet::~Greenlet()
36
+ {
37
+ // XXX: Can't do this. tp_clear is a virtual function, and by the
38
+ // time we're here, we've sliced off our child classes.
39
+ //this->tp_clear();
40
+ this->_self->pimpl = nullptr;
41
+ }
42
+
43
+ bool
44
+ Greenlet::force_slp_switch_error() const noexcept
45
+ {
46
+ return false;
47
+ }
48
+
49
+ void
50
+ Greenlet::release_args()
51
+ {
52
+ this->switch_args.CLEAR();
53
+ }
54
+
55
+ /**
56
+ * CAUTION: This will allocate memory and may trigger garbage
57
+ * collection and arbitrary Python code.
58
+ */
59
+ OwnedObject
60
+ Greenlet::throw_GreenletExit_during_dealloc(const ThreadState& UNUSED(current_thread_state))
61
+ {
62
+ // If we're killed because we lost all references in the
63
+ // middle of a switch, that's ok. Don't reset the args/kwargs,
64
+ // we still want to pass them to the parent.
65
+ PyErr_SetString(mod_globs->PyExc_GreenletExit,
66
+ "Killing the greenlet because all references have vanished.");
67
+ // To get here it had to have run before
68
+ return this->g_switch();
69
+ }
70
+
71
+ inline void
72
+ Greenlet::slp_restore_state() noexcept
73
+ {
74
+ #ifdef SLP_BEFORE_RESTORE_STATE
75
+ SLP_BEFORE_RESTORE_STATE();
76
+ #endif
77
+ this->stack_state.copy_heap_to_stack(
78
+ this->thread_state()->borrow_current()->stack_state);
79
+ }
80
+
81
+
82
+ inline int
83
+ Greenlet::slp_save_state(char *const stackref) noexcept
84
+ {
85
+ // XXX: This used to happen in the middle, before saving, but
86
+ // after finding the next owner. Does that matter? This is
87
+ // only defined for Sparc/GCC where it flushes register
88
+ // windows to the stack (I think)
89
+ #ifdef SLP_BEFORE_SAVE_STATE
90
+ SLP_BEFORE_SAVE_STATE();
91
+ #endif
92
+ return this->stack_state.copy_stack_to_heap(stackref,
93
+ this->thread_state()->borrow_current()->stack_state);
94
+ }
95
+
96
+ /**
97
+ * CAUTION: This will allocate memory and may trigger garbage
98
+ * collection and arbitrary Python code.
99
+ */
100
+ OwnedObject
101
+ Greenlet::on_switchstack_or_initialstub_failure(
102
+ Greenlet* target,
103
+ const Greenlet::switchstack_result_t& err,
104
+ const bool target_was_me,
105
+ const bool was_initial_stub)
106
+ {
107
+ // If we get here, either g_initialstub()
108
+ // failed, or g_switchstack() failed. Either one of those
109
+ // cases SHOULD leave us in the original greenlet with a valid stack.
110
+ if (!PyErr_Occurred()) {
111
+ PyErr_SetString(
112
+ PyExc_SystemError,
113
+ was_initial_stub
114
+ ? "Failed to switch stacks into a greenlet for the first time."
115
+ : "Failed to switch stacks into a running greenlet.");
116
+ }
117
+ this->release_args();
118
+
119
+ if (target && !target_was_me) {
120
+ target->murder_in_place();
121
+ }
122
+
123
+ assert(!err.the_new_current_greenlet);
124
+ assert(!err.origin_greenlet);
125
+ return OwnedObject();
126
+
127
+ }
128
+
129
+ OwnedGreenlet
130
+ Greenlet::g_switchstack_success() noexcept
131
+ {
132
+ PyThreadState* tstate = PyThreadState_GET();
133
+ // restore the saved state
134
+ this->python_state >> tstate;
135
+ this->exception_state >> tstate;
136
+
137
+ // The thread state hasn't been changed yet.
138
+ ThreadState* thread_state = this->thread_state();
139
+ OwnedGreenlet result(thread_state->get_current());
140
+ thread_state->set_current(this->self());
141
+ //assert(thread_state->borrow_current().borrow() == this->_self);
142
+ return result;
143
+ }
144
+
145
+ Greenlet::switchstack_result_t
146
+ Greenlet::g_switchstack(void)
147
+ {
148
+ // if any of these assertions fail, it's likely because we
149
+ // switched away and tried to switch back to us. Early stages of
150
+ // switching are not reentrant because we re-use ``this->args()``.
151
+ // Switching away would happen if we trigger a garbage collection
152
+ // (by just using some Python APIs that happen to allocate Python
153
+ // objects) and some garbage had weakref callbacks or __del__ that
154
+ // switches (people don't write code like that by hand, but with
155
+ // gevent it's possible without realizing it)
156
+ assert(this->args() || PyErr_Occurred());
157
+ { /* save state */
158
+ if (this->thread_state()->is_current(this->self())) {
159
+ // Hmm, nothing to do.
160
+ // TODO: Does this bypass trace events that are
161
+ // important?
162
+ return switchstack_result_t(0,
163
+ this, this->thread_state()->borrow_current());
164
+ }
165
+ BorrowedGreenlet current = this->thread_state()->borrow_current();
166
+ PyThreadState* tstate = PyThreadState_GET();
167
+
168
+ current->python_state << tstate;
169
+ current->exception_state << tstate;
170
+ this->python_state.will_switch_from(tstate);
171
+ switching_thread_state = this;
172
+ current->expose_frames();
173
+ }
174
+ assert(this->args() || PyErr_Occurred());
175
+ // If this is the first switch into a greenlet, this will
176
+ // return twice, once with 1 in the new greenlet, once with 0
177
+ // in the origin.
178
+ int err;
179
+ if (this->force_slp_switch_error()) {
180
+ err = -1;
181
+ }
182
+ else {
183
+ err = slp_switch();
184
+ }
185
+
186
+ if (err < 0) { /* error */
187
+ // Tested by
188
+ // test_greenlet.TestBrokenGreenlets.test_failed_to_slp_switch_into_running
189
+ //
190
+ // It's not clear if it's worth trying to clean up and
191
+ // continue here. Failing to switch stacks is a big deal which
192
+ // may not be recoverable (who knows what state the stack is in).
193
+ // Also, we've stolen references in preparation for calling
194
+ // ``g_switchstack_success()`` and we don't have a clean
195
+ // mechanism for backing that all out.
196
+ Py_FatalError("greenlet: Failed low-level slp_switch(). The stack is probably corrupt.");
197
+ }
198
+
199
+ // No stack-based variables are valid anymore.
200
+
201
+ // But the global is volatile so we can reload it without the
202
+ // compiler caching it from earlier.
203
+ Greenlet* greenlet_that_switched_in = switching_thread_state; // aka this
204
+ switching_thread_state = nullptr;
205
+ // except that no stack variables are valid, we would:
206
+ // assert(this == greenlet_that_switched_in);
207
+
208
+ // switchstack success is where we restore the exception state,
209
+ // etc. It returns the origin greenlet because its convenient.
210
+
211
+ OwnedGreenlet origin = greenlet_that_switched_in->g_switchstack_success();
212
+ assert(greenlet_that_switched_in->args() || PyErr_Occurred());
213
+ return switchstack_result_t(err, greenlet_that_switched_in, origin);
214
+ }
215
+
216
+
217
+ inline void
218
+ Greenlet::check_switch_allowed() const
219
+ {
220
+ // TODO: Make this take a parameter of the current greenlet,
221
+ // or current main greenlet, to make the check for
222
+ // cross-thread switching cheaper. Surely somewhere up the
223
+ // call stack we've already accessed the thread local variable.
224
+
225
+ // We expect to always have a main greenlet now; accessing the thread state
226
+ // created it. However, if we get here and cleanup has already
227
+ // begun because we're a greenlet that was running in a
228
+ // (now dead) thread, these invariants will not hold true. In
229
+ // fact, accessing `this->thread_state` may not even be possible.
230
+
231
+ // If the thread this greenlet was running in is dead,
232
+ // we'll still have a reference to a main greenlet, but the
233
+ // thread state pointer we have is bogus.
234
+ // TODO: Give the objects an API to determine if they belong
235
+ // to a dead thread.
236
+
237
+ const BorrowedMainGreenlet main_greenlet = this->find_main_greenlet_in_lineage();
238
+
239
+ if (!main_greenlet) {
240
+ throw PyErrOccurred(mod_globs->PyExc_GreenletError,
241
+ "cannot switch to a garbage collected greenlet");
242
+ }
243
+
244
+ if (!main_greenlet->thread_state()) {
245
+ throw PyErrOccurred(mod_globs->PyExc_GreenletError,
246
+ "cannot switch to a different thread (which happens to have exited)");
247
+ }
248
+
249
+ // The main greenlet we found was from the .parent lineage.
250
+ // That may or may not have any relationship to the main
251
+ // greenlet of the running thread. We can't actually access
252
+ // our this->thread_state members to try to check that,
253
+ // because it could be in the process of getting destroyed,
254
+ // but setting the main_greenlet->thread_state member to NULL
255
+ // may not be visible yet. So we need to check against the
256
+ // current thread state (once the cheaper checks are out of
257
+ // the way)
258
+ const BorrowedMainGreenlet current_main_greenlet = GET_THREAD_STATE().state().borrow_main_greenlet();
259
+ if (
260
+ // lineage main greenlet is not this thread's greenlet
261
+ current_main_greenlet != main_greenlet
262
+ || (
263
+ // atteched to some thread
264
+ this->main_greenlet()
265
+ // XXX: Same condition as above. Was this supposed to be
266
+ // this->main_greenlet()?
267
+ && current_main_greenlet != main_greenlet)
268
+ // switching into a known dead thread (XXX: which, if we get here,
269
+ // is bad, because we just accessed the thread state, which is
270
+ // gone!)
271
+ || (!current_main_greenlet->thread_state())) {
272
+ // CAUTION: This may trigger memory allocations, gc, and
273
+ // arbitrary Python code.
274
+ throw PyErrOccurred(
275
+ mod_globs->PyExc_GreenletError,
276
+ "Cannot switch to a different thread\n\tCurrent: %R\n\tExpected: %R",
277
+ current_main_greenlet, main_greenlet);
278
+ }
279
+ }
280
+
281
+ const OwnedObject
282
+ Greenlet::context() const
283
+ {
284
+ using greenlet::PythonStateContext;
285
+ OwnedObject result;
286
+
287
+ if (this->is_currently_running_in_some_thread()) {
288
+ /* Currently running greenlet: context is stored in the thread state,
289
+ not the greenlet object. */
290
+ if (GET_THREAD_STATE().state().is_current(this->self())) {
291
+ result = PythonStateContext::context(PyThreadState_GET());
292
+ }
293
+ else {
294
+ throw ValueError(
295
+ "cannot get context of a "
296
+ "greenlet that is running in a different thread");
297
+ }
298
+ }
299
+ else {
300
+ /* Greenlet is not running: just return context. */
301
+ result = this->python_state.context();
302
+ }
303
+ if (!result) {
304
+ result = OwnedObject::None();
305
+ }
306
+ return result;
307
+ }
308
+
309
+
310
+ void
311
+ Greenlet::context(BorrowedObject given)
312
+ {
313
+ using greenlet::PythonStateContext;
314
+ if (!given) {
315
+ throw AttributeError("can't delete context attribute");
316
+ }
317
+ if (given.is_None()) {
318
+ /* "Empty context" is stored as NULL, not None. */
319
+ given = nullptr;
320
+ }
321
+
322
+ //checks type, incrs refcnt
323
+ greenlet::refs::OwnedContext context(given);
324
+ PyThreadState* tstate = PyThreadState_GET();
325
+
326
+ if (this->is_currently_running_in_some_thread()) {
327
+ if (!GET_THREAD_STATE().state().is_current(this->self())) {
328
+ throw ValueError("cannot set context of a greenlet"
329
+ " that is running in a different thread");
330
+ }
331
+
332
+ /* Currently running greenlet: context is stored in the thread state,
333
+ not the greenlet object. */
334
+ OwnedObject octx = OwnedObject::consuming(PythonStateContext::context(tstate));
335
+ PythonStateContext::context(tstate, context.relinquish_ownership());
336
+ }
337
+ else {
338
+ /* Greenlet is not running: just set context. Note that the
339
+ greenlet may be dead.*/
340
+ this->python_state.context() = context;
341
+ }
342
+ }
343
+
344
+ /**
345
+ * CAUTION: May invoke arbitrary Python code.
346
+ *
347
+ * Figure out what the result of ``greenlet.switch(arg, kwargs)``
348
+ * should be and transfers ownership of it to the left-hand-side.
349
+ *
350
+ * If switch() was just passed an arg tuple, then we'll just return that.
351
+ * If only keyword arguments were passed, then we'll pass the keyword
352
+ * argument dict. Otherwise, we'll create a tuple of (args, kwargs) and
353
+ * return both.
354
+ *
355
+ * CAUTION: This may allocate a new tuple object, which may
356
+ * cause the Python garbage collector to run, which in turn may
357
+ * run arbitrary Python code that switches.
358
+ */
359
+ OwnedObject& operator<<=(OwnedObject& lhs, greenlet::SwitchingArgs& rhs) noexcept
360
+ {
361
+ // Because this may invoke arbitrary Python code, which could
362
+ // result in switching back to us, we need to get the
363
+ // arguments locally on the stack.
364
+ assert(rhs);
365
+ OwnedObject args = rhs.args();
366
+ OwnedObject kwargs = rhs.kwargs();
367
+ rhs.CLEAR();
368
+ // We shouldn't be called twice for the same switch.
369
+ assert(args || kwargs);
370
+ assert(!rhs);
371
+
372
+ if (!kwargs) {
373
+ lhs = args;
374
+ }
375
+ else if (!PyDict_Size(kwargs.borrow())) {
376
+ lhs = args;
377
+ }
378
+ else if (!PySequence_Length(args.borrow())) {
379
+ lhs = kwargs;
380
+ }
381
+ else {
382
+ // PyTuple_Pack allocates memory, may GC, may run arbitrary
383
+ // Python code.
384
+ lhs = OwnedObject::consuming(PyTuple_Pack(2, args.borrow(), kwargs.borrow()));
385
+ }
386
+ return lhs;
387
+ }
388
+
389
+ static OwnedObject
390
+ g_handle_exit(const OwnedObject& greenlet_result)
391
+ {
392
+ if (!greenlet_result && mod_globs->PyExc_GreenletExit.PyExceptionMatches()) {
393
+ /* catch and ignore GreenletExit */
394
+ PyErrFetchParam val;
395
+ PyErr_Fetch(PyErrFetchParam(), val, PyErrFetchParam());
396
+ if (!val) {
397
+ return OwnedObject::None();
398
+ }
399
+ return OwnedObject(val);
400
+ }
401
+
402
+ if (greenlet_result) {
403
+ // package the result into a 1-tuple
404
+ // PyTuple_Pack increments the reference of its arguments,
405
+ // so we always need to decref the greenlet result;
406
+ // the owner will do that.
407
+ return OwnedObject::consuming(PyTuple_Pack(1, greenlet_result.borrow()));
408
+ }
409
+
410
+ return OwnedObject();
411
+ }
412
+
413
+
414
+
415
+ /**
416
+ * May run arbitrary Python code.
417
+ */
418
+ OwnedObject
419
+ Greenlet::g_switch_finish(const switchstack_result_t& err)
420
+ {
421
+ assert(err.the_new_current_greenlet == this);
422
+
423
+ ThreadState& state = *this->thread_state();
424
+ // Because calling the trace function could do arbitrary things,
425
+ // including switching away from this greenlet and then maybe
426
+ // switching back, we need to capture the arguments now so that
427
+ // they don't change.
428
+ OwnedObject result;
429
+ if (this->args()) {
430
+ result <<= this->args();
431
+ }
432
+ else {
433
+ assert(PyErr_Occurred());
434
+ }
435
+ assert(!this->args());
436
+ try {
437
+ // Our only caller handles the bad error case
438
+ assert(err.status >= 0);
439
+ assert(state.borrow_current() == this->self());
440
+ if (OwnedObject tracefunc = state.get_tracefunc()) {
441
+ assert(result || PyErr_Occurred());
442
+ g_calltrace(tracefunc,
443
+ result ? mod_globs->event_switch : mod_globs->event_throw,
444
+ err.origin_greenlet,
445
+ this->self());
446
+ }
447
+ // The above could have invoked arbitrary Python code, but
448
+ // it couldn't switch back to this object and *also*
449
+ // throw an exception, so the args won't have changed.
450
+
451
+ if (PyErr_Occurred()) {
452
+ // We get here if we fell of the end of the run() function
453
+ // raising an exception. The switch itself was
454
+ // successful, but the function raised.
455
+ // valgrind reports that memory allocated here can still
456
+ // be reached after a test run.
457
+ throw PyErrOccurred::from_current();
458
+ }
459
+ return result;
460
+ }
461
+ catch (const PyErrOccurred&) {
462
+ /* Turn switch errors into switch throws */
463
+ /* Turn trace errors into switch throws */
464
+ this->release_args();
465
+ throw;
466
+ }
467
+ }
468
+
469
+ void
470
+ Greenlet::g_calltrace(const OwnedObject& tracefunc,
471
+ const greenlet::refs::ImmortalEventName& event,
472
+ const BorrowedGreenlet& origin,
473
+ const BorrowedGreenlet& target)
474
+ {
475
+ PyErrPieces saved_exc;
476
+ try {
477
+ TracingGuard tracing_guard;
478
+ // TODO: We have saved the active exception (if any) that's
479
+ // about to be raised. In the 'throw' case, we could provide
480
+ // the exception to the tracefunction, which seems very helpful.
481
+ tracing_guard.CallTraceFunction(tracefunc, event, origin, target);
482
+ }
483
+ catch (const PyErrOccurred&) {
484
+ // In case of exceptions trace function is removed,
485
+ // and any existing exception is replaced with the tracing
486
+ // exception.
487
+ GET_THREAD_STATE().state().set_tracefunc(Py_None);
488
+ throw;
489
+ }
490
+
491
+ saved_exc.PyErrRestore();
492
+ assert(
493
+ (event == mod_globs->event_throw && PyErr_Occurred())
494
+ || (event == mod_globs->event_switch && !PyErr_Occurred())
495
+ );
496
+ }
497
+
498
+ void
499
+ Greenlet::murder_in_place()
500
+ {
501
+ if (this->active()) {
502
+ assert(!this->is_currently_running_in_some_thread());
503
+ this->deactivate_and_free();
504
+ }
505
+ }
506
+
507
+ inline void
508
+ Greenlet::deactivate_and_free()
509
+ {
510
+ if (!this->active()) {
511
+ return;
512
+ }
513
+ // Throw away any saved stack.
514
+ this->stack_state = StackState();
515
+ assert(!this->stack_state.active());
516
+ // Throw away any Python references.
517
+ // We're holding a borrowed reference to the last
518
+ // frame we executed. Since we borrowed it, the
519
+ // normal traversal, clear, and dealloc functions
520
+ // ignore it, meaning it leaks. (The thread state
521
+ // object can't find it to clear it when that's
522
+ // deallocated either, because by definition if we
523
+ // got an object on this list, it wasn't
524
+ // running and the thread state doesn't have
525
+ // this frame.)
526
+ // So here, we *do* clear it.
527
+ this->python_state.tp_clear(true);
528
+ }
529
+
530
+ bool
531
+ Greenlet::belongs_to_thread(const ThreadState* thread_state) const
532
+ {
533
+ if (!this->thread_state() // not running anywhere, or thread
534
+ // exited
535
+ || !thread_state) { // same, or there is no thread state.
536
+ return false;
537
+ }
538
+ return true;
539
+ }
540
+
541
+
542
+ void
543
+ Greenlet::deallocing_greenlet_in_thread(const ThreadState* current_thread_state)
544
+ {
545
+ /* Cannot raise an exception to kill the greenlet if
546
+ it is not running in the same thread! */
547
+ if (this->belongs_to_thread(current_thread_state)) {
548
+ assert(current_thread_state);
549
+ // To get here it had to have run before
550
+ /* Send the greenlet a GreenletExit exception. */
551
+
552
+ // We don't care about the return value, only whether an
553
+ // exception happened.
554
+ this->throw_GreenletExit_during_dealloc(*current_thread_state);
555
+ return;
556
+ }
557
+
558
+ // Not the same thread! Temporarily save the greenlet
559
+ // into its thread's deleteme list, *if* it exists.
560
+ // If that thread has already exited, and processed its pending
561
+ // cleanup, we'll never be able to clean everything up: we won't
562
+ // be able to raise an exception.
563
+ // That's mostly OK! Since we can't add it to a list, our refcount
564
+ // won't increase, and we'll go ahead with the DECREFs later.
565
+ ThreadState *const thread_state = this->thread_state();
566
+ if (thread_state) {
567
+ thread_state->delete_when_thread_running(this->self());
568
+ }
569
+ else {
570
+ // The thread is dead, we can't raise an exception.
571
+ // We need to make it look non-active, though, so that dealloc
572
+ // finishes killing it.
573
+ this->deactivate_and_free();
574
+ }
575
+ return;
576
+ }
577
+
578
+
579
+ int
580
+ Greenlet::tp_traverse(visitproc visit, void* arg)
581
+ {
582
+
583
+ int result;
584
+ if ((result = this->exception_state.tp_traverse(visit, arg)) != 0) {
585
+ return result;
586
+ }
587
+ //XXX: This is ugly. But so is handling everything having to do
588
+ //with the top frame.
589
+ bool visit_top_frame = this->was_running_in_dead_thread();
590
+ // When true, the thread is dead. Our implicit weak reference to the
591
+ // frame is now all that's left; we consider ourselves to
592
+ // strongly own it now.
593
+ if ((result = this->python_state.tp_traverse(visit, arg, visit_top_frame)) != 0) {
594
+ return result;
595
+ }
596
+ return 0;
597
+ }
598
+
599
+ int
600
+ Greenlet::tp_clear()
601
+ {
602
+ bool own_top_frame = this->was_running_in_dead_thread();
603
+ this->exception_state.tp_clear();
604
+ this->python_state.tp_clear(own_top_frame);
605
+ return 0;
606
+ }
607
+
608
+ bool Greenlet::is_currently_running_in_some_thread() const
609
+ {
610
+ return this->stack_state.active() && !this->python_state.top_frame();
611
+ }
612
+
613
+ #if GREENLET_PY312
614
+ void GREENLET_NOINLINE(Greenlet::expose_frames)()
615
+ {
616
+ if (!this->python_state.top_frame()) {
617
+ return;
618
+ }
619
+
620
+ _PyInterpreterFrame* last_complete_iframe = nullptr;
621
+ _PyInterpreterFrame* iframe = this->python_state.top_frame()->f_frame;
622
+ while (iframe) {
623
+ // We must make a copy before looking at the iframe contents,
624
+ // since iframe might point to a portion of the greenlet's C stack
625
+ // that was spilled when switching greenlets.
626
+ _PyInterpreterFrame iframe_copy;
627
+ this->stack_state.copy_from_stack(&iframe_copy, iframe, sizeof(*iframe));
628
+ if (!_PyFrame_IsIncomplete(&iframe_copy)) {
629
+ // If the iframe were OWNED_BY_CSTACK then it would always be
630
+ // incomplete. Since it's not incomplete, it's not on the C stack
631
+ // and we can access it through the original `iframe` pointer
632
+ // directly. This is important since GetFrameObject might
633
+ // lazily _create_ the frame object and we don't want the
634
+ // interpreter to lose track of it.
635
+ assert(iframe_copy.owner != FRAME_OWNED_BY_CSTACK);
636
+
637
+ // We really want to just write:
638
+ // PyFrameObject* frame = _PyFrame_GetFrameObject(iframe);
639
+ // but _PyFrame_GetFrameObject calls _PyFrame_MakeAndSetFrameObject
640
+ // which is not a visible symbol in libpython. The easiest
641
+ // way to get a public function to call it is using
642
+ // PyFrame_GetBack, which is defined as follows:
643
+ // assert(frame != NULL);
644
+ // assert(!_PyFrame_IsIncomplete(frame->f_frame));
645
+ // PyFrameObject *back = frame->f_back;
646
+ // if (back == NULL) {
647
+ // _PyInterpreterFrame *prev = frame->f_frame->previous;
648
+ // prev = _PyFrame_GetFirstComplete(prev);
649
+ // if (prev) {
650
+ // back = _PyFrame_GetFrameObject(prev);
651
+ // }
652
+ // }
653
+ // return (PyFrameObject*)Py_XNewRef(back);
654
+ if (!iframe->frame_obj) {
655
+ PyFrameObject dummy_frame;
656
+ _PyInterpreterFrame dummy_iframe;
657
+ dummy_frame.f_back = nullptr;
658
+ dummy_frame.f_frame = &dummy_iframe;
659
+ // force the iframe to be considered complete without
660
+ // needing to check its code object:
661
+ dummy_iframe.owner = FRAME_OWNED_BY_GENERATOR;
662
+ dummy_iframe.previous = iframe;
663
+ assert(!_PyFrame_IsIncomplete(&dummy_iframe));
664
+ // Drop the returned reference immediately; the iframe
665
+ // continues to hold a strong reference
666
+ Py_XDECREF(PyFrame_GetBack(&dummy_frame));
667
+ assert(iframe->frame_obj);
668
+ }
669
+
670
+ // This is a complete frame, so make the last one of those we saw
671
+ // point at it, bypassing any incomplete frames (which may have
672
+ // been on the C stack) in between the two. We're overwriting
673
+ // last_complete_iframe->previous and need that to be reversible,
674
+ // so we store the original previous ptr in the frame object
675
+ // (which we must have created on a previous iteration through
676
+ // this loop). The frame object has a bunch of storage that is
677
+ // only used when its iframe is OWNED_BY_FRAME_OBJECT, which only
678
+ // occurs when the frame object outlives the frame's execution,
679
+ // which can't have happened yet because the frame is currently
680
+ // executing as far as the interpreter is concerned. So, we can
681
+ // reuse it for our own purposes.
682
+ assert(iframe->owner == FRAME_OWNED_BY_THREAD
683
+ || iframe->owner == FRAME_OWNED_BY_GENERATOR);
684
+ if (last_complete_iframe) {
685
+ assert(last_complete_iframe->frame_obj);
686
+ memcpy(&last_complete_iframe->frame_obj->_f_frame_data[0],
687
+ &last_complete_iframe->previous, sizeof(void *));
688
+ last_complete_iframe->previous = iframe;
689
+ }
690
+ last_complete_iframe = iframe;
691
+ }
692
+ // Frames that are OWNED_BY_FRAME_OBJECT are linked via the
693
+ // frame's f_back while all others are linked via the iframe's
694
+ // previous ptr. Since all the frames we traverse are running
695
+ // as far as the interpreter is concerned, we don't have to
696
+ // worry about the OWNED_BY_FRAME_OBJECT case.
697
+ iframe = iframe_copy.previous;
698
+ }
699
+
700
+ // Give the outermost complete iframe a null previous pointer to
701
+ // account for any potential incomplete/C-stack iframes between it
702
+ // and the actual top-of-stack
703
+ if (last_complete_iframe) {
704
+ assert(last_complete_iframe->frame_obj);
705
+ memcpy(&last_complete_iframe->frame_obj->_f_frame_data[0],
706
+ &last_complete_iframe->previous, sizeof(void *));
707
+ last_complete_iframe->previous = nullptr;
708
+ }
709
+ }
710
+ #else
711
+ void Greenlet::expose_frames()
712
+ {
713
+
714
+ }
715
+ #endif
716
+
717
+ }; // namespace greenlet
718
+ #endif
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/TGreenlet.hpp ADDED
@@ -0,0 +1,824 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #ifndef GREENLET_GREENLET_HPP
2
+ #define GREENLET_GREENLET_HPP
3
+ /*
4
+ * Declarations of the core data structures.
5
+ */
6
+
7
+ #define PY_SSIZE_T_CLEAN
8
+ #include <Python.h>
9
+
10
+ #include "greenlet_compiler_compat.hpp"
11
+ #include "greenlet_refs.hpp"
12
+ #include "greenlet_cpython_compat.hpp"
13
+ #include "greenlet_allocator.hpp"
14
+
15
+ using greenlet::refs::OwnedObject;
16
+ using greenlet::refs::OwnedGreenlet;
17
+ using greenlet::refs::OwnedMainGreenlet;
18
+ using greenlet::refs::BorrowedGreenlet;
19
+
20
+ #if PY_VERSION_HEX < 0x30B00A6
21
+ # define _PyCFrame CFrame
22
+ # define _PyInterpreterFrame _interpreter_frame
23
+ #endif
24
+
25
+ #if GREENLET_PY312
26
+ # define Py_BUILD_CORE
27
+ # include "internal/pycore_frame.h"
28
+ #endif
29
+
30
+ #if GREENLET_PY314
31
+ # include "internal/pycore_interpframe_structs.h"
32
+ #if defined(_MSC_VER) || defined(__MINGW64__)
33
+ # include "greenlet_msvc_compat.hpp"
34
+ #else
35
+ # include "internal/pycore_interpframe.h"
36
+ #endif
37
+ #endif
38
+
39
+ // XXX: TODO: Work to remove all virtual functions
40
+ // for speed of calling and size of objects (no vtable).
41
+ // One pattern is the Curiously Recurring Template
42
+ namespace greenlet
43
+ {
44
+ class ExceptionState
45
+ {
46
+ private:
47
+ G_NO_COPIES_OF_CLS(ExceptionState);
48
+
49
+ // Even though these are borrowed objects, we actually own
50
+ // them, when they're not null.
51
+ // XXX: Express that in the API.
52
+ private:
53
+ _PyErr_StackItem* exc_info;
54
+ _PyErr_StackItem exc_state;
55
+ public:
56
+ ExceptionState();
57
+ void operator<<(const PyThreadState *const tstate) noexcept;
58
+ void operator>>(PyThreadState* tstate) noexcept;
59
+ void clear() noexcept;
60
+
61
+ int tp_traverse(visitproc visit, void* arg) noexcept;
62
+ void tp_clear() noexcept;
63
+ };
64
+
65
+ template<typename T>
66
+ void operator<<(const PyThreadState *const tstate, T& exc);
67
+
68
+ class PythonStateContext
69
+ {
70
+ protected:
71
+ greenlet::refs::OwnedContext _context;
72
+ public:
73
+ inline const greenlet::refs::OwnedContext& context() const
74
+ {
75
+ return this->_context;
76
+ }
77
+ inline greenlet::refs::OwnedContext& context()
78
+ {
79
+ return this->_context;
80
+ }
81
+
82
+ inline void tp_clear()
83
+ {
84
+ this->_context.CLEAR();
85
+ }
86
+
87
+ template<typename T>
88
+ inline static PyObject* context(T* tstate)
89
+ {
90
+ return tstate->context;
91
+ }
92
+
93
+ template<typename T>
94
+ inline static void context(T* tstate, PyObject* new_context)
95
+ {
96
+ tstate->context = new_context;
97
+ tstate->context_ver++;
98
+ }
99
+ };
100
+ class SwitchingArgs;
101
+ class PythonState : public PythonStateContext
102
+ {
103
+ public:
104
+ typedef greenlet::refs::OwnedReference<struct _frame> OwnedFrame;
105
+ private:
106
+ G_NO_COPIES_OF_CLS(PythonState);
107
+ // We own this if we're suspended (although currently we don't
108
+ // tp_traverse into it; that's a TODO). If we're running, it's
109
+ // empty. If we get deallocated and *still* have a frame, it
110
+ // won't be reachable from the place that normally decref's
111
+ // it, so we need to do it (hence owning it).
112
+ OwnedFrame _top_frame;
113
+ #if GREENLET_USE_CFRAME
114
+ _PyCFrame* cframe;
115
+ int use_tracing;
116
+ #endif
117
+ #if GREENLET_PY314
118
+ int py_recursion_depth;
119
+ #elif GREENLET_PY312
120
+ int py_recursion_depth;
121
+ int c_recursion_depth;
122
+ #else
123
+ int recursion_depth;
124
+ #endif
125
+ #if GREENLET_PY313
126
+ PyObject *delete_later;
127
+ #else
128
+ int trash_delete_nesting;
129
+ #endif
130
+ #if GREENLET_PY311
131
+ _PyInterpreterFrame* current_frame;
132
+ _PyStackChunk* datastack_chunk;
133
+ PyObject** datastack_top;
134
+ PyObject** datastack_limit;
135
+ #endif
136
+ // The PyInterpreterFrame list on 3.12+ contains some entries that are
137
+ // on the C stack, which can't be directly accessed while a greenlet is
138
+ // suspended. In order to keep greenlet gr_frame introspection working,
139
+ // we adjust stack switching to rewrite the interpreter frame list
140
+ // to skip these C-stack frames; we call this "exposing" the greenlet's
141
+ // frames because it makes them valid to work with in Python. Then when
142
+ // the greenlet is resumed we need to remember to reverse the operation
143
+ // we did. The C-stack frames are "entry frames" which are a low-level
144
+ // interpreter detail; they're not needed for introspection, but do
145
+ // need to be present for the eval loop to work.
146
+ void unexpose_frames();
147
+
148
+ public:
149
+
150
+ PythonState();
151
+ // You can use this for testing whether we have a frame
152
+ // or not. It returns const so they can't modify it.
153
+ const OwnedFrame& top_frame() const noexcept;
154
+
155
+ inline void operator<<(const PyThreadState *const tstate) noexcept;
156
+ inline void operator>>(PyThreadState* tstate) noexcept;
157
+ void clear() noexcept;
158
+
159
+ int tp_traverse(visitproc visit, void* arg, bool visit_top_frame) noexcept;
160
+ void tp_clear(bool own_top_frame) noexcept;
161
+ void set_initial_state(const PyThreadState* const tstate) noexcept;
162
+ #if GREENLET_USE_CFRAME
163
+ void set_new_cframe(_PyCFrame& frame) noexcept;
164
+ #endif
165
+
166
+ void may_switch_away() noexcept;
167
+ inline void will_switch_from(PyThreadState *const origin_tstate) noexcept;
168
+ void did_finish(PyThreadState* tstate) noexcept;
169
+ };
170
+
171
+ class StackState
172
+ {
173
+ // By having only plain C (POD) members, no virtual functions
174
+ // or bases, we get a trivial assignment operator generated
175
+ // for us. However, that's not safe since we do manage memory.
176
+ // So we declare an assignment operator that only works if we
177
+ // don't have any memory allocated. (We don't use
178
+ // std::shared_ptr for reference counting just to keep this
179
+ // object small)
180
+ private:
181
+ char* _stack_start;
182
+ char* stack_stop;
183
+ char* stack_copy;
184
+ intptr_t _stack_saved;
185
+ StackState* stack_prev;
186
+ inline int copy_stack_to_heap_up_to(const char* const stop) noexcept;
187
+ inline void free_stack_copy() noexcept;
188
+
189
+ public:
190
+ /**
191
+ * Creates a started, but inactive, state, using *current*
192
+ * as the previous.
193
+ */
194
+ StackState(void* mark, StackState& current);
195
+ /**
196
+ * Creates an inactive, unstarted, state.
197
+ */
198
+ StackState();
199
+ ~StackState();
200
+ StackState(const StackState& other);
201
+ StackState& operator=(const StackState& other);
202
+ inline void copy_heap_to_stack(const StackState& current) noexcept;
203
+ inline int copy_stack_to_heap(char* const stackref, const StackState& current) noexcept;
204
+ inline bool started() const noexcept;
205
+ inline bool main() const noexcept;
206
+ inline bool active() const noexcept;
207
+ inline void set_active() noexcept;
208
+ inline void set_inactive() noexcept;
209
+ inline intptr_t stack_saved() const noexcept;
210
+ inline char* stack_start() const noexcept;
211
+ static inline StackState make_main() noexcept;
212
+ #ifdef GREENLET_USE_STDIO
213
+ friend std::ostream& operator<<(std::ostream& os, const StackState& s);
214
+ #endif
215
+
216
+ // Fill in [dest, dest + n) with the values that would be at
217
+ // [src, src + n) while this greenlet is running. This is like memcpy
218
+ // except that if the greenlet is suspended it accounts for the portion
219
+ // of the greenlet's stack that was spilled to the heap. `src` may
220
+ // be on this greenlet's stack, or on the heap, but not on a different
221
+ // greenlet's stack.
222
+ void copy_from_stack(void* dest, const void* src, size_t n) const;
223
+ };
224
+ #ifdef GREENLET_USE_STDIO
225
+ std::ostream& operator<<(std::ostream& os, const StackState& s);
226
+ #endif
227
+
228
+ class SwitchingArgs
229
+ {
230
+ private:
231
+ G_NO_ASSIGNMENT_OF_CLS(SwitchingArgs);
232
+ // If args and kwargs are both false (NULL), this is a *throw*, not a
233
+ // switch. PyErr_... must have been called already.
234
+ OwnedObject _args;
235
+ OwnedObject _kwargs;
236
+ public:
237
+
238
+ SwitchingArgs()
239
+ {}
240
+
241
+ SwitchingArgs(const OwnedObject& args, const OwnedObject& kwargs)
242
+ : _args(args),
243
+ _kwargs(kwargs)
244
+ {}
245
+
246
+ SwitchingArgs(const SwitchingArgs& other)
247
+ : _args(other._args),
248
+ _kwargs(other._kwargs)
249
+ {}
250
+
251
+ const OwnedObject& args()
252
+ {
253
+ return this->_args;
254
+ }
255
+
256
+ const OwnedObject& kwargs()
257
+ {
258
+ return this->_kwargs;
259
+ }
260
+
261
+ /**
262
+ * Moves ownership from the argument to this object.
263
+ */
264
+ SwitchingArgs& operator<<=(SwitchingArgs& other)
265
+ {
266
+ if (this != &other) {
267
+ this->_args = other._args;
268
+ this->_kwargs = other._kwargs;
269
+ other.CLEAR();
270
+ }
271
+ return *this;
272
+ }
273
+
274
+ /**
275
+ * Acquires ownership of the argument (consumes the reference).
276
+ */
277
+ SwitchingArgs& operator<<=(PyObject* args)
278
+ {
279
+ this->_args = OwnedObject::consuming(args);
280
+ this->_kwargs.CLEAR();
281
+ return *this;
282
+ }
283
+
284
+ /**
285
+ * Acquires ownership of the argument.
286
+ *
287
+ * Sets the args to be the given value; clears the kwargs.
288
+ */
289
+ SwitchingArgs& operator<<=(OwnedObject& args)
290
+ {
291
+ assert(&args != &this->_args);
292
+ this->_args = args;
293
+ this->_kwargs.CLEAR();
294
+ args.CLEAR();
295
+
296
+ return *this;
297
+ }
298
+
299
+ explicit operator bool() const noexcept
300
+ {
301
+ return this->_args || this->_kwargs;
302
+ }
303
+
304
+ inline void CLEAR()
305
+ {
306
+ this->_args.CLEAR();
307
+ this->_kwargs.CLEAR();
308
+ }
309
+
310
+ const std::string as_str() const noexcept
311
+ {
312
+ return PyUnicode_AsUTF8(
313
+ OwnedObject::consuming(
314
+ PyUnicode_FromFormat(
315
+ "SwitchingArgs(args=%R, kwargs=%R)",
316
+ this->_args.borrow(),
317
+ this->_kwargs.borrow()
318
+ )
319
+ ).borrow()
320
+ );
321
+ }
322
+ };
323
+
324
+ class ThreadState;
325
+
326
+ class UserGreenlet;
327
+ class MainGreenlet;
328
+
329
+ class Greenlet
330
+ {
331
+ private:
332
+ G_NO_COPIES_OF_CLS(Greenlet);
333
+ PyGreenlet* const _self;
334
+ private:
335
+ // XXX: Work to remove these.
336
+ friend class ThreadState;
337
+ friend class UserGreenlet;
338
+ friend class MainGreenlet;
339
+ protected:
340
+ ExceptionState exception_state;
341
+ SwitchingArgs switch_args;
342
+ StackState stack_state;
343
+ PythonState python_state;
344
+ Greenlet(PyGreenlet* p, const StackState& initial_state);
345
+ public:
346
+ // This constructor takes ownership of the PyGreenlet, by
347
+ // setting ``p->pimpl = this;``.
348
+ Greenlet(PyGreenlet* p);
349
+ virtual ~Greenlet();
350
+
351
+ const OwnedObject context() const;
352
+
353
+ // You MUST call this _very_ early in the switching process to
354
+ // prepare anything that may need prepared. This might perform
355
+ // garbage collections or otherwise run arbitrary Python code.
356
+ //
357
+ // One specific use of it is for Python 3.11+, preventing
358
+ // running arbitrary code at unsafe times. See
359
+ // PythonState::may_switch_away().
360
+ inline void may_switch_away()
361
+ {
362
+ this->python_state.may_switch_away();
363
+ }
364
+
365
+ inline void context(refs::BorrowedObject new_context);
366
+
367
+ inline SwitchingArgs& args()
368
+ {
369
+ return this->switch_args;
370
+ }
371
+
372
+ virtual const refs::BorrowedMainGreenlet main_greenlet() const = 0;
373
+
374
+ inline intptr_t stack_saved() const noexcept
375
+ {
376
+ return this->stack_state.stack_saved();
377
+ }
378
+
379
+ // This is used by the macro SLP_SAVE_STATE to compute the
380
+ // difference in stack sizes. It might be nice to handle the
381
+ // computation ourself, but the type of the result
382
+ // varies by platform, so doing it in the macro is the
383
+ // simplest way.
384
+ inline const char* stack_start() const noexcept
385
+ {
386
+ return this->stack_state.stack_start();
387
+ }
388
+
389
+ virtual OwnedObject throw_GreenletExit_during_dealloc(const ThreadState& current_thread_state);
390
+ virtual OwnedObject g_switch() = 0;
391
+ /**
392
+ * Force the greenlet to appear dead. Used when it's not
393
+ * possible to throw an exception into a greenlet anymore.
394
+ *
395
+ * This losses access to the thread state and the main greenlet.
396
+ */
397
+ virtual void murder_in_place();
398
+
399
+ /**
400
+ * Called when somebody notices we were running in a dead
401
+ * thread to allow cleaning up resources (because we can't
402
+ * raise GreenletExit into it anymore).
403
+ * This is very similar to ``murder_in_place()``, except that
404
+ * it DOES NOT lose the main greenlet or thread state.
405
+ */
406
+ inline void deactivate_and_free();
407
+
408
+
409
+ // Called when some thread wants to deallocate a greenlet
410
+ // object.
411
+ // The thread may or may not be the same thread the greenlet
412
+ // was running in.
413
+ // The thread state will be null if the thread the greenlet
414
+ // was running in was known to have exited.
415
+ void deallocing_greenlet_in_thread(const ThreadState* current_state);
416
+
417
+ // Must be called on 3.12+ before exposing a suspended greenlet's
418
+ // frames to user code. This rewrites the linked list of interpreter
419
+ // frames to skip the ones that are being stored on the C stack (which
420
+ // can't be safely accessed while the greenlet is suspended because
421
+ // that stack space might be hosting a different greenlet), and
422
+ // sets PythonState::frames_were_exposed so we remember to restore
423
+ // the original list before resuming the greenlet. The C-stack frames
424
+ // are a low-level interpreter implementation detail; while they're
425
+ // important to the bytecode eval loop, they're superfluous for
426
+ // introspection purposes.
427
+ void expose_frames();
428
+
429
+
430
+ // TODO: Figure out how to make these non-public.
431
+ inline void slp_restore_state() noexcept;
432
+ inline int slp_save_state(char *const stackref) noexcept;
433
+
434
+ inline bool is_currently_running_in_some_thread() const;
435
+ virtual bool belongs_to_thread(const ThreadState* state) const;
436
+
437
+ inline bool started() const
438
+ {
439
+ return this->stack_state.started();
440
+ }
441
+ inline bool active() const
442
+ {
443
+ return this->stack_state.active();
444
+ }
445
+ inline bool main() const
446
+ {
447
+ return this->stack_state.main();
448
+ }
449
+ virtual refs::BorrowedMainGreenlet find_main_greenlet_in_lineage() const = 0;
450
+
451
+ virtual const OwnedGreenlet parent() const = 0;
452
+ virtual void parent(const refs::BorrowedObject new_parent) = 0;
453
+
454
+ inline const PythonState::OwnedFrame& top_frame()
455
+ {
456
+ return this->python_state.top_frame();
457
+ }
458
+
459
+ virtual const OwnedObject& run() const = 0;
460
+ virtual void run(const refs::BorrowedObject nrun) = 0;
461
+
462
+
463
+ virtual int tp_traverse(visitproc visit, void* arg);
464
+ virtual int tp_clear();
465
+
466
+
467
+ // Return the thread state that the greenlet is running in, or
468
+ // null if the greenlet is not running or the thread is known
469
+ // to have exited.
470
+ virtual ThreadState* thread_state() const noexcept = 0;
471
+
472
+ // Return true if the greenlet is known to have been running
473
+ // (active) in a thread that has now exited.
474
+ virtual bool was_running_in_dead_thread() const noexcept = 0;
475
+
476
+ // Return a borrowed greenlet that is the Python object
477
+ // this object represents.
478
+ inline BorrowedGreenlet self() const noexcept
479
+ {
480
+ return BorrowedGreenlet(this->_self);
481
+ }
482
+
483
+ // For testing. If this returns true, we should pretend that
484
+ // slp_switch() failed.
485
+ virtual bool force_slp_switch_error() const noexcept;
486
+
487
+ protected:
488
+ inline void release_args();
489
+
490
+ // The functions that must not be inlined are declared virtual.
491
+ // We also mark them as protected, not private, so that the
492
+ // compiler is forced to call them through a function pointer.
493
+ // (A sufficiently smart compiler could directly call a private
494
+ // virtual function since it can never be overridden in a
495
+ // subclass).
496
+
497
+ // Also TODO: Switch away from integer error codes and to enums,
498
+ // or throw exceptions when possible.
499
+ struct switchstack_result_t
500
+ {
501
+ int status;
502
+ Greenlet* the_new_current_greenlet;
503
+ OwnedGreenlet origin_greenlet;
504
+
505
+ switchstack_result_t()
506
+ : status(0),
507
+ the_new_current_greenlet(nullptr)
508
+ {}
509
+
510
+ switchstack_result_t(int err)
511
+ : status(err),
512
+ the_new_current_greenlet(nullptr)
513
+ {}
514
+
515
+ switchstack_result_t(int err, Greenlet* state, OwnedGreenlet& origin)
516
+ : status(err),
517
+ the_new_current_greenlet(state),
518
+ origin_greenlet(origin)
519
+ {
520
+ }
521
+
522
+ switchstack_result_t(int err, Greenlet* state, const BorrowedGreenlet& origin)
523
+ : status(err),
524
+ the_new_current_greenlet(state),
525
+ origin_greenlet(origin)
526
+ {
527
+ }
528
+
529
+ switchstack_result_t(const switchstack_result_t& other)
530
+ : status(other.status),
531
+ the_new_current_greenlet(other.the_new_current_greenlet),
532
+ origin_greenlet(other.origin_greenlet)
533
+ {}
534
+
535
+ switchstack_result_t& operator=(const switchstack_result_t& other)
536
+ {
537
+ this->status = other.status;
538
+ this->the_new_current_greenlet = other.the_new_current_greenlet;
539
+ this->origin_greenlet = other.origin_greenlet;
540
+ return *this;
541
+ }
542
+ };
543
+
544
+ OwnedObject on_switchstack_or_initialstub_failure(
545
+ Greenlet* target,
546
+ const switchstack_result_t& err,
547
+ const bool target_was_me=false,
548
+ const bool was_initial_stub=false);
549
+
550
+ // Returns the previous greenlet we just switched away from.
551
+ virtual OwnedGreenlet g_switchstack_success() noexcept;
552
+
553
+
554
+ // Check the preconditions for switching to this greenlet; if they
555
+ // aren't met, throws PyErrOccurred. Most callers will want to
556
+ // catch this and clear the arguments
557
+ inline void check_switch_allowed() const;
558
+ class GreenletStartedWhileInPython : public std::runtime_error
559
+ {
560
+ public:
561
+ GreenletStartedWhileInPython() : std::runtime_error("")
562
+ {}
563
+ };
564
+
565
+ protected:
566
+
567
+
568
+ /**
569
+ Perform a stack switch into this greenlet.
570
+
571
+ This temporarily sets the global variable
572
+ ``switching_thread_state`` to this greenlet; as soon as the
573
+ call to ``slp_switch`` completes, this is reset to NULL.
574
+ Consequently, this depends on the GIL.
575
+
576
+ TODO: Adopt the stackman model and pass ``slp_switch`` a
577
+ callback function and context pointer; this eliminates the
578
+ need for global variables altogether.
579
+
580
+ Because the stack switch happens in this function, this
581
+ function can't use its own stack (local) variables, set
582
+ before the switch, and then accessed after the switch.
583
+
584
+ Further, you con't even access ``g_thread_state_global``
585
+ before and after the switch from the global variable.
586
+ Because it is thread local some compilers cache it in a
587
+ register/on the stack, notably new versions of MSVC; this
588
+ breaks with strange crashes sometime later, because writing
589
+ to anything in ``g_thread_state_global`` after the switch
590
+ is actually writing to random memory. For this reason, we
591
+ call a non-inlined function to finish the operation. (XXX:
592
+ The ``/GT`` MSVC compiler argument probably fixes that.)
593
+
594
+ It is very important that stack switch is 'atomic', i.e. no
595
+ calls into other Python code allowed (except very few that
596
+ are safe), because global variables are very fragile. (This
597
+ should no longer be the case with thread-local variables.)
598
+
599
+ */
600
+ // Made virtual to facilitate subclassing UserGreenlet for testing.
601
+ virtual switchstack_result_t g_switchstack(void);
602
+
603
+ class TracingGuard
604
+ {
605
+ private:
606
+ PyThreadState* tstate;
607
+ public:
608
+ TracingGuard()
609
+ : tstate(PyThreadState_GET())
610
+ {
611
+ PyThreadState_EnterTracing(this->tstate);
612
+ }
613
+
614
+ ~TracingGuard()
615
+ {
616
+ PyThreadState_LeaveTracing(this->tstate);
617
+ this->tstate = nullptr;
618
+ }
619
+
620
+ inline void CallTraceFunction(const OwnedObject& tracefunc,
621
+ const greenlet::refs::ImmortalEventName& event,
622
+ const BorrowedGreenlet& origin,
623
+ const BorrowedGreenlet& target)
624
+ {
625
+ // TODO: This calls tracefunc(event, (origin, target)). Add a shortcut
626
+ // function for that that's specialized to avoid the Py_BuildValue
627
+ // string parsing, or start with just using "ON" format with PyTuple_Pack(2,
628
+ // origin, target). That seems like what the N format is meant
629
+ // for.
630
+ // XXX: Why does event not automatically cast back to a PyObject?
631
+ // It tries to call the "deleted constructor ImmortalEventName
632
+ // const" instead.
633
+ assert(tracefunc);
634
+ assert(event);
635
+ assert(origin);
636
+ assert(target);
637
+ greenlet::refs::NewReference retval(
638
+ PyObject_CallFunction(
639
+ tracefunc.borrow(),
640
+ "O(OO)",
641
+ event.borrow(),
642
+ origin.borrow(),
643
+ target.borrow()
644
+ ));
645
+ if (!retval) {
646
+ throw PyErrOccurred::from_current();
647
+ }
648
+ }
649
+ };
650
+
651
+ static void
652
+ g_calltrace(const OwnedObject& tracefunc,
653
+ const greenlet::refs::ImmortalEventName& event,
654
+ const greenlet::refs::BorrowedGreenlet& origin,
655
+ const BorrowedGreenlet& target);
656
+ private:
657
+ OwnedObject g_switch_finish(const switchstack_result_t& err);
658
+
659
+ };
660
+
661
+ class UserGreenlet : public Greenlet
662
+ {
663
+ private:
664
+ static greenlet::PythonAllocator<UserGreenlet> allocator;
665
+ OwnedMainGreenlet _main_greenlet;
666
+ OwnedObject _run_callable;
667
+ OwnedGreenlet _parent;
668
+ public:
669
+ static void* operator new(size_t UNUSED(count));
670
+ static void operator delete(void* ptr);
671
+
672
+ UserGreenlet(PyGreenlet* p, BorrowedGreenlet the_parent);
673
+ virtual ~UserGreenlet();
674
+
675
+ virtual refs::BorrowedMainGreenlet find_main_greenlet_in_lineage() const;
676
+ virtual bool was_running_in_dead_thread() const noexcept;
677
+ virtual ThreadState* thread_state() const noexcept;
678
+ virtual OwnedObject g_switch();
679
+ virtual const OwnedObject& run() const
680
+ {
681
+ if (this->started() || !this->_run_callable) {
682
+ throw AttributeError("run");
683
+ }
684
+ return this->_run_callable;
685
+ }
686
+ virtual void run(const refs::BorrowedObject nrun);
687
+
688
+ virtual const OwnedGreenlet parent() const;
689
+ virtual void parent(const refs::BorrowedObject new_parent);
690
+
691
+ virtual const refs::BorrowedMainGreenlet main_greenlet() const;
692
+
693
+ virtual void murder_in_place();
694
+ virtual bool belongs_to_thread(const ThreadState* state) const;
695
+ virtual int tp_traverse(visitproc visit, void* arg);
696
+ virtual int tp_clear();
697
+ class ParentIsCurrentGuard
698
+ {
699
+ private:
700
+ OwnedGreenlet oldparent;
701
+ UserGreenlet* greenlet;
702
+ G_NO_COPIES_OF_CLS(ParentIsCurrentGuard);
703
+ public:
704
+ ParentIsCurrentGuard(UserGreenlet* p, const ThreadState& thread_state);
705
+ ~ParentIsCurrentGuard();
706
+ };
707
+ virtual OwnedObject throw_GreenletExit_during_dealloc(const ThreadState& current_thread_state);
708
+ protected:
709
+ virtual switchstack_result_t g_initialstub(void* mark);
710
+ private:
711
+ // This function isn't meant to return.
712
+ // This accepts raw pointers and the ownership of them at the
713
+ // same time. The caller should use ``inner_bootstrap(origin.relinquish_ownership())``.
714
+ void inner_bootstrap(PyGreenlet* origin_greenlet, PyObject* run);
715
+ };
716
+
717
+ class BrokenGreenlet : public UserGreenlet
718
+ {
719
+ private:
720
+ static greenlet::PythonAllocator<BrokenGreenlet> allocator;
721
+ public:
722
+ bool _force_switch_error = false;
723
+ bool _force_slp_switch_error = false;
724
+
725
+ static void* operator new(size_t UNUSED(count));
726
+ static void operator delete(void* ptr);
727
+ BrokenGreenlet(PyGreenlet* p, BorrowedGreenlet the_parent)
728
+ : UserGreenlet(p, the_parent)
729
+ {}
730
+ virtual ~BrokenGreenlet()
731
+ {}
732
+
733
+ virtual switchstack_result_t g_switchstack(void);
734
+ virtual bool force_slp_switch_error() const noexcept;
735
+
736
+ };
737
+
738
+ class MainGreenlet : public Greenlet
739
+ {
740
+ private:
741
+ static greenlet::PythonAllocator<MainGreenlet> allocator;
742
+ refs::BorrowedMainGreenlet _self;
743
+ ThreadState* _thread_state;
744
+ G_NO_COPIES_OF_CLS(MainGreenlet);
745
+ public:
746
+ static void* operator new(size_t UNUSED(count));
747
+ static void operator delete(void* ptr);
748
+
749
+ MainGreenlet(refs::BorrowedMainGreenlet::PyType*, ThreadState*);
750
+ virtual ~MainGreenlet();
751
+
752
+
753
+ virtual const OwnedObject& run() const;
754
+ virtual void run(const refs::BorrowedObject nrun);
755
+
756
+ virtual const OwnedGreenlet parent() const;
757
+ virtual void parent(const refs::BorrowedObject new_parent);
758
+
759
+ virtual const refs::BorrowedMainGreenlet main_greenlet() const;
760
+
761
+ virtual refs::BorrowedMainGreenlet find_main_greenlet_in_lineage() const;
762
+ virtual bool was_running_in_dead_thread() const noexcept;
763
+ virtual ThreadState* thread_state() const noexcept;
764
+ void thread_state(ThreadState*) noexcept;
765
+ virtual OwnedObject g_switch();
766
+ virtual int tp_traverse(visitproc visit, void* arg);
767
+ };
768
+
769
+ // Instantiate one on the stack to save the GC state,
770
+ // and then disable GC. When it goes out of scope, GC will be
771
+ // restored to its original state. Sadly, these APIs are only
772
+ // available on 3.10+; luckily, we only need them on 3.11+.
773
+ #if GREENLET_PY310
774
+ class GCDisabledGuard
775
+ {
776
+ private:
777
+ int was_enabled = 0;
778
+ public:
779
+ GCDisabledGuard()
780
+ : was_enabled(PyGC_IsEnabled())
781
+ {
782
+ PyGC_Disable();
783
+ }
784
+
785
+ ~GCDisabledGuard()
786
+ {
787
+ if (this->was_enabled) {
788
+ PyGC_Enable();
789
+ }
790
+ }
791
+ };
792
+ #endif
793
+
794
+ OwnedObject& operator<<=(OwnedObject& lhs, greenlet::SwitchingArgs& rhs) noexcept;
795
+
796
+ //TODO: Greenlet::g_switch() should call this automatically on its
797
+ //return value. As it is, the module code is calling it.
798
+ static inline OwnedObject
799
+ single_result(const OwnedObject& results)
800
+ {
801
+ if (results
802
+ && PyTuple_Check(results.borrow())
803
+ && PyTuple_GET_SIZE(results.borrow()) == 1) {
804
+ PyObject* result = PyTuple_GET_ITEM(results.borrow(), 0);
805
+ assert(result);
806
+ return OwnedObject::owning(result);
807
+ }
808
+ return results;
809
+ }
810
+
811
+
812
+ static OwnedObject
813
+ g_handle_exit(const OwnedObject& greenlet_result);
814
+
815
+
816
+ template<typename T>
817
+ void operator<<(const PyThreadState *const lhs, T& rhs)
818
+ {
819
+ rhs.operator<<(lhs);
820
+ }
821
+
822
+ } // namespace greenlet ;
823
+
824
+ #endif
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/TGreenletGlobals.cpp ADDED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* -*- indent-tabs-mode: nil; tab-width: 4; -*- */
2
+ /**
3
+ * Implementation of GreenletGlobals.
4
+ *
5
+ * Format with:
6
+ * clang-format -i --style=file src/greenlet/greenlet.c
7
+ *
8
+ *
9
+ * Fix missing braces with:
10
+ * clang-tidy src/greenlet/greenlet.c -fix -checks="readability-braces-around-statements"
11
+ */
12
+ #ifndef T_GREENLET_GLOBALS
13
+ #define T_GREENLET_GLOBALS
14
+
15
+ #include "greenlet_refs.hpp"
16
+ #include "greenlet_exceptions.hpp"
17
+ #include "greenlet_thread_support.hpp"
18
+ #include "greenlet_internal.hpp"
19
+
20
+ namespace greenlet {
21
+
22
+ // This encapsulates what were previously module global "constants"
23
+ // established at init time.
24
+ // This is a step towards Python3 style module state that allows
25
+ // reloading.
26
+ //
27
+ // In an earlier iteration of this code, we used placement new to be
28
+ // able to allocate this object statically still, so that references
29
+ // to its members don't incur an extra pointer indirection.
30
+ // But under some scenarios, that could result in crashes at
31
+ // shutdown because apparently the destructor was getting run twice?
32
+ class GreenletGlobals
33
+ {
34
+
35
+ public:
36
+ const greenlet::refs::ImmortalEventName event_switch;
37
+ const greenlet::refs::ImmortalEventName event_throw;
38
+ const greenlet::refs::ImmortalException PyExc_GreenletError;
39
+ const greenlet::refs::ImmortalException PyExc_GreenletExit;
40
+ const greenlet::refs::ImmortalObject empty_tuple;
41
+ const greenlet::refs::ImmortalObject empty_dict;
42
+ const greenlet::refs::ImmortalString str_run;
43
+ Mutex* const thread_states_to_destroy_lock;
44
+ greenlet::cleanup_queue_t thread_states_to_destroy;
45
+
46
+ GreenletGlobals() :
47
+ event_switch("switch"),
48
+ event_throw("throw"),
49
+ PyExc_GreenletError("greenlet.error"),
50
+ PyExc_GreenletExit("greenlet.GreenletExit", PyExc_BaseException),
51
+ empty_tuple(Require(PyTuple_New(0))),
52
+ empty_dict(Require(PyDict_New())),
53
+ str_run("run"),
54
+ thread_states_to_destroy_lock(new Mutex())
55
+ {}
56
+
57
+ ~GreenletGlobals()
58
+ {
59
+ // This object is (currently) effectively immortal, and not
60
+ // just because of those placement new tricks; if we try to
61
+ // deallocate the static object we allocated, and overwrote,
62
+ // we would be doing so at C++ teardown time, which is after
63
+ // the final Python GIL is released, and we can't use the API
64
+ // then.
65
+ // (The members will still be destructed, but they also don't
66
+ // do any deallocation.)
67
+ }
68
+
69
+ void queue_to_destroy(ThreadState* ts) const
70
+ {
71
+ // we're currently accessed through a static const object,
72
+ // implicitly marking our members as const, so code can't just
73
+ // call push_back (or pop_back) without casting away the
74
+ // const.
75
+ //
76
+ // Do that for callers.
77
+ greenlet::cleanup_queue_t& q = const_cast<greenlet::cleanup_queue_t&>(this->thread_states_to_destroy);
78
+ q.push_back(ts);
79
+ }
80
+
81
+ ThreadState* take_next_to_destroy() const
82
+ {
83
+ greenlet::cleanup_queue_t& q = const_cast<greenlet::cleanup_queue_t&>(this->thread_states_to_destroy);
84
+ ThreadState* result = q.back();
85
+ q.pop_back();
86
+ return result;
87
+ }
88
+ };
89
+
90
+ }; // namespace greenlet
91
+
92
+ static const greenlet::GreenletGlobals* mod_globs;
93
+
94
+ #endif // T_GREENLET_GLOBALS
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/TMainGreenlet.cpp ADDED
@@ -0,0 +1,153 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* -*- indent-tabs-mode: nil; tab-width: 4; -*- */
2
+ /**
3
+ * Implementation of greenlet::MainGreenlet.
4
+ *
5
+ * Format with:
6
+ * clang-format -i --style=file src/greenlet/greenlet.c
7
+ *
8
+ *
9
+ * Fix missing braces with:
10
+ * clang-tidy src/greenlet/greenlet.c -fix -checks="readability-braces-around-statements"
11
+ */
12
+ #ifndef T_MAIN_GREENLET_CPP
13
+ #define T_MAIN_GREENLET_CPP
14
+
15
+ #include "TGreenlet.hpp"
16
+
17
+
18
+
19
+ // Protected by the GIL. Incremented when we create a main greenlet,
20
+ // in a new thread, decremented when it is destroyed.
21
+ static Py_ssize_t G_TOTAL_MAIN_GREENLETS;
22
+
23
+ namespace greenlet {
24
+ greenlet::PythonAllocator<MainGreenlet> MainGreenlet::allocator;
25
+
26
+ void* MainGreenlet::operator new(size_t UNUSED(count))
27
+ {
28
+ return allocator.allocate(1);
29
+ }
30
+
31
+
32
+ void MainGreenlet::operator delete(void* ptr)
33
+ {
34
+ return allocator.deallocate(static_cast<MainGreenlet*>(ptr),
35
+ 1);
36
+ }
37
+
38
+
39
+ MainGreenlet::MainGreenlet(PyGreenlet* p, ThreadState* state)
40
+ : Greenlet(p, StackState::make_main()),
41
+ _self(p),
42
+ _thread_state(state)
43
+ {
44
+ G_TOTAL_MAIN_GREENLETS++;
45
+ }
46
+
47
+ MainGreenlet::~MainGreenlet()
48
+ {
49
+ G_TOTAL_MAIN_GREENLETS--;
50
+ this->tp_clear();
51
+ }
52
+
53
+ ThreadState*
54
+ MainGreenlet::thread_state() const noexcept
55
+ {
56
+ return this->_thread_state;
57
+ }
58
+
59
+ void
60
+ MainGreenlet::thread_state(ThreadState* t) noexcept
61
+ {
62
+ assert(!t);
63
+ this->_thread_state = t;
64
+ }
65
+
66
+
67
+ const BorrowedMainGreenlet
68
+ MainGreenlet::main_greenlet() const
69
+ {
70
+ return this->_self;
71
+ }
72
+
73
+ BorrowedMainGreenlet
74
+ MainGreenlet::find_main_greenlet_in_lineage() const
75
+ {
76
+ return BorrowedMainGreenlet(this->_self);
77
+ }
78
+
79
+ bool
80
+ MainGreenlet::was_running_in_dead_thread() const noexcept
81
+ {
82
+ return !this->_thread_state;
83
+ }
84
+
85
+ OwnedObject
86
+ MainGreenlet::g_switch()
87
+ {
88
+ try {
89
+ this->check_switch_allowed();
90
+ }
91
+ catch (const PyErrOccurred&) {
92
+ this->release_args();
93
+ throw;
94
+ }
95
+
96
+ switchstack_result_t err = this->g_switchstack();
97
+ if (err.status < 0) {
98
+ // XXX: This code path is untested, but it is shared
99
+ // with the UserGreenlet path that is tested.
100
+ return this->on_switchstack_or_initialstub_failure(
101
+ this,
102
+ err,
103
+ true, // target was me
104
+ false // was initial stub
105
+ );
106
+ }
107
+
108
+ return err.the_new_current_greenlet->g_switch_finish(err);
109
+ }
110
+
111
+ int
112
+ MainGreenlet::tp_traverse(visitproc visit, void* arg)
113
+ {
114
+ if (this->_thread_state) {
115
+ // we've already traversed main, (self), don't do it again.
116
+ int result = this->_thread_state->tp_traverse(visit, arg, false);
117
+ if (result) {
118
+ return result;
119
+ }
120
+ }
121
+ return Greenlet::tp_traverse(visit, arg);
122
+ }
123
+
124
+ const OwnedObject&
125
+ MainGreenlet::run() const
126
+ {
127
+ throw AttributeError("Main greenlets do not have a run attribute.");
128
+ }
129
+
130
+ void
131
+ MainGreenlet::run(const BorrowedObject UNUSED(nrun))
132
+ {
133
+ throw AttributeError("Main greenlets do not have a run attribute.");
134
+ }
135
+
136
+ void
137
+ MainGreenlet::parent(const BorrowedObject raw_new_parent)
138
+ {
139
+ if (!raw_new_parent) {
140
+ throw AttributeError("can't delete attribute");
141
+ }
142
+ throw AttributeError("cannot set the parent of a main greenlet");
143
+ }
144
+
145
+ const OwnedGreenlet
146
+ MainGreenlet::parent() const
147
+ {
148
+ return OwnedGreenlet(); // null becomes None
149
+ }
150
+
151
+ }; // namespace greenlet
152
+
153
+ #endif
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/TPythonState.cpp ADDED
@@ -0,0 +1,402 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #ifndef GREENLET_PYTHON_STATE_CPP
2
+ #define GREENLET_PYTHON_STATE_CPP
3
+
4
+ #include <Python.h>
5
+ #include "TGreenlet.hpp"
6
+
7
+ namespace greenlet {
8
+
9
+ PythonState::PythonState()
10
+ : _top_frame()
11
+ #if GREENLET_USE_CFRAME
12
+ ,cframe(nullptr)
13
+ ,use_tracing(0)
14
+ #endif
15
+ #if GREENLET_PY314
16
+ ,py_recursion_depth(0)
17
+ #elif GREENLET_PY312
18
+ ,py_recursion_depth(0)
19
+ ,c_recursion_depth(0)
20
+ #else
21
+ ,recursion_depth(0)
22
+ #endif
23
+ #if GREENLET_PY313
24
+ ,delete_later(nullptr)
25
+ #else
26
+ ,trash_delete_nesting(0)
27
+ #endif
28
+ #if GREENLET_PY311
29
+ ,current_frame(nullptr)
30
+ ,datastack_chunk(nullptr)
31
+ ,datastack_top(nullptr)
32
+ ,datastack_limit(nullptr)
33
+ #endif
34
+ {
35
+ #if GREENLET_USE_CFRAME
36
+ /*
37
+ The PyThreadState->cframe pointer usually points to memory on
38
+ the stack, alloceted in a call into PyEval_EvalFrameDefault.
39
+
40
+ Initially, before any evaluation begins, it points to the
41
+ initial PyThreadState object's ``root_cframe`` object, which is
42
+ statically allocated for the lifetime of the thread.
43
+
44
+ A greenlet can last for longer than a call to
45
+ PyEval_EvalFrameDefault, so we can't set its ``cframe`` pointer
46
+ to be the current ``PyThreadState->cframe``; nor could we use
47
+ one from the greenlet parent for the same reason. Yet a further
48
+ no: we can't allocate one scoped to the greenlet and then
49
+ destroy it when the greenlet is deallocated, because inside the
50
+ interpreter the _PyCFrame objects form a linked list, and that too
51
+ can result in accessing memory beyond its dynamic lifetime (if
52
+ the greenlet doesn't actually finish before it dies, its entry
53
+ could still be in the list).
54
+
55
+ Using the ``root_cframe`` is problematic, though, because its
56
+ members are never modified by the interpreter and are set to 0,
57
+ meaning that its ``use_tracing`` flag is never updated. We don't
58
+ want to modify that value in the ``root_cframe`` ourself: it
59
+ *shouldn't* matter much because we should probably never get
60
+ back to the point where that's the only cframe on the stack;
61
+ even if it did matter, the major consequence of an incorrect
62
+ value for ``use_tracing`` is that if its true the interpreter
63
+ does some extra work --- however, it's just good code hygiene.
64
+
65
+ Our solution: before a greenlet runs, after its initial
66
+ creation, it uses the ``root_cframe`` just to have something to
67
+ put there. However, once the greenlet is actually switched to
68
+ for the first time, ``g_initialstub`` (which doesn't actually
69
+ "return" while the greenlet is running) stores a new _PyCFrame on
70
+ its local stack, and copies the appropriate values from the
71
+ currently running _PyCFrame; this is then made the _PyCFrame for the
72
+ newly-minted greenlet. ``g_initialstub`` then proceeds to call
73
+ ``glet.run()``, which results in ``PyEval_...`` adding the
74
+ _PyCFrame to the list. Switches continue as normal. Finally, when
75
+ the greenlet finishes, the call to ``glet.run()`` returns and
76
+ the _PyCFrame is taken out of the linked list and the stack value
77
+ is now unused and free to expire.
78
+
79
+ XXX: I think we can do better. If we're deallocing in the same
80
+ thread, can't we traverse the list and unlink our frame?
81
+ Can we just keep a reference to the thread state in case we
82
+ dealloc in another thread? (Is that even possible if we're still
83
+ running and haven't returned from g_initialstub?)
84
+ */
85
+ this->cframe = &PyThreadState_GET()->root_cframe;
86
+ #endif
87
+ }
88
+
89
+
90
+ inline void PythonState::may_switch_away() noexcept
91
+ {
92
+ #if GREENLET_PY311
93
+ // PyThreadState_GetFrame is probably going to have to allocate a
94
+ // new frame object. That may trigger garbage collection. Because
95
+ // we call this during the early phases of a switch (it doesn't
96
+ // matter to which greenlet, as this has a global effect), if a GC
97
+ // triggers a switch away, two things can happen, both bad:
98
+ // - We might not get switched back to, halting forward progress.
99
+ // this is pathological, but possible.
100
+ // - We might get switched back to with a different set of
101
+ // arguments or a throw instead of a switch. That would corrupt
102
+ // our state (specifically, PyErr_Occurred() and this->args()
103
+ // would no longer agree).
104
+ //
105
+ // Thus, when we call this API, we need to have GC disabled.
106
+ // This method serves as a bottleneck we call when maybe beginning
107
+ // a switch. In this way, it is always safe -- no risk of GC -- to
108
+ // use ``_GetFrame()`` whenever we need to, just as it was in
109
+ // <=3.10 (because subsequent calls will be cached and not
110
+ // allocate memory).
111
+
112
+ GCDisabledGuard no_gc;
113
+ Py_XDECREF(PyThreadState_GetFrame(PyThreadState_GET()));
114
+ #endif
115
+ }
116
+
117
+ void PythonState::operator<<(const PyThreadState *const tstate) noexcept
118
+ {
119
+ this->_context.steal(tstate->context);
120
+ #if GREENLET_USE_CFRAME
121
+ /*
122
+ IMPORTANT: ``cframe`` is a pointer into the STACK. Thus, because
123
+ the call to ``slp_switch()`` changes the contents of the stack,
124
+ you cannot read from ``ts_current->cframe`` after that call and
125
+ necessarily get the same values you get from reading it here.
126
+ Anything you need to restore from now to then must be saved in a
127
+ global/threadlocal variable (because we can't use stack
128
+ variables here either). For things that need to persist across
129
+ the switch, use `will_switch_from`.
130
+ */
131
+ this->cframe = tstate->cframe;
132
+ #if !GREENLET_PY312
133
+ this->use_tracing = tstate->cframe->use_tracing;
134
+ #endif
135
+ #endif // GREENLET_USE_CFRAME
136
+ #if GREENLET_PY311
137
+ #if GREENLET_PY314
138
+ this->py_recursion_depth = tstate->py_recursion_limit - tstate->py_recursion_remaining;
139
+ #elif GREENLET_PY312
140
+ this->py_recursion_depth = tstate->py_recursion_limit - tstate->py_recursion_remaining;
141
+ this->c_recursion_depth = Py_C_RECURSION_LIMIT - tstate->c_recursion_remaining;
142
+ #else // not 312
143
+ this->recursion_depth = tstate->recursion_limit - tstate->recursion_remaining;
144
+ #endif // GREENLET_PY312
145
+ #if GREENLET_PY313
146
+ this->current_frame = tstate->current_frame;
147
+ #elif GREENLET_USE_CFRAME
148
+ this->current_frame = tstate->cframe->current_frame;
149
+ #endif
150
+ this->datastack_chunk = tstate->datastack_chunk;
151
+ this->datastack_top = tstate->datastack_top;
152
+ this->datastack_limit = tstate->datastack_limit;
153
+
154
+ PyFrameObject *frame = PyThreadState_GetFrame((PyThreadState *)tstate);
155
+ Py_XDECREF(frame); // PyThreadState_GetFrame gives us a new
156
+ // reference.
157
+ this->_top_frame.steal(frame);
158
+ #if GREENLET_PY313
159
+ this->delete_later = Py_XNewRef(tstate->delete_later);
160
+ #elif GREENLET_PY312
161
+ this->trash_delete_nesting = tstate->trash.delete_nesting;
162
+ #else // not 312
163
+ this->trash_delete_nesting = tstate->trash_delete_nesting;
164
+ #endif // GREENLET_PY312
165
+ #else // Not 311
166
+ this->recursion_depth = tstate->recursion_depth;
167
+ this->_top_frame.steal(tstate->frame);
168
+ this->trash_delete_nesting = tstate->trash_delete_nesting;
169
+ #endif // GREENLET_PY311
170
+ }
171
+
172
+ #if GREENLET_PY312
173
+ void GREENLET_NOINLINE(PythonState::unexpose_frames)()
174
+ {
175
+ if (!this->top_frame()) {
176
+ return;
177
+ }
178
+
179
+ // See GreenletState::expose_frames() and the comment on frames_were_exposed
180
+ // for more information about this logic.
181
+ _PyInterpreterFrame *iframe = this->_top_frame->f_frame;
182
+ while (iframe != nullptr) {
183
+ _PyInterpreterFrame *prev_exposed = iframe->previous;
184
+ assert(iframe->frame_obj);
185
+ memcpy(&iframe->previous, &iframe->frame_obj->_f_frame_data[0],
186
+ sizeof(void *));
187
+ iframe = prev_exposed;
188
+ }
189
+ }
190
+ #else
191
+ void PythonState::unexpose_frames()
192
+ {}
193
+ #endif
194
+
195
+ void PythonState::operator>>(PyThreadState *const tstate) noexcept
196
+ {
197
+ tstate->context = this->_context.relinquish_ownership();
198
+ /* Incrementing this value invalidates the contextvars cache,
199
+ which would otherwise remain valid across switches */
200
+ tstate->context_ver++;
201
+ #if GREENLET_USE_CFRAME
202
+ tstate->cframe = this->cframe;
203
+ /*
204
+ If we were tracing, we need to keep tracing.
205
+ There should never be the possibility of hitting the
206
+ root_cframe here. See note above about why we can't
207
+ just copy this from ``origin->cframe->use_tracing``.
208
+ */
209
+ #if !GREENLET_PY312
210
+ tstate->cframe->use_tracing = this->use_tracing;
211
+ #endif
212
+ #endif // GREENLET_USE_CFRAME
213
+ #if GREENLET_PY311
214
+ #if GREENLET_PY314
215
+ tstate->py_recursion_remaining = tstate->py_recursion_limit - this->py_recursion_depth;
216
+ this->unexpose_frames();
217
+ #elif GREENLET_PY312
218
+ tstate->py_recursion_remaining = tstate->py_recursion_limit - this->py_recursion_depth;
219
+ tstate->c_recursion_remaining = Py_C_RECURSION_LIMIT - this->c_recursion_depth;
220
+ this->unexpose_frames();
221
+ #else // \/ 3.11
222
+ tstate->recursion_remaining = tstate->recursion_limit - this->recursion_depth;
223
+ #endif // GREENLET_PY312
224
+ #if GREENLET_PY313
225
+ tstate->current_frame = this->current_frame;
226
+ #elif GREENLET_USE_CFRAME
227
+ tstate->cframe->current_frame = this->current_frame;
228
+ #endif
229
+ tstate->datastack_chunk = this->datastack_chunk;
230
+ tstate->datastack_top = this->datastack_top;
231
+ tstate->datastack_limit = this->datastack_limit;
232
+ this->_top_frame.relinquish_ownership();
233
+ #if GREENLET_PY313
234
+ Py_XDECREF(tstate->delete_later);
235
+ tstate->delete_later = this->delete_later;
236
+ Py_CLEAR(this->delete_later);
237
+ #elif GREENLET_PY312
238
+ tstate->trash.delete_nesting = this->trash_delete_nesting;
239
+ #else // not 3.12
240
+ tstate->trash_delete_nesting = this->trash_delete_nesting;
241
+ #endif // GREENLET_PY312
242
+ #else // not 3.11
243
+ tstate->frame = this->_top_frame.relinquish_ownership();
244
+ tstate->recursion_depth = this->recursion_depth;
245
+ tstate->trash_delete_nesting = this->trash_delete_nesting;
246
+ #endif // GREENLET_PY311
247
+ }
248
+
249
+ inline void PythonState::will_switch_from(PyThreadState *const origin_tstate) noexcept
250
+ {
251
+ #if GREENLET_USE_CFRAME && !GREENLET_PY312
252
+ // The weird thing is, we don't actually save this for an
253
+ // effect on the current greenlet, it's saved for an
254
+ // effect on the target greenlet. That is, we want
255
+ // continuity of this setting across the greenlet switch.
256
+ this->use_tracing = origin_tstate->cframe->use_tracing;
257
+ #endif
258
+ }
259
+
260
+ void PythonState::set_initial_state(const PyThreadState* const tstate) noexcept
261
+ {
262
+ this->_top_frame = nullptr;
263
+ #if GREENLET_PY314
264
+ this->py_recursion_depth = tstate->py_recursion_limit - tstate->py_recursion_remaining;
265
+ #elif GREENLET_PY312
266
+ this->py_recursion_depth = tstate->py_recursion_limit - tstate->py_recursion_remaining;
267
+ // XXX: TODO: Comment from a reviewer:
268
+ // Should this be ``Py_C_RECURSION_LIMIT - tstate->c_recursion_remaining``?
269
+ // But to me it looks more like that might not be the right
270
+ // initialization either?
271
+ this->c_recursion_depth = tstate->py_recursion_limit - tstate->py_recursion_remaining;
272
+ #elif GREENLET_PY311
273
+ this->recursion_depth = tstate->recursion_limit - tstate->recursion_remaining;
274
+ #else
275
+ this->recursion_depth = tstate->recursion_depth;
276
+ #endif
277
+ }
278
+ // TODO: Better state management about when we own the top frame.
279
+ int PythonState::tp_traverse(visitproc visit, void* arg, bool own_top_frame) noexcept
280
+ {
281
+ Py_VISIT(this->_context.borrow());
282
+ if (own_top_frame) {
283
+ Py_VISIT(this->_top_frame.borrow());
284
+ }
285
+ return 0;
286
+ }
287
+
288
+ void PythonState::tp_clear(bool own_top_frame) noexcept
289
+ {
290
+ PythonStateContext::tp_clear();
291
+ // If we get here owning a frame,
292
+ // we got dealloc'd without being finished. We may or may not be
293
+ // in the same thread.
294
+ if (own_top_frame) {
295
+ this->_top_frame.CLEAR();
296
+ }
297
+ }
298
+
299
+ #if GREENLET_USE_CFRAME
300
+ void PythonState::set_new_cframe(_PyCFrame& frame) noexcept
301
+ {
302
+ frame = *PyThreadState_GET()->cframe;
303
+ /* Make the target greenlet refer to the stack value. */
304
+ this->cframe = &frame;
305
+ /*
306
+ And restore the link to the previous frame so this one gets
307
+ unliked appropriately.
308
+ */
309
+ this->cframe->previous = &PyThreadState_GET()->root_cframe;
310
+ }
311
+ #endif
312
+
313
+ const PythonState::OwnedFrame& PythonState::top_frame() const noexcept
314
+ {
315
+ return this->_top_frame;
316
+ }
317
+
318
+ void PythonState::did_finish(PyThreadState* tstate) noexcept
319
+ {
320
+ #if GREENLET_PY311
321
+ // See https://github.com/gevent/gevent/issues/1924 and
322
+ // https://github.com/python-greenlet/greenlet/issues/328. In
323
+ // short, Python 3.11 allocates memory for frames as a sort of
324
+ // linked list that's kept as part of PyThreadState in the
325
+ // ``datastack_chunk`` member and friends. These are saved and
326
+ // restored as part of switching greenlets.
327
+ //
328
+ // When we initially switch to a greenlet, we set those to NULL.
329
+ // That causes the frame management code to treat this like a
330
+ // brand new thread and start a fresh list of chunks, beginning
331
+ // with a new "root" chunk. As we make calls in this greenlet,
332
+ // those chunks get added, and as calls return, they get popped.
333
+ // But the frame code (pystate.c) is careful to make sure that the
334
+ // root chunk never gets popped.
335
+ //
336
+ // Thus, when a greenlet exits for the last time, there will be at
337
+ // least a single root chunk that we must be responsible for
338
+ // deallocating.
339
+ //
340
+ // The complex part is that these chunks are allocated and freed
341
+ // using ``_PyObject_VirtualAlloc``/``Free``. Those aren't public
342
+ // functions, and they aren't exported for linking. It so happens
343
+ // that we know they are just thin wrappers around the Arena
344
+ // allocator, so we can use that directly to deallocate in a
345
+ // compatible way.
346
+ //
347
+ // CAUTION: Check this implementation detail on every major version.
348
+ //
349
+ // It might be nice to be able to do this in our destructor, but
350
+ // can we be sure that no one else is using that memory? Plus, as
351
+ // described below, our pointers may not even be valid anymore. As
352
+ // a special case, there is one time that we know we can do this,
353
+ // and that's from the destructor of the associated UserGreenlet
354
+ // (NOT main greenlet)
355
+ PyObjectArenaAllocator alloc;
356
+ _PyStackChunk* chunk = nullptr;
357
+ if (tstate) {
358
+ // We really did finish, we can never be switched to again.
359
+ chunk = tstate->datastack_chunk;
360
+ // Unfortunately, we can't do much sanity checking. Our
361
+ // this->datastack_chunk pointer is out of date (evaluation may
362
+ // have popped down through it already) so we can't verify that
363
+ // we deallocate it. I don't think we can even check datastack_top
364
+ // for the same reason.
365
+
366
+ PyObject_GetArenaAllocator(&alloc);
367
+ tstate->datastack_chunk = nullptr;
368
+ tstate->datastack_limit = nullptr;
369
+ tstate->datastack_top = nullptr;
370
+
371
+ }
372
+ else if (this->datastack_chunk) {
373
+ // The UserGreenlet (NOT the main greenlet!) is being deallocated. If we're
374
+ // still holding a stack chunk, it's garbage because we know
375
+ // we can never switch back to let cPython clean it up.
376
+ // Because the last time we got switched away from, and we
377
+ // haven't run since then, we know our chain is valid and can
378
+ // be dealloced.
379
+ chunk = this->datastack_chunk;
380
+ PyObject_GetArenaAllocator(&alloc);
381
+ }
382
+
383
+ if (alloc.free && chunk) {
384
+ // In case the arena mechanism has been torn down already.
385
+ while (chunk) {
386
+ _PyStackChunk *prev = chunk->previous;
387
+ chunk->previous = nullptr;
388
+ alloc.free(alloc.ctx, chunk, chunk->size);
389
+ chunk = prev;
390
+ }
391
+ }
392
+
393
+ this->datastack_chunk = nullptr;
394
+ this->datastack_limit = nullptr;
395
+ this->datastack_top = nullptr;
396
+ #endif
397
+ }
398
+
399
+
400
+ }; // namespace greenlet
401
+
402
+ #endif // GREENLET_PYTHON_STATE_CPP
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/TStackState.cpp ADDED
@@ -0,0 +1,265 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #ifndef GREENLET_STACK_STATE_CPP
2
+ #define GREENLET_STACK_STATE_CPP
3
+
4
+ #include "TGreenlet.hpp"
5
+
6
+ namespace greenlet {
7
+
8
+ #ifdef GREENLET_USE_STDIO
9
+ #include <iostream>
10
+ using std::cerr;
11
+ using std::endl;
12
+
13
+ std::ostream& operator<<(std::ostream& os, const StackState& s)
14
+ {
15
+ os << "StackState(stack_start=" << (void*)s._stack_start
16
+ << ", stack_stop=" << (void*)s.stack_stop
17
+ << ", stack_copy=" << (void*)s.stack_copy
18
+ << ", stack_saved=" << s._stack_saved
19
+ << ", stack_prev=" << s.stack_prev
20
+ << ", addr=" << &s
21
+ << ")";
22
+ return os;
23
+ }
24
+ #endif
25
+
26
+ StackState::StackState(void* mark, StackState& current)
27
+ : _stack_start(nullptr),
28
+ stack_stop((char*)mark),
29
+ stack_copy(nullptr),
30
+ _stack_saved(0),
31
+ /* Skip a dying greenlet */
32
+ stack_prev(current._stack_start
33
+ ? &current
34
+ : current.stack_prev)
35
+ {
36
+ }
37
+
38
+ StackState::StackState()
39
+ : _stack_start(nullptr),
40
+ stack_stop(nullptr),
41
+ stack_copy(nullptr),
42
+ _stack_saved(0),
43
+ stack_prev(nullptr)
44
+ {
45
+ }
46
+
47
+ StackState::StackState(const StackState& other)
48
+ // can't use a delegating constructor because of
49
+ // MSVC for Python 2.7
50
+ : _stack_start(nullptr),
51
+ stack_stop(nullptr),
52
+ stack_copy(nullptr),
53
+ _stack_saved(0),
54
+ stack_prev(nullptr)
55
+ {
56
+ this->operator=(other);
57
+ }
58
+
59
+ StackState& StackState::operator=(const StackState& other)
60
+ {
61
+ if (&other == this) {
62
+ return *this;
63
+ }
64
+ if (other._stack_saved) {
65
+ throw std::runtime_error("Refusing to steal memory.");
66
+ }
67
+
68
+ //If we have memory allocated, dispose of it
69
+ this->free_stack_copy();
70
+
71
+ this->_stack_start = other._stack_start;
72
+ this->stack_stop = other.stack_stop;
73
+ this->stack_copy = other.stack_copy;
74
+ this->_stack_saved = other._stack_saved;
75
+ this->stack_prev = other.stack_prev;
76
+ return *this;
77
+ }
78
+
79
+ inline void StackState::free_stack_copy() noexcept
80
+ {
81
+ PyMem_Free(this->stack_copy);
82
+ this->stack_copy = nullptr;
83
+ this->_stack_saved = 0;
84
+ }
85
+
86
+ inline void StackState::copy_heap_to_stack(const StackState& current) noexcept
87
+ {
88
+
89
+ /* Restore the heap copy back into the C stack */
90
+ if (this->_stack_saved != 0) {
91
+ memcpy(this->_stack_start, this->stack_copy, this->_stack_saved);
92
+ this->free_stack_copy();
93
+ }
94
+ StackState* owner = const_cast<StackState*>(&current);
95
+ if (!owner->_stack_start) {
96
+ owner = owner->stack_prev; /* greenlet is dying, skip it */
97
+ }
98
+ while (owner && owner->stack_stop <= this->stack_stop) {
99
+ // cerr << "\tOwner: " << owner << endl;
100
+ owner = owner->stack_prev; /* find greenlet with more stack */
101
+ }
102
+ this->stack_prev = owner;
103
+ // cerr << "\tFinished with: " << *this << endl;
104
+ }
105
+
106
+ inline int StackState::copy_stack_to_heap_up_to(const char* const stop) noexcept
107
+ {
108
+ /* Save more of g's stack into the heap -- at least up to 'stop'
109
+ g->stack_stop |________|
110
+ | |
111
+ | __ stop . . . . .
112
+ | | ==> . .
113
+ |________| _______
114
+ | | | |
115
+ | | | |
116
+ g->stack_start | | |_______| g->stack_copy
117
+ */
118
+ intptr_t sz1 = this->_stack_saved;
119
+ intptr_t sz2 = stop - this->_stack_start;
120
+ assert(this->_stack_start);
121
+ if (sz2 > sz1) {
122
+ char* c = (char*)PyMem_Realloc(this->stack_copy, sz2);
123
+ if (!c) {
124
+ PyErr_NoMemory();
125
+ return -1;
126
+ }
127
+ memcpy(c + sz1, this->_stack_start + sz1, sz2 - sz1);
128
+ this->stack_copy = c;
129
+ this->_stack_saved = sz2;
130
+ }
131
+ return 0;
132
+ }
133
+
134
+ inline int StackState::copy_stack_to_heap(char* const stackref,
135
+ const StackState& current) noexcept
136
+ {
137
+ /* must free all the C stack up to target_stop */
138
+ const char* const target_stop = this->stack_stop;
139
+
140
+ StackState* owner = const_cast<StackState*>(&current);
141
+ assert(owner->_stack_saved == 0); // everything is present on the stack
142
+ if (!owner->_stack_start) {
143
+ owner = owner->stack_prev; /* not saved if dying */
144
+ }
145
+ else {
146
+ owner->_stack_start = stackref;
147
+ }
148
+
149
+ while (owner->stack_stop < target_stop) {
150
+ /* ts_current is entierely within the area to free */
151
+ if (owner->copy_stack_to_heap_up_to(owner->stack_stop)) {
152
+ return -1; /* XXX */
153
+ }
154
+ owner = owner->stack_prev;
155
+ }
156
+ if (owner != this) {
157
+ if (owner->copy_stack_to_heap_up_to(target_stop)) {
158
+ return -1; /* XXX */
159
+ }
160
+ }
161
+ return 0;
162
+ }
163
+
164
+ inline bool StackState::started() const noexcept
165
+ {
166
+ return this->stack_stop != nullptr;
167
+ }
168
+
169
+ inline bool StackState::main() const noexcept
170
+ {
171
+ return this->stack_stop == (char*)-1;
172
+ }
173
+
174
+ inline bool StackState::active() const noexcept
175
+ {
176
+ return this->_stack_start != nullptr;
177
+ }
178
+
179
+ inline void StackState::set_active() noexcept
180
+ {
181
+ assert(this->_stack_start == nullptr);
182
+ this->_stack_start = (char*)1;
183
+ }
184
+
185
+ inline void StackState::set_inactive() noexcept
186
+ {
187
+ this->_stack_start = nullptr;
188
+ // XXX: What if we still have memory out there?
189
+ // That case is actually triggered by
190
+ // test_issue251_issue252_explicit_reference_not_collectable (greenlet.tests.test_leaks.TestLeaks)
191
+ // and
192
+ // test_issue251_issue252_need_to_collect_in_background
193
+ // (greenlet.tests.test_leaks.TestLeaks)
194
+ //
195
+ // Those objects never get deallocated, so the destructor never
196
+ // runs.
197
+ // It *seems* safe to clean up the memory here?
198
+ if (this->_stack_saved) {
199
+ this->free_stack_copy();
200
+ }
201
+ }
202
+
203
+ inline intptr_t StackState::stack_saved() const noexcept
204
+ {
205
+ return this->_stack_saved;
206
+ }
207
+
208
+ inline char* StackState::stack_start() const noexcept
209
+ {
210
+ return this->_stack_start;
211
+ }
212
+
213
+
214
+ inline StackState StackState::make_main() noexcept
215
+ {
216
+ StackState s;
217
+ s._stack_start = (char*)1;
218
+ s.stack_stop = (char*)-1;
219
+ return s;
220
+ }
221
+
222
+ StackState::~StackState()
223
+ {
224
+ if (this->_stack_saved != 0) {
225
+ this->free_stack_copy();
226
+ }
227
+ }
228
+
229
+ void StackState::copy_from_stack(void* vdest, const void* vsrc, size_t n) const
230
+ {
231
+ char* dest = static_cast<char*>(vdest);
232
+ const char* src = static_cast<const char*>(vsrc);
233
+ if (src + n <= this->_stack_start
234
+ || src >= this->_stack_start + this->_stack_saved
235
+ || this->_stack_saved == 0) {
236
+ // Nothing we're copying was spilled from the stack
237
+ memcpy(dest, src, n);
238
+ return;
239
+ }
240
+
241
+ if (src < this->_stack_start) {
242
+ // Copy the part before the saved stack.
243
+ // We know src + n > _stack_start due to the test above.
244
+ const size_t nbefore = this->_stack_start - src;
245
+ memcpy(dest, src, nbefore);
246
+ dest += nbefore;
247
+ src += nbefore;
248
+ n -= nbefore;
249
+ }
250
+ // We know src >= _stack_start after the before-copy, and
251
+ // src < _stack_start + _stack_saved due to the first if condition
252
+ size_t nspilled = std::min<size_t>(n, this->_stack_start + this->_stack_saved - src);
253
+ memcpy(dest, this->stack_copy + (src - this->_stack_start), nspilled);
254
+ dest += nspilled;
255
+ src += nspilled;
256
+ n -= nspilled;
257
+ if (n > 0) {
258
+ // Copy the part after the saved stack
259
+ memcpy(dest, src, n);
260
+ }
261
+ }
262
+
263
+ }; // namespace greenlet
264
+
265
+ #endif // GREENLET_STACK_STATE_CPP
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/TThreadState.hpp ADDED
@@ -0,0 +1,497 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #ifndef GREENLET_THREAD_STATE_HPP
2
+ #define GREENLET_THREAD_STATE_HPP
3
+
4
+ #include <ctime>
5
+ #include <stdexcept>
6
+
7
+ #include "greenlet_internal.hpp"
8
+ #include "greenlet_refs.hpp"
9
+ #include "greenlet_thread_support.hpp"
10
+
11
+ using greenlet::refs::BorrowedObject;
12
+ using greenlet::refs::BorrowedGreenlet;
13
+ using greenlet::refs::BorrowedMainGreenlet;
14
+ using greenlet::refs::OwnedMainGreenlet;
15
+ using greenlet::refs::OwnedObject;
16
+ using greenlet::refs::OwnedGreenlet;
17
+ using greenlet::refs::OwnedList;
18
+ using greenlet::refs::PyErrFetchParam;
19
+ using greenlet::refs::PyArgParseParam;
20
+ using greenlet::refs::ImmortalString;
21
+ using greenlet::refs::CreatedModule;
22
+ using greenlet::refs::PyErrPieces;
23
+ using greenlet::refs::NewReference;
24
+
25
+ namespace greenlet {
26
+ /**
27
+ * Thread-local state of greenlets.
28
+ *
29
+ * Each native thread will get exactly one of these objects,
30
+ * automatically accessed through the best available thread-local
31
+ * mechanism the compiler supports (``thread_local`` for C++11
32
+ * compilers or ``__thread``/``declspec(thread)`` for older GCC/clang
33
+ * or MSVC, respectively.)
34
+ *
35
+ * Previously, we kept thread-local state mostly in a bunch of
36
+ * ``static volatile`` variables in the main greenlet file.. This had
37
+ * the problem of requiring extra checks, loops, and great care
38
+ * accessing these variables if we potentially invoked any Python code
39
+ * that could release the GIL, because the state could change out from
40
+ * under us. Making the variables thread-local solves this problem.
41
+ *
42
+ * When we detected that a greenlet API accessing the current greenlet
43
+ * was invoked from a different thread than the greenlet belonged to,
44
+ * we stored a reference to the greenlet in the Python thread
45
+ * dictionary for the thread the greenlet belonged to. This could lead
46
+ * to memory leaks if the thread then exited (because of a reference
47
+ * cycle, as greenlets referred to the thread dictionary, and deleting
48
+ * non-current greenlets leaked their frame plus perhaps arguments on
49
+ * the C stack). If a thread exited while still having running
50
+ * greenlet objects (perhaps that had just switched back to the main
51
+ * greenlet), and did not invoke one of the greenlet APIs *in that
52
+ * thread, immediately before it exited, without some other thread
53
+ * then being invoked*, such a leak was guaranteed.
54
+ *
55
+ * This can be partly solved by using compiler thread-local variables
56
+ * instead of the Python thread dictionary, thus avoiding a cycle.
57
+ *
58
+ * To fully solve this problem, we need a reliable way to know that a
59
+ * thread is done and we should clean up the main greenlet. On POSIX,
60
+ * we can use the destructor function of ``pthread_key_create``, but
61
+ * there's nothing similar on Windows; a C++11 thread local object
62
+ * reliably invokes its destructor when the thread it belongs to exits
63
+ * (non-C++11 compilers offer ``__thread`` or ``declspec(thread)`` to
64
+ * create thread-local variables, but they can't hold C++ objects that
65
+ * invoke destructors; the C++11 version is the most portable solution
66
+ * I found). When the thread exits, we can drop references and
67
+ * otherwise manipulate greenlets and frames that we know can no
68
+ * longer be switched to. For compilers that don't support C++11
69
+ * thread locals, we have a solution that uses the python thread
70
+ * dictionary, though it may not collect everything as promptly as
71
+ * other compilers do, if some other library is using the thread
72
+ * dictionary and has a cycle or extra reference.
73
+ *
74
+ * There are two small wrinkles. The first is that when the thread
75
+ * exits, it is too late to actually invoke Python APIs: the Python
76
+ * thread state is gone, and the GIL is released. To solve *this*
77
+ * problem, our destructor uses ``Py_AddPendingCall`` to transfer the
78
+ * destruction work to the main thread. (This is not an issue for the
79
+ * dictionary solution.)
80
+ *
81
+ * The second is that once the thread exits, the thread local object
82
+ * is invalid and we can't even access a pointer to it, so we can't
83
+ * pass it to ``Py_AddPendingCall``. This is handled by actually using
84
+ * a second object that's thread local (ThreadStateCreator) and having
85
+ * it dynamically allocate this object so it can live until the
86
+ * pending call runs.
87
+ */
88
+
89
+
90
+
91
+ class ThreadState {
92
+ private:
93
+ // As of commit 08ad1dd7012b101db953f492e0021fb08634afad
94
+ // this class needed 56 bytes in o Py_DEBUG build
95
+ // on 64-bit macOS 11.
96
+ // Adding the vector takes us up to 80 bytes ()
97
+
98
+ /* Strong reference to the main greenlet */
99
+ OwnedMainGreenlet main_greenlet;
100
+
101
+ /* Strong reference to the current greenlet. */
102
+ OwnedGreenlet current_greenlet;
103
+
104
+ /* Strong reference to the trace function, if any. */
105
+ OwnedObject tracefunc;
106
+
107
+ typedef std::vector<PyGreenlet*, PythonAllocator<PyGreenlet*> > deleteme_t;
108
+ /* A vector of raw PyGreenlet pointers representing things that need
109
+ deleted when this thread is running. The vector owns the
110
+ references, but you need to manually INCREF/DECREF as you use
111
+ them. We don't use a vector<refs::OwnedGreenlet> because we
112
+ make copy of this vector, and that would become O(n) as all the
113
+ refcounts are incremented in the copy.
114
+ */
115
+ deleteme_t deleteme;
116
+
117
+ #ifdef GREENLET_NEEDS_EXCEPTION_STATE_SAVED
118
+ void* exception_state;
119
+ #endif
120
+
121
+ static std::clock_t _clocks_used_doing_gc;
122
+ static ImmortalString get_referrers_name;
123
+ static PythonAllocator<ThreadState> allocator;
124
+
125
+ G_NO_COPIES_OF_CLS(ThreadState);
126
+
127
+
128
+ // Allocates a main greenlet for the thread state. If this fails,
129
+ // exits the process. Called only during constructing a ThreadState.
130
+ MainGreenlet* alloc_main()
131
+ {
132
+ PyGreenlet* gmain;
133
+
134
+ /* create the main greenlet for this thread */
135
+ gmain = reinterpret_cast<PyGreenlet*>(PyType_GenericAlloc(&PyGreenlet_Type, 0));
136
+ if (gmain == NULL) {
137
+ throw PyFatalError("alloc_main failed to alloc"); //exits the process
138
+ }
139
+
140
+ MainGreenlet* const main = new MainGreenlet(gmain, this);
141
+
142
+ assert(Py_REFCNT(gmain) == 1);
143
+ assert(gmain->pimpl == main);
144
+ return main;
145
+ }
146
+
147
+
148
+ public:
149
+ static void* operator new(size_t UNUSED(count))
150
+ {
151
+ return ThreadState::allocator.allocate(1);
152
+ }
153
+
154
+ static void operator delete(void* ptr)
155
+ {
156
+ return ThreadState::allocator.deallocate(static_cast<ThreadState*>(ptr),
157
+ 1);
158
+ }
159
+
160
+ static void init()
161
+ {
162
+ ThreadState::get_referrers_name = "get_referrers";
163
+ ThreadState::_clocks_used_doing_gc = 0;
164
+ }
165
+
166
+ ThreadState()
167
+ {
168
+
169
+ #ifdef GREENLET_NEEDS_EXCEPTION_STATE_SAVED
170
+ this->exception_state = slp_get_exception_state();
171
+ #endif
172
+
173
+ // XXX: Potentially dangerous, exposing a not fully
174
+ // constructed object.
175
+ MainGreenlet* const main = this->alloc_main();
176
+ this->main_greenlet = OwnedMainGreenlet::consuming(
177
+ main->self()
178
+ );
179
+ assert(this->main_greenlet);
180
+ this->current_greenlet = main->self();
181
+ // The main greenlet starts with 1 refs: The returned one. We
182
+ // then copied it to the current greenlet.
183
+ assert(this->main_greenlet.REFCNT() == 2);
184
+ }
185
+
186
+ inline void restore_exception_state()
187
+ {
188
+ #ifdef GREENLET_NEEDS_EXCEPTION_STATE_SAVED
189
+ // It's probably important this be inlined and only call C
190
+ // functions to avoid adding an SEH frame.
191
+ slp_set_exception_state(this->exception_state);
192
+ #endif
193
+ }
194
+
195
+ inline bool has_main_greenlet() const noexcept
196
+ {
197
+ return bool(this->main_greenlet);
198
+ }
199
+
200
+ // Called from the ThreadStateCreator when we're in non-standard
201
+ // threading mode. In that case, there is an object in the Python
202
+ // thread state dictionary that points to us. The main greenlet
203
+ // also traverses into us, in which case it's crucial not to
204
+ // traverse back into the main greenlet.
205
+ int tp_traverse(visitproc visit, void* arg, bool traverse_main=true)
206
+ {
207
+ if (traverse_main) {
208
+ Py_VISIT(main_greenlet.borrow_o());
209
+ }
210
+ if (traverse_main || current_greenlet != main_greenlet) {
211
+ Py_VISIT(current_greenlet.borrow_o());
212
+ }
213
+ Py_VISIT(tracefunc.borrow());
214
+ return 0;
215
+ }
216
+
217
+ inline BorrowedMainGreenlet borrow_main_greenlet() const noexcept
218
+ {
219
+ assert(this->main_greenlet);
220
+ assert(this->main_greenlet.REFCNT() >= 2);
221
+ return this->main_greenlet;
222
+ };
223
+
224
+ inline OwnedMainGreenlet get_main_greenlet() const noexcept
225
+ {
226
+ return this->main_greenlet;
227
+ }
228
+
229
+ /**
230
+ * In addition to returning a new reference to the currunt
231
+ * greenlet, this performs any maintenance needed.
232
+ */
233
+ inline OwnedGreenlet get_current()
234
+ {
235
+ /* green_dealloc() cannot delete greenlets from other threads, so
236
+ it stores them in the thread dict; delete them now. */
237
+ this->clear_deleteme_list();
238
+ //assert(this->current_greenlet->main_greenlet == this->main_greenlet);
239
+ //assert(this->main_greenlet->main_greenlet == this->main_greenlet);
240
+ return this->current_greenlet;
241
+ }
242
+
243
+ /**
244
+ * As for non-const get_current();
245
+ */
246
+ inline BorrowedGreenlet borrow_current()
247
+ {
248
+ this->clear_deleteme_list();
249
+ return this->current_greenlet;
250
+ }
251
+
252
+ /**
253
+ * Does no maintenance.
254
+ */
255
+ inline OwnedGreenlet get_current() const
256
+ {
257
+ return this->current_greenlet;
258
+ }
259
+
260
+ template<typename T, refs::TypeChecker TC>
261
+ inline bool is_current(const refs::PyObjectPointer<T, TC>& obj) const
262
+ {
263
+ return this->current_greenlet.borrow_o() == obj.borrow_o();
264
+ }
265
+
266
+ inline void set_current(const OwnedGreenlet& target)
267
+ {
268
+ this->current_greenlet = target;
269
+ }
270
+
271
+ private:
272
+ /**
273
+ * Deref and remove the greenlets from the deleteme list. Must be
274
+ * holding the GIL.
275
+ *
276
+ * If *murder* is true, then we must be called from a different
277
+ * thread than the one that these greenlets were running in.
278
+ * In that case, if the greenlet was actually running, we destroy
279
+ * the frame reference and otherwise make it appear dead before
280
+ * proceeding; otherwise, we would try (and fail) to raise an
281
+ * exception in it and wind up right back in this list.
282
+ */
283
+ inline void clear_deleteme_list(const bool murder=false)
284
+ {
285
+ if (!this->deleteme.empty()) {
286
+ // It's possible we could add items to this list while
287
+ // running Python code if there's a thread switch, so we
288
+ // need to defensively copy it before that can happen.
289
+ deleteme_t copy = this->deleteme;
290
+ this->deleteme.clear(); // in case things come back on the list
291
+ for(deleteme_t::iterator it = copy.begin(), end = copy.end();
292
+ it != end;
293
+ ++it ) {
294
+ PyGreenlet* to_del = *it;
295
+ if (murder) {
296
+ // Force each greenlet to appear dead; we can't raise an
297
+ // exception into it anymore anyway.
298
+ to_del->pimpl->murder_in_place();
299
+ }
300
+
301
+ // The only reference to these greenlets should be in
302
+ // this list, decreffing them should let them be
303
+ // deleted again, triggering calls to green_dealloc()
304
+ // in the correct thread (if we're not murdering).
305
+ // This may run arbitrary Python code and switch
306
+ // threads or greenlets!
307
+ Py_DECREF(to_del);
308
+ if (PyErr_Occurred()) {
309
+ PyErr_WriteUnraisable(nullptr);
310
+ PyErr_Clear();
311
+ }
312
+ }
313
+ }
314
+ }
315
+
316
+ public:
317
+
318
+ /**
319
+ * Returns a new reference, or a false object.
320
+ */
321
+ inline OwnedObject get_tracefunc() const
322
+ {
323
+ return tracefunc;
324
+ };
325
+
326
+
327
+ inline void set_tracefunc(BorrowedObject tracefunc)
328
+ {
329
+ assert(tracefunc);
330
+ if (tracefunc == BorrowedObject(Py_None)) {
331
+ this->tracefunc.CLEAR();
332
+ }
333
+ else {
334
+ this->tracefunc = tracefunc;
335
+ }
336
+ }
337
+
338
+ /**
339
+ * Given a reference to a greenlet that some other thread
340
+ * attempted to delete (has a refcount of 0) store it for later
341
+ * deletion when the thread this state belongs to is current.
342
+ */
343
+ inline void delete_when_thread_running(PyGreenlet* to_del)
344
+ {
345
+ Py_INCREF(to_del);
346
+ this->deleteme.push_back(to_del);
347
+ }
348
+
349
+ /**
350
+ * Set to std::clock_t(-1) to disable.
351
+ */
352
+ inline static std::clock_t& clocks_used_doing_gc()
353
+ {
354
+ return ThreadState::_clocks_used_doing_gc;
355
+ }
356
+
357
+ ~ThreadState()
358
+ {
359
+ if (!PyInterpreterState_Head()) {
360
+ // We shouldn't get here (our callers protect us)
361
+ // but if we do, all we can do is bail early.
362
+ return;
363
+ }
364
+
365
+ // We should not have an "origin" greenlet; that only exists
366
+ // for the temporary time during a switch, which should not
367
+ // be in progress as the thread dies.
368
+ //assert(!this->switching_state.origin);
369
+
370
+ this->tracefunc.CLEAR();
371
+
372
+ // Forcibly GC as much as we can.
373
+ this->clear_deleteme_list(true);
374
+
375
+ // The pending call did this.
376
+ assert(this->main_greenlet->thread_state() == nullptr);
377
+
378
+ // If the main greenlet is the current greenlet,
379
+ // then we "fell off the end" and the thread died.
380
+ // It's possible that there is some other greenlet that
381
+ // switched to us, leaving a reference to the main greenlet
382
+ // on the stack, somewhere uncollectible. Try to detect that.
383
+ if (this->current_greenlet == this->main_greenlet && this->current_greenlet) {
384
+ assert(this->current_greenlet->is_currently_running_in_some_thread());
385
+ // Drop one reference we hold.
386
+ this->current_greenlet.CLEAR();
387
+ assert(!this->current_greenlet);
388
+ // Only our reference to the main greenlet should be left,
389
+ // But hold onto the pointer in case we need to do extra cleanup.
390
+ PyGreenlet* old_main_greenlet = this->main_greenlet.borrow();
391
+ Py_ssize_t cnt = this->main_greenlet.REFCNT();
392
+ this->main_greenlet.CLEAR();
393
+ if (ThreadState::_clocks_used_doing_gc != std::clock_t(-1)
394
+ && cnt == 2 && Py_REFCNT(old_main_greenlet) == 1) {
395
+ // Highly likely that the reference is somewhere on
396
+ // the stack, not reachable by GC. Verify.
397
+ // XXX: This is O(n) in the total number of objects.
398
+ // TODO: Add a way to disable this at runtime, and
399
+ // another way to report on it.
400
+ std::clock_t begin = std::clock();
401
+ NewReference gc(PyImport_ImportModule("gc"));
402
+ if (gc) {
403
+ OwnedObject get_referrers = gc.PyRequireAttr(ThreadState::get_referrers_name);
404
+ OwnedList refs(get_referrers.PyCall(old_main_greenlet));
405
+ if (refs && refs.empty()) {
406
+ assert(refs.REFCNT() == 1);
407
+ // We found nothing! So we left a dangling
408
+ // reference: Probably the last thing some
409
+ // other greenlet did was call
410
+ // 'getcurrent().parent.switch()' to switch
411
+ // back to us. Clean it up. This will be the
412
+ // case on CPython 3.7 and newer, as they use
413
+ // an internal calling conversion that avoids
414
+ // creating method objects and storing them on
415
+ // the stack.
416
+ Py_DECREF(old_main_greenlet);
417
+ }
418
+ else if (refs
419
+ && refs.size() == 1
420
+ && PyCFunction_Check(refs.at(0))
421
+ && Py_REFCNT(refs.at(0)) == 2) {
422
+ assert(refs.REFCNT() == 1);
423
+ // Ok, we found a C method that refers to the
424
+ // main greenlet, and its only referenced
425
+ // twice, once in the list we just created,
426
+ // once from...somewhere else. If we can't
427
+ // find where else, then this is a leak.
428
+ // This happens in older versions of CPython
429
+ // that create a bound method object somewhere
430
+ // on the stack that we'll never get back to.
431
+ if (PyCFunction_GetFunction(refs.at(0).borrow()) == (PyCFunction)green_switch) {
432
+ BorrowedObject function_w = refs.at(0);
433
+ refs.clear(); // destroy the reference
434
+ // from the list.
435
+ // back to one reference. Can *it* be
436
+ // found?
437
+ assert(function_w.REFCNT() == 1);
438
+ refs = get_referrers.PyCall(function_w);
439
+ if (refs && refs.empty()) {
440
+ // Nope, it can't be found so it won't
441
+ // ever be GC'd. Drop it.
442
+ Py_CLEAR(function_w);
443
+ }
444
+ }
445
+ }
446
+ std::clock_t end = std::clock();
447
+ ThreadState::_clocks_used_doing_gc += (end - begin);
448
+ }
449
+ }
450
+ }
451
+
452
+ // We need to make sure this greenlet appears to be dead,
453
+ // because otherwise deallocing it would fail to raise an
454
+ // exception in it (the thread is dead) and put it back in our
455
+ // deleteme list.
456
+ if (this->current_greenlet) {
457
+ this->current_greenlet->murder_in_place();
458
+ this->current_greenlet.CLEAR();
459
+ }
460
+
461
+ if (this->main_greenlet) {
462
+ // Couldn't have been the main greenlet that was running
463
+ // when the thread exited (because we already cleared this
464
+ // pointer if it was). This shouldn't be possible?
465
+
466
+ // If the main greenlet was current when the thread died (it
467
+ // should be, right?) then we cleared its self pointer above
468
+ // when we cleared the current greenlet's main greenlet pointer.
469
+ // assert(this->main_greenlet->main_greenlet == this->main_greenlet
470
+ // || !this->main_greenlet->main_greenlet);
471
+ // // self reference, probably gone
472
+ // this->main_greenlet->main_greenlet.CLEAR();
473
+
474
+ // This will actually go away when the ivar is destructed.
475
+ this->main_greenlet.CLEAR();
476
+ }
477
+
478
+ if (PyErr_Occurred()) {
479
+ PyErr_WriteUnraisable(NULL);
480
+ PyErr_Clear();
481
+ }
482
+
483
+ }
484
+
485
+ };
486
+
487
+ ImmortalString ThreadState::get_referrers_name(nullptr);
488
+ PythonAllocator<ThreadState> ThreadState::allocator;
489
+ std::clock_t ThreadState::_clocks_used_doing_gc(0);
490
+
491
+
492
+
493
+
494
+
495
+ }; // namespace greenlet
496
+
497
+ #endif
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/TThreadStateCreator.hpp ADDED
@@ -0,0 +1,102 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #ifndef GREENLET_THREAD_STATE_CREATOR_HPP
2
+ #define GREENLET_THREAD_STATE_CREATOR_HPP
3
+
4
+ #include <ctime>
5
+ #include <stdexcept>
6
+
7
+ #include "greenlet_internal.hpp"
8
+ #include "greenlet_refs.hpp"
9
+ #include "greenlet_thread_support.hpp"
10
+
11
+ #include "TThreadState.hpp"
12
+
13
+ namespace greenlet {
14
+
15
+
16
+ typedef void (*ThreadStateDestructor)(ThreadState* const);
17
+
18
+ template<ThreadStateDestructor Destructor>
19
+ class ThreadStateCreator
20
+ {
21
+ private:
22
+ // Initialized to 1, and, if still 1, created on access.
23
+ // Set to 0 on destruction.
24
+ ThreadState* _state;
25
+ G_NO_COPIES_OF_CLS(ThreadStateCreator);
26
+
27
+ inline bool has_initialized_state() const noexcept
28
+ {
29
+ return this->_state != (ThreadState*)1;
30
+ }
31
+
32
+ inline bool has_state() const noexcept
33
+ {
34
+ return this->has_initialized_state() && this->_state != nullptr;
35
+ }
36
+
37
+ public:
38
+
39
+ // Only one of these, auto created per thread.
40
+ // Constructing the state constructs the MainGreenlet.
41
+ ThreadStateCreator() :
42
+ _state((ThreadState*)1)
43
+ {
44
+ }
45
+
46
+ ~ThreadStateCreator()
47
+ {
48
+ if (this->has_state()) {
49
+ Destructor(this->_state);
50
+ }
51
+
52
+ this->_state = nullptr;
53
+ }
54
+
55
+ inline ThreadState& state()
56
+ {
57
+ // The main greenlet will own this pointer when it is created,
58
+ // which will be right after this. The plan is to give every
59
+ // greenlet a pointer to the main greenlet for the thread it
60
+ // runs in; if we are doing something cross-thread, we need to
61
+ // access the pointer from the main greenlet. Deleting the
62
+ // thread, and hence the thread-local storage, will delete the
63
+ // state pointer in the main greenlet.
64
+ if (!this->has_initialized_state()) {
65
+ // XXX: Assuming allocation never fails
66
+ this->_state = new ThreadState;
67
+ // For non-standard threading, we need to store an object
68
+ // in the Python thread state dictionary so that it can be
69
+ // DECREF'd when the thread ends (ideally; the dict could
70
+ // last longer) and clean this object up.
71
+ }
72
+ if (!this->_state) {
73
+ throw std::runtime_error("Accessing state after destruction.");
74
+ }
75
+ return *this->_state;
76
+ }
77
+
78
+ operator ThreadState&()
79
+ {
80
+ return this->state();
81
+ }
82
+
83
+ operator ThreadState*()
84
+ {
85
+ return &this->state();
86
+ }
87
+
88
+ inline int tp_traverse(visitproc visit, void* arg)
89
+ {
90
+ if (this->has_state()) {
91
+ return this->_state->tp_traverse(visit, arg);
92
+ }
93
+ return 0;
94
+ }
95
+
96
+ };
97
+
98
+
99
+
100
+ }; // namespace greenlet
101
+
102
+ #endif
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/TThreadStateDestroy.cpp ADDED
@@ -0,0 +1,217 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* -*- indent-tabs-mode: nil; tab-width: 4; -*- */
2
+ /**
3
+ * Implementation of the ThreadState destructors.
4
+ *
5
+ * Format with:
6
+ * clang-format -i --style=file src/greenlet/greenlet.c
7
+ *
8
+ *
9
+ * Fix missing braces with:
10
+ * clang-tidy src/greenlet/greenlet.c -fix -checks="readability-braces-around-statements"
11
+ */
12
+ #ifndef T_THREADSTATE_DESTROY
13
+ #define T_THREADSTATE_DESTROY
14
+
15
+ #include "TGreenlet.hpp"
16
+
17
+ #include "greenlet_thread_support.hpp"
18
+ #include "greenlet_compiler_compat.hpp"
19
+ #include "TGreenletGlobals.cpp"
20
+ #include "TThreadState.hpp"
21
+ #include "TThreadStateCreator.hpp"
22
+
23
+ namespace greenlet {
24
+
25
+ extern "C" {
26
+
27
+ struct ThreadState_DestroyNoGIL
28
+ {
29
+ /**
30
+ This function uses the same lock that the PendingCallback does
31
+ */
32
+ static void
33
+ MarkGreenletDeadAndQueueCleanup(ThreadState* const state)
34
+ {
35
+ #if GREENLET_BROKEN_THREAD_LOCAL_CLEANUP_JUST_LEAK
36
+ return;
37
+ #endif
38
+ // We are *NOT* holding the GIL. Our thread is in the middle
39
+ // of its death throes and the Python thread state is already
40
+ // gone so we can't use most Python APIs. One that is safe is
41
+ // ``Py_AddPendingCall``, unless the interpreter itself has
42
+ // been torn down. There is a limited number of calls that can
43
+ // be queued: 32 (NPENDINGCALLS) in CPython 3.10, so we
44
+ // coalesce these calls using our own queue.
45
+
46
+ if (!MarkGreenletDeadIfNeeded(state)) {
47
+ // No state, or no greenlet
48
+ return;
49
+ }
50
+
51
+ // XXX: Because we don't have the GIL, this is a race condition.
52
+ if (!PyInterpreterState_Head()) {
53
+ // We have to leak the thread state, if the
54
+ // interpreter has shut down when we're getting
55
+ // deallocated, we can't run the cleanup code that
56
+ // deleting it would imply.
57
+ return;
58
+ }
59
+
60
+ AddToCleanupQueue(state);
61
+
62
+ }
63
+
64
+ private:
65
+
66
+ // If the state has an allocated main greenlet:
67
+ // - mark the greenlet as dead by disassociating it from the state;
68
+ // - return 1
69
+ // Otherwise, return 0.
70
+ static bool
71
+ MarkGreenletDeadIfNeeded(ThreadState* const state)
72
+ {
73
+ if (state && state->has_main_greenlet()) {
74
+ // mark the thread as dead ASAP.
75
+ // this is racy! If we try to throw or switch to a
76
+ // greenlet from this thread from some other thread before
77
+ // we clear the state pointer, it won't realize the state
78
+ // is dead which can crash the process.
79
+ PyGreenlet* p(state->borrow_main_greenlet().borrow());
80
+ assert(p->pimpl->thread_state() == state || p->pimpl->thread_state() == nullptr);
81
+ dynamic_cast<MainGreenlet*>(p->pimpl)->thread_state(nullptr);
82
+ return true;
83
+ }
84
+ return false;
85
+ }
86
+
87
+ static void
88
+ AddToCleanupQueue(ThreadState* const state)
89
+ {
90
+ assert(state && state->has_main_greenlet());
91
+
92
+ // NOTE: Because we're not holding the GIL here, some other
93
+ // Python thread could run and call ``os.fork()``, which would
94
+ // be bad if that happened while we are holding the cleanup
95
+ // lock (it wouldn't function in the child process).
96
+ // Make a best effort to try to keep the duration we hold the
97
+ // lock short.
98
+ // TODO: On platforms that support it, use ``pthread_atfork`` to
99
+ // drop this lock.
100
+ LockGuard cleanup_lock(*mod_globs->thread_states_to_destroy_lock);
101
+
102
+ mod_globs->queue_to_destroy(state);
103
+ if (mod_globs->thread_states_to_destroy.size() == 1) {
104
+ // We added the first item to the queue. We need to schedule
105
+ // the cleanup.
106
+
107
+ // A size greater than 1 means that we have already added the pending call,
108
+ // and in fact, it may be executing now.
109
+ // If it is executing, our lock makes sure that it will see the item we just added
110
+ // to the queue on its next iteration (after we release the lock)
111
+ //
112
+ // A size of 1 means there is no pending call, OR the pending call is
113
+ // currently executing, has dropped the lock, and is deleting the last item
114
+ // from the queue; its next iteration will go ahead and delete the item we just added.
115
+ // And the pending call we schedule here will have no work to do.
116
+ int result = AddPendingCall(
117
+ PendingCallback_DestroyQueueWithGIL,
118
+ nullptr);
119
+ if (result < 0) {
120
+ // Hmm, what can we do here?
121
+ fprintf(stderr,
122
+ "greenlet: WARNING: failed in call to Py_AddPendingCall; "
123
+ "expect a memory leak.\n");
124
+ }
125
+ }
126
+ }
127
+
128
+ static int
129
+ PendingCallback_DestroyQueueWithGIL(void* UNUSED(arg))
130
+ {
131
+ // We're holding the GIL here, so no Python code should be able to
132
+ // run to call ``os.fork()``.
133
+ while (1) {
134
+ ThreadState* to_destroy;
135
+ {
136
+ LockGuard cleanup_lock(*mod_globs->thread_states_to_destroy_lock);
137
+ if (mod_globs->thread_states_to_destroy.empty()) {
138
+ break;
139
+ }
140
+ to_destroy = mod_globs->take_next_to_destroy();
141
+ }
142
+ assert(to_destroy);
143
+ assert(to_destroy->has_main_greenlet());
144
+ // Drop the lock while we do the actual deletion.
145
+ // This allows other calls to MarkGreenletDeadAndQueueCleanup
146
+ // to enter and add to our queue.
147
+ DestroyOneWithGIL(to_destroy);
148
+ }
149
+ return 0;
150
+ }
151
+
152
+ static void
153
+ DestroyOneWithGIL(const ThreadState* const state)
154
+ {
155
+ // Holding the GIL.
156
+ // Passed a non-shared pointer to the actual thread state.
157
+ // state -> main greenlet
158
+ assert(state->has_main_greenlet());
159
+ PyGreenlet* main(state->borrow_main_greenlet());
160
+ // When we need to do cross-thread operations, we check this.
161
+ // A NULL value means the thread died some time ago.
162
+ // We do this here, rather than in a Python dealloc function
163
+ // for the greenlet, in case there's still a reference out
164
+ // there.
165
+ dynamic_cast<MainGreenlet*>(main->pimpl)->thread_state(nullptr);
166
+
167
+ delete state; // Deleting this runs the destructor, DECREFs the main greenlet.
168
+ }
169
+
170
+
171
+ static int AddPendingCall(int (*func)(void*), void* arg)
172
+ {
173
+ // If the interpreter is in the middle of finalizing, we can't add a
174
+ // pending call. Trying to do so will end up in a SIGSEGV, as
175
+ // Py_AddPendingCall will not be able to get the interpreter and will
176
+ // try to dereference a NULL pointer. It's possible this can still
177
+ // segfault if we happen to get context switched, and maybe we should
178
+ // just always implement our own AddPendingCall, but I'd like to see if
179
+ // this works first
180
+ #if GREENLET_PY313
181
+ if (Py_IsFinalizing()) {
182
+ #else
183
+ if (_Py_IsFinalizing()) {
184
+ #endif
185
+ #ifdef GREENLET_DEBUG
186
+ // No need to log in the general case. Yes, we'll leak,
187
+ // but we're shutting down so it should be ok.
188
+ fprintf(stderr,
189
+ "greenlet: WARNING: Interpreter is finalizing. Ignoring "
190
+ "call to Py_AddPendingCall; \n");
191
+ #endif
192
+ return 0;
193
+ }
194
+ return Py_AddPendingCall(func, arg);
195
+ }
196
+
197
+
198
+
199
+
200
+
201
+ };
202
+ };
203
+
204
+ }; // namespace greenlet
205
+
206
+ // The intent when GET_THREAD_STATE() is needed multiple times in a
207
+ // function is to take a reference to its return value in a local
208
+ // variable, to avoid the thread-local indirection. On some platforms
209
+ // (macOS), accessing a thread-local involves a function call (plus an
210
+ // initial function call in each function that uses a thread local);
211
+ // in contrast, static volatile variables are at some pre-computed
212
+ // offset.
213
+ typedef greenlet::ThreadStateCreator<greenlet::ThreadState_DestroyNoGIL::MarkGreenletDeadAndQueueCleanup> ThreadStateCreator;
214
+ static thread_local ThreadStateCreator g_thread_state_global;
215
+ #define GET_THREAD_STATE() g_thread_state_global
216
+
217
+ #endif //T_THREADSTATE_DESTROY
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/TUserGreenlet.cpp ADDED
@@ -0,0 +1,662 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* -*- indent-tabs-mode: nil; tab-width: 4; -*- */
2
+ /**
3
+ * Implementation of greenlet::UserGreenlet.
4
+ *
5
+ * Format with:
6
+ * clang-format -i --style=file src/greenlet/greenlet.c
7
+ *
8
+ *
9
+ * Fix missing braces with:
10
+ * clang-tidy src/greenlet/greenlet.c -fix -checks="readability-braces-around-statements"
11
+ */
12
+ #ifndef T_USER_GREENLET_CPP
13
+ #define T_USER_GREENLET_CPP
14
+
15
+ #include "greenlet_internal.hpp"
16
+ #include "TGreenlet.hpp"
17
+
18
+ #include "TThreadStateDestroy.cpp"
19
+
20
+
21
+ namespace greenlet {
22
+ using greenlet::refs::BorrowedMainGreenlet;
23
+ greenlet::PythonAllocator<UserGreenlet> UserGreenlet::allocator;
24
+
25
+ void* UserGreenlet::operator new(size_t UNUSED(count))
26
+ {
27
+ return allocator.allocate(1);
28
+ }
29
+
30
+
31
+ void UserGreenlet::operator delete(void* ptr)
32
+ {
33
+ return allocator.deallocate(static_cast<UserGreenlet*>(ptr),
34
+ 1);
35
+ }
36
+
37
+
38
+ UserGreenlet::UserGreenlet(PyGreenlet* p, BorrowedGreenlet the_parent)
39
+ : Greenlet(p), _parent(the_parent)
40
+ {
41
+ }
42
+
43
+ UserGreenlet::~UserGreenlet()
44
+ {
45
+ // Python 3.11: If we don't clear out the raw frame datastack
46
+ // when deleting an unfinished greenlet,
47
+ // TestLeaks.test_untracked_memory_doesnt_increase_unfinished_thread_dealloc_in_main fails.
48
+ this->python_state.did_finish(nullptr);
49
+ this->tp_clear();
50
+ }
51
+
52
+
53
+ const BorrowedMainGreenlet
54
+ UserGreenlet::main_greenlet() const
55
+ {
56
+ return this->_main_greenlet;
57
+ }
58
+
59
+
60
+ BorrowedMainGreenlet
61
+ UserGreenlet::find_main_greenlet_in_lineage() const
62
+ {
63
+ if (this->started()) {
64
+ assert(this->_main_greenlet);
65
+ return BorrowedMainGreenlet(this->_main_greenlet);
66
+ }
67
+
68
+ if (!this->_parent) {
69
+ /* garbage collected greenlet in chain */
70
+ // XXX: WHAT?
71
+ return BorrowedMainGreenlet(nullptr);
72
+ }
73
+
74
+ return this->_parent->find_main_greenlet_in_lineage();
75
+ }
76
+
77
+
78
+ /**
79
+ * CAUTION: This will allocate memory and may trigger garbage
80
+ * collection and arbitrary Python code.
81
+ */
82
+ OwnedObject
83
+ UserGreenlet::throw_GreenletExit_during_dealloc(const ThreadState& current_thread_state)
84
+ {
85
+ /* The dying greenlet cannot be a parent of ts_current
86
+ because the 'parent' field chain would hold a
87
+ reference */
88
+ UserGreenlet::ParentIsCurrentGuard with_current_parent(this, current_thread_state);
89
+
90
+ // We don't care about the return value, only whether an
91
+ // exception happened. Whether or not an exception happens,
92
+ // we need to restore the parent in case the greenlet gets
93
+ // resurrected.
94
+ return Greenlet::throw_GreenletExit_during_dealloc(current_thread_state);
95
+ }
96
+
97
+ ThreadState*
98
+ UserGreenlet::thread_state() const noexcept
99
+ {
100
+ // TODO: maybe make this throw, if the thread state isn't there?
101
+ // if (!this->main_greenlet) {
102
+ // throw std::runtime_error("No thread state"); // TODO: Better exception
103
+ // }
104
+ if (!this->_main_greenlet) {
105
+ return nullptr;
106
+ }
107
+ return this->_main_greenlet->thread_state();
108
+ }
109
+
110
+
111
+ bool
112
+ UserGreenlet::was_running_in_dead_thread() const noexcept
113
+ {
114
+ return this->_main_greenlet && !this->thread_state();
115
+ }
116
+
117
+ OwnedObject
118
+ UserGreenlet::g_switch()
119
+ {
120
+ assert(this->args() || PyErr_Occurred());
121
+
122
+ try {
123
+ this->check_switch_allowed();
124
+ }
125
+ catch (const PyErrOccurred&) {
126
+ this->release_args();
127
+ throw;
128
+ }
129
+
130
+ // Switching greenlets used to attempt to clean out ones that need
131
+ // deleted *if* we detected a thread switch. Should it still do
132
+ // that?
133
+ // An issue is that if we delete a greenlet from another thread,
134
+ // it gets queued to this thread, and ``kill_greenlet()`` switches
135
+ // back into the greenlet
136
+
137
+ /* find the real target by ignoring dead greenlets,
138
+ and if necessary starting a greenlet. */
139
+ switchstack_result_t err;
140
+ Greenlet* target = this;
141
+ // TODO: probably cleaner to handle the case where we do
142
+ // switch to ourself separately from the other cases.
143
+ // This can probably even further be simplified if we keep
144
+ // track of the switching_state we're going for and just call
145
+ // into g_switch() if it's not ourself. The main problem with that
146
+ // is that we would be using more stack space.
147
+ bool target_was_me = true;
148
+ bool was_initial_stub = false;
149
+ while (target) {
150
+ if (target->active()) {
151
+ if (!target_was_me) {
152
+ target->args() <<= this->args();
153
+ assert(!this->args());
154
+ }
155
+ err = target->g_switchstack();
156
+ break;
157
+ }
158
+ if (!target->started()) {
159
+ // We never encounter a main greenlet that's not started.
160
+ assert(!target->main());
161
+ UserGreenlet* real_target = static_cast<UserGreenlet*>(target);
162
+ assert(real_target);
163
+ void* dummymarker;
164
+ was_initial_stub = true;
165
+ if (!target_was_me) {
166
+ target->args() <<= this->args();
167
+ assert(!this->args());
168
+ }
169
+ try {
170
+ // This can only throw back to us while we're
171
+ // still in this greenlet. Once the new greenlet
172
+ // is bootstrapped, it has its own exception state.
173
+ err = real_target->g_initialstub(&dummymarker);
174
+ }
175
+ catch (const PyErrOccurred&) {
176
+ this->release_args();
177
+ throw;
178
+ }
179
+ catch (const GreenletStartedWhileInPython&) {
180
+ // The greenlet was started sometime before this
181
+ // greenlet actually switched to it, i.e.,
182
+ // "concurrent" calls to switch() or throw().
183
+ // We need to retry the switch.
184
+ // Note that the current greenlet has been reset
185
+ // to this one (or we wouldn't be running!)
186
+ continue;
187
+ }
188
+ break;
189
+ }
190
+
191
+ target = target->parent();
192
+ target_was_me = false;
193
+ }
194
+ // The ``this`` pointer and all other stack or register based
195
+ // variables are invalid now, at least where things succeed
196
+ // above.
197
+ // But this one, probably not so much? It's not clear if it's
198
+ // safe to throw an exception at this point.
199
+
200
+ if (err.status < 0) {
201
+ // If we get here, either g_initialstub()
202
+ // failed, or g_switchstack() failed. Either one of those
203
+ // cases SHOULD leave us in the original greenlet with a valid
204
+ // stack.
205
+ return this->on_switchstack_or_initialstub_failure(target, err, target_was_me, was_initial_stub);
206
+ }
207
+
208
+ // err.the_new_current_greenlet would be the same as ``target``,
209
+ // if target wasn't probably corrupt.
210
+ return err.the_new_current_greenlet->g_switch_finish(err);
211
+ }
212
+
213
+
214
+
215
+ Greenlet::switchstack_result_t
216
+ UserGreenlet::g_initialstub(void* mark)
217
+ {
218
+ OwnedObject run;
219
+
220
+ // We need to grab a reference to the current switch arguments
221
+ // in case we're entered concurrently during the call to
222
+ // GetAttr() and have to try again.
223
+ // We'll restore them when we return in that case.
224
+ // Scope them tightly to avoid ref leaks.
225
+ {
226
+ SwitchingArgs args(this->args());
227
+
228
+ /* save exception in case getattr clears it */
229
+ PyErrPieces saved;
230
+
231
+ /*
232
+ self.run is the object to call in the new greenlet.
233
+ This could run arbitrary python code and switch greenlets!
234
+ */
235
+ run = this->self().PyRequireAttr(mod_globs->str_run);
236
+ /* restore saved exception */
237
+ saved.PyErrRestore();
238
+
239
+
240
+ /* recheck that it's safe to switch in case greenlet reparented anywhere above */
241
+ this->check_switch_allowed();
242
+
243
+ /* by the time we got here another start could happen elsewhere,
244
+ * that means it should now be a regular switch.
245
+ * This can happen if the Python code is a subclass that implements
246
+ * __getattribute__ or __getattr__, or makes ``run`` a descriptor;
247
+ * all of those can run arbitrary code that switches back into
248
+ * this greenlet.
249
+ */
250
+ if (this->stack_state.started()) {
251
+ // the successful switch cleared these out, we need to
252
+ // restore our version. They will be copied on up to the
253
+ // next target.
254
+ assert(!this->args());
255
+ this->args() <<= args;
256
+ throw GreenletStartedWhileInPython();
257
+ }
258
+ }
259
+
260
+ // Sweet, if we got here, we have the go-ahead and will switch
261
+ // greenlets.
262
+ // Nothing we do from here on out should allow for a thread or
263
+ // greenlet switch: No arbitrary calls to Python, including
264
+ // decref'ing
265
+
266
+ #if GREENLET_USE_CFRAME
267
+ /* OK, we need it, we're about to switch greenlets, save the state. */
268
+ /*
269
+ See green_new(). This is a stack-allocated variable used
270
+ while *self* is in PyObject_Call().
271
+ We want to defer copying the state info until we're sure
272
+ we need it and are in a stable place to do so.
273
+ */
274
+ _PyCFrame trace_info;
275
+
276
+ this->python_state.set_new_cframe(trace_info);
277
+ #endif
278
+ /* start the greenlet */
279
+ ThreadState& thread_state = GET_THREAD_STATE().state();
280
+ this->stack_state = StackState(mark,
281
+ thread_state.borrow_current()->stack_state);
282
+ this->python_state.set_initial_state(PyThreadState_GET());
283
+ this->exception_state.clear();
284
+ this->_main_greenlet = thread_state.get_main_greenlet();
285
+
286
+ /* perform the initial switch */
287
+ switchstack_result_t err = this->g_switchstack();
288
+ /* returns twice!
289
+ The 1st time with ``err == 1``: we are in the new greenlet.
290
+ This one owns a greenlet that used to be current.
291
+ The 2nd time with ``err <= 0``: back in the caller's
292
+ greenlet; this happens if the child finishes or switches
293
+ explicitly to us. Either way, the ``err`` variable is
294
+ created twice at the same memory location, but possibly
295
+ having different ``origin`` values. Note that it's not
296
+ constructed for the second time until the switch actually happens.
297
+ */
298
+ if (err.status == 1) {
299
+ // In the new greenlet.
300
+
301
+ // This never returns! Calling inner_bootstrap steals
302
+ // the contents of our run object within this stack frame, so
303
+ // it is not valid to do anything with it.
304
+ try {
305
+ this->inner_bootstrap(err.origin_greenlet.relinquish_ownership(),
306
+ run.relinquish_ownership());
307
+ }
308
+ // Getting a C++ exception here isn't good. It's probably a
309
+ // bug in the underlying greenlet, meaning it's probably a
310
+ // C++ extension. We're going to abort anyway, but try to
311
+ // display some nice information *if* possible. Some obscure
312
+ // platforms don't properly support this (old 32-bit Arm, see see
313
+ // https://github.com/python-greenlet/greenlet/issues/385); that's not
314
+ // great, but should usually be OK because, as mentioned above, we're
315
+ // terminating anyway.
316
+ //
317
+ // The catching is tested by
318
+ // ``test_cpp.CPPTests.test_unhandled_exception_in_greenlet_aborts``.
319
+ //
320
+ // PyErrOccurred can theoretically be thrown by
321
+ // inner_bootstrap() -> g_switch_finish(), but that should
322
+ // never make it back to here. It is a std::exception and
323
+ // would be caught if it is.
324
+ catch (const std::exception& e) {
325
+ std::string base = "greenlet: Unhandled C++ exception: ";
326
+ base += e.what();
327
+ Py_FatalError(base.c_str());
328
+ }
329
+ catch (...) {
330
+ // Some compilers/runtimes use exceptions internally.
331
+ // It appears that GCC on Linux with libstdc++ throws an
332
+ // exception internally at process shutdown time to unwind
333
+ // stacks and clean up resources. Depending on exactly
334
+ // where we are when the process exits, that could result
335
+ // in an unknown exception getting here. If we
336
+ // Py_FatalError() or abort() here, we interfere with
337
+ // orderly process shutdown. Throwing the exception on up
338
+ // is the right thing to do.
339
+ //
340
+ // gevent's ``examples/dns_mass_resolve.py`` demonstrates this.
341
+ #ifndef NDEBUG
342
+ fprintf(stderr,
343
+ "greenlet: inner_bootstrap threw unknown exception; "
344
+ "is the process terminating?\n");
345
+ #endif
346
+ throw;
347
+ }
348
+ Py_FatalError("greenlet: inner_bootstrap returned with no exception.\n");
349
+ }
350
+
351
+
352
+ // In contrast, notice that we're keeping the origin greenlet
353
+ // around as an owned reference; we need it to call the trace
354
+ // function for the switch back into the parent. It was only
355
+ // captured at the time the switch actually happened, though,
356
+ // so we haven't been keeping an extra reference around this
357
+ // whole time.
358
+
359
+ /* back in the parent */
360
+ if (err.status < 0) {
361
+ /* start failed badly, restore greenlet state */
362
+ this->stack_state = StackState();
363
+ this->_main_greenlet.CLEAR();
364
+ // CAUTION: This may run arbitrary Python code.
365
+ run.CLEAR(); // inner_bootstrap didn't run, we own the reference.
366
+ }
367
+
368
+ // In the success case, the spawned code (inner_bootstrap) will
369
+ // take care of decrefing this, so we relinquish ownership so as
370
+ // to not double-decref.
371
+
372
+ run.relinquish_ownership();
373
+
374
+ return err;
375
+ }
376
+
377
+
378
+ void
379
+ UserGreenlet::inner_bootstrap(PyGreenlet* origin_greenlet, PyObject* run)
380
+ {
381
+ // The arguments here would be another great place for move.
382
+ // As it is, we take them as a reference so that when we clear
383
+ // them we clear what's on the stack above us. Do that NOW, and
384
+ // without using a C++ RAII object,
385
+ // so there's no way that exiting the parent frame can clear it,
386
+ // or we clear it unexpectedly. This arises in the context of the
387
+ // interpreter shutting down. See https://github.com/python-greenlet/greenlet/issues/325
388
+ //PyObject* run = _run.relinquish_ownership();
389
+
390
+ /* in the new greenlet */
391
+ assert(this->thread_state()->borrow_current() == BorrowedGreenlet(this->_self));
392
+ // C++ exceptions cannot propagate to the parent greenlet from
393
+ // here. (TODO: Do we need a catch(...) clause, perhaps on the
394
+ // function itself? ALl we could do is terminate the program.)
395
+ // NOTE: On 32-bit Windows, the call chain is extremely
396
+ // important here in ways that are subtle, having to do with
397
+ // the depth of the SEH list. The call to restore it MUST NOT
398
+ // add a new SEH handler to the list, or we'll restore it to
399
+ // the wrong thing.
400
+ this->thread_state()->restore_exception_state();
401
+ /* stack variables from above are no good and also will not unwind! */
402
+ // EXCEPT: That can't be true, we access run, among others, here.
403
+
404
+ this->stack_state.set_active(); /* running */
405
+
406
+ // We're about to possibly run Python code again, which
407
+ // could switch back/away to/from us, so we need to grab the
408
+ // arguments locally.
409
+ SwitchingArgs args;
410
+ args <<= this->args();
411
+ assert(!this->args());
412
+
413
+ // XXX: We could clear this much earlier, right?
414
+ // Or would that introduce the possibility of running Python
415
+ // code when we don't want to?
416
+ // CAUTION: This may run arbitrary Python code.
417
+ this->_run_callable.CLEAR();
418
+
419
+
420
+ // The first switch we need to manually call the trace
421
+ // function here instead of in g_switch_finish, because we
422
+ // never return there.
423
+ if (OwnedObject tracefunc = this->thread_state()->get_tracefunc()) {
424
+ OwnedGreenlet trace_origin;
425
+ trace_origin = origin_greenlet;
426
+ try {
427
+ g_calltrace(tracefunc,
428
+ args ? mod_globs->event_switch : mod_globs->event_throw,
429
+ trace_origin,
430
+ this->_self);
431
+ }
432
+ catch (const PyErrOccurred&) {
433
+ /* Turn trace errors into switch throws */
434
+ args.CLEAR();
435
+ }
436
+ }
437
+
438
+ // We no longer need the origin, it was only here for
439
+ // tracing.
440
+ // We may never actually exit this stack frame so we need
441
+ // to explicitly clear it.
442
+ // This could run Python code and switch.
443
+ Py_CLEAR(origin_greenlet);
444
+
445
+ OwnedObject result;
446
+ if (!args) {
447
+ /* pending exception */
448
+ result = NULL;
449
+ }
450
+ else {
451
+ /* call g.run(*args, **kwargs) */
452
+ // This could result in further switches
453
+ try {
454
+ //result = run.PyCall(args.args(), args.kwargs());
455
+ // CAUTION: Just invoking this, before the function even
456
+ // runs, may cause memory allocations, which may trigger
457
+ // GC, which may run arbitrary Python code.
458
+ result = OwnedObject::consuming(PyObject_Call(run, args.args().borrow(), args.kwargs().borrow()));
459
+ }
460
+ catch (...) {
461
+ // Unhandled C++ exception!
462
+
463
+ // If we declare ourselves as noexcept, if we don't catch
464
+ // this here, most platforms will just abort() the
465
+ // process. But on 64-bit Windows with older versions of
466
+ // the C runtime, this can actually corrupt memory and
467
+ // just return. We see this when compiling with the
468
+ // Windows 7.0 SDK targeting Windows Server 2008, but not
469
+ // when using the Appveyor Visual Studio 2019 image. So
470
+ // this currently only affects Python 2.7 on Windows 64.
471
+ // That is, the tests pass and the runtime aborts
472
+ // everywhere else.
473
+ //
474
+ // However, if we catch it and try to continue with a
475
+ // Python error, then all Windows 64 bit platforms corrupt
476
+ // memory. So all we can do is manually abort, hopefully
477
+ // with a good error message. (Note that the above was
478
+ // tested WITHOUT the `/EHr` switch being used at compile
479
+ // time, so MSVC may have "optimized" out important
480
+ // checking. Using that switch, we may be in a better
481
+ // place in terms of memory corruption.) But sometimes it
482
+ // can't be caught here at all, which is confusing but not
483
+ // terribly surprising; so again, the G_NOEXCEPT_WIN32
484
+ // plus "/EHr".
485
+ //
486
+ // Hopefully the basic C stdlib is still functional enough
487
+ // for us to at least print an error.
488
+ //
489
+ // It gets more complicated than that, though, on some
490
+ // platforms, specifically at least Linux/gcc/libstdc++. They use
491
+ // an exception to unwind the stack when a background
492
+ // thread exits. (See comments about noexcept.) So this
493
+ // may not actually represent anything untoward. On those
494
+ // platforms we allow throws of this to propagate, or
495
+ // attempt to anyway.
496
+ # if defined(WIN32) || defined(_WIN32)
497
+ Py_FatalError(
498
+ "greenlet: Unhandled C++ exception from a greenlet run function. "
499
+ "Because memory is likely corrupted, terminating process.");
500
+ std::abort();
501
+ #else
502
+ throw;
503
+ #endif
504
+ }
505
+ }
506
+ // These lines may run arbitrary code
507
+ args.CLEAR();
508
+ Py_CLEAR(run);
509
+
510
+ if (!result
511
+ && mod_globs->PyExc_GreenletExit.PyExceptionMatches()
512
+ && (this->args())) {
513
+ // This can happen, for example, if our only reference
514
+ // goes away after we switch back to the parent.
515
+ // See test_dealloc_switch_args_not_lost
516
+ PyErrPieces clear_error;
517
+ result <<= this->args();
518
+ result = single_result(result);
519
+ }
520
+ this->release_args();
521
+ this->python_state.did_finish(PyThreadState_GET());
522
+
523
+ result = g_handle_exit(result);
524
+ assert(this->thread_state()->borrow_current() == this->_self);
525
+
526
+ /* jump back to parent */
527
+ this->stack_state.set_inactive(); /* dead */
528
+
529
+
530
+ // TODO: Can we decref some things here? Release our main greenlet
531
+ // and maybe parent?
532
+ for (Greenlet* parent = this->_parent;
533
+ parent;
534
+ parent = parent->parent()) {
535
+ // We need to somewhere consume a reference to
536
+ // the result; in most cases we'll never have control
537
+ // back in this stack frame again. Calling
538
+ // green_switch actually adds another reference!
539
+ // This would probably be clearer with a specific API
540
+ // to hand results to the parent.
541
+ parent->args() <<= result;
542
+ assert(!result);
543
+ // The parent greenlet now owns the result; in the
544
+ // typical case we'll never get back here to assign to
545
+ // result and thus release the reference.
546
+ try {
547
+ result = parent->g_switch();
548
+ }
549
+ catch (const PyErrOccurred&) {
550
+ // Ignore, keep passing the error on up.
551
+ }
552
+
553
+ /* Return here means switch to parent failed,
554
+ * in which case we throw *current* exception
555
+ * to the next parent in chain.
556
+ */
557
+ assert(!result);
558
+ }
559
+ /* We ran out of parents, cannot continue */
560
+ PyErr_WriteUnraisable(this->self().borrow_o());
561
+ Py_FatalError("greenlet: ran out of parent greenlets while propagating exception; "
562
+ "cannot continue");
563
+ std::abort();
564
+ }
565
+
566
+ void
567
+ UserGreenlet::run(const BorrowedObject nrun)
568
+ {
569
+ if (this->started()) {
570
+ throw AttributeError(
571
+ "run cannot be set "
572
+ "after the start of the greenlet");
573
+ }
574
+ this->_run_callable = nrun;
575
+ }
576
+
577
+ const OwnedGreenlet
578
+ UserGreenlet::parent() const
579
+ {
580
+ return this->_parent;
581
+ }
582
+
583
+ void
584
+ UserGreenlet::parent(const BorrowedObject raw_new_parent)
585
+ {
586
+ if (!raw_new_parent) {
587
+ throw AttributeError("can't delete attribute");
588
+ }
589
+
590
+ BorrowedMainGreenlet main_greenlet_of_new_parent;
591
+ BorrowedGreenlet new_parent(raw_new_parent.borrow()); // could
592
+ // throw
593
+ // TypeError!
594
+ for (BorrowedGreenlet p = new_parent; p; p = p->parent()) {
595
+ if (p == this->self()) {
596
+ throw ValueError("cyclic parent chain");
597
+ }
598
+ main_greenlet_of_new_parent = p->main_greenlet();
599
+ }
600
+
601
+ if (!main_greenlet_of_new_parent) {
602
+ throw ValueError("parent must not be garbage collected");
603
+ }
604
+
605
+ if (this->started()
606
+ && this->_main_greenlet != main_greenlet_of_new_parent) {
607
+ throw ValueError("parent cannot be on a different thread");
608
+ }
609
+
610
+ this->_parent = new_parent;
611
+ }
612
+
613
+ void
614
+ UserGreenlet::murder_in_place()
615
+ {
616
+ this->_main_greenlet.CLEAR();
617
+ Greenlet::murder_in_place();
618
+ }
619
+
620
+ bool
621
+ UserGreenlet::belongs_to_thread(const ThreadState* thread_state) const
622
+ {
623
+ return Greenlet::belongs_to_thread(thread_state) && this->_main_greenlet == thread_state->borrow_main_greenlet();
624
+ }
625
+
626
+
627
+ int
628
+ UserGreenlet::tp_traverse(visitproc visit, void* arg)
629
+ {
630
+ Py_VISIT(this->_parent.borrow_o());
631
+ Py_VISIT(this->_main_greenlet.borrow_o());
632
+ Py_VISIT(this->_run_callable.borrow_o());
633
+
634
+ return Greenlet::tp_traverse(visit, arg);
635
+ }
636
+
637
+ int
638
+ UserGreenlet::tp_clear()
639
+ {
640
+ Greenlet::tp_clear();
641
+ this->_parent.CLEAR();
642
+ this->_main_greenlet.CLEAR();
643
+ this->_run_callable.CLEAR();
644
+ return 0;
645
+ }
646
+
647
+ UserGreenlet::ParentIsCurrentGuard::ParentIsCurrentGuard(UserGreenlet* p,
648
+ const ThreadState& thread_state)
649
+ : oldparent(p->_parent),
650
+ greenlet(p)
651
+ {
652
+ p->_parent = thread_state.get_current();
653
+ }
654
+
655
+ UserGreenlet::ParentIsCurrentGuard::~ParentIsCurrentGuard()
656
+ {
657
+ this->greenlet->_parent = oldparent;
658
+ oldparent.CLEAR();
659
+ }
660
+
661
+ }; //namespace greenlet
662
+ #endif
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/__init__.py ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """
3
+ The root of the greenlet package.
4
+ """
5
+ from __future__ import absolute_import
6
+ from __future__ import division
7
+ from __future__ import print_function
8
+
9
+ __all__ = [
10
+ '__version__',
11
+ '_C_API',
12
+
13
+ 'GreenletExit',
14
+ 'error',
15
+
16
+ 'getcurrent',
17
+ 'greenlet',
18
+
19
+ 'gettrace',
20
+ 'settrace',
21
+ ]
22
+
23
+ # pylint:disable=no-name-in-module
24
+
25
+ ###
26
+ # Metadata
27
+ ###
28
+ __version__ = '3.2.3'
29
+ from ._greenlet import _C_API # pylint:disable=no-name-in-module
30
+
31
+ ###
32
+ # Exceptions
33
+ ###
34
+ from ._greenlet import GreenletExit
35
+ from ._greenlet import error
36
+
37
+ ###
38
+ # greenlets
39
+ ###
40
+ from ._greenlet import getcurrent
41
+ from ._greenlet import greenlet
42
+
43
+ ###
44
+ # tracing
45
+ ###
46
+ try:
47
+ from ._greenlet import gettrace
48
+ from ._greenlet import settrace
49
+ except ImportError:
50
+ # Tracing wasn't supported.
51
+ # XXX: The option to disable it was removed in 1.0,
52
+ # so this branch should be dead code.
53
+ pass
54
+
55
+ ###
56
+ # Constants
57
+ # These constants aren't documented and aren't recommended.
58
+ # In 1.0, USE_GC and USE_TRACING are always true, and USE_CONTEXT_VARS
59
+ # is the same as ``sys.version_info[:2] >= 3.7``
60
+ ###
61
+ from ._greenlet import GREENLET_USE_CONTEXT_VARS # pylint:disable=unused-import
62
+ from ._greenlet import GREENLET_USE_GC # pylint:disable=unused-import
63
+ from ._greenlet import GREENLET_USE_TRACING # pylint:disable=unused-import
64
+
65
+ # Controlling the use of the gc module. Provisional API for this greenlet
66
+ # implementation in 2.0.
67
+ from ._greenlet import CLOCKS_PER_SEC # pylint:disable=unused-import
68
+ from ._greenlet import enable_optional_cleanup # pylint:disable=unused-import
69
+ from ._greenlet import get_clocks_used_doing_optional_cleanup # pylint:disable=unused-import
70
+
71
+ # Other APIS in the _greenlet module are for test support.
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/greenlet.cpp ADDED
@@ -0,0 +1,320 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* -*- indent-tabs-mode: nil; tab-width: 4; -*- */
2
+ /* Format with:
3
+ * clang-format -i --style=file src/greenlet/greenlet.c
4
+ *
5
+ *
6
+ * Fix missing braces with:
7
+ * clang-tidy src/greenlet/greenlet.c -fix -checks="readability-braces-around-statements"
8
+ */
9
+ #include <cstdlib>
10
+ #include <string>
11
+ #include <algorithm>
12
+ #include <exception>
13
+
14
+
15
+ #define PY_SSIZE_T_CLEAN
16
+ #include <Python.h>
17
+ #include "structmember.h" // PyMemberDef
18
+
19
+ #include "greenlet_internal.hpp"
20
+ // Code after this point can assume access to things declared in stdint.h,
21
+ // including the fixed-width types. This goes for the platform-specific switch functions
22
+ // as well.
23
+ #include "greenlet_refs.hpp"
24
+ #include "greenlet_slp_switch.hpp"
25
+
26
+ #include "greenlet_thread_support.hpp"
27
+ #include "TGreenlet.hpp"
28
+
29
+ #include "TGreenletGlobals.cpp"
30
+
31
+ #include "TGreenlet.cpp"
32
+ #include "TMainGreenlet.cpp"
33
+ #include "TUserGreenlet.cpp"
34
+ #include "TBrokenGreenlet.cpp"
35
+ #include "TExceptionState.cpp"
36
+ #include "TPythonState.cpp"
37
+ #include "TStackState.cpp"
38
+
39
+ #include "TThreadState.hpp"
40
+ #include "TThreadStateCreator.hpp"
41
+ #include "TThreadStateDestroy.cpp"
42
+
43
+ #include "PyGreenlet.cpp"
44
+ #include "PyGreenletUnswitchable.cpp"
45
+ #include "CObjects.cpp"
46
+
47
+ using greenlet::LockGuard;
48
+ using greenlet::LockInitError;
49
+ using greenlet::PyErrOccurred;
50
+ using greenlet::Require;
51
+
52
+ using greenlet::g_handle_exit;
53
+ using greenlet::single_result;
54
+
55
+ using greenlet::Greenlet;
56
+ using greenlet::UserGreenlet;
57
+ using greenlet::MainGreenlet;
58
+ using greenlet::BrokenGreenlet;
59
+ using greenlet::ThreadState;
60
+ using greenlet::PythonState;
61
+
62
+
63
+
64
+ // ******* Implementation of things from included files
65
+ template<typename T, greenlet::refs::TypeChecker TC>
66
+ greenlet::refs::_BorrowedGreenlet<T, TC>& greenlet::refs::_BorrowedGreenlet<T, TC>::operator=(const greenlet::refs::BorrowedObject& other)
67
+ {
68
+ this->_set_raw_pointer(static_cast<PyObject*>(other));
69
+ return *this;
70
+ }
71
+
72
+ template <typename T, greenlet::refs::TypeChecker TC>
73
+ inline greenlet::refs::_BorrowedGreenlet<T, TC>::operator Greenlet*() const noexcept
74
+ {
75
+ if (!this->p) {
76
+ return nullptr;
77
+ }
78
+ return reinterpret_cast<PyGreenlet*>(this->p)->pimpl;
79
+ }
80
+
81
+ template<typename T, greenlet::refs::TypeChecker TC>
82
+ greenlet::refs::_BorrowedGreenlet<T, TC>::_BorrowedGreenlet(const BorrowedObject& p)
83
+ : BorrowedReference<T, TC>(nullptr)
84
+ {
85
+
86
+ this->_set_raw_pointer(p.borrow());
87
+ }
88
+
89
+ template <typename T, greenlet::refs::TypeChecker TC>
90
+ inline greenlet::refs::_OwnedGreenlet<T, TC>::operator Greenlet*() const noexcept
91
+ {
92
+ if (!this->p) {
93
+ return nullptr;
94
+ }
95
+ return reinterpret_cast<PyGreenlet*>(this->p)->pimpl;
96
+ }
97
+
98
+
99
+
100
+ #ifdef __clang__
101
+ # pragma clang diagnostic push
102
+ # pragma clang diagnostic ignored "-Wmissing-field-initializers"
103
+ # pragma clang diagnostic ignored "-Wwritable-strings"
104
+ #elif defined(__GNUC__)
105
+ # pragma GCC diagnostic push
106
+ // warning: ISO C++ forbids converting a string constant to ‘char*’
107
+ // (The python APIs aren't const correct and accept writable char*)
108
+ # pragma GCC diagnostic ignored "-Wwrite-strings"
109
+ #endif
110
+
111
+
112
+ /***********************************************************
113
+
114
+ A PyGreenlet is a range of C stack addresses that must be
115
+ saved and restored in such a way that the full range of the
116
+ stack contains valid data when we switch to it.
117
+
118
+ Stack layout for a greenlet:
119
+
120
+ | ^^^ |
121
+ | older data |
122
+ | |
123
+ stack_stop . |_______________|
124
+ . | |
125
+ . | greenlet data |
126
+ . | in stack |
127
+ . * |_______________| . . _____________ stack_copy + stack_saved
128
+ . | | | |
129
+ . | data | |greenlet data|
130
+ . | unrelated | | saved |
131
+ . | to | | in heap |
132
+ stack_start . | this | . . |_____________| stack_copy
133
+ | greenlet |
134
+ | |
135
+ | newer data |
136
+ | vvv |
137
+
138
+
139
+ Note that a greenlet's stack data is typically partly at its correct
140
+ place in the stack, and partly saved away in the heap, but always in
141
+ the above configuration: two blocks, the more recent one in the heap
142
+ and the older one still in the stack (either block may be empty).
143
+
144
+ Greenlets are chained: each points to the previous greenlet, which is
145
+ the one that owns the data currently in the C stack above my
146
+ stack_stop. The currently running greenlet is the first element of
147
+ this chain. The main (initial) greenlet is the last one. Greenlets
148
+ whose stack is entirely in the heap can be skipped from the chain.
149
+
150
+ The chain is not related to execution order, but only to the order
151
+ in which bits of C stack happen to belong to greenlets at a particular
152
+ point in time.
153
+
154
+ The main greenlet doesn't have a stack_stop: it is responsible for the
155
+ complete rest of the C stack, and we don't know where it begins. We
156
+ use (char*) -1, the largest possible address.
157
+
158
+ States:
159
+ stack_stop == NULL && stack_start == NULL: did not start yet
160
+ stack_stop != NULL && stack_start == NULL: already finished
161
+ stack_stop != NULL && stack_start != NULL: active
162
+
163
+ The running greenlet's stack_start is undefined but not NULL.
164
+
165
+ ***********************************************************/
166
+
167
+
168
+
169
+
170
+ /***********************************************************/
171
+
172
+ /* Some functions must not be inlined:
173
+ * slp_restore_state, when inlined into slp_switch might cause
174
+ it to restore stack over its own local variables
175
+ * slp_save_state, when inlined would add its own local
176
+ variables to the saved stack, wasting space
177
+ * slp_switch, cannot be inlined for obvious reasons
178
+ * g_initialstub, when inlined would receive a pointer into its
179
+ own stack frame, leading to incomplete stack save/restore
180
+
181
+ g_initialstub is a member function and declared virtual so that the
182
+ compiler always calls it through a vtable.
183
+
184
+ slp_save_state and slp_restore_state are also member functions. They
185
+ are called from trampoline functions that themselves are declared as
186
+ not eligible for inlining.
187
+ */
188
+
189
+ extern "C" {
190
+ static int GREENLET_NOINLINE(slp_save_state_trampoline)(char* stackref)
191
+ {
192
+ return switching_thread_state->slp_save_state(stackref);
193
+ }
194
+ static void GREENLET_NOINLINE(slp_restore_state_trampoline)()
195
+ {
196
+ switching_thread_state->slp_restore_state();
197
+ }
198
+ }
199
+
200
+
201
+ /***********************************************************/
202
+
203
+
204
+ #include "PyModule.cpp"
205
+
206
+
207
+
208
+ static PyObject*
209
+ greenlet_internal_mod_init() noexcept
210
+ {
211
+ static void* _PyGreenlet_API[PyGreenlet_API_pointers];
212
+
213
+ try {
214
+ CreatedModule m(greenlet_module_def);
215
+
216
+ Require(PyType_Ready(&PyGreenlet_Type));
217
+ Require(PyType_Ready(&PyGreenletUnswitchable_Type));
218
+
219
+ mod_globs = new greenlet::GreenletGlobals;
220
+ ThreadState::init();
221
+
222
+ m.PyAddObject("greenlet", PyGreenlet_Type);
223
+ m.PyAddObject("UnswitchableGreenlet", PyGreenletUnswitchable_Type);
224
+ m.PyAddObject("error", mod_globs->PyExc_GreenletError);
225
+ m.PyAddObject("GreenletExit", mod_globs->PyExc_GreenletExit);
226
+
227
+ m.PyAddObject("GREENLET_USE_GC", 1);
228
+ m.PyAddObject("GREENLET_USE_TRACING", 1);
229
+ m.PyAddObject("GREENLET_USE_CONTEXT_VARS", 1L);
230
+ m.PyAddObject("GREENLET_USE_STANDARD_THREADING", 1L);
231
+
232
+ OwnedObject clocks_per_sec = OwnedObject::consuming(PyLong_FromSsize_t(CLOCKS_PER_SEC));
233
+ m.PyAddObject("CLOCKS_PER_SEC", clocks_per_sec);
234
+
235
+ /* also publish module-level data as attributes of the greentype. */
236
+ // XXX: This is weird, and enables a strange pattern of
237
+ // confusing the class greenlet with the module greenlet; with
238
+ // the exception of (possibly) ``getcurrent()``, this
239
+ // shouldn't be encouraged so don't add new items here.
240
+ for (const char* const* p = copy_on_greentype; *p; p++) {
241
+ OwnedObject o = m.PyRequireAttr(*p);
242
+ PyDict_SetItemString(PyGreenlet_Type.tp_dict, *p, o.borrow());
243
+ }
244
+
245
+ /*
246
+ * Expose C API
247
+ */
248
+
249
+ /* types */
250
+ _PyGreenlet_API[PyGreenlet_Type_NUM] = (void*)&PyGreenlet_Type;
251
+
252
+ /* exceptions */
253
+ _PyGreenlet_API[PyExc_GreenletError_NUM] = (void*)mod_globs->PyExc_GreenletError;
254
+ _PyGreenlet_API[PyExc_GreenletExit_NUM] = (void*)mod_globs->PyExc_GreenletExit;
255
+
256
+ /* methods */
257
+ _PyGreenlet_API[PyGreenlet_New_NUM] = (void*)PyGreenlet_New;
258
+ _PyGreenlet_API[PyGreenlet_GetCurrent_NUM] = (void*)PyGreenlet_GetCurrent;
259
+ _PyGreenlet_API[PyGreenlet_Throw_NUM] = (void*)PyGreenlet_Throw;
260
+ _PyGreenlet_API[PyGreenlet_Switch_NUM] = (void*)PyGreenlet_Switch;
261
+ _PyGreenlet_API[PyGreenlet_SetParent_NUM] = (void*)PyGreenlet_SetParent;
262
+
263
+ /* Previously macros, but now need to be functions externally. */
264
+ _PyGreenlet_API[PyGreenlet_MAIN_NUM] = (void*)Extern_PyGreenlet_MAIN;
265
+ _PyGreenlet_API[PyGreenlet_STARTED_NUM] = (void*)Extern_PyGreenlet_STARTED;
266
+ _PyGreenlet_API[PyGreenlet_ACTIVE_NUM] = (void*)Extern_PyGreenlet_ACTIVE;
267
+ _PyGreenlet_API[PyGreenlet_GET_PARENT_NUM] = (void*)Extern_PyGreenlet_GET_PARENT;
268
+
269
+ /* XXX: Note that our module name is ``greenlet._greenlet``, but for
270
+ backwards compatibility with existing C code, we need the _C_API to
271
+ be directly in greenlet.
272
+ */
273
+ const NewReference c_api_object(Require(
274
+ PyCapsule_New(
275
+ (void*)_PyGreenlet_API,
276
+ "greenlet._C_API",
277
+ NULL)));
278
+ m.PyAddObject("_C_API", c_api_object);
279
+ assert(c_api_object.REFCNT() == 2);
280
+
281
+ // cerr << "Sizes:"
282
+ // << "\n\tGreenlet : " << sizeof(Greenlet)
283
+ // << "\n\tUserGreenlet : " << sizeof(UserGreenlet)
284
+ // << "\n\tMainGreenlet : " << sizeof(MainGreenlet)
285
+ // << "\n\tExceptionState : " << sizeof(greenlet::ExceptionState)
286
+ // << "\n\tPythonState : " << sizeof(greenlet::PythonState)
287
+ // << "\n\tStackState : " << sizeof(greenlet::StackState)
288
+ // << "\n\tSwitchingArgs : " << sizeof(greenlet::SwitchingArgs)
289
+ // << "\n\tOwnedObject : " << sizeof(greenlet::refs::OwnedObject)
290
+ // << "\n\tBorrowedObject : " << sizeof(greenlet::refs::BorrowedObject)
291
+ // << "\n\tPyGreenlet : " << sizeof(PyGreenlet)
292
+ // << endl;
293
+
294
+ return m.borrow(); // But really it's the main reference.
295
+ }
296
+ catch (const LockInitError& e) {
297
+ PyErr_SetString(PyExc_MemoryError, e.what());
298
+ return NULL;
299
+ }
300
+ catch (const PyErrOccurred&) {
301
+ return NULL;
302
+ }
303
+
304
+ }
305
+
306
+ extern "C" {
307
+
308
+ PyMODINIT_FUNC
309
+ PyInit__greenlet(void)
310
+ {
311
+ return greenlet_internal_mod_init();
312
+ }
313
+
314
+ }; // extern C
315
+
316
+ #ifdef __clang__
317
+ # pragma clang diagnostic pop
318
+ #elif defined(__GNUC__)
319
+ # pragma GCC diagnostic pop
320
+ #endif
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/greenlet.h ADDED
@@ -0,0 +1,164 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* -*- indent-tabs-mode: nil; tab-width: 4; -*- */
2
+
3
+ /* Greenlet object interface */
4
+
5
+ #ifndef Py_GREENLETOBJECT_H
6
+ #define Py_GREENLETOBJECT_H
7
+
8
+
9
+ #include <Python.h>
10
+
11
+ #ifdef __cplusplus
12
+ extern "C" {
13
+ #endif
14
+
15
+ /* This is deprecated and undocumented. It does not change. */
16
+ #define GREENLET_VERSION "1.0.0"
17
+
18
+ #ifndef GREENLET_MODULE
19
+ #define implementation_ptr_t void*
20
+ #endif
21
+
22
+ typedef struct _greenlet {
23
+ PyObject_HEAD
24
+ PyObject* weakreflist;
25
+ PyObject* dict;
26
+ implementation_ptr_t pimpl;
27
+ } PyGreenlet;
28
+
29
+ #define PyGreenlet_Check(op) (op && PyObject_TypeCheck(op, &PyGreenlet_Type))
30
+
31
+
32
+ /* C API functions */
33
+
34
+ /* Total number of symbols that are exported */
35
+ #define PyGreenlet_API_pointers 12
36
+
37
+ #define PyGreenlet_Type_NUM 0
38
+ #define PyExc_GreenletError_NUM 1
39
+ #define PyExc_GreenletExit_NUM 2
40
+
41
+ #define PyGreenlet_New_NUM 3
42
+ #define PyGreenlet_GetCurrent_NUM 4
43
+ #define PyGreenlet_Throw_NUM 5
44
+ #define PyGreenlet_Switch_NUM 6
45
+ #define PyGreenlet_SetParent_NUM 7
46
+
47
+ #define PyGreenlet_MAIN_NUM 8
48
+ #define PyGreenlet_STARTED_NUM 9
49
+ #define PyGreenlet_ACTIVE_NUM 10
50
+ #define PyGreenlet_GET_PARENT_NUM 11
51
+
52
+ #ifndef GREENLET_MODULE
53
+ /* This section is used by modules that uses the greenlet C API */
54
+ static void** _PyGreenlet_API = NULL;
55
+
56
+ # define PyGreenlet_Type \
57
+ (*(PyTypeObject*)_PyGreenlet_API[PyGreenlet_Type_NUM])
58
+
59
+ # define PyExc_GreenletError \
60
+ ((PyObject*)_PyGreenlet_API[PyExc_GreenletError_NUM])
61
+
62
+ # define PyExc_GreenletExit \
63
+ ((PyObject*)_PyGreenlet_API[PyExc_GreenletExit_NUM])
64
+
65
+ /*
66
+ * PyGreenlet_New(PyObject *args)
67
+ *
68
+ * greenlet.greenlet(run, parent=None)
69
+ */
70
+ # define PyGreenlet_New \
71
+ (*(PyGreenlet * (*)(PyObject * run, PyGreenlet * parent)) \
72
+ _PyGreenlet_API[PyGreenlet_New_NUM])
73
+
74
+ /*
75
+ * PyGreenlet_GetCurrent(void)
76
+ *
77
+ * greenlet.getcurrent()
78
+ */
79
+ # define PyGreenlet_GetCurrent \
80
+ (*(PyGreenlet * (*)(void)) _PyGreenlet_API[PyGreenlet_GetCurrent_NUM])
81
+
82
+ /*
83
+ * PyGreenlet_Throw(
84
+ * PyGreenlet *greenlet,
85
+ * PyObject *typ,
86
+ * PyObject *val,
87
+ * PyObject *tb)
88
+ *
89
+ * g.throw(...)
90
+ */
91
+ # define PyGreenlet_Throw \
92
+ (*(PyObject * (*)(PyGreenlet * self, \
93
+ PyObject * typ, \
94
+ PyObject * val, \
95
+ PyObject * tb)) \
96
+ _PyGreenlet_API[PyGreenlet_Throw_NUM])
97
+
98
+ /*
99
+ * PyGreenlet_Switch(PyGreenlet *greenlet, PyObject *args)
100
+ *
101
+ * g.switch(*args, **kwargs)
102
+ */
103
+ # define PyGreenlet_Switch \
104
+ (*(PyObject * \
105
+ (*)(PyGreenlet * greenlet, PyObject * args, PyObject * kwargs)) \
106
+ _PyGreenlet_API[PyGreenlet_Switch_NUM])
107
+
108
+ /*
109
+ * PyGreenlet_SetParent(PyObject *greenlet, PyObject *new_parent)
110
+ *
111
+ * g.parent = new_parent
112
+ */
113
+ # define PyGreenlet_SetParent \
114
+ (*(int (*)(PyGreenlet * greenlet, PyGreenlet * nparent)) \
115
+ _PyGreenlet_API[PyGreenlet_SetParent_NUM])
116
+
117
+ /*
118
+ * PyGreenlet_GetParent(PyObject* greenlet)
119
+ *
120
+ * return greenlet.parent;
121
+ *
122
+ * This could return NULL even if there is no exception active.
123
+ * If it does not return NULL, you are responsible for decrementing the
124
+ * reference count.
125
+ */
126
+ # define PyGreenlet_GetParent \
127
+ (*(PyGreenlet* (*)(PyGreenlet*)) \
128
+ _PyGreenlet_API[PyGreenlet_GET_PARENT_NUM])
129
+
130
+ /*
131
+ * deprecated, undocumented alias.
132
+ */
133
+ # define PyGreenlet_GET_PARENT PyGreenlet_GetParent
134
+
135
+ # define PyGreenlet_MAIN \
136
+ (*(int (*)(PyGreenlet*)) \
137
+ _PyGreenlet_API[PyGreenlet_MAIN_NUM])
138
+
139
+ # define PyGreenlet_STARTED \
140
+ (*(int (*)(PyGreenlet*)) \
141
+ _PyGreenlet_API[PyGreenlet_STARTED_NUM])
142
+
143
+ # define PyGreenlet_ACTIVE \
144
+ (*(int (*)(PyGreenlet*)) \
145
+ _PyGreenlet_API[PyGreenlet_ACTIVE_NUM])
146
+
147
+
148
+
149
+
150
+ /* Macro that imports greenlet and initializes C API */
151
+ /* NOTE: This has actually moved to ``greenlet._greenlet._C_API``, but we
152
+ keep the older definition to be sure older code that might have a copy of
153
+ the header still works. */
154
+ # define PyGreenlet_Import() \
155
+ { \
156
+ _PyGreenlet_API = (void**)PyCapsule_Import("greenlet._C_API", 0); \
157
+ }
158
+
159
+ #endif /* GREENLET_MODULE */
160
+
161
+ #ifdef __cplusplus
162
+ }
163
+ #endif
164
+ #endif /* !Py_GREENLETOBJECT_H */
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/greenlet_allocator.hpp ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #ifndef GREENLET_ALLOCATOR_HPP
2
+ #define GREENLET_ALLOCATOR_HPP
3
+
4
+ #define PY_SSIZE_T_CLEAN
5
+ #include <Python.h>
6
+ #include <memory>
7
+ #include "greenlet_compiler_compat.hpp"
8
+
9
+
10
+ namespace greenlet
11
+ {
12
+ // This allocator is stateless; all instances are identical.
13
+ // It can *ONLY* be used when we're sure we're holding the GIL
14
+ // (Python's allocators require the GIL).
15
+ template <class T>
16
+ struct PythonAllocator : public std::allocator<T> {
17
+
18
+ PythonAllocator(const PythonAllocator& UNUSED(other))
19
+ : std::allocator<T>()
20
+ {
21
+ }
22
+
23
+ PythonAllocator(const std::allocator<T> other)
24
+ : std::allocator<T>(other)
25
+ {}
26
+
27
+ template <class U>
28
+ PythonAllocator(const std::allocator<U>& other)
29
+ : std::allocator<T>(other)
30
+ {
31
+ }
32
+
33
+ PythonAllocator() : std::allocator<T>() {}
34
+
35
+ T* allocate(size_t number_objects, const void* UNUSED(hint)=0)
36
+ {
37
+ void* p;
38
+ if (number_objects == 1)
39
+ p = PyObject_Malloc(sizeof(T));
40
+ else
41
+ p = PyMem_Malloc(sizeof(T) * number_objects);
42
+ return static_cast<T*>(p);
43
+ }
44
+
45
+ void deallocate(T* t, size_t n)
46
+ {
47
+ void* p = t;
48
+ if (n == 1) {
49
+ PyObject_Free(p);
50
+ }
51
+ else
52
+ PyMem_Free(p);
53
+ }
54
+ // This member is deprecated in C++17 and removed in C++20
55
+ template< class U >
56
+ struct rebind {
57
+ typedef PythonAllocator<U> other;
58
+ };
59
+
60
+ };
61
+ }
62
+
63
+ #endif
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/greenlet_compiler_compat.hpp ADDED
@@ -0,0 +1,98 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* -*- indent-tabs-mode: nil; tab-width: 4; -*- */
2
+ #ifndef GREENLET_COMPILER_COMPAT_HPP
3
+ #define GREENLET_COMPILER_COMPAT_HPP
4
+
5
+ /**
6
+ * Definitions to aid with compatibility with different compilers.
7
+ *
8
+ * .. caution:: Use extreme care with noexcept.
9
+ * Some compilers and runtimes, specifically gcc/libgcc/libstdc++ on
10
+ * Linux, implement stack unwinding by throwing an uncatchable
11
+ * exception, one that specifically does not appear to be an active
12
+ * exception to the rest of the runtime. If this happens while we're in a noexcept function,
13
+ * we have violated our dynamic exception contract, and so the runtime
14
+ * will call std::terminate(), which kills the process with the
15
+ * unhelpful message "terminate called without an active exception".
16
+ *
17
+ * This has happened in this scenario: A background thread is running
18
+ * a greenlet that has made a native call and released the GIL.
19
+ * Meanwhile, the main thread finishes and starts shutting down the
20
+ * interpreter. When the background thread is scheduled again and
21
+ * attempts to obtain the GIL, it notices that the interpreter is
22
+ * exiting and calls ``pthread_exit()``. This in turn starts to unwind
23
+ * the stack by throwing that exception. But we had the ``PyCall``
24
+ * functions annotated as noexcept, so the runtime terminated us.
25
+ *
26
+ * #2 0x00007fab26fec2b7 in std::terminate() () from /lib/x86_64-linux-gnu/libstdc++.so.6
27
+ * #3 0x00007fab26febb3c in __gxx_personality_v0 () from /lib/x86_64-linux-gnu/libstdc++.so.6
28
+ * #4 0x00007fab26f34de6 in ?? () from /lib/x86_64-linux-gnu/libgcc_s.so.1
29
+ * #6 0x00007fab276a34c6 in __GI___pthread_unwind at ./nptl/unwind.c:130
30
+ * #7 0x00007fab2769bd3a in __do_cancel () at ../sysdeps/nptl/pthreadP.h:280
31
+ * #8 __GI___pthread_exit (value=value@entry=0x0) at ./nptl/pthread_exit.c:36
32
+ * #9 0x000000000052e567 in PyThread_exit_thread () at ../Python/thread_pthread.h:370
33
+ * #10 0x00000000004d60b5 in take_gil at ../Python/ceval_gil.h:224
34
+ * #11 0x00000000004d65f9 in PyEval_RestoreThread at ../Python/ceval.c:467
35
+ * #12 0x000000000060cce3 in setipaddr at ../Modules/socketmodule.c:1203
36
+ * #13 0x00000000006101cd in socket_gethostbyname
37
+ */
38
+
39
+ #include <cstdint>
40
+
41
+ # define G_NO_COPIES_OF_CLS(Cls) private: \
42
+ Cls(const Cls& other) = delete; \
43
+ Cls& operator=(const Cls& other) = delete
44
+
45
+ # define G_NO_ASSIGNMENT_OF_CLS(Cls) private: \
46
+ Cls& operator=(const Cls& other) = delete
47
+
48
+ # define G_NO_COPY_CONSTRUCTOR_OF_CLS(Cls) private: \
49
+ Cls(const Cls& other) = delete;
50
+
51
+
52
+ // CAUTION: MSVC is stupidly picky:
53
+ //
54
+ // "The compiler ignores, without warning, any __declspec keywords
55
+ // placed after * or & and in front of the variable identifier in a
56
+ // declaration."
57
+ // (https://docs.microsoft.com/en-us/cpp/cpp/declspec?view=msvc-160)
58
+ //
59
+ // So pointer return types must be handled differently (because of the
60
+ // trailing *), or you get inscrutable compiler warnings like "error
61
+ // C2059: syntax error: ''"
62
+ //
63
+ // In C++ 11, there is a standard syntax for attributes, and
64
+ // GCC defines an attribute to use with this: [[gnu:noinline]].
65
+ // In the future, this is expected to become standard.
66
+
67
+ #if defined(__GNUC__) || defined(__clang__)
68
+ /* We used to check for GCC 4+ or 3.4+, but those compilers are
69
+ laughably out of date. Just assume they support it. */
70
+ # define GREENLET_NOINLINE(name) __attribute__((noinline)) name
71
+ # define GREENLET_NOINLINE_P(rtype, name) rtype __attribute__((noinline)) name
72
+ # define UNUSED(x) UNUSED_ ## x __attribute__((__unused__))
73
+ #elif defined(_MSC_VER)
74
+ /* We used to check for && (_MSC_VER >= 1300) but that's also out of date. */
75
+ # define GREENLET_NOINLINE(name) __declspec(noinline) name
76
+ # define GREENLET_NOINLINE_P(rtype, name) __declspec(noinline) rtype name
77
+ # define UNUSED(x) UNUSED_ ## x
78
+ #endif
79
+
80
+ #if defined(_MSC_VER)
81
+ # define G_NOEXCEPT_WIN32 noexcept
82
+ #else
83
+ # define G_NOEXCEPT_WIN32
84
+ #endif
85
+
86
+ #if defined(__GNUC__) && defined(__POWERPC__) && defined(__APPLE__)
87
+ // 32-bit PPC/MacOSX. Only known to be tested on unreleased versions
88
+ // of macOS 10.6 using a macports build gcc 14. It appears that
89
+ // running C++ destructors of thread-local variables is broken.
90
+
91
+ // See https://github.com/python-greenlet/greenlet/pull/419
92
+ # define GREENLET_BROKEN_THREAD_LOCAL_CLEANUP_JUST_LEAK 1
93
+ #else
94
+ # define GREENLET_BROKEN_THREAD_LOCAL_CLEANUP_JUST_LEAK 0
95
+ #endif
96
+
97
+
98
+ #endif
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/greenlet_cpython_compat.hpp ADDED
@@ -0,0 +1,148 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* -*- indent-tabs-mode: nil; tab-width: 4; -*- */
2
+ #ifndef GREENLET_CPYTHON_COMPAT_H
3
+ #define GREENLET_CPYTHON_COMPAT_H
4
+
5
+ /**
6
+ * Helpers for compatibility with multiple versions of CPython.
7
+ */
8
+
9
+ #define PY_SSIZE_T_CLEAN
10
+ #include "Python.h"
11
+
12
+
13
+ #if PY_VERSION_HEX >= 0x30A00B1
14
+ # define GREENLET_PY310 1
15
+ #else
16
+ # define GREENLET_PY310 0
17
+ #endif
18
+
19
+ /*
20
+ Python 3.10 beta 1 changed tstate->use_tracing to a nested cframe member.
21
+ See https://github.com/python/cpython/pull/25276
22
+ We have to save and restore this as well.
23
+
24
+ Python 3.13 removed PyThreadState.cframe (GH-108035).
25
+ */
26
+ #if GREENLET_PY310 && PY_VERSION_HEX < 0x30D0000
27
+ # define GREENLET_USE_CFRAME 1
28
+ #else
29
+ # define GREENLET_USE_CFRAME 0
30
+ #endif
31
+
32
+
33
+ #if PY_VERSION_HEX >= 0x30B00A4
34
+ /*
35
+ Greenlet won't compile on anything older than Python 3.11 alpha 4 (see
36
+ https://bugs.python.org/issue46090). Summary of breaking internal changes:
37
+ - Python 3.11 alpha 1 changed how frame objects are represented internally.
38
+ - https://github.com/python/cpython/pull/30122
39
+ - Python 3.11 alpha 3 changed how recursion limits are stored.
40
+ - https://github.com/python/cpython/pull/29524
41
+ - Python 3.11 alpha 4 changed how exception state is stored. It also includes a
42
+ change to help greenlet save and restore the interpreter frame "data stack".
43
+ - https://github.com/python/cpython/pull/30122
44
+ - https://github.com/python/cpython/pull/30234
45
+ */
46
+ # define GREENLET_PY311 1
47
+ #else
48
+ # define GREENLET_PY311 0
49
+ #endif
50
+
51
+
52
+ #if PY_VERSION_HEX >= 0x30C0000
53
+ # define GREENLET_PY312 1
54
+ #else
55
+ # define GREENLET_PY312 0
56
+ #endif
57
+
58
+ #if PY_VERSION_HEX >= 0x30D0000
59
+ # define GREENLET_PY313 1
60
+ #else
61
+ # define GREENLET_PY313 0
62
+ #endif
63
+
64
+ #if PY_VERSION_HEX >= 0x30E0000
65
+ # define GREENLET_PY314 1
66
+ #else
67
+ # define GREENLET_PY314 0
68
+ #endif
69
+
70
+ #ifndef Py_SET_REFCNT
71
+ /* Py_REFCNT and Py_SIZE macros are converted to functions
72
+ https://bugs.python.org/issue39573 */
73
+ # define Py_SET_REFCNT(obj, refcnt) Py_REFCNT(obj) = (refcnt)
74
+ #endif
75
+
76
+ #ifndef _Py_DEC_REFTOTAL
77
+ /* _Py_DEC_REFTOTAL macro has been removed from Python 3.9 by:
78
+ https://github.com/python/cpython/commit/49932fec62c616ec88da52642339d83ae719e924
79
+
80
+ The symbol we use to replace it was removed by at least 3.12.
81
+ */
82
+ # ifdef Py_REF_DEBUG
83
+ # if GREENLET_PY312
84
+ # define _Py_DEC_REFTOTAL
85
+ # else
86
+ # define _Py_DEC_REFTOTAL _Py_RefTotal--
87
+ # endif
88
+ # else
89
+ # define _Py_DEC_REFTOTAL
90
+ # endif
91
+ #endif
92
+ // Define these flags like Cython does if we're on an old version.
93
+ #ifndef Py_TPFLAGS_CHECKTYPES
94
+ #define Py_TPFLAGS_CHECKTYPES 0
95
+ #endif
96
+ #ifndef Py_TPFLAGS_HAVE_INDEX
97
+ #define Py_TPFLAGS_HAVE_INDEX 0
98
+ #endif
99
+ #ifndef Py_TPFLAGS_HAVE_NEWBUFFER
100
+ #define Py_TPFLAGS_HAVE_NEWBUFFER 0
101
+ #endif
102
+
103
+ #ifndef Py_TPFLAGS_HAVE_VERSION_TAG
104
+ #define Py_TPFLAGS_HAVE_VERSION_TAG 0
105
+ #endif
106
+
107
+ #define G_TPFLAGS_DEFAULT Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_VERSION_TAG | Py_TPFLAGS_CHECKTYPES | Py_TPFLAGS_HAVE_NEWBUFFER | Py_TPFLAGS_HAVE_GC
108
+
109
+
110
+ #if PY_VERSION_HEX < 0x03090000
111
+ // The official version only became available in 3.9
112
+ # define PyObject_GC_IsTracked(o) _PyObject_GC_IS_TRACKED(o)
113
+ #endif
114
+
115
+
116
+ // bpo-43760 added PyThreadState_EnterTracing() to Python 3.11.0a2
117
+ #if PY_VERSION_HEX < 0x030B00A2 && !defined(PYPY_VERSION)
118
+ static inline void PyThreadState_EnterTracing(PyThreadState *tstate)
119
+ {
120
+ tstate->tracing++;
121
+ #if PY_VERSION_HEX >= 0x030A00A1
122
+ tstate->cframe->use_tracing = 0;
123
+ #else
124
+ tstate->use_tracing = 0;
125
+ #endif
126
+ }
127
+ #endif
128
+
129
+ // bpo-43760 added PyThreadState_LeaveTracing() to Python 3.11.0a2
130
+ #if PY_VERSION_HEX < 0x030B00A2 && !defined(PYPY_VERSION)
131
+ static inline void PyThreadState_LeaveTracing(PyThreadState *tstate)
132
+ {
133
+ tstate->tracing--;
134
+ int use_tracing = (tstate->c_tracefunc != NULL
135
+ || tstate->c_profilefunc != NULL);
136
+ #if PY_VERSION_HEX >= 0x030A00A1
137
+ tstate->cframe->use_tracing = use_tracing;
138
+ #else
139
+ tstate->use_tracing = use_tracing;
140
+ #endif
141
+ }
142
+ #endif
143
+
144
+ #if !defined(Py_C_RECURSION_LIMIT) && defined(C_RECURSION_LIMIT)
145
+ # define Py_C_RECURSION_LIMIT C_RECURSION_LIMIT
146
+ #endif
147
+
148
+ #endif /* GREENLET_CPYTHON_COMPAT_H */
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/greenlet_exceptions.hpp ADDED
@@ -0,0 +1,171 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #ifndef GREENLET_EXCEPTIONS_HPP
2
+ #define GREENLET_EXCEPTIONS_HPP
3
+
4
+ #define PY_SSIZE_T_CLEAN
5
+ #include <Python.h>
6
+ #include <stdexcept>
7
+ #include <string>
8
+
9
+ #ifdef __clang__
10
+ # pragma clang diagnostic push
11
+ # pragma clang diagnostic ignored "-Wunused-function"
12
+ #endif
13
+
14
+ namespace greenlet {
15
+
16
+ class PyErrOccurred : public std::runtime_error
17
+ {
18
+ public:
19
+
20
+ // CAUTION: In debug builds, may run arbitrary Python code.
21
+ static const PyErrOccurred
22
+ from_current()
23
+ {
24
+ assert(PyErr_Occurred());
25
+ #ifndef NDEBUG
26
+ // This is not exception safe, and
27
+ // not necessarily safe in general (what if it switches?)
28
+ // But we only do this in debug mode, where we are in
29
+ // tight control of what exceptions are getting raised and
30
+ // can prevent those issues.
31
+
32
+ // You can't call PyObject_Str with a pending exception.
33
+ PyObject* typ;
34
+ PyObject* val;
35
+ PyObject* tb;
36
+
37
+ PyErr_Fetch(&typ, &val, &tb);
38
+ PyObject* typs = PyObject_Str(typ);
39
+ PyObject* vals = PyObject_Str(val ? val : typ);
40
+ const char* typ_msg = PyUnicode_AsUTF8(typs);
41
+ const char* val_msg = PyUnicode_AsUTF8(vals);
42
+ PyErr_Restore(typ, val, tb);
43
+
44
+ std::string msg(typ_msg);
45
+ msg += ": ";
46
+ msg += val_msg;
47
+ PyErrOccurred ex(msg);
48
+ Py_XDECREF(typs);
49
+ Py_XDECREF(vals);
50
+
51
+ return ex;
52
+ #else
53
+ return PyErrOccurred();
54
+ #endif
55
+ }
56
+
57
+ PyErrOccurred() : std::runtime_error("")
58
+ {
59
+ assert(PyErr_Occurred());
60
+ }
61
+
62
+ PyErrOccurred(const std::string& msg) : std::runtime_error(msg)
63
+ {
64
+ assert(PyErr_Occurred());
65
+ }
66
+
67
+ PyErrOccurred(PyObject* exc_kind, const char* const msg)
68
+ : std::runtime_error(msg)
69
+ {
70
+ PyErr_SetString(exc_kind, msg);
71
+ }
72
+
73
+ PyErrOccurred(PyObject* exc_kind, const std::string msg)
74
+ : std::runtime_error(msg)
75
+ {
76
+ // This copies the c_str, so we don't have any lifetime
77
+ // issues to worry about.
78
+ PyErr_SetString(exc_kind, msg.c_str());
79
+ }
80
+
81
+ PyErrOccurred(PyObject* exc_kind,
82
+ const std::string msg, //This is the format
83
+ //string; that's not
84
+ //usually safe!
85
+
86
+ PyObject* borrowed_obj_one, PyObject* borrowed_obj_two)
87
+ : std::runtime_error(msg)
88
+ {
89
+
90
+ //This is designed specifically for the
91
+ //``check_switch_allowed`` function.
92
+
93
+ // PyObject_Str and PyObject_Repr are safe to call with
94
+ // NULL pointers; they return the string "<NULL>" in that
95
+ // case.
96
+ // This function always returns null.
97
+ PyErr_Format(exc_kind,
98
+ msg.c_str(),
99
+ borrowed_obj_one, borrowed_obj_two);
100
+ }
101
+ };
102
+
103
+ class TypeError : public PyErrOccurred
104
+ {
105
+ public:
106
+ TypeError(const char* const what)
107
+ : PyErrOccurred(PyExc_TypeError, what)
108
+ {
109
+ }
110
+ TypeError(const std::string what)
111
+ : PyErrOccurred(PyExc_TypeError, what)
112
+ {
113
+ }
114
+ };
115
+
116
+ class ValueError : public PyErrOccurred
117
+ {
118
+ public:
119
+ ValueError(const char* const what)
120
+ : PyErrOccurred(PyExc_ValueError, what)
121
+ {
122
+ }
123
+ };
124
+
125
+ class AttributeError : public PyErrOccurred
126
+ {
127
+ public:
128
+ AttributeError(const char* const what)
129
+ : PyErrOccurred(PyExc_AttributeError, what)
130
+ {
131
+ }
132
+ };
133
+
134
+ /**
135
+ * Calls `Py_FatalError` when constructed, so you can't actually
136
+ * throw this. It just makes static analysis easier.
137
+ */
138
+ class PyFatalError : public std::runtime_error
139
+ {
140
+ public:
141
+ PyFatalError(const char* const msg)
142
+ : std::runtime_error(msg)
143
+ {
144
+ Py_FatalError(msg);
145
+ }
146
+ };
147
+
148
+ static inline PyObject*
149
+ Require(PyObject* p, const std::string& msg="")
150
+ {
151
+ if (!p) {
152
+ throw PyErrOccurred(msg);
153
+ }
154
+ return p;
155
+ };
156
+
157
+ static inline void
158
+ Require(const int retval)
159
+ {
160
+ if (retval < 0) {
161
+ throw PyErrOccurred();
162
+ }
163
+ };
164
+
165
+
166
+ };
167
+ #ifdef __clang__
168
+ # pragma clang diagnostic pop
169
+ #endif
170
+
171
+ #endif
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/greenlet_internal.hpp ADDED
@@ -0,0 +1,107 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* -*- indent-tabs-mode: nil; tab-width: 4; -*- */
2
+ #ifndef GREENLET_INTERNAL_H
3
+ #define GREENLET_INTERNAL_H
4
+ #ifdef __clang__
5
+ # pragma clang diagnostic push
6
+ # pragma clang diagnostic ignored "-Wunused-function"
7
+ #endif
8
+
9
+ /**
10
+ * Implementation helpers.
11
+ *
12
+ * C++ templates and inline functions should go here.
13
+ */
14
+ #define PY_SSIZE_T_CLEAN
15
+ #include "greenlet_compiler_compat.hpp"
16
+ #include "greenlet_cpython_compat.hpp"
17
+ #include "greenlet_exceptions.hpp"
18
+ #include "TGreenlet.hpp"
19
+ #include "greenlet_allocator.hpp"
20
+
21
+ #include <vector>
22
+ #include <string>
23
+
24
+ #define GREENLET_MODULE
25
+ struct _greenlet;
26
+ typedef struct _greenlet PyGreenlet;
27
+ namespace greenlet {
28
+
29
+ class ThreadState;
30
+ // We can't use the PythonAllocator for this, because we push to it
31
+ // from the thread state destructor, which doesn't have the GIL,
32
+ // and Python's allocators can only be called with the GIL.
33
+ typedef std::vector<ThreadState*> cleanup_queue_t;
34
+
35
+ };
36
+
37
+
38
+ #define implementation_ptr_t greenlet::Greenlet*
39
+
40
+
41
+ #include "greenlet.h"
42
+
43
+ void
44
+ greenlet::refs::MainGreenletExactChecker(void *p)
45
+ {
46
+ if (!p) {
47
+ return;
48
+ }
49
+ // We control the class of the main greenlet exactly.
50
+ if (Py_TYPE(p) != &PyGreenlet_Type) {
51
+ std::string err("MainGreenlet: Expected exactly a greenlet, not a ");
52
+ err += Py_TYPE(p)->tp_name;
53
+ throw greenlet::TypeError(err);
54
+ }
55
+
56
+ // Greenlets from dead threads no longer respond to main() with a
57
+ // true value; so in that case we need to perform an additional
58
+ // check.
59
+ Greenlet* g = static_cast<PyGreenlet*>(p)->pimpl;
60
+ if (g->main()) {
61
+ return;
62
+ }
63
+ if (!dynamic_cast<MainGreenlet*>(g)) {
64
+ std::string err("MainGreenlet: Expected exactly a main greenlet, not a ");
65
+ err += Py_TYPE(p)->tp_name;
66
+ throw greenlet::TypeError(err);
67
+ }
68
+ }
69
+
70
+
71
+
72
+ template <typename T, greenlet::refs::TypeChecker TC>
73
+ inline greenlet::Greenlet* greenlet::refs::_OwnedGreenlet<T, TC>::operator->() const noexcept
74
+ {
75
+ return reinterpret_cast<PyGreenlet*>(this->p)->pimpl;
76
+ }
77
+
78
+ template <typename T, greenlet::refs::TypeChecker TC>
79
+ inline greenlet::Greenlet* greenlet::refs::_BorrowedGreenlet<T, TC>::operator->() const noexcept
80
+ {
81
+ return reinterpret_cast<PyGreenlet*>(this->p)->pimpl;
82
+ }
83
+
84
+ #include <memory>
85
+ #include <stdexcept>
86
+
87
+
88
+ extern PyTypeObject PyGreenlet_Type;
89
+
90
+
91
+
92
+ /**
93
+ * Forward declarations needed in multiple files.
94
+ */
95
+ static PyObject* green_switch(PyGreenlet* self, PyObject* args, PyObject* kwargs);
96
+
97
+
98
+ #ifdef __clang__
99
+ # pragma clang diagnostic pop
100
+ #endif
101
+
102
+
103
+ #endif
104
+
105
+ // Local Variables:
106
+ // flycheck-clang-include-path: ("../../include" "/opt/local/Library/Frameworks/Python.framework/Versions/3.10/include/python3.10")
107
+ // End:
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/greenlet_msvc_compat.hpp ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
31
+ extern "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
+
47
+ static const _PyStackRef PyStackRef_NULL = { .bits = Py_TAG_DEFERRED};
48
+ #define PyStackRef_IsNull(stackref) ((stackref).bits == PyStackRef_NULL.bits)
49
+
50
+ static inline PyObject *
51
+ PyStackRef_AsPyObjectBorrow(_PyStackRef stackref)
52
+ {
53
+ PyObject *cleared = ((PyObject *)((stackref).bits & (~Py_TAG_BITS)));
54
+ return cleared;
55
+ }
56
+
57
+ static 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
+
65
+ static 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
+
78
+ static 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
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/greenlet_refs.hpp ADDED
@@ -0,0 +1,1118 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #ifndef GREENLET_REFS_HPP
2
+ #define GREENLET_REFS_HPP
3
+
4
+ #define PY_SSIZE_T_CLEAN
5
+ #include <Python.h>
6
+
7
+ #include <string>
8
+
9
+ //#include "greenlet_internal.hpp"
10
+ #include "greenlet_compiler_compat.hpp"
11
+ #include "greenlet_cpython_compat.hpp"
12
+ #include "greenlet_exceptions.hpp"
13
+
14
+ struct _greenlet;
15
+ struct _PyMainGreenlet;
16
+
17
+ typedef struct _greenlet PyGreenlet;
18
+ extern PyTypeObject PyGreenlet_Type;
19
+
20
+
21
+ #ifdef GREENLET_USE_STDIO
22
+ #include <iostream>
23
+ using std::cerr;
24
+ using std::endl;
25
+ #endif
26
+
27
+ namespace greenlet
28
+ {
29
+ class Greenlet;
30
+
31
+ namespace refs
32
+ {
33
+ // Type checkers throw a TypeError if the argument is not
34
+ // null, and isn't of the required Python type.
35
+ // (We can't use most of the defined type checkers
36
+ // like PyList_Check, etc, directly, because they are
37
+ // implemented as macros.)
38
+ typedef void (*TypeChecker)(void*);
39
+
40
+ void
41
+ NoOpChecker(void*)
42
+ {
43
+ return;
44
+ }
45
+
46
+ void
47
+ GreenletChecker(void *p)
48
+ {
49
+ if (!p) {
50
+ return;
51
+ }
52
+
53
+ PyTypeObject* typ = Py_TYPE(p);
54
+ // fast, common path. (PyObject_TypeCheck is a macro or
55
+ // static inline function, and it also does a
56
+ // direct comparison of the type pointers, but its fast
57
+ // path only handles one type)
58
+ if (typ == &PyGreenlet_Type) {
59
+ return;
60
+ }
61
+
62
+ if (!PyObject_TypeCheck(p, &PyGreenlet_Type)) {
63
+ std::string err("GreenletChecker: Expected any type of greenlet, not ");
64
+ err += Py_TYPE(p)->tp_name;
65
+ throw TypeError(err);
66
+ }
67
+ }
68
+
69
+ void
70
+ MainGreenletExactChecker(void *p);
71
+
72
+ template <typename T, TypeChecker>
73
+ class PyObjectPointer;
74
+
75
+ template<typename T, TypeChecker>
76
+ class OwnedReference;
77
+
78
+
79
+ template<typename T, TypeChecker>
80
+ class BorrowedReference;
81
+
82
+ typedef BorrowedReference<PyObject, NoOpChecker> BorrowedObject;
83
+ typedef OwnedReference<PyObject, NoOpChecker> OwnedObject;
84
+
85
+ class ImmortalObject;
86
+ class ImmortalString;
87
+
88
+ template<typename T, TypeChecker TC>
89
+ class _OwnedGreenlet;
90
+
91
+ typedef _OwnedGreenlet<PyGreenlet, GreenletChecker> OwnedGreenlet;
92
+ typedef _OwnedGreenlet<PyGreenlet, MainGreenletExactChecker> OwnedMainGreenlet;
93
+
94
+ template<typename T, TypeChecker TC>
95
+ class _BorrowedGreenlet;
96
+
97
+ typedef _BorrowedGreenlet<PyGreenlet, GreenletChecker> BorrowedGreenlet;
98
+
99
+ void
100
+ ContextExactChecker(void *p)
101
+ {
102
+ if (!p) {
103
+ return;
104
+ }
105
+ if (!PyContext_CheckExact(p)) {
106
+ throw TypeError(
107
+ "greenlet context must be a contextvars.Context or None"
108
+ );
109
+ }
110
+ }
111
+
112
+ typedef OwnedReference<PyObject, ContextExactChecker> OwnedContext;
113
+ }
114
+ }
115
+
116
+ namespace greenlet {
117
+
118
+
119
+ namespace refs {
120
+ // A set of classes to make reference counting rules in python
121
+ // code explicit.
122
+ //
123
+ // Rules of use:
124
+ // (1) Functions returning a new reference that the caller of the
125
+ // function is expected to dispose of should return a
126
+ // ``OwnedObject`` object. This object automatically releases its
127
+ // reference when it goes out of scope. It works like a ``std::shared_ptr``
128
+ // and can be copied or used as a function parameter (but don't do
129
+ // that). Note that constructing a ``OwnedObject`` from a
130
+ // PyObject* steals the reference.
131
+ // (2) Parameters to functions should be either a
132
+ // ``OwnedObject&``, or, more generally, a ``PyObjectPointer&``.
133
+ // If the function needs to create its own new reference, it can
134
+ // do so by copying to a local ``OwnedObject``.
135
+ // (3) Functions returning an existing pointer that is NOT
136
+ // incref'd, and which the caller MUST NOT decref,
137
+ // should return a ``BorrowedObject``.
138
+
139
+ // XXX: The following two paragraphs do not hold for all platforms.
140
+ // Notably, 32-bit PPC Linux passes structs by reference, not by
141
+ // value, so this actually doesn't work. (Although that's the only
142
+ // platform that doesn't work on.) DO NOT ATTEMPT IT. The
143
+ // unfortunate consequence of that is that the slots which we
144
+ // *know* are already type safe will wind up calling the type
145
+ // checker function (when we had the slots accepting
146
+ // BorrowedGreenlet, this was bypassed), so this slows us down.
147
+ // TODO: Optimize this again.
148
+
149
+ // For a class with a single pointer member, whose constructor
150
+ // does nothing but copy a pointer parameter into the member, and
151
+ // which can then be converted back to the pointer type, compilers
152
+ // generate code that's the same as just passing the pointer.
153
+ // That is, func(BorrowedObject x) called like ``PyObject* p =
154
+ // ...; f(p)`` has 0 overhead. Similarly, they "unpack" to the
155
+ // pointer type with 0 overhead.
156
+ //
157
+ // If there are no virtual functions, no complex inheritance (maybe?) and
158
+ // no destructor, these can be directly used as parameters in
159
+ // Python callbacks like tp_init: the layout is the same as a
160
+ // single pointer. Only subclasses with trivial constructors that
161
+ // do nothing but set the single pointer member are safe to use
162
+ // that way.
163
+
164
+
165
+ // This is the base class for things that can be done with a
166
+ // PyObject pointer. It assumes nothing about memory management.
167
+ // NOTE: Nothing is virtual, so subclasses shouldn't add new
168
+ // storage fields or try to override these methods.
169
+ template <typename T=PyObject, TypeChecker TC=NoOpChecker>
170
+ class PyObjectPointer
171
+ {
172
+ public:
173
+ typedef T PyType;
174
+ protected:
175
+ T* p;
176
+ public:
177
+ PyObjectPointer(T* it=nullptr) : p(it)
178
+ {
179
+ TC(p);
180
+ }
181
+
182
+ // We don't allow automatic casting to PyObject* at this
183
+ // level, because then we could be passed to Py_DECREF/INCREF,
184
+ // but we want nothing to do with memory management. If you
185
+ // know better, then you can use the get() method, like on a
186
+ // std::shared_ptr. Except we name it borrow() to clarify that
187
+ // if this is a reference-tracked object, the pointer you get
188
+ // back will go away when the object does.
189
+ // TODO: This should probably not exist here, but be moved
190
+ // down to relevant sub-types.
191
+
192
+ T* borrow() const noexcept
193
+ {
194
+ return this->p;
195
+ }
196
+
197
+ PyObject* borrow_o() const noexcept
198
+ {
199
+ return reinterpret_cast<PyObject*>(this->p);
200
+ }
201
+
202
+ T* operator->() const noexcept
203
+ {
204
+ return this->p;
205
+ }
206
+
207
+ bool is_None() const noexcept
208
+ {
209
+ return this->p == Py_None;
210
+ }
211
+
212
+ PyObject* acquire_or_None() const noexcept
213
+ {
214
+ PyObject* result = this->p ? reinterpret_cast<PyObject*>(this->p) : Py_None;
215
+ Py_INCREF(result);
216
+ return result;
217
+ }
218
+
219
+ explicit operator bool() const noexcept
220
+ {
221
+ return this->p != nullptr;
222
+ }
223
+
224
+ bool operator!() const noexcept
225
+ {
226
+ return this->p == nullptr;
227
+ }
228
+
229
+ Py_ssize_t REFCNT() const noexcept
230
+ {
231
+ return p ? Py_REFCNT(p) : -42;
232
+ }
233
+
234
+ PyTypeObject* TYPE() const noexcept
235
+ {
236
+ return p ? Py_TYPE(p) : nullptr;
237
+ }
238
+
239
+ inline OwnedObject PyStr() const noexcept;
240
+ inline const std::string as_str() const noexcept;
241
+ inline OwnedObject PyGetAttr(const ImmortalObject& name) const noexcept;
242
+ inline OwnedObject PyRequireAttr(const char* const name) const;
243
+ inline OwnedObject PyRequireAttr(const ImmortalString& name) const;
244
+ inline OwnedObject PyCall(const BorrowedObject& arg) const;
245
+ inline OwnedObject PyCall(PyGreenlet* arg) const ;
246
+ inline OwnedObject PyCall(PyObject* arg) const ;
247
+ // PyObject_Call(this, args, kwargs);
248
+ inline OwnedObject PyCall(const BorrowedObject args,
249
+ const BorrowedObject kwargs) const;
250
+ inline OwnedObject PyCall(const OwnedObject& args,
251
+ const OwnedObject& kwargs) const;
252
+
253
+ protected:
254
+ void _set_raw_pointer(void* t)
255
+ {
256
+ TC(t);
257
+ p = reinterpret_cast<T*>(t);
258
+ }
259
+ void* _get_raw_pointer() const
260
+ {
261
+ return p;
262
+ }
263
+ };
264
+
265
+ #ifdef GREENLET_USE_STDIO
266
+ template<typename T, TypeChecker TC>
267
+ std::ostream& operator<<(std::ostream& os, const PyObjectPointer<T, TC>& s)
268
+ {
269
+ const std::type_info& t = typeid(s);
270
+ os << t.name()
271
+ << "(addr=" << s.borrow()
272
+ << ", refcnt=" << s.REFCNT()
273
+ << ", value=" << s.as_str()
274
+ << ")";
275
+
276
+ return os;
277
+ }
278
+ #endif
279
+
280
+ template<typename T, TypeChecker TC>
281
+ inline bool operator==(const PyObjectPointer<T, TC>& lhs, const PyObject* const rhs) noexcept
282
+ {
283
+ return static_cast<const void*>(lhs.borrow_o()) == static_cast<const void*>(rhs);
284
+ }
285
+
286
+ template<typename T, TypeChecker TC, typename X, TypeChecker XC>
287
+ inline bool operator==(const PyObjectPointer<T, TC>& lhs, const PyObjectPointer<X, XC>& rhs) noexcept
288
+ {
289
+ return lhs.borrow_o() == rhs.borrow_o();
290
+ }
291
+
292
+ template<typename T, TypeChecker TC, typename X, TypeChecker XC>
293
+ inline bool operator!=(const PyObjectPointer<T, TC>& lhs,
294
+ const PyObjectPointer<X, XC>& rhs) noexcept
295
+ {
296
+ return lhs.borrow_o() != rhs.borrow_o();
297
+ }
298
+
299
+ template<typename T=PyObject, TypeChecker TC=NoOpChecker>
300
+ class OwnedReference : public PyObjectPointer<T, TC>
301
+ {
302
+ private:
303
+ friend class OwnedList;
304
+
305
+ protected:
306
+ explicit OwnedReference(T* it) : PyObjectPointer<T, TC>(it)
307
+ {
308
+ }
309
+
310
+ public:
311
+
312
+ // Constructors
313
+
314
+ static OwnedReference<T, TC> consuming(PyObject* p)
315
+ {
316
+ return OwnedReference<T, TC>(reinterpret_cast<T*>(p));
317
+ }
318
+
319
+ static OwnedReference<T, TC> owning(T* p)
320
+ {
321
+ OwnedReference<T, TC> result(p);
322
+ Py_XINCREF(result.p);
323
+ return result;
324
+ }
325
+
326
+ OwnedReference() : PyObjectPointer<T, TC>(nullptr)
327
+ {}
328
+
329
+ explicit OwnedReference(const PyObjectPointer<>& other)
330
+ : PyObjectPointer<T, TC>(nullptr)
331
+ {
332
+ T* op = other.borrow();
333
+ TC(op);
334
+ this->p = other.borrow();
335
+ Py_XINCREF(this->p);
336
+ }
337
+
338
+ // It would be good to make use of the C++11 distinction
339
+ // between move and copy operations, e.g., constructing from a
340
+ // pointer should be a move operation.
341
+ // In the common case of ``OwnedObject x = Py_SomeFunction()``,
342
+ // the call to the copy constructor will be elided completely.
343
+ OwnedReference(const OwnedReference<T, TC>& other)
344
+ : PyObjectPointer<T, TC>(other.p)
345
+ {
346
+ Py_XINCREF(this->p);
347
+ }
348
+
349
+ static OwnedReference<PyObject> None()
350
+ {
351
+ Py_INCREF(Py_None);
352
+ return OwnedReference<PyObject>(Py_None);
353
+ }
354
+
355
+ // We can assign from exactly our type without any extra checking
356
+ OwnedReference<T, TC>& operator=(const OwnedReference<T, TC>& other)
357
+ {
358
+ Py_XINCREF(other.p);
359
+ const T* tmp = this->p;
360
+ this->p = other.p;
361
+ Py_XDECREF(tmp);
362
+ return *this;
363
+ }
364
+
365
+ OwnedReference<T, TC>& operator=(const BorrowedReference<T, TC> other)
366
+ {
367
+ return this->operator=(other.borrow());
368
+ }
369
+
370
+ OwnedReference<T, TC>& operator=(T* const other)
371
+ {
372
+ TC(other);
373
+ Py_XINCREF(other);
374
+ T* tmp = this->p;
375
+ this->p = other;
376
+ Py_XDECREF(tmp);
377
+ return *this;
378
+ }
379
+
380
+ // We can assign from an arbitrary reference type
381
+ // if it passes our check.
382
+ template<typename X, TypeChecker XC>
383
+ OwnedReference<T, TC>& operator=(const OwnedReference<X, XC>& other)
384
+ {
385
+ X* op = other.borrow();
386
+ TC(op);
387
+ return this->operator=(reinterpret_cast<T*>(op));
388
+ }
389
+
390
+ inline void steal(T* other)
391
+ {
392
+ assert(this->p == nullptr);
393
+ TC(other);
394
+ this->p = other;
395
+ }
396
+
397
+ T* relinquish_ownership()
398
+ {
399
+ T* result = this->p;
400
+ this->p = nullptr;
401
+ return result;
402
+ }
403
+
404
+ T* acquire() const
405
+ {
406
+ // Return a new reference.
407
+ // TODO: This may go away when we have reference objects
408
+ // throughout the code.
409
+ Py_XINCREF(this->p);
410
+ return this->p;
411
+ }
412
+
413
+ // Nothing else declares a destructor, we're the leaf, so we
414
+ // should be able to get away without virtual.
415
+ ~OwnedReference()
416
+ {
417
+ Py_CLEAR(this->p);
418
+ }
419
+
420
+ void CLEAR()
421
+ {
422
+ Py_CLEAR(this->p);
423
+ assert(this->p == nullptr);
424
+ }
425
+ };
426
+
427
+ static inline
428
+ void operator<<=(PyObject*& target, OwnedObject& o)
429
+ {
430
+ target = o.relinquish_ownership();
431
+ }
432
+
433
+
434
+ class NewReference : public OwnedObject
435
+ {
436
+ private:
437
+ G_NO_COPIES_OF_CLS(NewReference);
438
+ public:
439
+ // Consumes the reference. Only use this
440
+ // for API return values.
441
+ NewReference(PyObject* it) : OwnedObject(it)
442
+ {
443
+ }
444
+ };
445
+
446
+ class NewDictReference : public NewReference
447
+ {
448
+ private:
449
+ G_NO_COPIES_OF_CLS(NewDictReference);
450
+ public:
451
+ NewDictReference() : NewReference(PyDict_New())
452
+ {
453
+ if (!this->p) {
454
+ throw PyErrOccurred();
455
+ }
456
+ }
457
+
458
+ void SetItem(const char* const key, PyObject* value)
459
+ {
460
+ Require(PyDict_SetItemString(this->p, key, value));
461
+ }
462
+
463
+ void SetItem(const PyObjectPointer<>& key, PyObject* value)
464
+ {
465
+ Require(PyDict_SetItem(this->p, key.borrow_o(), value));
466
+ }
467
+ };
468
+
469
+ template<typename T=PyGreenlet, TypeChecker TC=GreenletChecker>
470
+ class _OwnedGreenlet: public OwnedReference<T, TC>
471
+ {
472
+ private:
473
+ protected:
474
+ _OwnedGreenlet(T* it) : OwnedReference<T, TC>(it)
475
+ {}
476
+
477
+ public:
478
+ _OwnedGreenlet() : OwnedReference<T, TC>()
479
+ {}
480
+
481
+ _OwnedGreenlet(const _OwnedGreenlet<T, TC>& other) : OwnedReference<T, TC>(other)
482
+ {
483
+ }
484
+ _OwnedGreenlet(OwnedMainGreenlet& other) :
485
+ OwnedReference<T, TC>(reinterpret_cast<T*>(other.acquire()))
486
+ {
487
+ }
488
+ _OwnedGreenlet(const BorrowedGreenlet& other);
489
+ // Steals a reference.
490
+ static _OwnedGreenlet<T, TC> consuming(PyGreenlet* it)
491
+ {
492
+ return _OwnedGreenlet<T, TC>(reinterpret_cast<T*>(it));
493
+ }
494
+
495
+ inline _OwnedGreenlet<T, TC>& operator=(const OwnedGreenlet& other)
496
+ {
497
+ return this->operator=(other.borrow());
498
+ }
499
+
500
+ inline _OwnedGreenlet<T, TC>& operator=(const BorrowedGreenlet& other);
501
+
502
+ _OwnedGreenlet<T, TC>& operator=(const OwnedMainGreenlet& other)
503
+ {
504
+ PyGreenlet* owned = other.acquire();
505
+ Py_XDECREF(this->p);
506
+ this->p = reinterpret_cast<T*>(owned);
507
+ return *this;
508
+ }
509
+
510
+ _OwnedGreenlet<T, TC>& operator=(T* const other)
511
+ {
512
+ OwnedReference<T, TC>::operator=(other);
513
+ return *this;
514
+ }
515
+
516
+ T* relinquish_ownership()
517
+ {
518
+ T* result = this->p;
519
+ this->p = nullptr;
520
+ return result;
521
+ }
522
+
523
+ PyObject* relinquish_ownership_o()
524
+ {
525
+ return reinterpret_cast<PyObject*>(relinquish_ownership());
526
+ }
527
+
528
+ inline Greenlet* operator->() const noexcept;
529
+ inline operator Greenlet*() const noexcept;
530
+ };
531
+
532
+ template <typename T=PyObject, TypeChecker TC=NoOpChecker>
533
+ class BorrowedReference : public PyObjectPointer<T, TC>
534
+ {
535
+ public:
536
+ // Allow implicit creation from PyObject* pointers as we
537
+ // transition to using these classes. Also allow automatic
538
+ // conversion to PyObject* for passing to C API calls and even
539
+ // for Py_INCREF/DECREF, because we ourselves do no memory management.
540
+ BorrowedReference(T* it) : PyObjectPointer<T, TC>(it)
541
+ {}
542
+
543
+ BorrowedReference(const PyObjectPointer<T>& ref) : PyObjectPointer<T, TC>(ref.borrow())
544
+ {}
545
+
546
+ BorrowedReference() : PyObjectPointer<T, TC>(nullptr)
547
+ {}
548
+
549
+ operator T*() const
550
+ {
551
+ return this->p;
552
+ }
553
+ };
554
+
555
+ typedef BorrowedReference<PyObject> BorrowedObject;
556
+ //typedef BorrowedReference<PyGreenlet> BorrowedGreenlet;
557
+
558
+ template<typename T=PyGreenlet, TypeChecker TC=GreenletChecker>
559
+ class _BorrowedGreenlet : public BorrowedReference<T, TC>
560
+ {
561
+ public:
562
+ _BorrowedGreenlet() :
563
+ BorrowedReference<T, TC>(nullptr)
564
+ {}
565
+
566
+ _BorrowedGreenlet(T* it) :
567
+ BorrowedReference<T, TC>(it)
568
+ {}
569
+
570
+ _BorrowedGreenlet(const BorrowedObject& it);
571
+
572
+ _BorrowedGreenlet(const OwnedGreenlet& it) :
573
+ BorrowedReference<T, TC>(it.borrow())
574
+ {}
575
+
576
+ _BorrowedGreenlet<T, TC>& operator=(const BorrowedObject& other);
577
+
578
+ // We get one of these for PyGreenlet, but one for PyObject
579
+ // is handy as well
580
+ operator PyObject*() const
581
+ {
582
+ return reinterpret_cast<PyObject*>(this->p);
583
+ }
584
+ Greenlet* operator->() const noexcept;
585
+ operator Greenlet*() const noexcept;
586
+ };
587
+
588
+ typedef _BorrowedGreenlet<PyGreenlet> BorrowedGreenlet;
589
+
590
+ template<typename T, TypeChecker TC>
591
+ _OwnedGreenlet<T, TC>::_OwnedGreenlet(const BorrowedGreenlet& other)
592
+ : OwnedReference<T, TC>(reinterpret_cast<T*>(other.borrow()))
593
+ {
594
+ Py_XINCREF(this->p);
595
+ }
596
+
597
+
598
+ class BorrowedMainGreenlet
599
+ : public _BorrowedGreenlet<PyGreenlet, MainGreenletExactChecker>
600
+ {
601
+ public:
602
+ BorrowedMainGreenlet(const OwnedMainGreenlet& it) :
603
+ _BorrowedGreenlet<PyGreenlet, MainGreenletExactChecker>(it.borrow())
604
+ {}
605
+ BorrowedMainGreenlet(PyGreenlet* it=nullptr)
606
+ : _BorrowedGreenlet<PyGreenlet, MainGreenletExactChecker>(it)
607
+ {}
608
+ };
609
+
610
+ template<typename T, TypeChecker TC>
611
+ _OwnedGreenlet<T, TC>& _OwnedGreenlet<T, TC>::operator=(const BorrowedGreenlet& other)
612
+ {
613
+ return this->operator=(other.borrow());
614
+ }
615
+
616
+
617
+ class ImmortalObject : public PyObjectPointer<>
618
+ {
619
+ private:
620
+ G_NO_ASSIGNMENT_OF_CLS(ImmortalObject);
621
+ public:
622
+ explicit ImmortalObject(PyObject* it) : PyObjectPointer<>(it)
623
+ {
624
+ }
625
+
626
+ ImmortalObject(const ImmortalObject& other)
627
+ : PyObjectPointer<>(other.p)
628
+ {
629
+
630
+ }
631
+
632
+ /**
633
+ * Become the new owner of the object. Does not change the
634
+ * reference count.
635
+ */
636
+ ImmortalObject& operator=(PyObject* it)
637
+ {
638
+ assert(this->p == nullptr);
639
+ this->p = it;
640
+ return *this;
641
+ }
642
+
643
+ static ImmortalObject consuming(PyObject* it)
644
+ {
645
+ return ImmortalObject(it);
646
+ }
647
+
648
+ inline operator PyObject*() const
649
+ {
650
+ return this->p;
651
+ }
652
+ };
653
+
654
+ class ImmortalString : public ImmortalObject
655
+ {
656
+ private:
657
+ G_NO_COPIES_OF_CLS(ImmortalString);
658
+ const char* str;
659
+ public:
660
+ ImmortalString(const char* const str) :
661
+ ImmortalObject(str ? Require(PyUnicode_InternFromString(str)) : nullptr)
662
+ {
663
+ this->str = str;
664
+ }
665
+
666
+ inline ImmortalString& operator=(const char* const str)
667
+ {
668
+ if (!this->p) {
669
+ this->p = Require(PyUnicode_InternFromString(str));
670
+ this->str = str;
671
+ }
672
+ else {
673
+ assert(this->str == str);
674
+ }
675
+ return *this;
676
+ }
677
+
678
+ inline operator std::string() const
679
+ {
680
+ return this->str;
681
+ }
682
+
683
+ };
684
+
685
+ class ImmortalEventName : public ImmortalString
686
+ {
687
+ private:
688
+ G_NO_COPIES_OF_CLS(ImmortalEventName);
689
+ public:
690
+ ImmortalEventName(const char* const str) : ImmortalString(str)
691
+ {}
692
+ };
693
+
694
+ class ImmortalException : public ImmortalObject
695
+ {
696
+ private:
697
+ G_NO_COPIES_OF_CLS(ImmortalException);
698
+ public:
699
+ ImmortalException(const char* const name, PyObject* base=nullptr) :
700
+ ImmortalObject(name
701
+ // Python 2.7 isn't const correct
702
+ ? Require(PyErr_NewException((char*)name, base, nullptr))
703
+ : nullptr)
704
+ {}
705
+
706
+ inline bool PyExceptionMatches() const
707
+ {
708
+ return PyErr_ExceptionMatches(this->p) > 0;
709
+ }
710
+
711
+ };
712
+
713
+ template<typename T, TypeChecker TC>
714
+ inline OwnedObject PyObjectPointer<T, TC>::PyStr() const noexcept
715
+ {
716
+ if (!this->p) {
717
+ return OwnedObject();
718
+ }
719
+ return OwnedObject::consuming(PyObject_Str(reinterpret_cast<PyObject*>(this->p)));
720
+ }
721
+
722
+ template<typename T, TypeChecker TC>
723
+ inline const std::string PyObjectPointer<T, TC>::as_str() const noexcept
724
+ {
725
+ // NOTE: This is not Python exception safe.
726
+ if (this->p) {
727
+ // The Python APIs return a cached char* value that's only valid
728
+ // as long as the original object stays around, and we're
729
+ // about to (probably) toss it. Hence the copy to std::string.
730
+ OwnedObject py_str = this->PyStr();
731
+ if (!py_str) {
732
+ return "(nil)";
733
+ }
734
+ return PyUnicode_AsUTF8(py_str.borrow());
735
+ }
736
+ return "(nil)";
737
+ }
738
+
739
+ template<typename T, TypeChecker TC>
740
+ inline OwnedObject PyObjectPointer<T, TC>::PyGetAttr(const ImmortalObject& name) const noexcept
741
+ {
742
+ assert(this->p);
743
+ return OwnedObject::consuming(PyObject_GetAttr(reinterpret_cast<PyObject*>(this->p), name));
744
+ }
745
+
746
+ template<typename T, TypeChecker TC>
747
+ inline OwnedObject PyObjectPointer<T, TC>::PyRequireAttr(const char* const name) const
748
+ {
749
+ assert(this->p);
750
+ return OwnedObject::consuming(Require(PyObject_GetAttrString(this->p, name), name));
751
+ }
752
+
753
+ template<typename T, TypeChecker TC>
754
+ inline OwnedObject PyObjectPointer<T, TC>::PyRequireAttr(const ImmortalString& name) const
755
+ {
756
+ assert(this->p);
757
+ return OwnedObject::consuming(Require(
758
+ PyObject_GetAttr(
759
+ reinterpret_cast<PyObject*>(this->p),
760
+ name
761
+ ),
762
+ name
763
+ ));
764
+ }
765
+
766
+ template<typename T, TypeChecker TC>
767
+ inline OwnedObject PyObjectPointer<T, TC>::PyCall(const BorrowedObject& arg) const
768
+ {
769
+ return this->PyCall(arg.borrow());
770
+ }
771
+
772
+ template<typename T, TypeChecker TC>
773
+ inline OwnedObject PyObjectPointer<T, TC>::PyCall(PyGreenlet* arg) const
774
+ {
775
+ return this->PyCall(reinterpret_cast<PyObject*>(arg));
776
+ }
777
+
778
+ template<typename T, TypeChecker TC>
779
+ inline OwnedObject PyObjectPointer<T, TC>::PyCall(PyObject* arg) const
780
+ {
781
+ assert(this->p);
782
+ return OwnedObject::consuming(PyObject_CallFunctionObjArgs(this->p, arg, NULL));
783
+ }
784
+
785
+ template<typename T, TypeChecker TC>
786
+ inline OwnedObject PyObjectPointer<T, TC>::PyCall(const BorrowedObject args,
787
+ const BorrowedObject kwargs) const
788
+ {
789
+ assert(this->p);
790
+ return OwnedObject::consuming(PyObject_Call(this->p, args, kwargs));
791
+ }
792
+
793
+ template<typename T, TypeChecker TC>
794
+ inline OwnedObject PyObjectPointer<T, TC>::PyCall(const OwnedObject& args,
795
+ const OwnedObject& kwargs) const
796
+ {
797
+ assert(this->p);
798
+ return OwnedObject::consuming(PyObject_Call(this->p, args.borrow(), kwargs.borrow()));
799
+ }
800
+
801
+ inline void
802
+ ListChecker(void * p)
803
+ {
804
+ if (!p) {
805
+ return;
806
+ }
807
+ if (!PyList_Check(p)) {
808
+ throw TypeError("Expected a list");
809
+ }
810
+ }
811
+
812
+ class OwnedList : public OwnedReference<PyObject, ListChecker>
813
+ {
814
+ private:
815
+ G_NO_ASSIGNMENT_OF_CLS(OwnedList);
816
+ public:
817
+ // TODO: Would like to use move.
818
+ explicit OwnedList(const OwnedObject& other)
819
+ : OwnedReference<PyObject, ListChecker>(other)
820
+ {
821
+ }
822
+
823
+ OwnedList& operator=(const OwnedObject& other)
824
+ {
825
+ if (other && PyList_Check(other.p)) {
826
+ // Valid list. Own a new reference to it, discard the
827
+ // reference to what we did own.
828
+ PyObject* new_ptr = other.p;
829
+ Py_INCREF(new_ptr);
830
+ Py_XDECREF(this->p);
831
+ this->p = new_ptr;
832
+ }
833
+ else {
834
+ // Either the other object was NULL (an error) or it
835
+ // wasn't a list. Either way, we're now invalidated.
836
+ Py_XDECREF(this->p);
837
+ this->p = nullptr;
838
+ }
839
+ return *this;
840
+ }
841
+
842
+ inline bool empty() const
843
+ {
844
+ return PyList_GET_SIZE(p) == 0;
845
+ }
846
+
847
+ inline Py_ssize_t size() const
848
+ {
849
+ return PyList_GET_SIZE(p);
850
+ }
851
+
852
+ inline BorrowedObject at(const Py_ssize_t index) const
853
+ {
854
+ return PyList_GET_ITEM(p, index);
855
+ }
856
+
857
+ inline void clear()
858
+ {
859
+ PyList_SetSlice(p, 0, PyList_GET_SIZE(p), NULL);
860
+ }
861
+ };
862
+
863
+ // Use this to represent the module object used at module init
864
+ // time.
865
+ // This could either be a borrowed (Py2) or new (Py3) reference;
866
+ // either way, we don't want to do any memory management
867
+ // on it here, Python itself will handle that.
868
+ // XXX: Actually, that's not quite right. On Python 3, if an
869
+ // exception occurs before we return to the interpreter, this will
870
+ // leak; but all previous versions also had that problem.
871
+ class CreatedModule : public PyObjectPointer<>
872
+ {
873
+ private:
874
+ G_NO_COPIES_OF_CLS(CreatedModule);
875
+ public:
876
+ CreatedModule(PyModuleDef& mod_def) : PyObjectPointer<>(
877
+ Require(PyModule_Create(&mod_def)))
878
+ {
879
+ }
880
+
881
+ // PyAddObject(): Add a reference to the object to the module.
882
+ // On return, the reference count of the object is unchanged.
883
+ //
884
+ // The docs warn that PyModule_AddObject only steals the
885
+ // reference on success, so if it fails after we've incref'd
886
+ // or allocated, we're responsible for the decref.
887
+ void PyAddObject(const char* name, const long new_bool)
888
+ {
889
+ OwnedObject p = OwnedObject::consuming(Require(PyBool_FromLong(new_bool)));
890
+ this->PyAddObject(name, p);
891
+ }
892
+
893
+ void PyAddObject(const char* name, const OwnedObject& new_object)
894
+ {
895
+ // The caller already owns a reference they will decref
896
+ // when their variable goes out of scope, we still need to
897
+ // incref/decref.
898
+ this->PyAddObject(name, new_object.borrow());
899
+ }
900
+
901
+ void PyAddObject(const char* name, const ImmortalObject& new_object)
902
+ {
903
+ this->PyAddObject(name, new_object.borrow());
904
+ }
905
+
906
+ void PyAddObject(const char* name, PyTypeObject& type)
907
+ {
908
+ this->PyAddObject(name, reinterpret_cast<PyObject*>(&type));
909
+ }
910
+
911
+ void PyAddObject(const char* name, PyObject* new_object)
912
+ {
913
+ Py_INCREF(new_object);
914
+ try {
915
+ Require(PyModule_AddObject(this->p, name, new_object));
916
+ }
917
+ catch (const PyErrOccurred&) {
918
+ Py_DECREF(p);
919
+ throw;
920
+ }
921
+ }
922
+ };
923
+
924
+ class PyErrFetchParam : public PyObjectPointer<>
925
+ {
926
+ // Not an owned object, because we can't be initialized with
927
+ // one, and we only sometimes acquire ownership.
928
+ private:
929
+ G_NO_COPIES_OF_CLS(PyErrFetchParam);
930
+ public:
931
+ // To allow declaring these and passing them to
932
+ // PyErr_Fetch we implement the empty constructor,
933
+ // and the address operator.
934
+ PyErrFetchParam() : PyObjectPointer<>(nullptr)
935
+ {
936
+ }
937
+
938
+ PyObject** operator&()
939
+ {
940
+ return &this->p;
941
+ }
942
+
943
+ // This allows us to pass one directly without the &,
944
+ // BUT it has higher precedence than the bool operator
945
+ // if it's not explicit.
946
+ operator PyObject**()
947
+ {
948
+ return &this->p;
949
+ }
950
+
951
+ // We don't want to be able to pass these to Py_DECREF and
952
+ // such so we don't have the implicit PyObject* conversion.
953
+
954
+ inline PyObject* relinquish_ownership()
955
+ {
956
+ PyObject* result = this->p;
957
+ this->p = nullptr;
958
+ return result;
959
+ }
960
+
961
+ ~PyErrFetchParam()
962
+ {
963
+ Py_XDECREF(p);
964
+ }
965
+ };
966
+
967
+ class OwnedErrPiece : public OwnedObject
968
+ {
969
+ private:
970
+
971
+ public:
972
+ // Unlike OwnedObject, this increments the refcount.
973
+ OwnedErrPiece(PyObject* p=nullptr) : OwnedObject(p)
974
+ {
975
+ this->acquire();
976
+ }
977
+
978
+ PyObject** operator&()
979
+ {
980
+ return &this->p;
981
+ }
982
+
983
+ inline operator PyObject*() const
984
+ {
985
+ return this->p;
986
+ }
987
+
988
+ operator PyTypeObject*() const
989
+ {
990
+ return reinterpret_cast<PyTypeObject*>(this->p);
991
+ }
992
+ };
993
+
994
+ class PyErrPieces
995
+ {
996
+ private:
997
+ OwnedErrPiece type;
998
+ OwnedErrPiece instance;
999
+ OwnedErrPiece traceback;
1000
+ bool restored;
1001
+ public:
1002
+ // Takes new references; if we're destroyed before
1003
+ // restoring the error, we drop the references.
1004
+ PyErrPieces(PyObject* t, PyObject* v, PyObject* tb) :
1005
+ type(t),
1006
+ instance(v),
1007
+ traceback(tb),
1008
+ restored(0)
1009
+ {
1010
+ this->normalize();
1011
+ }
1012
+
1013
+ PyErrPieces() :
1014
+ restored(0)
1015
+ {
1016
+ // PyErr_Fetch transfers ownership to us, so
1017
+ // we don't actually need to INCREF; but we *do*
1018
+ // need to DECREF if we're not restored.
1019
+ PyErrFetchParam t, v, tb;
1020
+ PyErr_Fetch(&t, &v, &tb);
1021
+ type.steal(t.relinquish_ownership());
1022
+ instance.steal(v.relinquish_ownership());
1023
+ traceback.steal(tb.relinquish_ownership());
1024
+ }
1025
+
1026
+ void PyErrRestore()
1027
+ {
1028
+ // can only do this once
1029
+ assert(!this->restored);
1030
+ this->restored = true;
1031
+ PyErr_Restore(
1032
+ this->type.relinquish_ownership(),
1033
+ this->instance.relinquish_ownership(),
1034
+ this->traceback.relinquish_ownership());
1035
+ assert(!this->type && !this->instance && !this->traceback);
1036
+ }
1037
+
1038
+ private:
1039
+ void normalize()
1040
+ {
1041
+ // First, check the traceback argument, replacing None,
1042
+ // with NULL
1043
+ if (traceback.is_None()) {
1044
+ traceback = nullptr;
1045
+ }
1046
+
1047
+ if (traceback && !PyTraceBack_Check(traceback.borrow())) {
1048
+ throw PyErrOccurred(PyExc_TypeError,
1049
+ "throw() third argument must be a traceback object");
1050
+ }
1051
+
1052
+ if (PyExceptionClass_Check(type)) {
1053
+ // If we just had a type, we'll now have a type and
1054
+ // instance.
1055
+ // The type's refcount will have gone up by one
1056
+ // because of the instance and the instance will have
1057
+ // a refcount of one. Either way, we owned, and still
1058
+ // do own, exactly one reference.
1059
+ PyErr_NormalizeException(&type, &instance, &traceback);
1060
+
1061
+ }
1062
+ else if (PyExceptionInstance_Check(type)) {
1063
+ /* Raising an instance --- usually that means an
1064
+ object that is a subclass of BaseException, but on
1065
+ Python 2, that can also mean an arbitrary old-style
1066
+ object. The value should be a dummy. */
1067
+ if (instance && !instance.is_None()) {
1068
+ throw PyErrOccurred(
1069
+ PyExc_TypeError,
1070
+ "instance exception may not have a separate value");
1071
+ }
1072
+ /* Normalize to raise <class>, <instance> */
1073
+ this->instance = this->type;
1074
+ this->type = PyExceptionInstance_Class(instance.borrow());
1075
+
1076
+ /*
1077
+ It would be tempting to do this:
1078
+
1079
+ Py_ssize_t type_count = Py_REFCNT(Py_TYPE(instance.borrow()));
1080
+ this->type = PyExceptionInstance_Class(instance.borrow());
1081
+ assert(this->type.REFCNT() == type_count + 1);
1082
+
1083
+ But that doesn't work on Python 2 in the case of
1084
+ old-style instances: The result of Py_TYPE is going to
1085
+ be the global shared <type instance> that all
1086
+ old-style classes have, while the return of Instance_Class()
1087
+ will be the Python-level class object. The two are unrelated.
1088
+ */
1089
+ }
1090
+ else {
1091
+ /* Not something you can raise. throw() fails. */
1092
+ PyErr_Format(PyExc_TypeError,
1093
+ "exceptions must be classes, or instances, not %s",
1094
+ Py_TYPE(type.borrow())->tp_name);
1095
+ throw PyErrOccurred();
1096
+ }
1097
+ }
1098
+ };
1099
+
1100
+ // PyArg_Parse's O argument returns a borrowed reference.
1101
+ class PyArgParseParam : public BorrowedObject
1102
+ {
1103
+ private:
1104
+ G_NO_COPIES_OF_CLS(PyArgParseParam);
1105
+ public:
1106
+ explicit PyArgParseParam(PyObject* p=nullptr) : BorrowedObject(p)
1107
+ {
1108
+ }
1109
+
1110
+ inline PyObject** operator&()
1111
+ {
1112
+ return &this->p;
1113
+ }
1114
+ };
1115
+
1116
+ };};
1117
+
1118
+ #endif
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/greenlet_slp_switch.hpp ADDED
@@ -0,0 +1,99 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #ifndef GREENLET_SLP_SWITCH_HPP
2
+ #define GREENLET_SLP_SWITCH_HPP
3
+
4
+ #include "greenlet_compiler_compat.hpp"
5
+ #include "greenlet_refs.hpp"
6
+
7
+ /*
8
+ * the following macros are spliced into the OS/compiler
9
+ * specific code, in order to simplify maintenance.
10
+ */
11
+ // We can save about 10% of the time it takes to switch greenlets if
12
+ // we thread the thread state through the slp_save_state() and the
13
+ // following slp_restore_state() calls from
14
+ // slp_switch()->g_switchstack() (which already needs to access it).
15
+ //
16
+ // However:
17
+ //
18
+ // that requires changing the prototypes and implementations of the
19
+ // switching functions. If we just change the prototype of
20
+ // slp_switch() to accept the argument and update the macros, without
21
+ // changing the implementation of slp_switch(), we get crashes on
22
+ // 64-bit Linux and 32-bit x86 (for reasons that aren't 100% clear);
23
+ // on the other hand, 64-bit macOS seems to be fine. Also, 64-bit
24
+ // windows is an issue because slp_switch is written fully in assembly
25
+ // and currently ignores its argument so some code would have to be
26
+ // adjusted there to pass the argument on to the
27
+ // ``slp_save_state_asm()`` function (but interestingly, because of
28
+ // the calling convention, the extra argument is just ignored and
29
+ // things function fine, albeit slower, if we just modify
30
+ // ``slp_save_state_asm`()` to fetch the pointer to pass to the
31
+ // macro.)
32
+ //
33
+ // Our compromise is to use a *glabal*, untracked, weak, pointer
34
+ // to the necessary thread state during the process of switching only.
35
+ // This is safe because we're protected by the GIL, and if we're
36
+ // running this code, the thread isn't exiting. This also nets us a
37
+ // 10-12% speed improvement.
38
+
39
+ static greenlet::Greenlet* volatile switching_thread_state = nullptr;
40
+
41
+
42
+ extern "C" {
43
+ static int GREENLET_NOINLINE(slp_save_state_trampoline)(char* stackref);
44
+ static void GREENLET_NOINLINE(slp_restore_state_trampoline)();
45
+ }
46
+
47
+
48
+ #define SLP_SAVE_STATE(stackref, stsizediff) \
49
+ do { \
50
+ assert(switching_thread_state); \
51
+ stackref += STACK_MAGIC; \
52
+ if (slp_save_state_trampoline((char*)stackref)) \
53
+ return -1; \
54
+ if (!switching_thread_state->active()) \
55
+ return 1; \
56
+ stsizediff = switching_thread_state->stack_start() - (char*)stackref; \
57
+ } while (0)
58
+
59
+ #define SLP_RESTORE_STATE() slp_restore_state_trampoline()
60
+
61
+ #define SLP_EVAL
62
+ extern "C" {
63
+ #define slp_switch GREENLET_NOINLINE(slp_switch)
64
+ #include "slp_platformselect.h"
65
+ }
66
+ #undef slp_switch
67
+
68
+ #ifndef STACK_MAGIC
69
+ # error \
70
+ "greenlet needs to be ported to this platform, or taught how to detect your compiler properly."
71
+ #endif /* !STACK_MAGIC */
72
+
73
+
74
+
75
+ #ifdef EXTERNAL_ASM
76
+ /* CCP addition: Make these functions, to be called from assembler.
77
+ * The token include file for the given platform should enable the
78
+ * EXTERNAL_ASM define so that this is included.
79
+ */
80
+ extern "C" {
81
+ intptr_t
82
+ slp_save_state_asm(intptr_t* ref)
83
+ {
84
+ intptr_t diff;
85
+ SLP_SAVE_STATE(ref, diff);
86
+ return diff;
87
+ }
88
+
89
+ void
90
+ slp_restore_state_asm(void)
91
+ {
92
+ SLP_RESTORE_STATE();
93
+ }
94
+
95
+ extern int slp_switch(void);
96
+ };
97
+ #endif
98
+
99
+ #endif
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/greenlet_thread_support.hpp ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #ifndef GREENLET_THREAD_SUPPORT_HPP
2
+ #define GREENLET_THREAD_SUPPORT_HPP
3
+
4
+ /**
5
+ * Defines various utility functions to help greenlet integrate well
6
+ * with threads. This used to be needed when we supported Python
7
+ * 2.7 on Windows, which used a very old compiler. We wrote an
8
+ * alternative implementation using Python APIs and POSIX or Windows
9
+ * APIs, but that's no longer needed. So this file is a shadow of its
10
+ * former self --- but may be needed in the future.
11
+ */
12
+
13
+ #include <stdexcept>
14
+ #include <thread>
15
+ #include <mutex>
16
+
17
+ #include "greenlet_compiler_compat.hpp"
18
+
19
+ namespace greenlet {
20
+ typedef std::mutex Mutex;
21
+ typedef std::lock_guard<Mutex> LockGuard;
22
+ class LockInitError : public std::runtime_error
23
+ {
24
+ public:
25
+ LockInitError(const char* what) : std::runtime_error(what)
26
+ {};
27
+ };
28
+ };
29
+
30
+
31
+ #endif /* GREENLET_THREAD_SUPPORT_HPP */
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/platform/__init__.py ADDED
File without changes
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/platform/setup_switch_x64_masm.cmd ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ call "C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\vcvarsall.bat" amd64
2
+ ml64 /nologo /c /Fo switch_x64_masm.obj switch_x64_masm.asm
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/platform/switch_aarch64_gcc.h ADDED
@@ -0,0 +1,124 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * this is the internal transfer function.
3
+ *
4
+ * HISTORY
5
+ * 07-Sep-16 Add clang support using x register naming. Fredrik Fornwall
6
+ * 13-Apr-13 Add support for strange GCC caller-save decisions
7
+ * 08-Apr-13 File creation. Michael Matz
8
+ *
9
+ * NOTES
10
+ *
11
+ * Simply save all callee saved registers
12
+ *
13
+ */
14
+
15
+ #define STACK_REFPLUS 1
16
+
17
+ #ifdef SLP_EVAL
18
+ #define STACK_MAGIC 0
19
+ #define REGS_TO_SAVE "x19", "x20", "x21", "x22", "x23", "x24", "x25", "x26", \
20
+ "x27", "x28", "x30" /* aka lr */, \
21
+ "v8", "v9", "v10", "v11", \
22
+ "v12", "v13", "v14", "v15"
23
+
24
+ /*
25
+ * Recall:
26
+ asm asm-qualifiers ( AssemblerTemplate
27
+ : OutputOperands
28
+ [ : InputOperands
29
+ [ : Clobbers ] ])
30
+
31
+ or (if asm-qualifiers contains 'goto')
32
+
33
+ asm asm-qualifiers ( AssemblerTemplate
34
+ : OutputOperands
35
+ : InputOperands
36
+ : Clobbers
37
+ : GotoLabels)
38
+
39
+ and OutputOperands are
40
+
41
+ [ [asmSymbolicName] ] constraint (cvariablename)
42
+
43
+ When a name is given, refer to it as ``%[the name]``.
44
+ When not given, ``%i`` where ``i`` is the zero-based index.
45
+
46
+ constraints starting with ``=`` means only writing; ``+`` means
47
+ reading and writing.
48
+
49
+ This is followed by ``r`` (must be register) or ``m`` (must be memory)
50
+ and these can be combined.
51
+
52
+ The ``cvariablename`` is actually an lvalue expression.
53
+
54
+ In AArch65, 31 general purpose registers. If named X0... they are
55
+ 64-bit. If named W0... they are the bottom 32 bits of the
56
+ corresponding 64 bit register.
57
+
58
+ XZR and WZR are hardcoded to 0, and ignore writes.
59
+
60
+ Arguments are in X0..X7. C++ uses X0 for ``this``. X0 holds simple return
61
+ values (?)
62
+
63
+ Whenever a W register is written, the top half of the X register is zeroed.
64
+ */
65
+
66
+ static int
67
+ slp_switch(void)
68
+ {
69
+ int err;
70
+ void *fp;
71
+ /* Windowz uses a 32-bit long on a 64-bit platform, unlike the rest of
72
+ the world, and in theory we can be compiled with GCC/llvm on 64-bit
73
+ windows. So we need a fixed-width type.
74
+ */
75
+ int64_t *stackref, stsizediff;
76
+ __asm__ volatile ("" : : : REGS_TO_SAVE);
77
+ __asm__ volatile ("str x29, %0" : "=m"(fp) : : );
78
+ __asm__ ("mov %0, sp" : "=r" (stackref));
79
+ {
80
+ SLP_SAVE_STATE(stackref, stsizediff);
81
+ __asm__ volatile (
82
+ "add sp,sp,%0\n"
83
+ "add x29,x29,%0\n"
84
+ :
85
+ : "r" (stsizediff)
86
+ );
87
+ SLP_RESTORE_STATE();
88
+ /* SLP_SAVE_STATE macro contains some return statements
89
+ (of -1 and 1). It falls through only when
90
+ the return value of slp_save_state() is zero, which
91
+ is placed in x0.
92
+ In that case we (slp_switch) also want to return zero
93
+ (also in x0 of course).
94
+ Now, some GCC versions (seen with 4.8) think it's a
95
+ good idea to save/restore x0 around the call to
96
+ slp_restore_state(), instead of simply zeroing it
97
+ at the return below. But slp_restore_state
98
+ writes random values to the stack slot used for this
99
+ save/restore (from when it once was saved above in
100
+ SLP_SAVE_STATE, when it was still uninitialized), so
101
+ "restoring" that precious zero actually makes us
102
+ return random values. There are some ways to make
103
+ GCC not use that zero value in the normal return path
104
+ (e.g. making err volatile, but that costs a little
105
+ stack space), and the simplest is to call a function
106
+ that returns an unknown value (which happens to be zero),
107
+ so the saved/restored value is unused.
108
+
109
+ Thus, this line stores a 0 into the ``err`` variable
110
+ (which must be held in a register for this instruction,
111
+ of course). The ``w`` qualifier causes the instruction
112
+ to use W0 instead of X0, otherwise we get a warning
113
+ about a value size mismatch (because err is an int,
114
+ and aarch64 platforms are LP64: 32-bit int, 64 bit long
115
+ and pointer).
116
+ */
117
+ __asm__ volatile ("mov %w0, #0" : "=r" (err));
118
+ }
119
+ __asm__ volatile ("ldr x29, %0" : : "m" (fp) :);
120
+ __asm__ volatile ("" : : : REGS_TO_SAVE);
121
+ return err;
122
+ }
123
+
124
+ #endif
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/platform/switch_alpha_unix.h ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #define STACK_REFPLUS 1
2
+
3
+ #ifdef SLP_EVAL
4
+ #define STACK_MAGIC 0
5
+
6
+ #define REGS_TO_SAVE "$9", "$10", "$11", "$12", "$13", "$14", "$15", \
7
+ "$f2", "$f3", "$f4", "$f5", "$f6", "$f7", "$f8", "$f9"
8
+
9
+ static int
10
+ slp_switch(void)
11
+ {
12
+ int ret;
13
+ long *stackref, stsizediff;
14
+ __asm__ volatile ("" : : : REGS_TO_SAVE);
15
+ __asm__ volatile ("mov $30, %0" : "=r" (stackref) : );
16
+ {
17
+ SLP_SAVE_STATE(stackref, stsizediff);
18
+ __asm__ volatile (
19
+ "addq $30, %0, $30\n\t"
20
+ : /* no outputs */
21
+ : "r" (stsizediff)
22
+ );
23
+ SLP_RESTORE_STATE();
24
+ }
25
+ __asm__ volatile ("" : : : REGS_TO_SAVE);
26
+ __asm__ volatile ("mov $31, %0" : "=r" (ret) : );
27
+ return ret;
28
+ }
29
+
30
+ #endif
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/platform/switch_amd64_unix.h ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * this is the internal transfer function.
3
+ *
4
+ * HISTORY
5
+ * 3-May-13 Ralf Schmitt <ralf@systemexit.de>
6
+ * Add support for strange GCC caller-save decisions
7
+ * (ported from switch_aarch64_gcc.h)
8
+ * 18-Aug-11 Alexey Borzenkov <snaury@gmail.com>
9
+ * Correctly save rbp, csr and cw
10
+ * 01-Apr-04 Hye-Shik Chang <perky@FreeBSD.org>
11
+ * Ported from i386 to amd64.
12
+ * 24-Nov-02 Christian Tismer <tismer@tismer.com>
13
+ * needed to add another magic constant to insure
14
+ * that f in slp_eval_frame(PyFrameObject *f)
15
+ * STACK_REFPLUS will probably be 1 in most cases.
16
+ * gets included into the saved stack area.
17
+ * 17-Sep-02 Christian Tismer <tismer@tismer.com>
18
+ * after virtualizing stack save/restore, the
19
+ * stack size shrunk a bit. Needed to introduce
20
+ * an adjustment STACK_MAGIC per platform.
21
+ * 15-Sep-02 Gerd Woetzel <gerd.woetzel@GMD.DE>
22
+ * slightly changed framework for spark
23
+ * 31-Avr-02 Armin Rigo <arigo@ulb.ac.be>
24
+ * Added ebx, esi and edi register-saves.
25
+ * 01-Mar-02 Samual M. Rushing <rushing@ironport.com>
26
+ * Ported from i386.
27
+ */
28
+
29
+ #define STACK_REFPLUS 1
30
+
31
+ #ifdef SLP_EVAL
32
+
33
+ /* #define STACK_MAGIC 3 */
34
+ /* the above works fine with gcc 2.96, but 2.95.3 wants this */
35
+ #define STACK_MAGIC 0
36
+
37
+ #define REGS_TO_SAVE "r12", "r13", "r14", "r15"
38
+
39
+ static int
40
+ slp_switch(void)
41
+ {
42
+ int err;
43
+ void* rbp;
44
+ void* rbx;
45
+ unsigned int csr;
46
+ unsigned short cw;
47
+ /* This used to be declared 'register', but that does nothing in
48
+ modern compilers and is explicitly forbidden in some new
49
+ standards. */
50
+ long *stackref, stsizediff;
51
+ __asm__ volatile ("" : : : REGS_TO_SAVE);
52
+ __asm__ volatile ("fstcw %0" : "=m" (cw));
53
+ __asm__ volatile ("stmxcsr %0" : "=m" (csr));
54
+ __asm__ volatile ("movq %%rbp, %0" : "=m" (rbp));
55
+ __asm__ volatile ("movq %%rbx, %0" : "=m" (rbx));
56
+ __asm__ ("movq %%rsp, %0" : "=g" (stackref));
57
+ {
58
+ SLP_SAVE_STATE(stackref, stsizediff);
59
+ __asm__ volatile (
60
+ "addq %0, %%rsp\n"
61
+ "addq %0, %%rbp\n"
62
+ :
63
+ : "r" (stsizediff)
64
+ );
65
+ SLP_RESTORE_STATE();
66
+ __asm__ volatile ("xorq %%rax, %%rax" : "=a" (err));
67
+ }
68
+ __asm__ volatile ("movq %0, %%rbx" : : "m" (rbx));
69
+ __asm__ volatile ("movq %0, %%rbp" : : "m" (rbp));
70
+ __asm__ volatile ("ldmxcsr %0" : : "m" (csr));
71
+ __asm__ volatile ("fldcw %0" : : "m" (cw));
72
+ __asm__ volatile ("" : : : REGS_TO_SAVE);
73
+ return err;
74
+ }
75
+
76
+ #endif
77
+
78
+ /*
79
+ * further self-processing support
80
+ */
81
+
82
+ /*
83
+ * if you want to add self-inspection tools, place them
84
+ * here. See the x86_msvc for the necessary defines.
85
+ * These features are highly experimental und not
86
+ * essential yet.
87
+ */
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/platform/switch_arm32_gcc.h ADDED
@@ -0,0 +1,79 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * this is the internal transfer function.
3
+ *
4
+ * HISTORY
5
+ * 14-Aug-06 File creation. Ported from Arm Thumb. Sylvain Baro
6
+ * 3-Sep-06 Commented out saving of r1-r3 (r4 already commented out) as I
7
+ * read that these do not need to be saved. Also added notes and
8
+ * errors related to the frame pointer. Richard Tew.
9
+ *
10
+ * NOTES
11
+ *
12
+ * It is not possible to detect if fp is used or not, so the supplied
13
+ * switch function needs to support it, so that you can remove it if
14
+ * it does not apply to you.
15
+ *
16
+ * POSSIBLE ERRORS
17
+ *
18
+ * "fp cannot be used in asm here"
19
+ *
20
+ * - Try commenting out "fp" in REGS_TO_SAVE.
21
+ *
22
+ */
23
+
24
+ #define STACK_REFPLUS 1
25
+
26
+ #ifdef SLP_EVAL
27
+ #define STACK_MAGIC 0
28
+ #define REG_SP "sp"
29
+ #define REG_SPSP "sp,sp"
30
+ #ifdef __thumb__
31
+ #define REG_FP "r7"
32
+ #define REG_FPFP "r7,r7"
33
+ #define REGS_TO_SAVE_GENERAL "r4", "r5", "r6", "r8", "r9", "r10", "r11", "lr"
34
+ #else
35
+ #define REG_FP "fp"
36
+ #define REG_FPFP "fp,fp"
37
+ #define REGS_TO_SAVE_GENERAL "r4", "r5", "r6", "r7", "r8", "r9", "r10", "lr"
38
+ #endif
39
+ #if defined(__SOFTFP__)
40
+ #define REGS_TO_SAVE REGS_TO_SAVE_GENERAL
41
+ #elif defined(__VFP_FP__)
42
+ #define REGS_TO_SAVE REGS_TO_SAVE_GENERAL, "d8", "d9", "d10", "d11", \
43
+ "d12", "d13", "d14", "d15"
44
+ #elif defined(__MAVERICK__)
45
+ #define REGS_TO_SAVE REGS_TO_SAVE_GENERAL, "mvf4", "mvf5", "mvf6", "mvf7", \
46
+ "mvf8", "mvf9", "mvf10", "mvf11", \
47
+ "mvf12", "mvf13", "mvf14", "mvf15"
48
+ #else
49
+ #define REGS_TO_SAVE REGS_TO_SAVE_GENERAL, "f4", "f5", "f6", "f7"
50
+ #endif
51
+
52
+ static int
53
+ #ifdef __GNUC__
54
+ __attribute__((optimize("no-omit-frame-pointer")))
55
+ #endif
56
+ slp_switch(void)
57
+ {
58
+ void *fp;
59
+ int *stackref, stsizediff;
60
+ int result;
61
+ __asm__ volatile ("" : : : REGS_TO_SAVE);
62
+ __asm__ volatile ("mov r0," REG_FP "\n\tstr r0,%0" : "=m" (fp) : : "r0");
63
+ __asm__ ("mov %0," REG_SP : "=r" (stackref));
64
+ {
65
+ SLP_SAVE_STATE(stackref, stsizediff);
66
+ __asm__ volatile (
67
+ "add " REG_SPSP ",%0\n"
68
+ "add " REG_FPFP ",%0\n"
69
+ :
70
+ : "r" (stsizediff)
71
+ );
72
+ SLP_RESTORE_STATE();
73
+ }
74
+ __asm__ volatile ("ldr r0,%1\n\tmov " REG_FP ",r0\n\tmov %0, #0" : "=r" (result) : "m" (fp) : "r0");
75
+ __asm__ volatile ("" : : : REGS_TO_SAVE);
76
+ return result;
77
+ }
78
+
79
+ #endif
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/platform/switch_arm32_ios.h ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * this is the internal transfer function.
3
+ *
4
+ * HISTORY
5
+ * 31-May-15 iOS support. Ported from arm32. Proton <feisuzhu@163.com>
6
+ *
7
+ * NOTES
8
+ *
9
+ * It is not possible to detect if fp is used or not, so the supplied
10
+ * switch function needs to support it, so that you can remove it if
11
+ * it does not apply to you.
12
+ *
13
+ * POSSIBLE ERRORS
14
+ *
15
+ * "fp cannot be used in asm here"
16
+ *
17
+ * - Try commenting out "fp" in REGS_TO_SAVE.
18
+ *
19
+ */
20
+
21
+ #define STACK_REFPLUS 1
22
+
23
+ #ifdef SLP_EVAL
24
+
25
+ #define STACK_MAGIC 0
26
+ #define REG_SP "sp"
27
+ #define REG_SPSP "sp,sp"
28
+ #define REG_FP "r7"
29
+ #define REG_FPFP "r7,r7"
30
+ #define REGS_TO_SAVE_GENERAL "r4", "r5", "r6", "r8", "r10", "r11", "lr"
31
+ #define REGS_TO_SAVE REGS_TO_SAVE_GENERAL, "d8", "d9", "d10", "d11", \
32
+ "d12", "d13", "d14", "d15"
33
+
34
+ static int
35
+ #ifdef __GNUC__
36
+ __attribute__((optimize("no-omit-frame-pointer")))
37
+ #endif
38
+ slp_switch(void)
39
+ {
40
+ void *fp;
41
+ int *stackref, stsizediff, result;
42
+ __asm__ volatile ("" : : : REGS_TO_SAVE);
43
+ __asm__ volatile ("str " REG_FP ",%0" : "=m" (fp));
44
+ __asm__ ("mov %0," REG_SP : "=r" (stackref));
45
+ {
46
+ SLP_SAVE_STATE(stackref, stsizediff);
47
+ __asm__ volatile (
48
+ "add " REG_SPSP ",%0\n"
49
+ "add " REG_FPFP ",%0\n"
50
+ :
51
+ : "r" (stsizediff)
52
+ : REGS_TO_SAVE /* Clobber registers, force compiler to
53
+ * recalculate address of void *fp from REG_SP or REG_FP */
54
+ );
55
+ SLP_RESTORE_STATE();
56
+ }
57
+ __asm__ volatile (
58
+ "ldr " REG_FP ", %1\n\t"
59
+ "mov %0, #0"
60
+ : "=r" (result)
61
+ : "m" (fp)
62
+ : REGS_TO_SAVE /* Force compiler to restore saved registers after this */
63
+ );
64
+ return result;
65
+ }
66
+
67
+ #endif
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/platform/switch_arm64_masm.asm ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ AREA switch_arm64_masm, CODE, READONLY;
2
+ GLOBAL slp_switch [FUNC]
3
+ EXTERN slp_save_state_asm
4
+ EXTERN slp_restore_state_asm
5
+
6
+ slp_switch
7
+ ; push callee saved registers to stack
8
+ stp x19, x20, [sp, #-16]!
9
+ stp x21, x22, [sp, #-16]!
10
+ stp x23, x24, [sp, #-16]!
11
+ stp x25, x26, [sp, #-16]!
12
+ stp x27, x28, [sp, #-16]!
13
+ stp x29, x30, [sp, #-16]!
14
+ stp d8, d9, [sp, #-16]!
15
+ stp d10, d11, [sp, #-16]!
16
+ stp d12, d13, [sp, #-16]!
17
+ stp d14, d15, [sp, #-16]!
18
+
19
+ ; call slp_save_state_asm with stack pointer
20
+ mov x0, sp
21
+ bl slp_save_state_asm
22
+
23
+ ; early return for return value of 1 and -1
24
+ cmp x0, #-1
25
+ b.eq RETURN
26
+ cmp x0, #1
27
+ b.eq RETURN
28
+
29
+ ; increment stack and frame pointer
30
+ add sp, sp, x0
31
+ add x29, x29, x0
32
+
33
+ bl slp_restore_state_asm
34
+
35
+ ; store return value for successful completion of routine
36
+ mov x0, #0
37
+
38
+ RETURN
39
+ ; pop registers from stack
40
+ ldp d14, d15, [sp], #16
41
+ ldp d12, d13, [sp], #16
42
+ ldp d10, d11, [sp], #16
43
+ ldp d8, d9, [sp], #16
44
+ ldp x29, x30, [sp], #16
45
+ ldp x27, x28, [sp], #16
46
+ ldp x25, x26, [sp], #16
47
+ ldp x23, x24, [sp], #16
48
+ ldp x21, x22, [sp], #16
49
+ ldp x19, x20, [sp], #16
50
+
51
+ ret
52
+
53
+ END
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/platform/switch_arm64_masm.obj ADDED
Binary file (746 Bytes). View file
 
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/platform/switch_arm64_msvc.h ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * this is the internal transfer function.
3
+ *
4
+ * HISTORY
5
+ * 21-Oct-21 Niyas Sait <niyas.sait@linaro.org>
6
+ * First version to enable win/arm64 support.
7
+ */
8
+
9
+ #define STACK_REFPLUS 1
10
+ #define STACK_MAGIC 0
11
+
12
+ /* Use the generic support for an external assembly language slp_switch function. */
13
+ #define EXTERNAL_ASM
14
+
15
+ #ifdef SLP_EVAL
16
+ /* This always uses the external masm assembly file. */
17
+ #endif
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/platform/switch_csky_gcc.h ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #ifdef SLP_EVAL
2
+ #define STACK_MAGIC 0
3
+ #define REG_FP "r8"
4
+ #ifdef __CSKYABIV2__
5
+ #define REGS_TO_SAVE_GENERAL "r4", "r5", "r6", "r7", "r9", "r10", "r11", "r15",\
6
+ "r16", "r17", "r18", "r19", "r20", "r21", "r22",\
7
+ "r23", "r24", "r25"
8
+
9
+ #if defined (__CSKY_HARD_FLOAT__) || (__CSKY_VDSP__)
10
+ #define REGS_TO_SAVE REGS_TO_SAVE_GENERAL, "vr8", "vr9", "vr10", "vr11", "vr12",\
11
+ "vr13", "vr14", "vr15"
12
+ #else
13
+ #define REGS_TO_SAVE REGS_TO_SAVE_GENERAL
14
+ #endif
15
+ #else
16
+ #define REGS_TO_SAVE "r9", "r10", "r11", "r12", "r13", "r15"
17
+ #endif
18
+
19
+
20
+ static int
21
+ #ifdef __GNUC__
22
+ __attribute__((optimize("no-omit-frame-pointer")))
23
+ #endif
24
+ slp_switch(void)
25
+ {
26
+ int *stackref, stsizediff;
27
+ int result;
28
+
29
+ __asm__ volatile ("" : : : REGS_TO_SAVE);
30
+ __asm__ ("mov %0, sp" : "=r" (stackref));
31
+ {
32
+ SLP_SAVE_STATE(stackref, stsizediff);
33
+ __asm__ volatile (
34
+ "addu sp,%0\n"
35
+ "addu "REG_FP",%0\n"
36
+ :
37
+ : "r" (stsizediff)
38
+ );
39
+
40
+ SLP_RESTORE_STATE();
41
+ }
42
+ __asm__ volatile ("movi %0, 0" : "=r" (result));
43
+ __asm__ volatile ("" : : : REGS_TO_SAVE);
44
+
45
+ return result;
46
+ }
47
+
48
+ #endif
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/platform/switch_loongarch64_linux.h ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #define STACK_REFPLUS 1
2
+
3
+ #ifdef SLP_EVAL
4
+ #define STACK_MAGIC 0
5
+
6
+ #define REGS_TO_SAVE "s0", "s1", "s2", "s3", "s4", "s5", \
7
+ "s6", "s7", "s8", "fp", \
8
+ "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31"
9
+
10
+ static int
11
+ slp_switch(void)
12
+ {
13
+ int ret;
14
+ long *stackref, stsizediff;
15
+ __asm__ volatile ("" : : : REGS_TO_SAVE);
16
+ __asm__ volatile ("move %0, $sp" : "=r" (stackref) : );
17
+ {
18
+ SLP_SAVE_STATE(stackref, stsizediff);
19
+ __asm__ volatile (
20
+ "add.d $sp, $sp, %0\n\t"
21
+ : /* no outputs */
22
+ : "r" (stsizediff)
23
+ );
24
+ SLP_RESTORE_STATE();
25
+ }
26
+ __asm__ volatile ("" : : : REGS_TO_SAVE);
27
+ __asm__ volatile ("move %0, $zero" : "=r" (ret) : );
28
+ return ret;
29
+ }
30
+
31
+ #endif
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/platform/switch_m68k_gcc.h ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * this is the internal transfer function.
3
+ *
4
+ * HISTORY
5
+ * 2014-01-06 Andreas Schwab <schwab@linux-m68k.org>
6
+ * File created.
7
+ */
8
+
9
+ #ifdef SLP_EVAL
10
+
11
+ #define STACK_MAGIC 0
12
+
13
+ #define REGS_TO_SAVE "%d2", "%d3", "%d4", "%d5", "%d6", "%d7", \
14
+ "%a2", "%a3", "%a4"
15
+
16
+ static int
17
+ slp_switch(void)
18
+ {
19
+ int err;
20
+ int *stackref, stsizediff;
21
+ void *fp, *a5;
22
+ __asm__ volatile ("" : : : REGS_TO_SAVE);
23
+ __asm__ volatile ("move.l %%fp, %0" : "=m"(fp));
24
+ __asm__ volatile ("move.l %%a5, %0" : "=m"(a5));
25
+ __asm__ ("move.l %%sp, %0" : "=r"(stackref));
26
+ {
27
+ SLP_SAVE_STATE(stackref, stsizediff);
28
+ __asm__ volatile ("add.l %0, %%sp; add.l %0, %%fp" : : "r"(stsizediff));
29
+ SLP_RESTORE_STATE();
30
+ __asm__ volatile ("clr.l %0" : "=g" (err));
31
+ }
32
+ __asm__ volatile ("move.l %0, %%a5" : : "m"(a5));
33
+ __asm__ volatile ("move.l %0, %%fp" : : "m"(fp));
34
+ __asm__ volatile ("" : : : REGS_TO_SAVE);
35
+ return err;
36
+ }
37
+
38
+ #endif
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/platform/switch_mips_unix.h ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * this is the internal transfer function.
3
+ *
4
+ * HISTORY
5
+ * 20-Sep-14 Matt Madison <madison@bliss-m.org>
6
+ * Re-code the saving of the gp register for MIPS64.
7
+ * 05-Jan-08 Thiemo Seufer <ths@debian.org>
8
+ * Ported from ppc.
9
+ */
10
+
11
+ #define STACK_REFPLUS 1
12
+
13
+ #ifdef SLP_EVAL
14
+
15
+ #define STACK_MAGIC 0
16
+
17
+ #define REGS_TO_SAVE "$16", "$17", "$18", "$19", "$20", "$21", "$22", \
18
+ "$23", "$30"
19
+ static int
20
+ slp_switch(void)
21
+ {
22
+ int err;
23
+ int *stackref, stsizediff;
24
+ #ifdef __mips64
25
+ uint64_t gpsave;
26
+ #endif
27
+ __asm__ __volatile__ ("" : : : REGS_TO_SAVE);
28
+ #ifdef __mips64
29
+ __asm__ __volatile__ ("sd $28,%0" : "=m" (gpsave) : : );
30
+ #endif
31
+ __asm__ ("move %0, $29" : "=r" (stackref) : );
32
+ {
33
+ SLP_SAVE_STATE(stackref, stsizediff);
34
+ __asm__ __volatile__ (
35
+ #ifdef __mips64
36
+ "daddu $29, %0\n"
37
+ #else
38
+ "addu $29, %0\n"
39
+ #endif
40
+ : /* no outputs */
41
+ : "r" (stsizediff)
42
+ );
43
+ SLP_RESTORE_STATE();
44
+ }
45
+ #ifdef __mips64
46
+ __asm__ __volatile__ ("ld $28,%0" : : "m" (gpsave) : );
47
+ #endif
48
+ __asm__ __volatile__ ("" : : : REGS_TO_SAVE);
49
+ __asm__ __volatile__ ("move %0, $0" : "=r" (err));
50
+ return err;
51
+ }
52
+
53
+ #endif
54
+
55
+ /*
56
+ * further self-processing support
57
+ */
58
+
59
+ /*
60
+ * if you want to add self-inspection tools, place them
61
+ * here. See the x86_msvc for the necessary defines.
62
+ * These features are highly experimental und not
63
+ * essential yet.
64
+ */
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/platform/switch_ppc64_aix.h ADDED
@@ -0,0 +1,103 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * this is the internal transfer function.
3
+ *
4
+ * HISTORY
5
+ * 16-Oct-20 Jesse Gorzinski <jgorzins@us.ibm.com>
6
+ * Copied from Linux PPC64 implementation
7
+ * 04-Sep-18 Alexey Borzenkov <snaury@gmail.com>
8
+ * Workaround a gcc bug using manual save/restore of r30
9
+ * 21-Mar-18 Tulio Magno Quites Machado Filho <tuliom@linux.vnet.ibm.com>
10
+ * Added r30 to the list of saved registers in order to fully comply with
11
+ * both ppc64 ELFv1 ABI and the ppc64le ELFv2 ABI, that classify this
12
+ * register as a nonvolatile register used for local variables.
13
+ * 21-Mar-18 Laszlo Boszormenyi <gcs@debian.org>
14
+ * Save r2 (TOC pointer) manually.
15
+ * 10-Dec-13 Ulrich Weigand <uweigand@de.ibm.com>
16
+ * Support ELFv2 ABI. Save float/vector registers.
17
+ * 09-Mar-12 Michael Ellerman <michael@ellerman.id.au>
18
+ * 64-bit implementation, copied from 32-bit.
19
+ * 07-Sep-05 (py-dev mailing list discussion)
20
+ * removed 'r31' from the register-saved. !!!! WARNING !!!!
21
+ * It means that this file can no longer be compiled statically!
22
+ * It is now only suitable as part of a dynamic library!
23
+ * 14-Jan-04 Bob Ippolito <bob@redivi.com>
24
+ * added cr2-cr4 to the registers to be saved.
25
+ * Open questions: Should we save FP registers?
26
+ * What about vector registers?
27
+ * Differences between darwin and unix?
28
+ * 24-Nov-02 Christian Tismer <tismer@tismer.com>
29
+ * needed to add another magic constant to insure
30
+ * that f in slp_eval_frame(PyFrameObject *f)
31
+ * STACK_REFPLUS will probably be 1 in most cases.
32
+ * gets included into the saved stack area.
33
+ * 04-Oct-02 Gustavo Niemeyer <niemeyer@conectiva.com>
34
+ * Ported from MacOS version.
35
+ * 17-Sep-02 Christian Tismer <tismer@tismer.com>
36
+ * after virtualizing stack save/restore, the
37
+ * stack size shrunk a bit. Needed to introduce
38
+ * an adjustment STACK_MAGIC per platform.
39
+ * 15-Sep-02 Gerd Woetzel <gerd.woetzel@GMD.DE>
40
+ * slightly changed framework for sparc
41
+ * 29-Jun-02 Christian Tismer <tismer@tismer.com>
42
+ * Added register 13-29, 31 saves. The same way as
43
+ * Armin Rigo did for the x86_unix version.
44
+ * This seems to be now fully functional!
45
+ * 04-Mar-02 Hye-Shik Chang <perky@fallin.lv>
46
+ * Ported from i386.
47
+ * 31-Jul-12 Trevor Bowen <trevorbowen@gmail.com>
48
+ * Changed memory constraints to register only.
49
+ */
50
+
51
+ #define STACK_REFPLUS 1
52
+
53
+ #ifdef SLP_EVAL
54
+
55
+ #define STACK_MAGIC 6
56
+
57
+ #if defined(__ALTIVEC__)
58
+ #define ALTIVEC_REGS \
59
+ "v20", "v21", "v22", "v23", "v24", "v25", "v26", "v27", \
60
+ "v28", "v29", "v30", "v31",
61
+ #else
62
+ #define ALTIVEC_REGS
63
+ #endif
64
+
65
+ #define REGS_TO_SAVE "r14", "r15", "r16", "r17", "r18", "r19", "r20", \
66
+ "r21", "r22", "r23", "r24", "r25", "r26", "r27", "r28", "r29", \
67
+ "r31", \
68
+ "fr14", "fr15", "fr16", "fr17", "fr18", "fr19", "fr20", "fr21", \
69
+ "fr22", "fr23", "fr24", "fr25", "fr26", "fr27", "fr28", "fr29", \
70
+ "fr30", "fr31", \
71
+ ALTIVEC_REGS \
72
+ "cr2", "cr3", "cr4"
73
+
74
+ static int
75
+ slp_switch(void)
76
+ {
77
+ int err;
78
+ long *stackref, stsizediff;
79
+ void * toc;
80
+ void * r30;
81
+ __asm__ volatile ("" : : : REGS_TO_SAVE);
82
+ __asm__ volatile ("std 2, %0" : "=m" (toc));
83
+ __asm__ volatile ("std 30, %0" : "=m" (r30));
84
+ __asm__ ("mr %0, 1" : "=r" (stackref) : );
85
+ {
86
+ SLP_SAVE_STATE(stackref, stsizediff);
87
+ __asm__ volatile (
88
+ "mr 11, %0\n"
89
+ "add 1, 1, 11\n"
90
+ : /* no outputs */
91
+ : "r" (stsizediff)
92
+ : "11"
93
+ );
94
+ SLP_RESTORE_STATE();
95
+ }
96
+ __asm__ volatile ("ld 30, %0" : : "m" (r30));
97
+ __asm__ volatile ("ld 2, %0" : : "m" (toc));
98
+ __asm__ volatile ("" : : : REGS_TO_SAVE);
99
+ __asm__ volatile ("li %0, 0" : "=r" (err));
100
+ return err;
101
+ }
102
+
103
+ #endif
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/platform/switch_ppc64_linux.h ADDED
@@ -0,0 +1,105 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * this is the internal transfer function.
3
+ *
4
+ * HISTORY
5
+ * 04-Sep-18 Alexey Borzenkov <snaury@gmail.com>
6
+ * Workaround a gcc bug using manual save/restore of r30
7
+ * 21-Mar-18 Tulio Magno Quites Machado Filho <tuliom@linux.vnet.ibm.com>
8
+ * Added r30 to the list of saved registers in order to fully comply with
9
+ * both ppc64 ELFv1 ABI and the ppc64le ELFv2 ABI, that classify this
10
+ * register as a nonvolatile register used for local variables.
11
+ * 21-Mar-18 Laszlo Boszormenyi <gcs@debian.org>
12
+ * Save r2 (TOC pointer) manually.
13
+ * 10-Dec-13 Ulrich Weigand <uweigand@de.ibm.com>
14
+ * Support ELFv2 ABI. Save float/vector registers.
15
+ * 09-Mar-12 Michael Ellerman <michael@ellerman.id.au>
16
+ * 64-bit implementation, copied from 32-bit.
17
+ * 07-Sep-05 (py-dev mailing list discussion)
18
+ * removed 'r31' from the register-saved. !!!! WARNING !!!!
19
+ * It means that this file can no longer be compiled statically!
20
+ * It is now only suitable as part of a dynamic library!
21
+ * 14-Jan-04 Bob Ippolito <bob@redivi.com>
22
+ * added cr2-cr4 to the registers to be saved.
23
+ * Open questions: Should we save FP registers?
24
+ * What about vector registers?
25
+ * Differences between darwin and unix?
26
+ * 24-Nov-02 Christian Tismer <tismer@tismer.com>
27
+ * needed to add another magic constant to insure
28
+ * that f in slp_eval_frame(PyFrameObject *f)
29
+ * STACK_REFPLUS will probably be 1 in most cases.
30
+ * gets included into the saved stack area.
31
+ * 04-Oct-02 Gustavo Niemeyer <niemeyer@conectiva.com>
32
+ * Ported from MacOS version.
33
+ * 17-Sep-02 Christian Tismer <tismer@tismer.com>
34
+ * after virtualizing stack save/restore, the
35
+ * stack size shrunk a bit. Needed to introduce
36
+ * an adjustment STACK_MAGIC per platform.
37
+ * 15-Sep-02 Gerd Woetzel <gerd.woetzel@GMD.DE>
38
+ * slightly changed framework for sparc
39
+ * 29-Jun-02 Christian Tismer <tismer@tismer.com>
40
+ * Added register 13-29, 31 saves. The same way as
41
+ * Armin Rigo did for the x86_unix version.
42
+ * This seems to be now fully functional!
43
+ * 04-Mar-02 Hye-Shik Chang <perky@fallin.lv>
44
+ * Ported from i386.
45
+ * 31-Jul-12 Trevor Bowen <trevorbowen@gmail.com>
46
+ * Changed memory constraints to register only.
47
+ */
48
+
49
+ #define STACK_REFPLUS 1
50
+
51
+ #ifdef SLP_EVAL
52
+
53
+ #if _CALL_ELF == 2
54
+ #define STACK_MAGIC 4
55
+ #else
56
+ #define STACK_MAGIC 6
57
+ #endif
58
+
59
+ #if defined(__ALTIVEC__)
60
+ #define ALTIVEC_REGS \
61
+ "v20", "v21", "v22", "v23", "v24", "v25", "v26", "v27", \
62
+ "v28", "v29", "v30", "v31",
63
+ #else
64
+ #define ALTIVEC_REGS
65
+ #endif
66
+
67
+ #define REGS_TO_SAVE "r14", "r15", "r16", "r17", "r18", "r19", "r20", \
68
+ "r21", "r22", "r23", "r24", "r25", "r26", "r27", "r28", "r29", \
69
+ "r31", \
70
+ "fr14", "fr15", "fr16", "fr17", "fr18", "fr19", "fr20", "fr21", \
71
+ "fr22", "fr23", "fr24", "fr25", "fr26", "fr27", "fr28", "fr29", \
72
+ "fr30", "fr31", \
73
+ ALTIVEC_REGS \
74
+ "cr2", "cr3", "cr4"
75
+
76
+ static int
77
+ slp_switch(void)
78
+ {
79
+ int err;
80
+ long *stackref, stsizediff;
81
+ void * toc;
82
+ void * r30;
83
+ __asm__ volatile ("" : : : REGS_TO_SAVE);
84
+ __asm__ volatile ("std 2, %0" : "=m" (toc));
85
+ __asm__ volatile ("std 30, %0" : "=m" (r30));
86
+ __asm__ ("mr %0, 1" : "=r" (stackref) : );
87
+ {
88
+ SLP_SAVE_STATE(stackref, stsizediff);
89
+ __asm__ volatile (
90
+ "mr 11, %0\n"
91
+ "add 1, 1, 11\n"
92
+ : /* no outputs */
93
+ : "r" (stsizediff)
94
+ : "11"
95
+ );
96
+ SLP_RESTORE_STATE();
97
+ }
98
+ __asm__ volatile ("ld 30, %0" : : "m" (r30));
99
+ __asm__ volatile ("ld 2, %0" : : "m" (toc));
100
+ __asm__ volatile ("" : : : REGS_TO_SAVE);
101
+ __asm__ volatile ("li %0, 0" : "=r" (err));
102
+ return err;
103
+ }
104
+
105
+ #endif
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/platform/switch_ppc_aix.h ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * this is the internal transfer function.
3
+ *
4
+ * HISTORY
5
+ * 07-Mar-11 Floris Bruynooghe <flub@devork.be>
6
+ * Do not add stsizediff to general purpose
7
+ * register (GPR) 30 as this is a non-volatile and
8
+ * unused by the PowerOpen Environment, therefore
9
+ * this was modifying a user register instead of the
10
+ * frame pointer (which does not seem to exist).
11
+ * 07-Sep-05 (py-dev mailing list discussion)
12
+ * removed 'r31' from the register-saved. !!!! WARNING !!!!
13
+ * It means that this file can no longer be compiled statically!
14
+ * It is now only suitable as part of a dynamic library!
15
+ * 14-Jan-04 Bob Ippolito <bob@redivi.com>
16
+ * added cr2-cr4 to the registers to be saved.
17
+ * Open questions: Should we save FP registers?
18
+ * What about vector registers?
19
+ * Differences between darwin and unix?
20
+ * 24-Nov-02 Christian Tismer <tismer@tismer.com>
21
+ * needed to add another magic constant to insure
22
+ * that f in slp_eval_frame(PyFrameObject *f)
23
+ * STACK_REFPLUS will probably be 1 in most cases.
24
+ * gets included into the saved stack area.
25
+ * 04-Oct-02 Gustavo Niemeyer <niemeyer@conectiva.com>
26
+ * Ported from MacOS version.
27
+ * 17-Sep-02 Christian Tismer <tismer@tismer.com>
28
+ * after virtualizing stack save/restore, the
29
+ * stack size shrunk a bit. Needed to introduce
30
+ * an adjustment STACK_MAGIC per platform.
31
+ * 15-Sep-02 Gerd Woetzel <gerd.woetzel@GMD.DE>
32
+ * slightly changed framework for sparc
33
+ * 29-Jun-02 Christian Tismer <tismer@tismer.com>
34
+ * Added register 13-29, 31 saves. The same way as
35
+ * Armin Rigo did for the x86_unix version.
36
+ * This seems to be now fully functional!
37
+ * 04-Mar-02 Hye-Shik Chang <perky@fallin.lv>
38
+ * Ported from i386.
39
+ */
40
+
41
+ #define STACK_REFPLUS 1
42
+
43
+ #ifdef SLP_EVAL
44
+
45
+ #define STACK_MAGIC 3
46
+
47
+ /* !!!!WARNING!!!! need to add "r31" in the next line if this header file
48
+ * is meant to be compiled non-dynamically!
49
+ */
50
+ #define REGS_TO_SAVE "r13", "r14", "r15", "r16", "r17", "r18", "r19", "r20", \
51
+ "r21", "r22", "r23", "r24", "r25", "r26", "r27", "r28", "r29", \
52
+ "cr2", "cr3", "cr4"
53
+ static int
54
+ slp_switch(void)
55
+ {
56
+ int err;
57
+ int *stackref, stsizediff;
58
+ __asm__ volatile ("" : : : REGS_TO_SAVE);
59
+ __asm__ ("mr %0, 1" : "=r" (stackref) : );
60
+ {
61
+ SLP_SAVE_STATE(stackref, stsizediff);
62
+ __asm__ volatile (
63
+ "mr 11, %0\n"
64
+ "add 1, 1, 11\n"
65
+ : /* no outputs */
66
+ : "r" (stsizediff)
67
+ : "11"
68
+ );
69
+ SLP_RESTORE_STATE();
70
+ }
71
+ __asm__ volatile ("" : : : REGS_TO_SAVE);
72
+ __asm__ volatile ("li %0, 0" : "=r" (err));
73
+ return err;
74
+ }
75
+
76
+ #endif
77
+
78
+ /*
79
+ * further self-processing support
80
+ */
81
+
82
+ /*
83
+ * if you want to add self-inspection tools, place them
84
+ * here. See the x86_msvc for the necessary defines.
85
+ * These features are highly experimental und not
86
+ * essential yet.
87
+ */
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/platform/switch_ppc_linux.h ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * this is the internal transfer function.
3
+ *
4
+ * HISTORY
5
+ * 07-Sep-05 (py-dev mailing list discussion)
6
+ * removed 'r31' from the register-saved. !!!! WARNING !!!!
7
+ * It means that this file can no longer be compiled statically!
8
+ * It is now only suitable as part of a dynamic library!
9
+ * 14-Jan-04 Bob Ippolito <bob@redivi.com>
10
+ * added cr2-cr4 to the registers to be saved.
11
+ * Open questions: Should we save FP registers?
12
+ * What about vector registers?
13
+ * Differences between darwin and unix?
14
+ * 24-Nov-02 Christian Tismer <tismer@tismer.com>
15
+ * needed to add another magic constant to insure
16
+ * that f in slp_eval_frame(PyFrameObject *f)
17
+ * STACK_REFPLUS will probably be 1 in most cases.
18
+ * gets included into the saved stack area.
19
+ * 04-Oct-02 Gustavo Niemeyer <niemeyer@conectiva.com>
20
+ * Ported from MacOS version.
21
+ * 17-Sep-02 Christian Tismer <tismer@tismer.com>
22
+ * after virtualizing stack save/restore, the
23
+ * stack size shrunk a bit. Needed to introduce
24
+ * an adjustment STACK_MAGIC per platform.
25
+ * 15-Sep-02 Gerd Woetzel <gerd.woetzel@GMD.DE>
26
+ * slightly changed framework for sparc
27
+ * 29-Jun-02 Christian Tismer <tismer@tismer.com>
28
+ * Added register 13-29, 31 saves. The same way as
29
+ * Armin Rigo did for the x86_unix version.
30
+ * This seems to be now fully functional!
31
+ * 04-Mar-02 Hye-Shik Chang <perky@fallin.lv>
32
+ * Ported from i386.
33
+ * 31-Jul-12 Trevor Bowen <trevorbowen@gmail.com>
34
+ * Changed memory constraints to register only.
35
+ */
36
+
37
+ #define STACK_REFPLUS 1
38
+
39
+ #ifdef SLP_EVAL
40
+
41
+ #define STACK_MAGIC 3
42
+
43
+ /* !!!!WARNING!!!! need to add "r31" in the next line if this header file
44
+ * is meant to be compiled non-dynamically!
45
+ */
46
+ #define REGS_TO_SAVE "r13", "r14", "r15", "r16", "r17", "r18", "r19", "r20", \
47
+ "r21", "r22", "r23", "r24", "r25", "r26", "r27", "r28", "r29", \
48
+ "cr2", "cr3", "cr4"
49
+ static int
50
+ slp_switch(void)
51
+ {
52
+ int err;
53
+ int *stackref, stsizediff;
54
+ __asm__ volatile ("" : : : REGS_TO_SAVE);
55
+ __asm__ ("mr %0, 1" : "=r" (stackref) : );
56
+ {
57
+ SLP_SAVE_STATE(stackref, stsizediff);
58
+ __asm__ volatile (
59
+ "mr 11, %0\n"
60
+ "add 1, 1, 11\n"
61
+ "add 30, 30, 11\n"
62
+ : /* no outputs */
63
+ : "r" (stsizediff)
64
+ : "11"
65
+ );
66
+ SLP_RESTORE_STATE();
67
+ }
68
+ __asm__ volatile ("" : : : REGS_TO_SAVE);
69
+ __asm__ volatile ("li %0, 0" : "=r" (err));
70
+ return err;
71
+ }
72
+
73
+ #endif
74
+
75
+ /*
76
+ * further self-processing support
77
+ */
78
+
79
+ /*
80
+ * if you want to add self-inspection tools, place them
81
+ * here. See the x86_msvc for the necessary defines.
82
+ * These features are highly experimental und not
83
+ * essential yet.
84
+ */
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/platform/switch_ppc_macosx.h ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * this is the internal transfer function.
3
+ *
4
+ * HISTORY
5
+ * 07-Sep-05 (py-dev mailing list discussion)
6
+ * removed 'r31' from the register-saved. !!!! WARNING !!!!
7
+ * It means that this file can no longer be compiled statically!
8
+ * It is now only suitable as part of a dynamic library!
9
+ * 14-Jan-04 Bob Ippolito <bob@redivi.com>
10
+ * added cr2-cr4 to the registers to be saved.
11
+ * Open questions: Should we save FP registers?
12
+ * What about vector registers?
13
+ * Differences between darwin and unix?
14
+ * 24-Nov-02 Christian Tismer <tismer@tismer.com>
15
+ * needed to add another magic constant to insure
16
+ * that f in slp_eval_frame(PyFrameObject *f)
17
+ * STACK_REFPLUS will probably be 1 in most cases.
18
+ * gets included into the saved stack area.
19
+ * 17-Sep-02 Christian Tismer <tismer@tismer.com>
20
+ * after virtualizing stack save/restore, the
21
+ * stack size shrunk a bit. Needed to introduce
22
+ * an adjustment STACK_MAGIC per platform.
23
+ * 15-Sep-02 Gerd Woetzel <gerd.woetzel@GMD.DE>
24
+ * slightly changed framework for sparc
25
+ * 29-Jun-02 Christian Tismer <tismer@tismer.com>
26
+ * Added register 13-29, 31 saves. The same way as
27
+ * Armin Rigo did for the x86_unix version.
28
+ * This seems to be now fully functional!
29
+ * 04-Mar-02 Hye-Shik Chang <perky@fallin.lv>
30
+ * Ported from i386.
31
+ */
32
+
33
+ #define STACK_REFPLUS 1
34
+
35
+ #ifdef SLP_EVAL
36
+
37
+ #define STACK_MAGIC 3
38
+
39
+ /* !!!!WARNING!!!! need to add "r31" in the next line if this header file
40
+ * is meant to be compiled non-dynamically!
41
+ */
42
+ #define REGS_TO_SAVE "r13", "r14", "r15", "r16", "r17", "r18", "r19", "r20", \
43
+ "r21", "r22", "r23", "r24", "r25", "r26", "r27", "r28", "r29", \
44
+ "cr2", "cr3", "cr4"
45
+
46
+ static int
47
+ slp_switch(void)
48
+ {
49
+ int err;
50
+ int *stackref, stsizediff;
51
+ __asm__ volatile ("" : : : REGS_TO_SAVE);
52
+ __asm__ ("; asm block 2\n\tmr %0, r1" : "=r" (stackref) : );
53
+ {
54
+ SLP_SAVE_STATE(stackref, stsizediff);
55
+ __asm__ volatile (
56
+ "; asm block 3\n"
57
+ "\tmr r11, %0\n"
58
+ "\tadd r1, r1, r11\n"
59
+ "\tadd r30, r30, r11\n"
60
+ : /* no outputs */
61
+ : "r" (stsizediff)
62
+ : "r11"
63
+ );
64
+ SLP_RESTORE_STATE();
65
+ }
66
+ __asm__ volatile ("" : : : REGS_TO_SAVE);
67
+ __asm__ volatile ("li %0, 0" : "=r" (err));
68
+ return err;
69
+ }
70
+
71
+ #endif
72
+
73
+ /*
74
+ * further self-processing support
75
+ */
76
+
77
+ /*
78
+ * if you want to add self-inspection tools, place them
79
+ * here. See the x86_msvc for the necessary defines.
80
+ * These features are highly experimental und not
81
+ * essential yet.
82
+ */
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/greenlet/platform/switch_ppc_unix.h ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /*
2
+ * this is the internal transfer function.
3
+ *
4
+ * HISTORY
5
+ * 07-Sep-05 (py-dev mailing list discussion)
6
+ * removed 'r31' from the register-saved. !!!! WARNING !!!!
7
+ * It means that this file can no longer be compiled statically!
8
+ * It is now only suitable as part of a dynamic library!
9
+ * 14-Jan-04 Bob Ippolito <bob@redivi.com>
10
+ * added cr2-cr4 to the registers to be saved.
11
+ * Open questions: Should we save FP registers?
12
+ * What about vector registers?
13
+ * Differences between darwin and unix?
14
+ * 24-Nov-02 Christian Tismer <tismer@tismer.com>
15
+ * needed to add another magic constant to insure
16
+ * that f in slp_eval_frame(PyFrameObject *f)
17
+ * STACK_REFPLUS will probably be 1 in most cases.
18
+ * gets included into the saved stack area.
19
+ * 04-Oct-02 Gustavo Niemeyer <niemeyer@conectiva.com>
20
+ * Ported from MacOS version.
21
+ * 17-Sep-02 Christian Tismer <tismer@tismer.com>
22
+ * after virtualizing stack save/restore, the
23
+ * stack size shrunk a bit. Needed to introduce
24
+ * an adjustment STACK_MAGIC per platform.
25
+ * 15-Sep-02 Gerd Woetzel <gerd.woetzel@GMD.DE>
26
+ * slightly changed framework for sparc
27
+ * 29-Jun-02 Christian Tismer <tismer@tismer.com>
28
+ * Added register 13-29, 31 saves. The same way as
29
+ * Armin Rigo did for the x86_unix version.
30
+ * This seems to be now fully functional!
31
+ * 04-Mar-02 Hye-Shik Chang <perky@fallin.lv>
32
+ * Ported from i386.
33
+ */
34
+
35
+ #define STACK_REFPLUS 1
36
+
37
+ #ifdef SLP_EVAL
38
+
39
+ #define STACK_MAGIC 3
40
+
41
+ /* !!!!WARNING!!!! need to add "r31" in the next line if this header file
42
+ * is meant to be compiled non-dynamically!
43
+ */
44
+ #define REGS_TO_SAVE "r13", "r14", "r15", "r16", "r17", "r18", "r19", "r20", \
45
+ "r21", "r22", "r23", "r24", "r25", "r26", "r27", "r28", "r29", \
46
+ "cr2", "cr3", "cr4"
47
+ static int
48
+ slp_switch(void)
49
+ {
50
+ int err;
51
+ int *stackref, stsizediff;
52
+ __asm__ volatile ("" : : : REGS_TO_SAVE);
53
+ __asm__ ("mr %0, 1" : "=r" (stackref) : );
54
+ {
55
+ SLP_SAVE_STATE(stackref, stsizediff);
56
+ __asm__ volatile (
57
+ "mr 11, %0\n"
58
+ "add 1, 1, 11\n"
59
+ "add 30, 30, 11\n"
60
+ : /* no outputs */
61
+ : "r" (stsizediff)
62
+ : "11"
63
+ );
64
+ SLP_RESTORE_STATE();
65
+ }
66
+ __asm__ volatile ("" : : : REGS_TO_SAVE);
67
+ __asm__ volatile ("li %0, 0" : "=r" (err));
68
+ return err;
69
+ }
70
+
71
+ #endif
72
+
73
+ /*
74
+ * further self-processing support
75
+ */
76
+
77
+ /*
78
+ * if you want to add self-inspection tools, place them
79
+ * here. See the x86_msvc for the necessary defines.
80
+ * These features are highly experimental und not
81
+ * essential yet.
82
+ */