blob: 6d240e5b68766cabd0b1c721b94fb0bf072e2a9f [file] [log] [blame]
Guido van Rossuma3309961993-07-28 09:05:47 +00001#ifndef Py_CEVAL_H
2#define Py_CEVAL_H
3#ifdef __cplusplus
4extern "C" {
5#endif
6
Guido van Rossumf70e43a1991-02-19 12:39:46 +00007
Guido van Rossumff4949e1992-08-05 19:58:53 +00008/* Interface to random parts in ceval.c */
Guido van Rossum3f5da241990-12-20 15:06:42 +00009
Mark Hammond91a681d2002-08-12 07:21:58 +000010PyAPI_FUNC(PyObject *) PyEval_CallObjectWithKeywords(
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000011 PyObject *, PyObject *, PyObject *);
Guido van Rossuma0490311991-07-27 21:33:03 +000012
Guido van Rossum83684531999-03-17 18:44:39 +000013/* DLL-level Backwards compatibility: */
14#undef PyEval_CallObject
Mark Hammond91a681d2002-08-12 07:21:58 +000015PyAPI_FUNC(PyObject *) PyEval_CallObject(PyObject *, PyObject *);
Guido van Rossum83684531999-03-17 18:44:39 +000016
Guido van Rossumd7ed6831997-08-30 15:02:50 +000017/* Inline this */
18#define PyEval_CallObject(func,arg) \
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000019 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
Guido van Rossumd7ed6831997-08-30 15:02:50 +000020
Jeremy Hyltonaf68c872005-12-10 18:50:16 +000021PyAPI_FUNC(PyObject *) PyEval_CallFunction(PyObject *obj,
22 const char *format, ...);
Mark Hammond91a681d2002-08-12 07:21:58 +000023PyAPI_FUNC(PyObject *) PyEval_CallMethod(PyObject *obj,
Jeremy Hyltonaf68c872005-12-10 18:50:16 +000024 const char *methodname,
25 const char *format, ...);
Guido van Rossum3d109a01998-08-08 20:53:36 +000026
Mark Hammond91a681d2002-08-12 07:21:58 +000027PyAPI_FUNC(void) PyEval_SetProfile(Py_tracefunc, PyObject *);
28PyAPI_FUNC(void) PyEval_SetTrace(Py_tracefunc, PyObject *);
Fred Drake55fb6e02001-06-27 19:18:03 +000029
Guido van Rossum6297a7a2003-02-19 15:53:17 +000030struct _frame; /* Avoid including frameobject.h */
31
Mark Hammond91a681d2002-08-12 07:21:58 +000032PyAPI_FUNC(PyObject *) PyEval_GetBuiltins(void);
33PyAPI_FUNC(PyObject *) PyEval_GetGlobals(void);
34PyAPI_FUNC(PyObject *) PyEval_GetLocals(void);
Guido van Rossum6297a7a2003-02-19 15:53:17 +000035PyAPI_FUNC(struct _frame *) PyEval_GetFrame(void);
Tim Peters5ba58662001-07-16 02:29:45 +000036
37/* Look at the current frame's (if any) code's co_flags, and turn on
38 the corresponding compiler flags in cf->cf_flags. Return 1 if any
39 flag was set, else return 0. */
Mark Hammond91a681d2002-08-12 07:21:58 +000040PyAPI_FUNC(int) PyEval_MergeCompilerFlags(PyCompilerFlags *cf);
Guido van Rossum3f5da241990-12-20 15:06:42 +000041
Mark Hammond91a681d2002-08-12 07:21:58 +000042PyAPI_FUNC(int) Py_AddPendingCall(int (*func)(void *), void *arg);
43PyAPI_FUNC(int) Py_MakePendingCalls(void);
Guido van Rossum95664081994-09-14 13:23:36 +000044
Antoine Pitrou658fad82008-09-03 18:34:34 +000045/* Protection against deeply nested recursive calls
46
47 In Python 3.0, this protection has two levels:
48 * normal anti-recursion protection is triggered when the recursion level
49 exceeds the current recursion limit. It raises a RuntimeError, and sets
50 the "overflowed" flag in the thread state structure. This flag
51 temporarily *disables* the normal protection; this allows cleanup code
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000052 to potentially outgrow the recursion limit while processing the
Antoine Pitrou658fad82008-09-03 18:34:34 +000053 RuntimeError.
54 * "last chance" anti-recursion protection is triggered when the recursion
55 level exceeds "current recursion limit + 50". By construction, this
56 protection can only be triggered when the "overflowed" flag is set. It
57 means the cleanup code has itself gone into an infinite loop, or the
58 RuntimeError has been mistakingly ignored. When this protection is
59 triggered, the interpreter aborts with a Fatal Error.
60
61 In addition, the "overflowed" flag is automatically reset when the
62 recursion level drops below "current recursion limit - 50". This heuristic
63 is meant to ensure that the normal anti-recursion protection doesn't get
64 disabled too long.
65
66 Please note: this scheme has its own limitations. See:
67 http://mail.python.org/pipermail/python-dev/2008-August/082106.html
68 for some observations.
69*/
Mark Hammond91a681d2002-08-12 07:21:58 +000070PyAPI_FUNC(void) Py_SetRecursionLimit(int);
71PyAPI_FUNC(int) Py_GetRecursionLimit(void);
Guido van Rossumff4949e1992-08-05 19:58:53 +000072
Antoine Pitrou658fad82008-09-03 18:34:34 +000073#define Py_EnterRecursiveCall(where) \
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000074 (_Py_MakeRecCheck(PyThreadState_GET()->recursion_depth) && \
75 _Py_CheckRecursiveCall(where))
76#define Py_LeaveRecursiveCall() \
Antoine Pitrou658fad82008-09-03 18:34:34 +000077 do{ if(_Py_MakeEndRecCheck(PyThreadState_GET()->recursion_depth)) \
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000078 PyThreadState_GET()->overflowed = 0; \
79 } while(0)
Armin Rigo2b3eb402003-10-28 12:05:48 +000080PyAPI_FUNC(int) _Py_CheckRecursiveCall(char *where);
81PyAPI_DATA(int) _Py_CheckRecursionLimit;
Antoine Pitrou658fad82008-09-03 18:34:34 +000082
Armin Rigo2b3eb402003-10-28 12:05:48 +000083#ifdef USE_STACKCHECK
Antoine Pitrou658fad82008-09-03 18:34:34 +000084/* With USE_STACKCHECK, we artificially decrement the recursion limit in order
85 to trigger regular stack checks in _Py_CheckRecursiveCall(), except if
86 the "overflowed" flag is set, in which case we need the true value
87 of _Py_CheckRecursionLimit for _Py_MakeEndRecCheck() to function properly.
88*/
89# define _Py_MakeRecCheck(x) \
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000090 (++(x) > (_Py_CheckRecursionLimit += PyThreadState_GET()->overflowed - 1))
Armin Rigo2b3eb402003-10-28 12:05:48 +000091#else
92# define _Py_MakeRecCheck(x) (++(x) > _Py_CheckRecursionLimit)
93#endif
94
Antoine Pitrou652e7072009-03-13 19:25:20 +000095#define _Py_MakeEndRecCheck(x) \
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +000096 (--(x) < ((_Py_CheckRecursionLimit > 100) \
97 ? (_Py_CheckRecursionLimit - 50) \
98 : (3 * (_Py_CheckRecursionLimit >> 2))))
Antoine Pitrou658fad82008-09-03 18:34:34 +000099
Martin v. Löwis5b222132007-06-10 09:51:05 +0000100#define Py_ALLOW_RECURSION \
101 do { unsigned char _old = PyThreadState_GET()->recursion_critical;\
102 PyThreadState_GET()->recursion_critical = 1;
103
104#define Py_END_ALLOW_RECURSION \
105 PyThreadState_GET()->recursion_critical = _old; \
106 } while(0);
107
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000108PyAPI_FUNC(const char *) PyEval_GetFuncName(PyObject *);
109PyAPI_FUNC(const char *) PyEval_GetFuncDesc(PyObject *);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000110
Jeremy Hylton985eba52003-02-05 23:13:00 +0000111PyAPI_FUNC(PyObject *) PyEval_GetCallStats(PyObject *);
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000112PyAPI_FUNC(PyObject *) PyEval_EvalFrame(struct _frame *);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000113PyAPI_FUNC(PyObject *) PyEval_EvalFrameEx(struct _frame *f, int exc);
Jeremy Hylton985eba52003-02-05 23:13:00 +0000114
Skip Montanarod581d772002-09-03 20:10:45 +0000115/* this used to be handled on a per-thread basis - now just two globals */
116PyAPI_DATA(volatile int) _Py_Ticker;
117PyAPI_DATA(int) _Py_CheckInterval;
118
Guido van Rossumff4949e1992-08-05 19:58:53 +0000119/* Interface for threads.
120
121 A module that plans to do a blocking system call (or something else
122 that lasts a long time and doesn't touch Python data) can allow other
123 threads to run as follows:
124
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000125 ...preparations here...
126 Py_BEGIN_ALLOW_THREADS
127 ...blocking system call here...
128 Py_END_ALLOW_THREADS
129 ...interpret result here...
Guido van Rossumff4949e1992-08-05 19:58:53 +0000130
Guido van Rossumcaa63801995-01-12 11:45:45 +0000131 The Py_BEGIN_ALLOW_THREADS/Py_END_ALLOW_THREADS pair expands to a
132 {}-surrounded block.
Guido van Rossumff4949e1992-08-05 19:58:53 +0000133 To leave the block in the middle (e.g., with return), you must insert
Skip Montanaro682c25a2000-09-15 18:19:27 +0000134 a line containing Py_BLOCK_THREADS before the return, e.g.
Guido van Rossumff4949e1992-08-05 19:58:53 +0000135
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000136 if (...premature_exit...) {
137 Py_BLOCK_THREADS
138 PyErr_SetFromErrno(PyExc_IOError);
139 return NULL;
140 }
Guido van Rossumff4949e1992-08-05 19:58:53 +0000141
142 An alternative is:
143
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000144 Py_BLOCK_THREADS
145 if (...premature_exit...) {
146 PyErr_SetFromErrno(PyExc_IOError);
147 return NULL;
148 }
149 Py_UNBLOCK_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000150
151 For convenience, that the value of 'errno' is restored across
Skip Montanaro682c25a2000-09-15 18:19:27 +0000152 Py_END_ALLOW_THREADS and Py_BLOCK_THREADS.
Guido van Rossumff4949e1992-08-05 19:58:53 +0000153
Guido van Rossumcaa63801995-01-12 11:45:45 +0000154 WARNING: NEVER NEST CALLS TO Py_BEGIN_ALLOW_THREADS AND
155 Py_END_ALLOW_THREADS!!!
Guido van Rossumff4949e1992-08-05 19:58:53 +0000156
Guido van Rossumcaa63801995-01-12 11:45:45 +0000157 The function PyEval_InitThreads() should be called only from
Georg Brandl2067bfd2008-05-25 13:05:15 +0000158 init_thread() in "_threadmodule.c".
Guido van Rossumff4949e1992-08-05 19:58:53 +0000159
160 Note that not yet all candidates have been converted to use this
161 mechanism!
162*/
163
Mark Hammond91a681d2002-08-12 07:21:58 +0000164PyAPI_FUNC(PyThreadState *) PyEval_SaveThread(void);
165PyAPI_FUNC(void) PyEval_RestoreThread(PyThreadState *);
Guido van Rossumff4949e1992-08-05 19:58:53 +0000166
Guido van Rossumb6775db1994-08-01 11:34:53 +0000167#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +0000168
Tim Peters7f468f22004-10-11 02:40:51 +0000169PyAPI_FUNC(int) PyEval_ThreadsInitialized(void);
Mark Hammond91a681d2002-08-12 07:21:58 +0000170PyAPI_FUNC(void) PyEval_InitThreads(void);
171PyAPI_FUNC(void) PyEval_AcquireLock(void);
172PyAPI_FUNC(void) PyEval_ReleaseLock(void);
173PyAPI_FUNC(void) PyEval_AcquireThread(PyThreadState *tstate);
174PyAPI_FUNC(void) PyEval_ReleaseThread(PyThreadState *tstate);
175PyAPI_FUNC(void) PyEval_ReInitThreads(void);
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000176
Guido van Rossumcaa63801995-01-12 11:45:45 +0000177#define Py_BEGIN_ALLOW_THREADS { \
Antoine Pitrou7f14f0d2010-05-09 16:14:21 +0000178 PyThreadState *_save; \
179 _save = PyEval_SaveThread();
180#define Py_BLOCK_THREADS PyEval_RestoreThread(_save);
181#define Py_UNBLOCK_THREADS _save = PyEval_SaveThread();
182#define Py_END_ALLOW_THREADS PyEval_RestoreThread(_save); \
183 }
Guido van Rossumff4949e1992-08-05 19:58:53 +0000184
Guido van Rossumb6775db1994-08-01 11:34:53 +0000185#else /* !WITH_THREAD */
Guido van Rossumff4949e1992-08-05 19:58:53 +0000186
Guido van Rossumcaa63801995-01-12 11:45:45 +0000187#define Py_BEGIN_ALLOW_THREADS {
188#define Py_BLOCK_THREADS
189#define Py_UNBLOCK_THREADS
190#define Py_END_ALLOW_THREADS }
Guido van Rossumff4949e1992-08-05 19:58:53 +0000191
Guido van Rossumb6775db1994-08-01 11:34:53 +0000192#endif /* !WITH_THREAD */
Guido van Rossuma3309961993-07-28 09:05:47 +0000193
Martin v. Löwis18e16552006-02-15 17:27:45 +0000194PyAPI_FUNC(int) _PyEval_SliceIndex(PyObject *, Py_ssize_t *);
Guido van Rossum7c36ada2000-05-08 14:04:54 +0000195
196
Guido van Rossuma3309961993-07-28 09:05:47 +0000197#ifdef __cplusplus
198}
199#endif
200#endif /* !Py_CEVAL_H */