blob: e5cb4112274794549b2e7fd8168efeb905435d0a [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
INADA Naokiaa289a52017-03-14 18:00:59 +090010/* 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
Mark Hammond91a681d2002-08-12 07:21:58 +000016PyAPI_FUNC(PyObject *) PyEval_CallObjectWithKeywords(
Victor Stinner2d0eb652016-12-06 16:27:24 +010017 PyObject *callable,
18 PyObject *args,
19 PyObject *kwargs);
Guido van Rossuma0490311991-07-27 21:33:03 +000020
Guido van Rossumd7ed6831997-08-30 15:02:50 +000021/* Inline this */
Victor Stinner2d0eb652016-12-06 16:27:24 +010022#define PyEval_CallObject(callable, arg) \
23 PyEval_CallObjectWithKeywords(callable, arg, (PyObject *)NULL)
Guido van Rossumd7ed6831997-08-30 15:02:50 +000024
Victor Stinner2d0eb652016-12-06 16:27:24 +010025PyAPI_FUNC(PyObject *) PyEval_CallFunction(PyObject *callable,
Jeremy Hyltonaf68c872005-12-10 18:50:16 +000026 const char *format, ...);
Mark Hammond91a681d2002-08-12 07:21:58 +000027PyAPI_FUNC(PyObject *) PyEval_CallMethod(PyObject *obj,
Victor Stinner2d0eb652016-12-06 16:27:24 +010028 const char *name,
Jeremy Hyltonaf68c872005-12-10 18:50:16 +000029 const char *format, ...);
Guido van Rossum3d109a01998-08-08 20:53:36 +000030
Martin v. Löwis4d0d4712010-12-03 20:14:31 +000031#ifndef Py_LIMITED_API
Mark Hammond91a681d2002-08-12 07:21:58 +000032PyAPI_FUNC(void) PyEval_SetProfile(Py_tracefunc, PyObject *);
33PyAPI_FUNC(void) PyEval_SetTrace(Py_tracefunc, PyObject *);
Yury Selivanovaab3c4a2015-06-02 18:43:51 -040034PyAPI_FUNC(void) _PyEval_SetCoroutineWrapper(PyObject *);
Yury Selivanovd8cf3822015-06-01 12:15:23 -040035PyAPI_FUNC(PyObject *) _PyEval_GetCoroutineWrapper(void);
Yury Selivanoveb636452016-09-08 22:01:51 -070036PyAPI_FUNC(void) _PyEval_SetAsyncGenFirstiter(PyObject *);
37PyAPI_FUNC(PyObject *) _PyEval_GetAsyncGenFirstiter(void);
38PyAPI_FUNC(void) _PyEval_SetAsyncGenFinalizer(PyObject *);
39PyAPI_FUNC(PyObject *) _PyEval_GetAsyncGenFinalizer(void);
Martin v. Löwis4d0d4712010-12-03 20:14:31 +000040#endif
Fred Drake55fb6e02001-06-27 19:18:03 +000041
Guido van Rossum6297a7a2003-02-19 15:53:17 +000042struct _frame; /* Avoid including frameobject.h */
43
Mark Hammond91a681d2002-08-12 07:21:58 +000044PyAPI_FUNC(PyObject *) PyEval_GetBuiltins(void);
45PyAPI_FUNC(PyObject *) PyEval_GetGlobals(void);
46PyAPI_FUNC(PyObject *) PyEval_GetLocals(void);
Guido van Rossum6297a7a2003-02-19 15:53:17 +000047PyAPI_FUNC(struct _frame *) PyEval_GetFrame(void);
Tim Peters5ba58662001-07-16 02:29:45 +000048
49/* Look at the current frame's (if any) code's co_flags, and turn on
50 the corresponding compiler flags in cf->cf_flags. Return 1 if any
51 flag was set, else return 0. */
Martin v. Löwis4d0d4712010-12-03 20:14:31 +000052#ifndef Py_LIMITED_API
Mark Hammond91a681d2002-08-12 07:21:58 +000053PyAPI_FUNC(int) PyEval_MergeCompilerFlags(PyCompilerFlags *cf);
Martin v. Löwis4d0d4712010-12-03 20:14:31 +000054#endif
Guido van Rossum3f5da241990-12-20 15:06:42 +000055
Mark Hammond91a681d2002-08-12 07:21:58 +000056PyAPI_FUNC(int) Py_AddPendingCall(int (*func)(void *), void *arg);
57PyAPI_FUNC(int) Py_MakePendingCalls(void);
Guido van Rossum95664081994-09-14 13:23:36 +000058
Antoine Pitrou658fad82008-09-03 18:34:34 +000059/* Protection against deeply nested recursive calls
60
61 In Python 3.0, this protection has two levels:
62 * normal anti-recursion protection is triggered when the recursion level
Yury Selivanovf488fb42015-07-03 01:04:23 -040063 exceeds the current recursion limit. It raises a RecursionError, and sets
Antoine Pitrou658fad82008-09-03 18:34:34 +000064 the "overflowed" flag in the thread state structure. This flag
65 temporarily *disables* the normal protection; this allows cleanup code
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000066 to potentially outgrow the recursion limit while processing the
Yury Selivanovf488fb42015-07-03 01:04:23 -040067 RecursionError.
Antoine Pitrou658fad82008-09-03 18:34:34 +000068 * "last chance" anti-recursion protection is triggered when the recursion
69 level exceeds "current recursion limit + 50". By construction, this
70 protection can only be triggered when the "overflowed" flag is set. It
71 means the cleanup code has itself gone into an infinite loop, or the
Yury Selivanovf488fb42015-07-03 01:04:23 -040072 RecursionError has been mistakingly ignored. When this protection is
Antoine Pitrou658fad82008-09-03 18:34:34 +000073 triggered, the interpreter aborts with a Fatal Error.
74
75 In addition, the "overflowed" flag is automatically reset when the
76 recursion level drops below "current recursion limit - 50". This heuristic
77 is meant to ensure that the normal anti-recursion protection doesn't get
78 disabled too long.
79
80 Please note: this scheme has its own limitations. See:
81 http://mail.python.org/pipermail/python-dev/2008-August/082106.html
82 for some observations.
83*/
Mark Hammond91a681d2002-08-12 07:21:58 +000084PyAPI_FUNC(void) Py_SetRecursionLimit(int);
85PyAPI_FUNC(int) Py_GetRecursionLimit(void);
Guido van Rossumff4949e1992-08-05 19:58:53 +000086
Antoine Pitrou658fad82008-09-03 18:34:34 +000087#define Py_EnterRecursiveCall(where) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000088 (_Py_MakeRecCheck(PyThreadState_GET()->recursion_depth) && \
89 _Py_CheckRecursiveCall(where))
90#define Py_LeaveRecursiveCall() \
Antoine Pitrou658fad82008-09-03 18:34:34 +000091 do{ if(_Py_MakeEndRecCheck(PyThreadState_GET()->recursion_depth)) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000092 PyThreadState_GET()->overflowed = 0; \
93 } while(0)
Serhiy Storchaka5fa22fc2015-06-21 16:26:28 +030094PyAPI_FUNC(int) _Py_CheckRecursiveCall(const char *where);
Armin Rigo2b3eb402003-10-28 12:05:48 +000095PyAPI_DATA(int) _Py_CheckRecursionLimit;
Antoine Pitrou658fad82008-09-03 18:34:34 +000096
Armin Rigo2b3eb402003-10-28 12:05:48 +000097#ifdef USE_STACKCHECK
Antoine Pitrou658fad82008-09-03 18:34:34 +000098/* With USE_STACKCHECK, we artificially decrement the recursion limit in order
99 to trigger regular stack checks in _Py_CheckRecursiveCall(), except if
100 the "overflowed" flag is set, in which case we need the true value
101 of _Py_CheckRecursionLimit for _Py_MakeEndRecCheck() to function properly.
102*/
103# define _Py_MakeRecCheck(x) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000104 (++(x) > (_Py_CheckRecursionLimit += PyThreadState_GET()->overflowed - 1))
Armin Rigo2b3eb402003-10-28 12:05:48 +0000105#else
106# define _Py_MakeRecCheck(x) (++(x) > _Py_CheckRecursionLimit)
107#endif
108
Victor Stinner50856d52015-10-13 00:11:21 +0200109/* Compute the "lower-water mark" for a recursion limit. When
110 * Py_LeaveRecursiveCall() is called with a recursion depth below this mark,
111 * the overflowed flag is reset to 0. */
112#define _Py_RecursionLimitLowerWaterMark(limit) \
113 (((limit) > 200) \
114 ? ((limit) - 50) \
115 : (3 * ((limit) >> 2)))
116
Antoine Pitrou652e7072009-03-13 19:25:20 +0000117#define _Py_MakeEndRecCheck(x) \
Victor Stinner50856d52015-10-13 00:11:21 +0200118 (--(x) < _Py_RecursionLimitLowerWaterMark(_Py_CheckRecursionLimit))
Antoine Pitrou658fad82008-09-03 18:34:34 +0000119
Martin v. Löwis5b222132007-06-10 09:51:05 +0000120#define Py_ALLOW_RECURSION \
121 do { unsigned char _old = PyThreadState_GET()->recursion_critical;\
122 PyThreadState_GET()->recursion_critical = 1;
123
124#define Py_END_ALLOW_RECURSION \
125 PyThreadState_GET()->recursion_critical = _old; \
126 } while(0);
127
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000128PyAPI_FUNC(const char *) PyEval_GetFuncName(PyObject *);
129PyAPI_FUNC(const char *) PyEval_GetFuncDesc(PyObject *);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000130
Martin v. Löwis8d97e332004-06-27 15:43:12 +0000131PyAPI_FUNC(PyObject *) PyEval_EvalFrame(struct _frame *);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +0000132PyAPI_FUNC(PyObject *) PyEval_EvalFrameEx(struct _frame *f, int exc);
Brett Cannon3cebf932016-09-05 15:33:46 -0700133#ifndef Py_LIMITED_API
134PyAPI_FUNC(PyObject *) _PyEval_EvalFrameDefault(struct _frame *f, int exc);
135#endif
Jeremy Hylton985eba52003-02-05 23:13:00 +0000136
Guido van Rossumff4949e1992-08-05 19:58:53 +0000137/* Interface for threads.
138
139 A module that plans to do a blocking system call (or something else
140 that lasts a long time and doesn't touch Python data) can allow other
141 threads to run as follows:
142
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000143 ...preparations here...
144 Py_BEGIN_ALLOW_THREADS
145 ...blocking system call here...
146 Py_END_ALLOW_THREADS
147 ...interpret result here...
Guido van Rossumff4949e1992-08-05 19:58:53 +0000148
Guido van Rossumcaa63801995-01-12 11:45:45 +0000149 The Py_BEGIN_ALLOW_THREADS/Py_END_ALLOW_THREADS pair expands to a
150 {}-surrounded block.
Guido van Rossumff4949e1992-08-05 19:58:53 +0000151 To leave the block in the middle (e.g., with return), you must insert
Skip Montanaro682c25a2000-09-15 18:19:27 +0000152 a line containing Py_BLOCK_THREADS before the return, e.g.
Guido van Rossumff4949e1992-08-05 19:58:53 +0000153
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000154 if (...premature_exit...) {
155 Py_BLOCK_THREADS
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +0300156 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000157 return NULL;
158 }
Guido van Rossumff4949e1992-08-05 19:58:53 +0000159
160 An alternative is:
161
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000162 Py_BLOCK_THREADS
163 if (...premature_exit...) {
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +0300164 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000165 return NULL;
166 }
167 Py_UNBLOCK_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000168
169 For convenience, that the value of 'errno' is restored across
Skip Montanaro682c25a2000-09-15 18:19:27 +0000170 Py_END_ALLOW_THREADS and Py_BLOCK_THREADS.
Guido van Rossumff4949e1992-08-05 19:58:53 +0000171
Guido van Rossumcaa63801995-01-12 11:45:45 +0000172 WARNING: NEVER NEST CALLS TO Py_BEGIN_ALLOW_THREADS AND
173 Py_END_ALLOW_THREADS!!!
Guido van Rossumff4949e1992-08-05 19:58:53 +0000174
Guido van Rossumcaa63801995-01-12 11:45:45 +0000175 The function PyEval_InitThreads() should be called only from
Georg Brandl2067bfd2008-05-25 13:05:15 +0000176 init_thread() in "_threadmodule.c".
Guido van Rossumff4949e1992-08-05 19:58:53 +0000177
178 Note that not yet all candidates have been converted to use this
179 mechanism!
180*/
181
Mark Hammond91a681d2002-08-12 07:21:58 +0000182PyAPI_FUNC(PyThreadState *) PyEval_SaveThread(void);
183PyAPI_FUNC(void) PyEval_RestoreThread(PyThreadState *);
Guido van Rossumff4949e1992-08-05 19:58:53 +0000184
Guido van Rossumb6775db1994-08-01 11:34:53 +0000185#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +0000186
Tim Peters7f468f22004-10-11 02:40:51 +0000187PyAPI_FUNC(int) PyEval_ThreadsInitialized(void);
Mark Hammond91a681d2002-08-12 07:21:58 +0000188PyAPI_FUNC(void) PyEval_InitThreads(void);
Serhiy Storchaka9fab79b2016-09-11 11:03:14 +0300189#ifndef Py_LIMITED_API
Antoine Pitrou1df15362010-09-13 14:16:46 +0000190PyAPI_FUNC(void) _PyEval_FiniThreads(void);
Serhiy Storchaka9fab79b2016-09-11 11:03:14 +0300191#endif /* !Py_LIMITED_API */
Serhiy Storchaka460bd0d2016-11-20 12:16:46 +0200192PyAPI_FUNC(void) PyEval_AcquireLock(void) Py_DEPRECATED(3.2);
193PyAPI_FUNC(void) PyEval_ReleaseLock(void) /* Py_DEPRECATED(3.2) */;
Mark Hammond91a681d2002-08-12 07:21:58 +0000194PyAPI_FUNC(void) PyEval_AcquireThread(PyThreadState *tstate);
195PyAPI_FUNC(void) PyEval_ReleaseThread(PyThreadState *tstate);
196PyAPI_FUNC(void) PyEval_ReInitThreads(void);
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000197
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000198#ifndef Py_LIMITED_API
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000199PyAPI_FUNC(void) _PyEval_SetSwitchInterval(unsigned long microseconds);
200PyAPI_FUNC(unsigned long) _PyEval_GetSwitchInterval(void);
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000201#endif
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000202
Brett Cannon5c4de282016-09-07 11:16:41 -0700203#ifndef Py_LIMITED_API
204PyAPI_FUNC(Py_ssize_t) _PyEval_RequestCodeExtraIndex(freefunc);
205#endif
206
Guido van Rossumcaa63801995-01-12 11:45:45 +0000207#define Py_BEGIN_ALLOW_THREADS { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000208 PyThreadState *_save; \
209 _save = PyEval_SaveThread();
210#define Py_BLOCK_THREADS PyEval_RestoreThread(_save);
211#define Py_UNBLOCK_THREADS _save = PyEval_SaveThread();
212#define Py_END_ALLOW_THREADS PyEval_RestoreThread(_save); \
213 }
Guido van Rossumff4949e1992-08-05 19:58:53 +0000214
Guido van Rossumb6775db1994-08-01 11:34:53 +0000215#else /* !WITH_THREAD */
Guido van Rossumff4949e1992-08-05 19:58:53 +0000216
Guido van Rossumcaa63801995-01-12 11:45:45 +0000217#define Py_BEGIN_ALLOW_THREADS {
218#define Py_BLOCK_THREADS
219#define Py_UNBLOCK_THREADS
220#define Py_END_ALLOW_THREADS }
Guido van Rossumff4949e1992-08-05 19:58:53 +0000221
Guido van Rossumb6775db1994-08-01 11:34:53 +0000222#endif /* !WITH_THREAD */
Guido van Rossuma3309961993-07-28 09:05:47 +0000223
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000224#ifndef Py_LIMITED_API
Martin v. Löwis18e16552006-02-15 17:27:45 +0000225PyAPI_FUNC(int) _PyEval_SliceIndex(PyObject *, Py_ssize_t *);
Serhiy Storchakad4edfc92017-03-30 18:29:23 +0300226PyAPI_FUNC(int) _PyEval_SliceIndexNotNone(PyObject *, Py_ssize_t *);
Antoine Pitrou074e5ed2009-11-10 19:50:40 +0000227PyAPI_FUNC(void) _PyEval_SignalAsyncExc(void);
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000228#endif
Guido van Rossum7c36ada2000-05-08 14:04:54 +0000229
Eric V. Smitha78c7952015-11-03 12:45:05 -0500230/* Masks and values used by FORMAT_VALUE opcode. */
231#define FVC_MASK 0x3
232#define FVC_NONE 0x0
233#define FVC_STR 0x1
234#define FVC_REPR 0x2
235#define FVC_ASCII 0x3
236#define FVS_MASK 0x4
237#define FVS_HAVE_SPEC 0x4
Guido van Rossum7c36ada2000-05-08 14:04:54 +0000238
Guido van Rossuma3309961993-07-28 09:05:47 +0000239#ifdef __cplusplus
240}
241#endif
242#endif /* !Py_CEVAL_H */