1 #ifndef Py_CEVAL_H
2 #define Py_CEVAL_H
3 #ifdef __cplusplus
4 extern "C" {
5 #endif
6 
7 
8 /* Interface to random parts in ceval.c */
9 
10 PyAPI_FUNC(PyObject *) PyEval_CallObjectWithKeywords(
11     PyObject *func, PyObject *args, PyObject *kwargs);
12 
13 /* Inline this */
14 #define PyEval_CallObject(func,arg) \
15     PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
16 
17 PyAPI_FUNC(PyObject *) PyEval_CallFunction(PyObject *obj,
18                                            const char *format, ...);
19 PyAPI_FUNC(PyObject *) PyEval_CallMethod(PyObject *obj,
20                                          const char *methodname,
21                                          const char *format, ...);
22 
23 #ifndef Py_LIMITED_API
24 PyAPI_FUNC(void) PyEval_SetProfile(Py_tracefunc, PyObject *);
25 PyAPI_FUNC(void) PyEval_SetTrace(Py_tracefunc, PyObject *);
26 PyAPI_FUNC(void) _PyEval_SetCoroutineWrapper(PyObject *);
27 PyAPI_FUNC(PyObject *) _PyEval_GetCoroutineWrapper(void);
28 PyAPI_FUNC(void) _PyEval_SetAsyncGenFirstiter(PyObject *);
29 PyAPI_FUNC(PyObject *) _PyEval_GetAsyncGenFirstiter(void);
30 PyAPI_FUNC(void) _PyEval_SetAsyncGenFinalizer(PyObject *);
31 PyAPI_FUNC(PyObject *) _PyEval_GetAsyncGenFinalizer(void);
32 #endif
33 
34 struct _frame; /* Avoid including frameobject.h */
35 
36 PyAPI_FUNC(PyObject *) PyEval_GetBuiltins(void);
37 PyAPI_FUNC(PyObject *) PyEval_GetGlobals(void);
38 PyAPI_FUNC(PyObject *) PyEval_GetLocals(void);
39 PyAPI_FUNC(struct _frame *) PyEval_GetFrame(void);
40 
41 #ifndef Py_LIMITED_API
42 /* Helper to look up a builtin object */
43 PyAPI_FUNC(PyObject *) _PyEval_GetBuiltinId(_Py_Identifier *);
44 /* Look at the current frame's (if any) code's co_flags, and turn on
45    the corresponding compiler flags in cf->cf_flags.  Return 1 if any
46    flag was set, else return 0. */
47 PyAPI_FUNC(int) PyEval_MergeCompilerFlags(PyCompilerFlags *cf);
48 #endif
49 
50 PyAPI_FUNC(int) Py_AddPendingCall(int (*func)(void *), void *arg);
51 PyAPI_FUNC(void) _PyEval_SignalReceived(void);
52 PyAPI_FUNC(int) Py_MakePendingCalls(void);
53 
54 /* Protection against deeply nested recursive calls
55 
56    In Python 3.0, this protection has two levels:
57    * normal anti-recursion protection is triggered when the recursion level
58      exceeds the current recursion limit. It raises a RecursionError, and sets
59      the "overflowed" flag in the thread state structure. This flag
60      temporarily *disables* the normal protection; this allows cleanup code
61      to potentially outgrow the recursion limit while processing the
62      RecursionError.
63    * "last chance" anti-recursion protection is triggered when the recursion
64      level exceeds "current recursion limit + 50". By construction, this
65      protection can only be triggered when the "overflowed" flag is set. It
66      means the cleanup code has itself gone into an infinite loop, or the
67      RecursionError has been mistakingly ignored. When this protection is
68      triggered, the interpreter aborts with a Fatal Error.
69 
70    In addition, the "overflowed" flag is automatically reset when the
71    recursion level drops below "current recursion limit - 50". This heuristic
72    is meant to ensure that the normal anti-recursion protection doesn't get
73    disabled too long.
74 
75    Please note: this scheme has its own limitations. See:
76    http://mail.python.org/pipermail/python-dev/2008-August/082106.html
77    for some observations.
78 */
79 PyAPI_FUNC(void) Py_SetRecursionLimit(int);
80 PyAPI_FUNC(int) Py_GetRecursionLimit(void);
81 
82 #define Py_EnterRecursiveCall(where)  \
83             (_Py_MakeRecCheck(PyThreadState_GET()->recursion_depth) &&  \
84              _Py_CheckRecursiveCall(where))
85 #define Py_LeaveRecursiveCall()                         \
86     do{ if(_Py_MakeEndRecCheck(PyThreadState_GET()->recursion_depth))  \
87       PyThreadState_GET()->overflowed = 0;  \
88     } while(0)
89 PyAPI_FUNC(int) _Py_CheckRecursiveCall(const char *where);
90 PyAPI_DATA(int) _Py_CheckRecursionLimit;
91 
92 #ifdef USE_STACKCHECK
93 /* With USE_STACKCHECK, we artificially decrement the recursion limit in order
94    to trigger regular stack checks in _Py_CheckRecursiveCall(), except if
95    the "overflowed" flag is set, in which case we need the true value
96    of _Py_CheckRecursionLimit for _Py_MakeEndRecCheck() to function properly.
97 */
98 #  define _Py_MakeRecCheck(x)  \
99     (++(x) > (_Py_CheckRecursionLimit += PyThreadState_GET()->overflowed - 1))
100 #else
101 #  define _Py_MakeRecCheck(x)  (++(x) > _Py_CheckRecursionLimit)
102 #endif
103 
104 /* Compute the "lower-water mark" for a recursion limit. When
105  * Py_LeaveRecursiveCall() is called with a recursion depth below this mark,
106  * the overflowed flag is reset to 0. */
107 #define _Py_RecursionLimitLowerWaterMark(limit) \
108     (((limit) > 200) \
109         ? ((limit) - 50) \
110         : (3 * ((limit) >> 2)))
111 
112 #define _Py_MakeEndRecCheck(x) \
113     (--(x) < _Py_RecursionLimitLowerWaterMark(_Py_CheckRecursionLimit))
114 
115 #define Py_ALLOW_RECURSION \
116   do { unsigned char _old = PyThreadState_GET()->recursion_critical;\
117     PyThreadState_GET()->recursion_critical = 1;
118 
119 #define Py_END_ALLOW_RECURSION \
120     PyThreadState_GET()->recursion_critical = _old; \
121   } while(0);
122 
123 PyAPI_FUNC(const char *) PyEval_GetFuncName(PyObject *);
124 PyAPI_FUNC(const char *) PyEval_GetFuncDesc(PyObject *);
125 
126 PyAPI_FUNC(PyObject *) PyEval_GetCallStats(PyObject *);
127 PyAPI_FUNC(PyObject *) PyEval_EvalFrame(struct _frame *);
128 PyAPI_FUNC(PyObject *) PyEval_EvalFrameEx(struct _frame *f, int exc);
129 #ifndef Py_LIMITED_API
130 PyAPI_FUNC(PyObject *) _PyEval_EvalFrameDefault(struct _frame *f, int exc);
131 #endif
132 
133 /* Interface for threads.
134 
135    A module that plans to do a blocking system call (or something else
136    that lasts a long time and doesn't touch Python data) can allow other
137    threads to run as follows:
138 
139     ...preparations here...
140     Py_BEGIN_ALLOW_THREADS
141     ...blocking system call here...
142     Py_END_ALLOW_THREADS
143     ...interpret result here...
144 
145    The Py_BEGIN_ALLOW_THREADS/Py_END_ALLOW_THREADS pair expands to a
146    {}-surrounded block.
147    To leave the block in the middle (e.g., with return), you must insert
148    a line containing Py_BLOCK_THREADS before the return, e.g.
149 
150     if (...premature_exit...) {
151         Py_BLOCK_THREADS
152         PyErr_SetFromErrno(PyExc_IOError);
153         return NULL;
154     }
155 
156    An alternative is:
157 
158     Py_BLOCK_THREADS
159     if (...premature_exit...) {
160         PyErr_SetFromErrno(PyExc_IOError);
161         return NULL;
162     }
163     Py_UNBLOCK_THREADS
164 
165    For convenience, that the value of 'errno' is restored across
166    Py_END_ALLOW_THREADS and Py_BLOCK_THREADS.
167 
168    WARNING: NEVER NEST CALLS TO Py_BEGIN_ALLOW_THREADS AND
169    Py_END_ALLOW_THREADS!!!
170 
171    The function PyEval_InitThreads() should be called only from
172    init_thread() in "_threadmodule.c".
173 
174    Note that not yet all candidates have been converted to use this
175    mechanism!
176 */
177 
178 PyAPI_FUNC(PyThreadState *) PyEval_SaveThread(void);
179 PyAPI_FUNC(void) PyEval_RestoreThread(PyThreadState *);
180 
181 #ifdef WITH_THREAD
182 
183 PyAPI_FUNC(int)  PyEval_ThreadsInitialized(void);
184 PyAPI_FUNC(void) PyEval_InitThreads(void);
185 #ifndef Py_LIMITED_API
186 PyAPI_FUNC(void) _PyEval_FiniThreads(void);
187 #endif /* !Py_LIMITED_API */
188 PyAPI_FUNC(void) PyEval_AcquireLock(void);
189 PyAPI_FUNC(void) PyEval_ReleaseLock(void);
190 PyAPI_FUNC(void) PyEval_AcquireThread(PyThreadState *tstate);
191 PyAPI_FUNC(void) PyEval_ReleaseThread(PyThreadState *tstate);
192 PyAPI_FUNC(void) PyEval_ReInitThreads(void);
193 
194 #ifndef Py_LIMITED_API
195 PyAPI_FUNC(void) _PyEval_SetSwitchInterval(unsigned long microseconds);
196 PyAPI_FUNC(unsigned long) _PyEval_GetSwitchInterval(void);
197 #endif
198 
199 #ifndef Py_LIMITED_API
200 PyAPI_FUNC(Py_ssize_t) _PyEval_RequestCodeExtraIndex(freefunc);
201 #endif
202 
203 #define Py_BEGIN_ALLOW_THREADS { \
204                         PyThreadState *_save; \
205                         _save = PyEval_SaveThread();
206 #define Py_BLOCK_THREADS        PyEval_RestoreThread(_save);
207 #define Py_UNBLOCK_THREADS      _save = PyEval_SaveThread();
208 #define Py_END_ALLOW_THREADS    PyEval_RestoreThread(_save); \
209                  }
210 
211 #else /* !WITH_THREAD */
212 
213 #define Py_BEGIN_ALLOW_THREADS {
214 #define Py_BLOCK_THREADS
215 #define Py_UNBLOCK_THREADS
216 #define Py_END_ALLOW_THREADS }
217 
218 #endif /* !WITH_THREAD */
219 
220 #ifndef Py_LIMITED_API
221 PyAPI_FUNC(int) _PyEval_SliceIndex(PyObject *, Py_ssize_t *);
222 PyAPI_FUNC(int) _PyEval_SliceIndexNotNone(PyObject *, Py_ssize_t *);
223 PyAPI_FUNC(void) _PyEval_SignalAsyncExc(void);
224 #endif
225 
226 /* Masks and values used by FORMAT_VALUE opcode. */
227 #define FVC_MASK      0x3
228 #define FVC_NONE      0x0
229 #define FVC_STR       0x1
230 #define FVC_REPR      0x2
231 #define FVC_ASCII     0x3
232 #define FVS_MASK      0x4
233 #define FVS_HAVE_SPEC 0x4
234 
235 #ifdef __cplusplus
236 }
237 #endif
238 #endif /* !Py_CEVAL_H */
239