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