blob: 62c6489ed1c35134eba53735adf9e0b6fb8556b0 [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
Jeroen Demeyer151b91d2019-07-24 14:02:49 +020011 * and PyEval_CallMethod are deprecated. Since they are officially part of the
12 * stable ABI (PEP 384), they must be kept for backward compatibility.
13 * PyObject_Call(), PyObject_CallFunction() and PyObject_CallMethod() are
14 * recommended to call a callable object.
INADA Naokiaa289a52017-03-14 18:00:59 +090015 */
16
Jeroen Demeyer151b91d2019-07-24 14:02:49 +020017Py_DEPRECATED(3.9) PyAPI_FUNC(PyObject *) PyEval_CallObjectWithKeywords(
Victor Stinner2d0eb652016-12-06 16:27:24 +010018 PyObject *callable,
19 PyObject *args,
20 PyObject *kwargs);
Guido van Rossuma0490311991-07-27 21:33:03 +000021
Jeroen Demeyer151b91d2019-07-24 14:02:49 +020022/* Deprecated since PyEval_CallObjectWithKeywords is deprecated */
Victor Stinner2d0eb652016-12-06 16:27:24 +010023#define PyEval_CallObject(callable, arg) \
24 PyEval_CallObjectWithKeywords(callable, arg, (PyObject *)NULL)
Guido van Rossumd7ed6831997-08-30 15:02:50 +000025
Jeroen Demeyer151b91d2019-07-24 14:02:49 +020026Py_DEPRECATED(3.9) PyAPI_FUNC(PyObject *) PyEval_CallFunction(
27 PyObject *callable, const char *format, ...);
28Py_DEPRECATED(3.9) PyAPI_FUNC(PyObject *) PyEval_CallMethod(
29 PyObject *obj, const char *name, const char *format, ...);
Guido van Rossum3d109a01998-08-08 20:53:36 +000030
Guido van Rossum6297a7a2003-02-19 15:53:17 +000031struct _frame; /* Avoid including frameobject.h */
32
Mark Hammond91a681d2002-08-12 07:21:58 +000033PyAPI_FUNC(PyObject *) PyEval_GetBuiltins(void);
34PyAPI_FUNC(PyObject *) PyEval_GetGlobals(void);
35PyAPI_FUNC(PyObject *) PyEval_GetLocals(void);
Guido van Rossum6297a7a2003-02-19 15:53:17 +000036PyAPI_FUNC(struct _frame *) PyEval_GetFrame(void);
Tim Peters5ba58662001-07-16 02:29:45 +000037
Mark Hammond91a681d2002-08-12 07:21:58 +000038PyAPI_FUNC(int) Py_AddPendingCall(int (*func)(void *), void *arg);
39PyAPI_FUNC(int) Py_MakePendingCalls(void);
Guido van Rossum95664081994-09-14 13:23:36 +000040
Antoine Pitrou658fad82008-09-03 18:34:34 +000041/* Protection against deeply nested recursive calls
42
43 In Python 3.0, this protection has two levels:
44 * normal anti-recursion protection is triggered when the recursion level
Yury Selivanovf488fb42015-07-03 01:04:23 -040045 exceeds the current recursion limit. It raises a RecursionError, and sets
Antoine Pitrou658fad82008-09-03 18:34:34 +000046 the "overflowed" flag in the thread state structure. This flag
47 temporarily *disables* the normal protection; this allows cleanup code
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000048 to potentially outgrow the recursion limit while processing the
Yury Selivanovf488fb42015-07-03 01:04:23 -040049 RecursionError.
Antoine Pitrou658fad82008-09-03 18:34:34 +000050 * "last chance" anti-recursion protection is triggered when the recursion
51 level exceeds "current recursion limit + 50". By construction, this
52 protection can only be triggered when the "overflowed" flag is set. It
53 means the cleanup code has itself gone into an infinite loop, or the
Yury Selivanovf488fb42015-07-03 01:04:23 -040054 RecursionError has been mistakingly ignored. When this protection is
Antoine Pitrou658fad82008-09-03 18:34:34 +000055 triggered, the interpreter aborts with a Fatal Error.
56
57 In addition, the "overflowed" flag is automatically reset when the
58 recursion level drops below "current recursion limit - 50". This heuristic
59 is meant to ensure that the normal anti-recursion protection doesn't get
60 disabled too long.
61
62 Please note: this scheme has its own limitations. See:
63 http://mail.python.org/pipermail/python-dev/2008-August/082106.html
64 for some observations.
65*/
Mark Hammond91a681d2002-08-12 07:21:58 +000066PyAPI_FUNC(void) Py_SetRecursionLimit(int);
67PyAPI_FUNC(int) Py_GetRecursionLimit(void);
Guido van Rossumff4949e1992-08-05 19:58:53 +000068
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +010069PyAPI_FUNC(int) Py_EnterRecursiveCall(const char *where);
70PyAPI_FUNC(void) Py_LeaveRecursiveCall(void);
Antoine Pitrou658fad82008-09-03 18:34:34 +000071
Martin v. Löwis5b222132007-06-10 09:51:05 +000072#define Py_ALLOW_RECURSION \
73 do { unsigned char _old = PyThreadState_GET()->recursion_critical;\
74 PyThreadState_GET()->recursion_critical = 1;
75
76#define Py_END_ALLOW_RECURSION \
77 PyThreadState_GET()->recursion_critical = _old; \
78 } while(0);
79
Jeremy Hyltonaf68c872005-12-10 18:50:16 +000080PyAPI_FUNC(const char *) PyEval_GetFuncName(PyObject *);
81PyAPI_FUNC(const char *) PyEval_GetFuncDesc(PyObject *);
Tim Peters6d6c1a32001-08-02 04:15:00 +000082
Martin v. Löwis8d97e332004-06-27 15:43:12 +000083PyAPI_FUNC(PyObject *) PyEval_EvalFrame(struct _frame *);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +000084PyAPI_FUNC(PyObject *) PyEval_EvalFrameEx(struct _frame *f, int exc);
Jeremy Hylton985eba52003-02-05 23:13:00 +000085
Guido van Rossumff4949e1992-08-05 19:58:53 +000086/* Interface for threads.
87
88 A module that plans to do a blocking system call (or something else
89 that lasts a long time and doesn't touch Python data) can allow other
90 threads to run as follows:
91
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000092 ...preparations here...
93 Py_BEGIN_ALLOW_THREADS
94 ...blocking system call here...
95 Py_END_ALLOW_THREADS
96 ...interpret result here...
Guido van Rossumff4949e1992-08-05 19:58:53 +000097
Guido van Rossumcaa63801995-01-12 11:45:45 +000098 The Py_BEGIN_ALLOW_THREADS/Py_END_ALLOW_THREADS pair expands to a
99 {}-surrounded block.
Guido van Rossumff4949e1992-08-05 19:58:53 +0000100 To leave the block in the middle (e.g., with return), you must insert
Skip Montanaro682c25a2000-09-15 18:19:27 +0000101 a line containing Py_BLOCK_THREADS before the return, e.g.
Guido van Rossumff4949e1992-08-05 19:58:53 +0000102
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000103 if (...premature_exit...) {
104 Py_BLOCK_THREADS
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +0300105 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000106 return NULL;
107 }
Guido van Rossumff4949e1992-08-05 19:58:53 +0000108
109 An alternative is:
110
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000111 Py_BLOCK_THREADS
112 if (...premature_exit...) {
Serhiy Storchaka55fe1ae2017-04-16 10:46:38 +0300113 PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000114 return NULL;
115 }
116 Py_UNBLOCK_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000117
118 For convenience, that the value of 'errno' is restored across
Skip Montanaro682c25a2000-09-15 18:19:27 +0000119 Py_END_ALLOW_THREADS and Py_BLOCK_THREADS.
Guido van Rossumff4949e1992-08-05 19:58:53 +0000120
Guido van Rossumcaa63801995-01-12 11:45:45 +0000121 WARNING: NEVER NEST CALLS TO Py_BEGIN_ALLOW_THREADS AND
122 Py_END_ALLOW_THREADS!!!
Guido van Rossumff4949e1992-08-05 19:58:53 +0000123
Guido van Rossumcaa63801995-01-12 11:45:45 +0000124 The function PyEval_InitThreads() should be called only from
Georg Brandl2067bfd2008-05-25 13:05:15 +0000125 init_thread() in "_threadmodule.c".
Guido van Rossumff4949e1992-08-05 19:58:53 +0000126
127 Note that not yet all candidates have been converted to use this
128 mechanism!
129*/
130
Mark Hammond91a681d2002-08-12 07:21:58 +0000131PyAPI_FUNC(PyThreadState *) PyEval_SaveThread(void);
132PyAPI_FUNC(void) PyEval_RestoreThread(PyThreadState *);
Guido van Rossumff4949e1992-08-05 19:58:53 +0000133
Tim Peters7f468f22004-10-11 02:40:51 +0000134PyAPI_FUNC(int) PyEval_ThreadsInitialized(void);
Mark Hammond91a681d2002-08-12 07:21:58 +0000135PyAPI_FUNC(void) PyEval_InitThreads(void);
Zackery Spytz3c8724f2019-05-28 09:16:33 -0600136Py_DEPRECATED(3.2) PyAPI_FUNC(void) PyEval_AcquireLock(void);
137/* Py_DEPRECATED(3.2) */ PyAPI_FUNC(void) PyEval_ReleaseLock(void);
Mark Hammond91a681d2002-08-12 07:21:58 +0000138PyAPI_FUNC(void) PyEval_AcquireThread(PyThreadState *tstate);
139PyAPI_FUNC(void) PyEval_ReleaseThread(PyThreadState *tstate);
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000140
Guido van Rossumcaa63801995-01-12 11:45:45 +0000141#define Py_BEGIN_ALLOW_THREADS { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000142 PyThreadState *_save; \
143 _save = PyEval_SaveThread();
144#define Py_BLOCK_THREADS PyEval_RestoreThread(_save);
145#define Py_UNBLOCK_THREADS _save = PyEval_SaveThread();
146#define Py_END_ALLOW_THREADS PyEval_RestoreThread(_save); \
147 }
Guido van Rossumff4949e1992-08-05 19:58:53 +0000148
Eric V. Smitha78c7952015-11-03 12:45:05 -0500149/* Masks and values used by FORMAT_VALUE opcode. */
150#define FVC_MASK 0x3
151#define FVC_NONE 0x0
152#define FVC_STR 0x1
153#define FVC_REPR 0x2
154#define FVC_ASCII 0x3
155#define FVS_MASK 0x4
156#define FVS_HAVE_SPEC 0x4
Guido van Rossum7c36ada2000-05-08 14:04:54 +0000157
Victor Stinnerf4b1e3d2019-11-04 19:48:34 +0100158#ifndef Py_LIMITED_API
159# define Py_CPYTHON_CEVAL_H
160# include "cpython/ceval.h"
161# undef Py_CPYTHON_CEVAL_H
162#endif
163
Guido van Rossuma3309961993-07-28 09:05:47 +0000164#ifdef __cplusplus
165}
166#endif
167#endif /* !Py_CEVAL_H */