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