ceval.h 4.87 KB
Newer Older
1 2 3 4 5 6
#ifndef Py_CEVAL_H
#define Py_CEVAL_H
#ifdef __cplusplus
extern "C" {
#endif

7

Guido van Rossum's avatar
Guido van Rossum committed
8
/* Interface to random parts in ceval.c */
Guido van Rossum's avatar
Guido van Rossum committed
9

10
PyAPI_FUNC(PyObject *) PyEval_CallObjectWithKeywords(
11
    PyObject *, PyObject *, PyObject *);
12

13 14
/* Inline this */
#define PyEval_CallObject(func,arg) \
15
    PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
16

17 18
PyAPI_FUNC(PyObject *) PyEval_CallFunction(PyObject *obj,
                                           const char *format, ...);
19
PyAPI_FUNC(PyObject *) PyEval_CallMethod(PyObject *obj,
20 21
                                         const char *methodname,
                                         const char *format, ...);
22

23 24
PyAPI_FUNC(void) PyEval_SetProfile(Py_tracefunc, PyObject *);
PyAPI_FUNC(void) PyEval_SetTrace(Py_tracefunc, PyObject *);
25

26 27
struct _frame; /* Avoid including frameobject.h */

28 29 30
PyAPI_FUNC(PyObject *) PyEval_GetBuiltins(void);
PyAPI_FUNC(PyObject *) PyEval_GetGlobals(void);
PyAPI_FUNC(PyObject *) PyEval_GetLocals(void);
31
PyAPI_FUNC(struct _frame *) PyEval_GetFrame(void);
32
PyAPI_FUNC(int) PyEval_GetRestricted(void);
33 34 35 36

/* Look at the current frame's (if any) code's co_flags, and turn on
   the corresponding compiler flags in cf->cf_flags.  Return 1 if any
   flag was set, else return 0. */
37
PyAPI_FUNC(int) PyEval_MergeCompilerFlags(PyCompilerFlags *cf);
Guido van Rossum's avatar
Guido van Rossum committed
38

39
PyAPI_FUNC(int) Py_FlushLine(void);
Guido van Rossum's avatar
Guido van Rossum committed
40

41 42
PyAPI_FUNC(int) Py_AddPendingCall(int (*func)(void *), void *arg);
PyAPI_FUNC(int) Py_MakePendingCalls(void);
43

44
/* Protection against deeply nested recursive calls */
45 46
PyAPI_FUNC(void) Py_SetRecursionLimit(int);
PyAPI_FUNC(int) Py_GetRecursionLimit(void);
Guido van Rossum's avatar
Guido van Rossum committed
47

48
#define Py_EnterRecursiveCall(where)                                    \
49 50 51 52
            (_Py_MakeRecCheck(PyThreadState_GET()->recursion_depth) &&  \
             _Py_CheckRecursiveCall(where))
#define Py_LeaveRecursiveCall()                         \
            (--PyThreadState_GET()->recursion_depth)
53 54 55 56 57 58 59 60
PyAPI_FUNC(int) _Py_CheckRecursiveCall(char *where);
PyAPI_DATA(int) _Py_CheckRecursionLimit;
#ifdef USE_STACKCHECK
#  define _Py_MakeRecCheck(x)  (++(x) > --_Py_CheckRecursionLimit)
#else
#  define _Py_MakeRecCheck(x)  (++(x) > _Py_CheckRecursionLimit)
#endif

61 62
PyAPI_FUNC(const char *) PyEval_GetFuncName(PyObject *);
PyAPI_FUNC(const char *) PyEval_GetFuncDesc(PyObject *);
63

64
PyAPI_FUNC(PyObject *) PyEval_GetCallStats(PyObject *);
65
PyAPI_FUNC(PyObject *) PyEval_EvalFrame(struct _frame *);
66
PyAPI_FUNC(PyObject *) PyEval_EvalFrameEx(struct _frame *f, int exc);
67

68 69 70 71
/* this used to be handled on a per-thread basis - now just two globals */
PyAPI_DATA(volatile int) _Py_Ticker;
PyAPI_DATA(int) _Py_CheckInterval;

Guido van Rossum's avatar
Guido van Rossum committed
72 73 74 75 76 77
/* Interface for threads.

   A module that plans to do a blocking system call (or something else
   that lasts a long time and doesn't touch Python data) can allow other
   threads to run as follows:

78 79 80 81 82
    ...preparations here...
    Py_BEGIN_ALLOW_THREADS
    ...blocking system call here...
    Py_END_ALLOW_THREADS
    ...interpret result here...
Guido van Rossum's avatar
Guido van Rossum committed
83

84 85
   The Py_BEGIN_ALLOW_THREADS/Py_END_ALLOW_THREADS pair expands to a
   {}-surrounded block.
Guido van Rossum's avatar
Guido van Rossum committed
86
   To leave the block in the middle (e.g., with return), you must insert
87
   a line containing Py_BLOCK_THREADS before the return, e.g.
Guido van Rossum's avatar
Guido van Rossum committed
88

89 90 91 92 93
    if (...premature_exit...) {
        Py_BLOCK_THREADS
        PyErr_SetFromErrno(PyExc_IOError);
        return NULL;
    }
Guido van Rossum's avatar
Guido van Rossum committed
94 95 96

   An alternative is:

97 98 99 100 101 102
    Py_BLOCK_THREADS
    if (...premature_exit...) {
        PyErr_SetFromErrno(PyExc_IOError);
        return NULL;
    }
    Py_UNBLOCK_THREADS
Guido van Rossum's avatar
Guido van Rossum committed
103 104

   For convenience, that the value of 'errno' is restored across
105
   Py_END_ALLOW_THREADS and Py_BLOCK_THREADS.
Guido van Rossum's avatar
Guido van Rossum committed
106

107 108
   WARNING: NEVER NEST CALLS TO Py_BEGIN_ALLOW_THREADS AND
   Py_END_ALLOW_THREADS!!!
Guido van Rossum's avatar
Guido van Rossum committed
109

110
   The function PyEval_InitThreads() should be called only from
Guido van Rossum's avatar
Guido van Rossum committed
111 112 113 114 115 116
   initthread() in "threadmodule.c".

   Note that not yet all candidates have been converted to use this
   mechanism!
*/

117 118
PyAPI_FUNC(PyThreadState *) PyEval_SaveThread(void);
PyAPI_FUNC(void) PyEval_RestoreThread(PyThreadState *);
Guido van Rossum's avatar
Guido van Rossum committed
119

120
#ifdef WITH_THREAD
Guido van Rossum's avatar
Guido van Rossum committed
121

122
PyAPI_FUNC(int)  PyEval_ThreadsInitialized(void);
123 124 125 126 127 128
PyAPI_FUNC(void) PyEval_InitThreads(void);
PyAPI_FUNC(void) PyEval_AcquireLock(void);
PyAPI_FUNC(void) PyEval_ReleaseLock(void);
PyAPI_FUNC(void) PyEval_AcquireThread(PyThreadState *tstate);
PyAPI_FUNC(void) PyEval_ReleaseThread(PyThreadState *tstate);
PyAPI_FUNC(void) PyEval_ReInitThreads(void);
129

130
#define Py_BEGIN_ALLOW_THREADS { \
131 132 133 134 135 136
                        PyThreadState *_save; \
                        _save = PyEval_SaveThread();
#define Py_BLOCK_THREADS        PyEval_RestoreThread(_save);
#define Py_UNBLOCK_THREADS      _save = PyEval_SaveThread();
#define Py_END_ALLOW_THREADS    PyEval_RestoreThread(_save); \
                 }
Guido van Rossum's avatar
Guido van Rossum committed
137

138
#else /* !WITH_THREAD */
Guido van Rossum's avatar
Guido van Rossum committed
139

140 141 142 143
#define Py_BEGIN_ALLOW_THREADS {
#define Py_BLOCK_THREADS
#define Py_UNBLOCK_THREADS
#define Py_END_ALLOW_THREADS }
Guido van Rossum's avatar
Guido van Rossum committed
144

145
#endif /* !WITH_THREAD */
146

Martin v. Löwis's avatar
Martin v. Löwis committed
147
PyAPI_FUNC(int) _PyEval_SliceIndex(PyObject *, Py_ssize_t *);
Guido van Rossum's avatar
Guido van Rossum committed
148 149


150 151 152 153
#ifdef __cplusplus
}
#endif
#endif /* !Py_CEVAL_H */