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