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 | |
Guido van Rossum | 43466ec | 1998-12-04 18:48:25 +0000 | [diff] [blame] | 10 | DL_IMPORT(PyObject *) PyEval_CallObjectWithKeywords |
Fred Drake | 5eb6d4e | 2000-07-08 23:37:28 +0000 | [diff] [blame] | 11 | (PyObject *, PyObject *, PyObject *); |
Guido van Rossum | a049031 | 1991-07-27 21:33:03 +0000 | [diff] [blame] | 12 | |
Guido van Rossum | 8368453 | 1999-03-17 18:44:39 +0000 | [diff] [blame] | 13 | /* DLL-level Backwards compatibility: */ |
| 14 | #undef PyEval_CallObject |
Fred Drake | 5eb6d4e | 2000-07-08 23:37:28 +0000 | [diff] [blame] | 15 | DL_IMPORT(PyObject *) PyEval_CallObject(PyObject *, PyObject *); |
Guido van Rossum | 8368453 | 1999-03-17 18:44:39 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | d7ed683 | 1997-08-30 15:02:50 +0000 | [diff] [blame] | 17 | /* Inline this */ |
| 18 | #define PyEval_CallObject(func,arg) \ |
| 19 | PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL) |
| 20 | |
Fred Drake | 5eb6d4e | 2000-07-08 23:37:28 +0000 | [diff] [blame] | 21 | DL_IMPORT(PyObject *) PyEval_CallFunction(PyObject *obj, char *format, ...); |
| 22 | DL_IMPORT(PyObject *) PyEval_CallMethod(PyObject *obj, |
| 23 | char *methodname, char *format, ...); |
Guido van Rossum | 3d109a0 | 1998-08-08 20:53:36 +0000 | [diff] [blame] | 24 | |
Fred Drake | 55fb6e0 | 2001-06-27 19:18:03 +0000 | [diff] [blame] | 25 | DL_IMPORT(void) PyEval_SetProfile(Py_tracefunc, PyObject *); |
| 26 | DL_IMPORT(void) PyEval_SetTrace(Py_tracefunc, PyObject *); |
| 27 | |
Fred Drake | 5eb6d4e | 2000-07-08 23:37:28 +0000 | [diff] [blame] | 28 | DL_IMPORT(PyObject *) PyEval_GetBuiltins(void); |
| 29 | DL_IMPORT(PyObject *) PyEval_GetGlobals(void); |
| 30 | DL_IMPORT(PyObject *) PyEval_GetLocals(void); |
| 31 | DL_IMPORT(PyObject *) PyEval_GetOwner(void); |
| 32 | DL_IMPORT(PyObject *) PyEval_GetFrame(void); |
| 33 | DL_IMPORT(int) PyEval_GetRestricted(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. */ |
| 38 | DL_IMPORT(int) PyEval_MergeCompilerFlags(PyCompilerFlags *cf); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 39 | |
Fred Drake | 5eb6d4e | 2000-07-08 23:37:28 +0000 | [diff] [blame] | 40 | DL_IMPORT(int) Py_FlushLine(void); |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 41 | |
Thomas Wouters | 334fb89 | 2000-07-25 12:56:38 +0000 | [diff] [blame] | 42 | DL_IMPORT(int) Py_AddPendingCall(int (*func)(void *), void *arg); |
Fred Drake | 5eb6d4e | 2000-07-08 23:37:28 +0000 | [diff] [blame] | 43 | DL_IMPORT(int) Py_MakePendingCalls(void); |
Guido van Rossum | 9566408 | 1994-09-14 13:23:36 +0000 | [diff] [blame] | 44 | |
Tim Peters | 51de690 | 2000-09-01 00:01:58 +0000 | [diff] [blame] | 45 | DL_IMPORT(void) Py_SetRecursionLimit(int); |
| 46 | DL_IMPORT(int) Py_GetRecursionLimit(void); |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 47 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 48 | DL_IMPORT(char *) PyEval_GetFuncName(PyObject *); |
| 49 | DL_IMPORT(char *) PyEval_GetFuncDesc(PyObject *); |
| 50 | |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 51 | /* Interface for threads. |
| 52 | |
| 53 | A module that plans to do a blocking system call (or something else |
| 54 | that lasts a long time and doesn't touch Python data) can allow other |
| 55 | threads to run as follows: |
| 56 | |
| 57 | ...preparations here... |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 58 | Py_BEGIN_ALLOW_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 59 | ...blocking system call here... |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 60 | Py_END_ALLOW_THREADS |
Guido van Rossum | 66cb311 | 1994-12-30 15:33:50 +0000 | [diff] [blame] | 61 | ...interpret result here... |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 62 | |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 63 | The Py_BEGIN_ALLOW_THREADS/Py_END_ALLOW_THREADS pair expands to a |
| 64 | {}-surrounded block. |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 65 | 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] | 66 | a line containing Py_BLOCK_THREADS before the return, e.g. |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 67 | |
| 68 | if (...premature_exit...) { |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 69 | Py_BLOCK_THREADS |
| 70 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 71 | return NULL; |
| 72 | } |
| 73 | |
| 74 | An alternative is: |
| 75 | |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 76 | Py_BLOCK_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 77 | if (...premature_exit...) { |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 78 | PyErr_SetFromErrno(PyExc_IOError); |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 79 | return NULL; |
| 80 | } |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 81 | Py_UNBLOCK_THREADS |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 82 | |
| 83 | For convenience, that the value of 'errno' is restored across |
Skip Montanaro | 682c25a | 2000-09-15 18:19:27 +0000 | [diff] [blame] | 84 | Py_END_ALLOW_THREADS and Py_BLOCK_THREADS. |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 85 | |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 86 | WARNING: NEVER NEST CALLS TO Py_BEGIN_ALLOW_THREADS AND |
| 87 | Py_END_ALLOW_THREADS!!! |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 88 | |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 89 | The function PyEval_InitThreads() should be called only from |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 90 | initthread() in "threadmodule.c". |
| 91 | |
| 92 | Note that not yet all candidates have been converted to use this |
| 93 | mechanism! |
| 94 | */ |
| 95 | |
Fred Drake | 5eb6d4e | 2000-07-08 23:37:28 +0000 | [diff] [blame] | 96 | extern DL_IMPORT(PyThreadState *) PyEval_SaveThread(void); |
| 97 | extern DL_IMPORT(void) PyEval_RestoreThread(PyThreadState *); |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 98 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 99 | #ifdef WITH_THREAD |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 100 | |
Fred Drake | 5eb6d4e | 2000-07-08 23:37:28 +0000 | [diff] [blame] | 101 | extern DL_IMPORT(void) PyEval_InitThreads(void); |
| 102 | extern DL_IMPORT(void) PyEval_AcquireLock(void); |
| 103 | extern DL_IMPORT(void) PyEval_ReleaseLock(void); |
| 104 | extern DL_IMPORT(void) PyEval_AcquireThread(PyThreadState *tstate); |
| 105 | extern DL_IMPORT(void) PyEval_ReleaseThread(PyThreadState *tstate); |
Tim Peters | 8ebe84d | 2000-08-27 20:00:35 +0000 | [diff] [blame] | 106 | extern DL_IMPORT(void) PyEval_ReInitThreads(void); |
Guido van Rossum | 2fca21f7 | 1997-07-18 23:56:58 +0000 | [diff] [blame] | 107 | |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 108 | #define Py_BEGIN_ALLOW_THREADS { \ |
Guido van Rossum | 2fca21f7 | 1997-07-18 23:56:58 +0000 | [diff] [blame] | 109 | PyThreadState *_save; \ |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 110 | _save = PyEval_SaveThread(); |
| 111 | #define Py_BLOCK_THREADS PyEval_RestoreThread(_save); |
| 112 | #define Py_UNBLOCK_THREADS _save = PyEval_SaveThread(); |
| 113 | #define Py_END_ALLOW_THREADS PyEval_RestoreThread(_save); \ |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 116 | #else /* !WITH_THREAD */ |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 117 | |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 118 | #define Py_BEGIN_ALLOW_THREADS { |
| 119 | #define Py_BLOCK_THREADS |
| 120 | #define Py_UNBLOCK_THREADS |
| 121 | #define Py_END_ALLOW_THREADS } |
Guido van Rossum | ff4949e | 1992-08-05 19:58:53 +0000 | [diff] [blame] | 122 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 123 | #endif /* !WITH_THREAD */ |
Guido van Rossum | a330996 | 1993-07-28 09:05:47 +0000 | [diff] [blame] | 124 | |
Fred Drake | 5eb6d4e | 2000-07-08 23:37:28 +0000 | [diff] [blame] | 125 | extern DL_IMPORT(int) _PyEval_SliceIndex(PyObject *, int *); |
Guido van Rossum | 7c36ada | 2000-05-08 14:04:54 +0000 | [diff] [blame] | 126 | |
| 127 | |
Guido van Rossum | a330996 | 1993-07-28 09:05:47 +0000 | [diff] [blame] | 128 | #ifdef __cplusplus |
| 129 | } |
| 130 | #endif |
| 131 | #endif /* !Py_CEVAL_H */ |