Guido van Rossum | a330996 | 1993-07-28 09:05:47 +0000 | [diff] [blame] | 1 | #ifndef Py_CEVAL_H |
| 2 | #define Py_CEVAL_H |
| 3 | #ifdef __cplusplus |
| 4 | extern "C" { |
| 5 | #endif |
| 6 | |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 7 | |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 8 | /* Interface to random parts in ceval.c */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 9 | |
Mark Hammond | 91a681d | 2002-08-12 07:21:58 +0000 | [diff] [blame] | 10 | PyAPI_FUNC(PyObject *) PyEval_CallObjectWithKeywords( |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 11 | PyObject *, PyObject *, PyObject *); |
Guido van Rossum | a049031 | 1991-07-27 21:33:03 +0000 | [diff] [blame] | 12 | |
Guido van Rossum | d7ed683 | 1997-08-30 15:02:50 +0000 | [diff] [blame] | 13 | /* Inline this */ |
| 14 | #define PyEval_CallObject(func,arg) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 15 | PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL) |
Guido van Rossum | d7ed683 | 1997-08-30 15:02:50 +0000 | [diff] [blame] | 16 | |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 17 | PyAPI_FUNC(PyObject *) PyEval_CallFunction(PyObject *obj, |
| 18 | const char *format, ...); |
Mark Hammond | 91a681d | 2002-08-12 07:21:58 +0000 | [diff] [blame] | 19 | PyAPI_FUNC(PyObject *) PyEval_CallMethod(PyObject *obj, |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 20 | const char *methodname, |
| 21 | const char *format, ...); |
Guido van Rossum | 3d109a0 | 1998-08-08 20:53:36 +0000 | [diff] [blame] | 22 | |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 23 | #ifndef Py_LIMITED_API |
Mark Hammond | 91a681d | 2002-08-12 07:21:58 +0000 | [diff] [blame] | 24 | PyAPI_FUNC(void) PyEval_SetProfile(Py_tracefunc, PyObject *); |
| 25 | PyAPI_FUNC(void) PyEval_SetTrace(Py_tracefunc, PyObject *); |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 26 | #endif |
Fred Drake | 55fb6e0 | 2001-06-27 19:18:03 +0000 | [diff] [blame] | 27 | |
Guido van Rossum | 6297a7a | 2003-02-19 15:53:17 +0000 | [diff] [blame] | 28 | struct _frame; /* Avoid including frameobject.h */ |
| 29 | |
Mark Hammond | 91a681d | 2002-08-12 07:21:58 +0000 | [diff] [blame] | 30 | PyAPI_FUNC(PyObject *) PyEval_GetBuiltins(void); |
| 31 | PyAPI_FUNC(PyObject *) PyEval_GetGlobals(void); |
| 32 | PyAPI_FUNC(PyObject *) PyEval_GetLocals(void); |
Guido van Rossum | 6297a7a | 2003-02-19 15:53:17 +0000 | [diff] [blame] | 33 | PyAPI_FUNC(struct _frame *) PyEval_GetFrame(void); |
Tim Peters | 5ba5866 | 2001-07-16 02:29:45 +0000 | [diff] [blame] | 34 | |
| 35 | /* Look at the current frame's (if any) code's co_flags, and turn on |
| 36 | the corresponding compiler flags in cf->cf_flags. Return 1 if any |
| 37 | flag was set, else return 0. */ |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 38 | #ifndef Py_LIMITED_API |
Mark Hammond | 91a681d | 2002-08-12 07:21:58 +0000 | [diff] [blame] | 39 | PyAPI_FUNC(int) PyEval_MergeCompilerFlags(PyCompilerFlags *cf); |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 40 | #endif |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 41 | |
Mark Hammond | 91a681d | 2002-08-12 07:21:58 +0000 | [diff] [blame] | 42 | PyAPI_FUNC(int) Py_AddPendingCall(int (*func)(void *), void *arg); |
| 43 | PyAPI_FUNC(int) Py_MakePendingCalls(void); |
Guido van Rossum | 9566408 | 1994-09-14 13:23:36 +0000 | [diff] [blame] | 44 | |
Antoine Pitrou | 658fad8 | 2008-09-03 18:34:34 +0000 | [diff] [blame] | 45 | /* 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 Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 52 | to potentially outgrow the recursion limit while processing the |
Antoine Pitrou | 658fad8 | 2008-09-03 18:34:34 +0000 | [diff] [blame] | 53 | 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 Hammond | 91a681d | 2002-08-12 07:21:58 +0000 | [diff] [blame] | 70 | PyAPI_FUNC(void) Py_SetRecursionLimit(int); |
| 71 | PyAPI_FUNC(int) Py_GetRecursionLimit(void); |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 72 | |
Antoine Pitrou | 658fad8 | 2008-09-03 18:34:34 +0000 | [diff] [blame] | 73 | #define Py_EnterRecursiveCall(where) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 74 | (_Py_MakeRecCheck(PyThreadState_GET()->recursion_depth) && \ |
| 75 | _Py_CheckRecursiveCall(where)) |
| 76 | #define Py_LeaveRecursiveCall() \ |
Antoine Pitrou | 658fad8 | 2008-09-03 18:34:34 +0000 | [diff] [blame] | 77 | do{ if(_Py_MakeEndRecCheck(PyThreadState_GET()->recursion_depth)) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 78 | PyThreadState_GET()->overflowed = 0; \ |
| 79 | } while(0) |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 80 | PyAPI_FUNC(int) _Py_CheckRecursiveCall(char *where); |
| 81 | PyAPI_DATA(int) _Py_CheckRecursionLimit; |
Antoine Pitrou | 658fad8 | 2008-09-03 18:34:34 +0000 | [diff] [blame] | 82 | |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 83 | #ifdef USE_STACKCHECK |
Antoine Pitrou | 658fad8 | 2008-09-03 18:34:34 +0000 | [diff] [blame] | 84 | /* 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 Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 90 | (++(x) > (_Py_CheckRecursionLimit += PyThreadState_GET()->overflowed - 1)) |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 91 | #else |
| 92 | # define _Py_MakeRecCheck(x) (++(x) > _Py_CheckRecursionLimit) |
| 93 | #endif |
| 94 | |
Antoine Pitrou | 652e707 | 2009-03-13 19:25:20 +0000 | [diff] [blame] | 95 | #define _Py_MakeEndRecCheck(x) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 96 | (--(x) < ((_Py_CheckRecursionLimit > 100) \ |
| 97 | ? (_Py_CheckRecursionLimit - 50) \ |
| 98 | : (3 * (_Py_CheckRecursionLimit >> 2)))) |
Antoine Pitrou | 658fad8 | 2008-09-03 18:34:34 +0000 | [diff] [blame] | 99 | |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 100 | #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 Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 108 | PyAPI_FUNC(const char *) PyEval_GetFuncName(PyObject *); |
| 109 | PyAPI_FUNC(const char *) PyEval_GetFuncDesc(PyObject *); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 110 | |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 111 | PyAPI_FUNC(PyObject *) PyEval_GetCallStats(PyObject *); |
Martin v. Löwis | 8d97e33 | 2004-06-27 15:43:12 +0000 | [diff] [blame] | 112 | PyAPI_FUNC(PyObject *) PyEval_EvalFrame(struct _frame *); |
Phillip J. Eby | 0d6615f | 2005-08-02 00:46:46 +0000 | [diff] [blame] | 113 | PyAPI_FUNC(PyObject *) PyEval_EvalFrameEx(struct _frame *f, int exc); |
Jeremy Hylton | 985eba5 | 2003-02-05 23:13:00 +0000 | [diff] [blame] | 114 | |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 115 | /* Interface for threads. |
| 116 | |
| 117 | A module that plans to do a blocking system call (or something else |
| 118 | that lasts a long time and doesn't touch Python data) can allow other |
| 119 | threads to run as follows: |
| 120 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 121 | ...preparations here... |
| 122 | Py_BEGIN_ALLOW_THREADS |
| 123 | ...blocking system call here... |
| 124 | Py_END_ALLOW_THREADS |
| 125 | ...interpret result here... |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 126 | |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 127 | The Py_BEGIN_ALLOW_THREADS/Py_END_ALLOW_THREADS pair expands to a |
| 128 | {}-surrounded block. |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 129 | To leave the block in the middle (e.g., with return), you must insert |
Skip Montanaro | 682c25a | 2000-09-15 18:19:27 +0000 | [diff] [blame] | 130 | a line containing Py_BLOCK_THREADS before the return, e.g. |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 131 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 132 | if (...premature_exit...) { |
| 133 | Py_BLOCK_THREADS |
| 134 | PyErr_SetFromErrno(PyExc_IOError); |
| 135 | return NULL; |
| 136 | } |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 137 | |
| 138 | An alternative is: |
| 139 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 140 | Py_BLOCK_THREADS |
| 141 | if (...premature_exit...) { |
| 142 | PyErr_SetFromErrno(PyExc_IOError); |
| 143 | return NULL; |
| 144 | } |
| 145 | Py_UNBLOCK_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 146 | |
| 147 | For convenience, that the value of 'errno' is restored across |
Skip Montanaro | 682c25a | 2000-09-15 18:19:27 +0000 | [diff] [blame] | 148 | Py_END_ALLOW_THREADS and Py_BLOCK_THREADS. |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 149 | |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 150 | WARNING: NEVER NEST CALLS TO Py_BEGIN_ALLOW_THREADS AND |
| 151 | Py_END_ALLOW_THREADS!!! |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 152 | |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 153 | The function PyEval_InitThreads() should be called only from |
Georg Brandl | 2067bfd | 2008-05-25 13:05:15 +0000 | [diff] [blame] | 154 | init_thread() in "_threadmodule.c". |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 155 | |
| 156 | Note that not yet all candidates have been converted to use this |
| 157 | mechanism! |
| 158 | */ |
| 159 | |
Mark Hammond | 91a681d | 2002-08-12 07:21:58 +0000 | [diff] [blame] | 160 | PyAPI_FUNC(PyThreadState *) PyEval_SaveThread(void); |
| 161 | PyAPI_FUNC(void) PyEval_RestoreThread(PyThreadState *); |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 162 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 163 | #ifdef WITH_THREAD |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 164 | |
Tim Peters | 7f468f2 | 2004-10-11 02:40:51 +0000 | [diff] [blame] | 165 | PyAPI_FUNC(int) PyEval_ThreadsInitialized(void); |
Mark Hammond | 91a681d | 2002-08-12 07:21:58 +0000 | [diff] [blame] | 166 | PyAPI_FUNC(void) PyEval_InitThreads(void); |
Antoine Pitrou | 1df1536 | 2010-09-13 14:16:46 +0000 | [diff] [blame] | 167 | PyAPI_FUNC(void) _PyEval_FiniThreads(void); |
Mark Hammond | 91a681d | 2002-08-12 07:21:58 +0000 | [diff] [blame] | 168 | PyAPI_FUNC(void) PyEval_AcquireLock(void); |
| 169 | PyAPI_FUNC(void) PyEval_ReleaseLock(void); |
| 170 | PyAPI_FUNC(void) PyEval_AcquireThread(PyThreadState *tstate); |
| 171 | PyAPI_FUNC(void) PyEval_ReleaseThread(PyThreadState *tstate); |
| 172 | PyAPI_FUNC(void) PyEval_ReInitThreads(void); |
Guido van Rossum | 2fca21f7 | 1997-07-18 23:56:58 +0000 | [diff] [blame] | 173 | |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 174 | #ifndef Py_LIMITED_API |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 175 | PyAPI_FUNC(void) _PyEval_SetSwitchInterval(unsigned long microseconds); |
| 176 | PyAPI_FUNC(unsigned long) _PyEval_GetSwitchInterval(void); |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 177 | #endif |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 178 | |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 179 | #define Py_BEGIN_ALLOW_THREADS { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 180 | PyThreadState *_save; \ |
| 181 | _save = PyEval_SaveThread(); |
| 182 | #define Py_BLOCK_THREADS PyEval_RestoreThread(_save); |
| 183 | #define Py_UNBLOCK_THREADS _save = PyEval_SaveThread(); |
| 184 | #define Py_END_ALLOW_THREADS PyEval_RestoreThread(_save); \ |
| 185 | } |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 186 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 187 | #else /* !WITH_THREAD */ |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 188 | |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 189 | #define Py_BEGIN_ALLOW_THREADS { |
| 190 | #define Py_BLOCK_THREADS |
| 191 | #define Py_UNBLOCK_THREADS |
| 192 | #define Py_END_ALLOW_THREADS } |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 193 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 194 | #endif /* !WITH_THREAD */ |
Guido van Rossum | a330996 | 1993-07-28 09:05:47 +0000 | [diff] [blame] | 195 | |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 196 | #ifndef Py_LIMITED_API |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 197 | PyAPI_FUNC(int) _PyEval_SliceIndex(PyObject *, Py_ssize_t *); |
Antoine Pitrou | 074e5ed | 2009-11-10 19:50:40 +0000 | [diff] [blame] | 198 | PyAPI_FUNC(void) _PyEval_SignalAsyncExc(void); |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 199 | #endif |
Guido van Rossum | 7c36ada | 2000-05-08 14:04:54 +0000 | [diff] [blame] | 200 | |
| 201 | |
Guido van Rossum | a330996 | 1993-07-28 09:05:47 +0000 | [diff] [blame] | 202 | #ifdef __cplusplus |
| 203 | } |
| 204 | #endif |
| 205 | #endif /* !Py_CEVAL_H */ |