blob: 6db08182612114059fbc98116ede6df0766b64fb [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
Mark Hammond91a681d2002-08-12 07:21:58 +000010PyAPI_FUNC(PyObject *) PyEval_CallObjectWithKeywords(
11 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
Mark Hammond91a681d2002-08-12 07:21:58 +000015PyAPI_FUNC(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
Jeremy Hyltonaf68c872005-12-10 18:50:16 +000021PyAPI_FUNC(PyObject *) PyEval_CallFunction(PyObject *obj,
22 const char *format, ...);
Mark Hammond91a681d2002-08-12 07:21:58 +000023PyAPI_FUNC(PyObject *) PyEval_CallMethod(PyObject *obj,
Jeremy Hyltonaf68c872005-12-10 18:50:16 +000024 const char *methodname,
25 const char *format, ...);
Guido van Rossum3d109a01998-08-08 20:53:36 +000026
Mark Hammond91a681d2002-08-12 07:21:58 +000027PyAPI_FUNC(void) PyEval_SetProfile(Py_tracefunc, PyObject *);
28PyAPI_FUNC(void) PyEval_SetTrace(Py_tracefunc, PyObject *);
Fred Drake55fb6e02001-06-27 19:18:03 +000029
Guido van Rossum6297a7a2003-02-19 15:53:17 +000030struct _frame; /* Avoid including frameobject.h */
31
Mark Hammond91a681d2002-08-12 07:21:58 +000032PyAPI_FUNC(PyObject *) PyEval_GetBuiltins(void);
33PyAPI_FUNC(PyObject *) PyEval_GetGlobals(void);
34PyAPI_FUNC(PyObject *) PyEval_GetLocals(void);
Guido van Rossum6297a7a2003-02-19 15:53:17 +000035PyAPI_FUNC(struct _frame *) PyEval_GetFrame(void);
Mark Hammond91a681d2002-08-12 07:21:58 +000036PyAPI_FUNC(int) PyEval_GetRestricted(void);
Tim Peters5ba58662001-07-16 02:29:45 +000037
38/* Look at the current frame's (if any) code's co_flags, and turn on
39 the corresponding compiler flags in cf->cf_flags. Return 1 if any
40 flag was set, else return 0. */
Mark Hammond91a681d2002-08-12 07:21:58 +000041PyAPI_FUNC(int) PyEval_MergeCompilerFlags(PyCompilerFlags *cf);
Guido van Rossum3f5da241990-12-20 15:06:42 +000042
Mark Hammond91a681d2002-08-12 07:21:58 +000043PyAPI_FUNC(int) Py_FlushLine(void);
Guido van Rossumff4949e1992-08-05 19:58:53 +000044
Mark Hammond91a681d2002-08-12 07:21:58 +000045PyAPI_FUNC(int) Py_AddPendingCall(int (*func)(void *), void *arg);
46PyAPI_FUNC(int) Py_MakePendingCalls(void);
Guido van Rossum95664081994-09-14 13:23:36 +000047
Armin Rigo2b3eb402003-10-28 12:05:48 +000048/* Protection against deeply nested recursive calls */
Mark Hammond91a681d2002-08-12 07:21:58 +000049PyAPI_FUNC(void) Py_SetRecursionLimit(int);
50PyAPI_FUNC(int) Py_GetRecursionLimit(void);
Guido van Rossumff4949e1992-08-05 19:58:53 +000051
Armin Rigo2b3eb402003-10-28 12:05:48 +000052#define Py_EnterRecursiveCall(where) \
53 (_Py_MakeRecCheck(PyThreadState_GET()->recursion_depth) && \
54 _Py_CheckRecursiveCall(where))
55#define Py_LeaveRecursiveCall() \
56 (--PyThreadState_GET()->recursion_depth)
57PyAPI_FUNC(int) _Py_CheckRecursiveCall(char *where);
58PyAPI_DATA(int) _Py_CheckRecursionLimit;
59#ifdef USE_STACKCHECK
60# define _Py_MakeRecCheck(x) (++(x) > --_Py_CheckRecursionLimit)
61#else
62# define _Py_MakeRecCheck(x) (++(x) > _Py_CheckRecursionLimit)
63#endif
64
Jeremy Hyltonaf68c872005-12-10 18:50:16 +000065PyAPI_FUNC(const char *) PyEval_GetFuncName(PyObject *);
66PyAPI_FUNC(const char *) PyEval_GetFuncDesc(PyObject *);
Tim Peters6d6c1a32001-08-02 04:15:00 +000067
Jeremy Hylton985eba52003-02-05 23:13:00 +000068PyAPI_FUNC(PyObject *) PyEval_GetCallStats(PyObject *);
Martin v. Löwis8d97e332004-06-27 15:43:12 +000069PyAPI_FUNC(PyObject *) PyEval_EvalFrame(struct _frame *);
Phillip J. Eby0d6615f2005-08-02 00:46:46 +000070PyAPI_FUNC(PyObject *) PyEval_EvalFrameEx(struct _frame *f, int exc);
Jeremy Hylton985eba52003-02-05 23:13:00 +000071
Skip Montanarod581d772002-09-03 20:10:45 +000072/* this used to be handled on a per-thread basis - now just two globals */
73PyAPI_DATA(volatile int) _Py_Ticker;
74PyAPI_DATA(int) _Py_CheckInterval;
75
Guido van Rossumff4949e1992-08-05 19:58:53 +000076/* Interface for threads.
77
78 A module that plans to do a blocking system call (or something else
79 that lasts a long time and doesn't touch Python data) can allow other
80 threads to run as follows:
81
82 ...preparations here...
Guido van Rossumcaa63801995-01-12 11:45:45 +000083 Py_BEGIN_ALLOW_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +000084 ...blocking system call here...
Guido van Rossumcaa63801995-01-12 11:45:45 +000085 Py_END_ALLOW_THREADS
Guido van Rossum66cb3111994-12-30 15:33:50 +000086 ...interpret result here...
Guido van Rossumff4949e1992-08-05 19:58:53 +000087
Guido van Rossumcaa63801995-01-12 11:45:45 +000088 The Py_BEGIN_ALLOW_THREADS/Py_END_ALLOW_THREADS pair expands to a
89 {}-surrounded block.
Guido van Rossumff4949e1992-08-05 19:58:53 +000090 To leave the block in the middle (e.g., with return), you must insert
Skip Montanaro682c25a2000-09-15 18:19:27 +000091 a line containing Py_BLOCK_THREADS before the return, e.g.
Guido van Rossumff4949e1992-08-05 19:58:53 +000092
93 if (...premature_exit...) {
Guido van Rossumcaa63801995-01-12 11:45:45 +000094 Py_BLOCK_THREADS
95 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossumff4949e1992-08-05 19:58:53 +000096 return NULL;
97 }
98
99 An alternative is:
100
Guido van Rossumcaa63801995-01-12 11:45:45 +0000101 Py_BLOCK_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000102 if (...premature_exit...) {
Guido van Rossumcaa63801995-01-12 11:45:45 +0000103 PyErr_SetFromErrno(PyExc_IOError);
Guido van Rossumff4949e1992-08-05 19:58:53 +0000104 return NULL;
105 }
Guido van Rossumcaa63801995-01-12 11:45:45 +0000106 Py_UNBLOCK_THREADS
Guido van Rossumff4949e1992-08-05 19:58:53 +0000107
108 For convenience, that the value of 'errno' is restored across
Skip Montanaro682c25a2000-09-15 18:19:27 +0000109 Py_END_ALLOW_THREADS and Py_BLOCK_THREADS.
Guido van Rossumff4949e1992-08-05 19:58:53 +0000110
Guido van Rossumcaa63801995-01-12 11:45:45 +0000111 WARNING: NEVER NEST CALLS TO Py_BEGIN_ALLOW_THREADS AND
112 Py_END_ALLOW_THREADS!!!
Guido van Rossumff4949e1992-08-05 19:58:53 +0000113
Guido van Rossumcaa63801995-01-12 11:45:45 +0000114 The function PyEval_InitThreads() should be called only from
Guido van Rossumff4949e1992-08-05 19:58:53 +0000115 initthread() in "threadmodule.c".
116
117 Note that not yet all candidates have been converted to use this
118 mechanism!
119*/
120
Mark Hammond91a681d2002-08-12 07:21:58 +0000121PyAPI_FUNC(PyThreadState *) PyEval_SaveThread(void);
122PyAPI_FUNC(void) PyEval_RestoreThread(PyThreadState *);
Guido van Rossumff4949e1992-08-05 19:58:53 +0000123
Guido van Rossumb6775db1994-08-01 11:34:53 +0000124#ifdef WITH_THREAD
Guido van Rossumff4949e1992-08-05 19:58:53 +0000125
Tim Peters7f468f22004-10-11 02:40:51 +0000126PyAPI_FUNC(int) PyEval_ThreadsInitialized(void);
Mark Hammond91a681d2002-08-12 07:21:58 +0000127PyAPI_FUNC(void) PyEval_InitThreads(void);
128PyAPI_FUNC(void) PyEval_AcquireLock(void);
129PyAPI_FUNC(void) PyEval_ReleaseLock(void);
130PyAPI_FUNC(void) PyEval_AcquireThread(PyThreadState *tstate);
131PyAPI_FUNC(void) PyEval_ReleaseThread(PyThreadState *tstate);
132PyAPI_FUNC(void) PyEval_ReInitThreads(void);
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000133
Guido van Rossumcaa63801995-01-12 11:45:45 +0000134#define Py_BEGIN_ALLOW_THREADS { \
Guido van Rossum2fca21f71997-07-18 23:56:58 +0000135 PyThreadState *_save; \
Guido van Rossumcaa63801995-01-12 11:45:45 +0000136 _save = PyEval_SaveThread();
137#define Py_BLOCK_THREADS PyEval_RestoreThread(_save);
138#define Py_UNBLOCK_THREADS _save = PyEval_SaveThread();
139#define Py_END_ALLOW_THREADS PyEval_RestoreThread(_save); \
Guido van Rossumff4949e1992-08-05 19:58:53 +0000140 }
141
Guido van Rossumb6775db1994-08-01 11:34:53 +0000142#else /* !WITH_THREAD */
Guido van Rossumff4949e1992-08-05 19:58:53 +0000143
Guido van Rossumcaa63801995-01-12 11:45:45 +0000144#define Py_BEGIN_ALLOW_THREADS {
145#define Py_BLOCK_THREADS
146#define Py_UNBLOCK_THREADS
147#define Py_END_ALLOW_THREADS }
Guido van Rossumff4949e1992-08-05 19:58:53 +0000148
Guido van Rossumb6775db1994-08-01 11:34:53 +0000149#endif /* !WITH_THREAD */
Guido van Rossuma3309961993-07-28 09:05:47 +0000150
Mark Hammond91a681d2002-08-12 07:21:58 +0000151PyAPI_FUNC(int) _PyEval_SliceIndex(PyObject *, int *);
Guido van Rossum7c36ada2000-05-08 14:04:54 +0000152
153
Guido van Rossuma3309961993-07-28 09:05:47 +0000154#ifdef __cplusplus
155}
156#endif
157#endif /* !Py_CEVAL_H */