1 /**
2   Mirror _ceval.h
3   */
4 module deimos.python.ceval;
5 
6 import deimos.python.pyport;
7 import deimos.python.object;
8 import deimos.python.frameobject;
9 import deimos.python.pystate;
10 import deimos.python.pythonrun;
11 
12 extern(C):
13 // Python-header-file: Include/ceval.h:
14 
15 /// _
16 PyObject* PyEval_CallObjectWithKeywords(
17         PyObject* func, PyObject* args, PyObject* kwargs);
18 
19 version(Python_2_5_Or_Later){
20     /// _
21     PyObject* PyEval_CallObject()(PyObject* func, PyObject* arg) {
22         return PyEval_CallObjectWithKeywords(func, arg, null);
23     }
24 }else{
25     /// _
26     PyObject* PyEval_CallObject(PyObject* , PyObject* );
27 }
28 /// _
29 PyObject* PyEval_CallFunction(PyObject* obj, const(char)* format, ...);
30 /// _
31 PyObject* PyEval_CallMethod(
32         PyObject* obj,
33         const(char)* methodname,
34         const(char)* format, ...);
35 
36 /// _
37 void PyEval_SetProfile(Py_tracefunc, PyObject*);
38 /// _
39 void PyEval_SetTrace(Py_tracefunc, PyObject*);
40 /// _
41 Borrowed!PyObject* PyEval_GetBuiltins();
42 /// _
43 Borrowed!PyObject* PyEval_GetGlobals();
44 /// _
45 Borrowed!PyObject* PyEval_GetLocals();
46 /// _
47 Borrowed!PyFrameObject* PyEval_GetFrame();
48 version(Python_3_0_Or_Later) {
49 }else {
50     /// Availability: 2.*
51     int PyEval_GetRestricted();
52     /// Availability: 2.*
53     int Py_FlushLine();
54 }
55 
56 /** Look at the current frame's (if any) code's co_flags, and turn on
57    the corresponding compiler flags in cf->cf_flags.  Return 1 if any
58    flag was set, else return 0. */
59 int PyEval_MergeCompilerFlags(PyCompilerFlags* cf);
60 /// _
61 int Py_AddPendingCall(int function(void*) func, void* arg);
62 /// _
63 int Py_MakePendingCalls();
64 /// _
65 void Py_SetRecursionLimit(int);
66 /// _
67 int Py_GetRecursionLimit();
68 
69 // d translation of c macro:
70 /// _
71 int Py_EnterRecursiveCall()(char* where) {
72     return _Py_MakeRecCheck(PyThreadState_GET().recursion_depth) &&
73         _Py_CheckRecursiveCall(where);
74 }
75 /// _
76 void Py_LeaveRecursiveCall()() {
77     version(Python_3_0_Or_Later) {
78         if(_Py_MakeEndRecCheck(PyThreadState_GET().recursion_depth))
79             PyThreadState_GET().overflowed = 0;
80     }else {
81         --PyThreadState_GET().recursion_depth;
82     }
83 }
84 /// _
85 int _Py_CheckRecursiveCall(char* where);
86 /// _
87 mixin(PyAPI_DATA!"int _Py_CheckRecursionLimit");
88 
89 // TODO: version(STACKCHECK)
90 /// _
91 int _Py_MakeRecCheck()(int x) {
92     return (++(x) > _Py_CheckRecursionLimit);
93 }
94 
95 version(Python_3_0_Or_Later) {
96     // d translation of c macro:
97     /// Availability: 3.*
98     auto _Py_MakeEndRecCheck()(x) {
99         return (--(x) < ((_Py_CheckRecursionLimit > 100)
100                     ? (_Py_CheckRecursionLimit - 50)
101                     : (3 * (_Py_CheckRecursionLimit >> 2))));
102     }
103     /*
104     auto Py_ALLOW_RECURSION()() {
105         do{
106         ubyte _old = PyThreadState_GET()->recursion_critical;
107         PyThreadState_GET()->recursion_critical = 1;
108     }
109 
110     auto Py_END_ALLOW_RECURSION()() {
111         PyThreadState_GET()->recursion_critical = _old;
112         }while(0);
113     }
114     */
115 
116     /**
117       D's answer to C's
118       ---
119       Py_ALLOW_RECURSION
120       ..code..
121       Py_END_ALLOW_RECURSION
122       ---
123 
124       is
125       ---
126       mixin(Py_ALLOW_RECURSION(q{
127         ..code..
128       }));
129       ---
130       */
131     string Py_ALLOW_RECURSION()(string inner_code) {
132         import std.array;
133         return replace(q{
134                 {
135                 ubyte _old = PyThreadState_GET().recursion_critical;
136                 PyThreadState_GET().recursion_critical = 1;
137                 $inner_code;
138                 PyThreadState_GET().recursion_critical = _old;
139                 }
140         }, "$inner_code", inner_code);
141     }
142 }
143 
144 /// _
145 const(char)* PyEval_GetFuncName(PyObject*);
146 /// _
147 const(char)* PyEval_GetFuncDesc(PyObject*);
148 /// _
149 PyObject* PyEval_GetCallStats(PyObject*);
150 /// _
151 PyObject* PyEval_EvalFrame(PyFrameObject*);
152 version(Python_2_5_Or_Later){
153     /// Availability: >= 2.5
154     PyObject* PyEval_EvalFrameEx(PyFrameObject*, int);
155 }
156 
157 version(Python_3_0_Or_Later) {
158 }else{
159     /// _
160     mixin(PyAPI_DATA!"/*volatile*/ int _Py_Ticker");
161     /// _
162     mixin(PyAPI_DATA!"int _Py_CheckInterval");
163 }
164 
165 /// _
166 PyThreadState* PyEval_SaveThread();
167 /// _
168 void PyEval_RestoreThread(PyThreadState*);
169 
170 // version(WITH_THREAD) assumed?
171 /// _
172 int PyEval_ThreadsInitialized();
173 /// _
174 void PyEval_InitThreads();
175 version(Python_3_0_Or_Later) {
176     /// Availability: 3.*
177     void _PyEval_FiniThreads();
178 }
179 /// _
180 void PyEval_AcquireLock();
181 /// _
182 void PyEval_ReleaseLock();
183 /// _
184 void PyEval_AcquireThread(PyThreadState* tstate);
185 /// _
186 void PyEval_ReleaseThread(PyThreadState* tstate);
187 /// _
188 void PyEval_ReInitThreads();
189 
190 version(Python_3_0_Or_Later) {
191     /// Availability: 3.*
192     void _PyEval_SetSwitchInterval(C_ulong microseconds);
193     /// Availability: 3.*
194     C_ulong _PyEval_GetSwitchInterval();
195 }
196 
197 /* ??
198 #define Py_BLOCK_THREADS        PyEval_RestoreThread(_save);
199 #define Py_UNBLOCK_THREADS      _save = PyEval_SaveThread();
200 */
201 
202 /**
203   D's answer to C's
204   ---
205   Py_BEGIN_ALLOW_THREADS
206   ..code..
207   Py_END_ALLOW_THREADS
208   ---
209   is
210   ---
211   mixin(Py_ALLOW_THREADS(q{
212   ..code..
213   }));
214   ---
215   */
216 string Py_ALLOW_THREADS()(string inner_code) {
217     import std.array;
218     return replace(q{
219             {
220             PyThreadState* _save = PyEval_SaveThread();
221             $inner_code;
222             PyEval_RestoreThread(_save);
223             }
224     }, "$inner_code", inner_code);
225 }
226 
227 ///_
228 int _PyEval_SliceIndex(PyObject*, Py_ssize_t*);
229 version(Python_3_0_Or_Later) {
230     /// Availability: 3.*
231     void _PyEval_SignalAsyncExc();
232 }