blob: 0fc5cbabfe0b5275c9fcfd188ab903a674aa3a30 [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
Guido van Rossum43466ec1998-12-04 18:48:25 +000010DL_IMPORT(PyObject *) PyEval_CallObjectWithKeywords
Fred Drake5eb6d4e2000-07-08 23:37:28 +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
Fred Drake5eb6d4e2000-07-08 23:37:28 +000015DL_IMPORT(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) \
19 PyEval_CallObjectWithKeywords(func, arg, (PyObject *)NULL)
20
Fred Drake5eb6d4e2000-07-08 23:37:28 +000021DL_IMPORT(PyObject *) PyEval_CallFunction(PyObject *obj, char *format, ...);
22DL_IMPORT(PyObject *) PyEval_CallMethod(PyObject *obj,
23 char *methodname, char *format, ...);
Guido van Rossum3d109a01998-08-08 20:53:36 +000024
Fred Drake55fb6e02001-06-27 19:18:03 +000025DL_IMPORT(void) PyEval_SetProfile(Py_tracefunc, PyObject *);
26DL_IMPORT(void) PyEval_SetTrace(Py_tracefunc, PyObject *);
27
Fred Drake5eb6d4e2000-07-08 23:37:28 +000028DL_IMPORT(PyObject *) PyEval_GetBuiltins(void);
29DL_IMPORT(PyObject *) PyEval_GetGlobals(void);
30DL_IMPORT(PyObject *) PyEval_GetLocals(void);
31DL_IMPORT(PyObject *) PyEval_GetOwner(void);
32DL_IMPORT(PyObject *) PyEval_GetFrame(void);
33DL_IMPORT(int) PyEval_GetRestricted(void);
Tim Peters5ba58662001-07-16 02:29:45 +000034
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. */
38DL_IMPORT(int) PyEval_MergeCompilerFlags(PyCompilerFlags *cf);
Guido van Rossum3f5da241990-12-20 15:06:42 +000039
Fred Drake5eb6d4e2000-07-08 23:37:28 +000040DL_IMPORT(int) Py_FlushLine(void);
Guido van Rossumff4949e1992-08-05 19:58:53 +000041
Thomas Wouters334fb892000-07-25 12:56:38 +000042DL_IMPORT(int) Py_AddPendingCall(int (*func)(void *), void *arg);
Fred Drake5eb6d4e2000-07-08 23:37:28 +000043DL_IMPORT(int) Py_MakePendingCalls(void);
Guido van Rossum95664081994-09-14 13:23:36 +000044
Tim Peters51de6902000-09-01 00:01:58 +000045DL_IMPORT(void) Py_SetRecursionLimit(int);
46DL_IMPORT(int) Py_GetRecursionLimit(void);
Guido van Rossumff4949e1992-08-05 19:58:53 +000047
48/* Interface for threads.
49
50 A module that plans to do a blocking system call (or something else
51 that lasts a long time and doesn't touch Python data) can allow other
52 threads to run as follows:
53
54 ...preparations here...
Guido van Rossumcaa63801995-01-12 11:45:45 +000055 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +000056 ...blocking system call here...
Guido van Rossumcaa63801995-01-12 11:45:45 +000057 Py_END_ALLOW_THREADS
Guido van Rossum66cb3111994-12-30 15:33:50 +000058 ...interpret result here...
Guido van Rossumff4949e1992-08-05 19:58:53 +000059
Guido van Rossumcaa63801995-01-12 11:45:45 +000060 The Py_BEGIN_ALLOW_THREADS/Py_END_ALLOW_THREADS pair expands to a
61 {}-surrounded block.
Guido van Rossumff4949e1992-08-05 19:58:53 +000062 To leave the block in the middle (e.g., with return), you must insert
Skip Montanaro682c25a2000-09-15 18:19:27 +000063 a line containing Py_BLOCK_THREADS before the return, e.g.
Guido van Rossumff4949e1992-08-05 19:58:53 +000064
65 if (...premature_exit...) {
Guido van Rossumcaa63801995-01-12 11:45:45 +000066 Py_BLOCK_THREADS
67 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossumff4949e1992-08-05 19:58:53 +000068 return NULL;
69 }
70
71 An alternative is:
72
Guido van Rossumcaa63801995-01-12 11:45:45 +000073 Py_BLOCK_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +000074 if (...premature_exit...) {
Guido van Rossumcaa63801995-01-12 11:45:45 +000075 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossumff4949e1992-08-05 19:58:53 +000076 return NULL;
77 }
Guido van Rossumcaa63801995-01-12 11:45:45 +000078 Py_UNBLOCK_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +000079
80 For convenience, that the value of 'errno' is restored across
Skip Montanaro682c25a2000-09-15 18:19:27 +000081 Py_END_ALLOW_THREADS and Py_BLOCK_THREADS.
Guido van Rossumff4949e1992-08-05 19:58:53 +000082
Guido van Rossumcaa63801995-01-12 11:45:45 +000083 WARNING: NEVER NEST CALLS TO Py_BEGIN_ALLOW_THREADS AND
84 Py_END_ALLOW_THREADS!!!
Guido van Rossumff4949e1992-08-05 19:58:53 +000085
Guido van Rossumcaa63801995-01-12 11:45:45 +000086 The function PyEval_InitThreads() should be called only from
Guido van Rossumff4949e1992-08-05 19:58:53 +000087 initthread() in "threadmodule.c".
88
89 Note that not yet all candidates have been converted to use this
90 mechanism!
91*/
92
Fred Drake5eb6d4e2000-07-08 23:37:28 +000093extern DL_IMPORT(PyThreadState *) PyEval_SaveThread(void);
94extern DL_IMPORT(void) PyEval_RestoreThread(PyThreadState *);
Guido van Rossumff4949e1992-08-05 19:58:53 +000095
Guido van Rossumb6775db1994-08-01 11:34:53 +000096#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +000097
Fred Drake5eb6d4e2000-07-08 23:37:28 +000098extern DL_IMPORT(void) PyEval_InitThreads(void);
99extern DL_IMPORT(void) PyEval_AcquireLock(void);
100extern DL_IMPORT(void) PyEval_ReleaseLock(void);
101extern DL_IMPORT(void) PyEval_AcquireThread(PyThreadState *tstate);
102extern DL_IMPORT(void) PyEval_ReleaseThread(PyThreadState *tstate);
Tim Peters8ebe84d2000-08-27 20:00:35 +0000103extern DL_IMPORT(void) PyEval_ReInitThreads(void);
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000104
Guido van Rossumcaa63801995-01-12 11:45:45 +0000105#define Py_BEGIN_ALLOW_THREADS { \
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000106 PyThreadState *_save; \
Guido van Rossumcaa63801995-01-12 11:45:45 +0000107 _save = PyEval_SaveThread();
108#define Py_BLOCK_THREADS PyEval_RestoreThread(_save);
109#define Py_UNBLOCK_THREADS _save = PyEval_SaveThread();
110#define Py_END_ALLOW_THREADS PyEval_RestoreThread(_save); \
Guido van Rossumff4949e1992-08-05 19:58:53 +0000111 }
112
Guido van Rossumb6775db1994-08-01 11:34:53 +0000113#else /* !WITH_THREAD */
Guido van Rossumff4949e1992-08-05 19:58:53 +0000114
Guido van Rossumcaa63801995-01-12 11:45:45 +0000115#define Py_BEGIN_ALLOW_THREADS {
116#define Py_BLOCK_THREADS
117#define Py_UNBLOCK_THREADS
118#define Py_END_ALLOW_THREADS }
Guido van Rossumff4949e1992-08-05 19:58:53 +0000119
Guido van Rossumb6775db1994-08-01 11:34:53 +0000120#endif /* !WITH_THREAD */
Guido van Rossuma3309961993-07-28 09:05:47 +0000121
Fred Drake5eb6d4e2000-07-08 23:37:28 +0000122extern DL_IMPORT(int) _PyEval_SliceIndex(PyObject *, int *);
Guido van Rossum7c36ada2000-05-08 14:04:54 +0000123
124
Guido van Rossuma3309961993-07-28 09:05:47 +0000125#ifdef __cplusplus
126}
127#endif
128#endif /* !Py_CEVAL_H */