ceval.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. #ifndef Py_CEVAL_H
  2. #define Py_CEVAL_H
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6. /* Interface to random parts in ceval.c */
  7. /* PyEval_CallObjectWithKeywords(), PyEval_CallObject(), PyEval_CallFunction
  8. * and PyEval_CallMethod are kept for backward compatibility: PyObject_Call(),
  9. * PyObject_CallFunction() and PyObject_CallMethod() are recommended to call
  10. * a callable object.
  11. */
  12. PyAPI_FUNC(PyObject *) PyEval_CallObjectWithKeywords(
  13. PyObject *callable,
  14. PyObject *args,
  15. PyObject *kwargs);
  16. /* Inline this */
  17. #define PyEval_CallObject(callable, arg) \
  18. PyEval_CallObjectWithKeywords(callable, arg, (PyObject *)NULL)
  19. PyAPI_FUNC(PyObject *) PyEval_CallFunction(PyObject *callable,
  20. const char *format, ...);
  21. PyAPI_FUNC(PyObject *) PyEval_CallMethod(PyObject *obj,
  22. const char *name,
  23. const char *format, ...);
  24. #ifndef Py_LIMITED_API
  25. PyAPI_FUNC(void) PyEval_SetProfile(Py_tracefunc, PyObject *);
  26. PyAPI_FUNC(void) PyEval_SetTrace(Py_tracefunc, PyObject *);
  27. PyAPI_FUNC(void) _PyEval_SetCoroutineOriginTrackingDepth(int new_depth);
  28. PyAPI_FUNC(int) _PyEval_GetCoroutineOriginTrackingDepth(void);
  29. PyAPI_FUNC(void) _PyEval_SetCoroutineWrapper(PyObject *);
  30. PyAPI_FUNC(PyObject *) _PyEval_GetCoroutineWrapper(void);
  31. PyAPI_FUNC(void) _PyEval_SetAsyncGenFirstiter(PyObject *);
  32. PyAPI_FUNC(PyObject *) _PyEval_GetAsyncGenFirstiter(void);
  33. PyAPI_FUNC(void) _PyEval_SetAsyncGenFinalizer(PyObject *);
  34. PyAPI_FUNC(PyObject *) _PyEval_GetAsyncGenFinalizer(void);
  35. #endif
  36. struct _frame; /* Avoid including frameobject.h */
  37. PyAPI_FUNC(PyObject *) PyEval_GetBuiltins(void);
  38. PyAPI_FUNC(PyObject *) PyEval_GetGlobals(void);
  39. PyAPI_FUNC(PyObject *) PyEval_GetLocals(void);
  40. PyAPI_FUNC(struct _frame *) PyEval_GetFrame(void);
  41. #ifndef Py_LIMITED_API
  42. /* Helper to look up a builtin object */
  43. PyAPI_FUNC(PyObject *) _PyEval_GetBuiltinId(_Py_Identifier *);
  44. /* Look at the current frame's (if any) code's co_flags, and turn on
  45. the corresponding compiler flags in cf->cf_flags. Return 1 if any
  46. flag was set, else return 0. */
  47. PyAPI_FUNC(int) PyEval_MergeCompilerFlags(PyCompilerFlags *cf);
  48. #endif
  49. PyAPI_FUNC(int) Py_AddPendingCall(int (*func)(void *), void *arg);
  50. PyAPI_FUNC(void) _PyEval_SignalReceived(void);
  51. PyAPI_FUNC(int) Py_MakePendingCalls(void);
  52. /* Protection against deeply nested recursive calls
  53. In Python 3.0, this protection has two levels:
  54. * normal anti-recursion protection is triggered when the recursion level
  55. exceeds the current recursion limit. It raises a RecursionError, and sets
  56. the "overflowed" flag in the thread state structure. This flag
  57. temporarily *disables* the normal protection; this allows cleanup code
  58. to potentially outgrow the recursion limit while processing the
  59. RecursionError.
  60. * "last chance" anti-recursion protection is triggered when the recursion
  61. level exceeds "current recursion limit + 50". By construction, this
  62. protection can only be triggered when the "overflowed" flag is set. It
  63. means the cleanup code has itself gone into an infinite loop, or the
  64. RecursionError has been mistakingly ignored. When this protection is
  65. triggered, the interpreter aborts with a Fatal Error.
  66. In addition, the "overflowed" flag is automatically reset when the
  67. recursion level drops below "current recursion limit - 50". This heuristic
  68. is meant to ensure that the normal anti-recursion protection doesn't get
  69. disabled too long.
  70. Please note: this scheme has its own limitations. See:
  71. http://mail.python.org/pipermail/python-dev/2008-August/082106.html
  72. for some observations.
  73. */
  74. PyAPI_FUNC(void) Py_SetRecursionLimit(int);
  75. PyAPI_FUNC(int) Py_GetRecursionLimit(void);
  76. #define Py_EnterRecursiveCall(where) \
  77. (_Py_MakeRecCheck(PyThreadState_GET()->recursion_depth) && \
  78. _Py_CheckRecursiveCall(where))
  79. #define Py_LeaveRecursiveCall() \
  80. do{ if(_Py_MakeEndRecCheck(PyThreadState_GET()->recursion_depth)) \
  81. PyThreadState_GET()->overflowed = 0; \
  82. } while(0)
  83. PyAPI_FUNC(int) _Py_CheckRecursiveCall(const char *where);
  84. /* Due to the macros in which it's used, _Py_CheckRecursionLimit is in
  85. the stable ABI. It should be removed therefrom when possible.
  86. */
  87. PyAPI_DATA(int) _Py_CheckRecursionLimit;
  88. #ifdef USE_STACKCHECK
  89. /* With USE_STACKCHECK, trigger stack checks in _Py_CheckRecursiveCall()
  90. on every 64th call to Py_EnterRecursiveCall.
  91. */
  92. # define _Py_MakeRecCheck(x) \
  93. (++(x) > _Py_CheckRecursionLimit || \
  94. ++(PyThreadState_GET()->stackcheck_counter) > 64)
  95. #else
  96. # define _Py_MakeRecCheck(x) (++(x) > _Py_CheckRecursionLimit)
  97. #endif
  98. /* Compute the "lower-water mark" for a recursion limit. When
  99. * Py_LeaveRecursiveCall() is called with a recursion depth below this mark,
  100. * the overflowed flag is reset to 0. */
  101. #define _Py_RecursionLimitLowerWaterMark(limit) \
  102. (((limit) > 200) \
  103. ? ((limit) - 50) \
  104. : (3 * ((limit) >> 2)))
  105. #define _Py_MakeEndRecCheck(x) \
  106. (--(x) < _Py_RecursionLimitLowerWaterMark(_Py_CheckRecursionLimit))
  107. #define Py_ALLOW_RECURSION \
  108. do { unsigned char _old = PyThreadState_GET()->recursion_critical;\
  109. PyThreadState_GET()->recursion_critical = 1;
  110. #define Py_END_ALLOW_RECURSION \
  111. PyThreadState_GET()->recursion_critical = _old; \
  112. } while(0);
  113. PyAPI_FUNC(const char *) PyEval_GetFuncName(PyObject *);
  114. PyAPI_FUNC(const char *) PyEval_GetFuncDesc(PyObject *);
  115. PyAPI_FUNC(PyObject *) PyEval_EvalFrame(struct _frame *);
  116. PyAPI_FUNC(PyObject *) PyEval_EvalFrameEx(struct _frame *f, int exc);
  117. #ifndef Py_LIMITED_API
  118. PyAPI_FUNC(PyObject *) _PyEval_EvalFrameDefault(struct _frame *f, int exc);
  119. #endif
  120. /* Interface for threads.
  121. A module that plans to do a blocking system call (or something else
  122. that lasts a long time and doesn't touch Python data) can allow other
  123. threads to run as follows:
  124. ...preparations here...
  125. Py_BEGIN_ALLOW_THREADS
  126. ...blocking system call here...
  127. Py_END_ALLOW_THREADS
  128. ...interpret result here...
  129. The Py_BEGIN_ALLOW_THREADS/Py_END_ALLOW_THREADS pair expands to a
  130. {}-surrounded block.
  131. To leave the block in the middle (e.g., with return), you must insert
  132. a line containing Py_BLOCK_THREADS before the return, e.g.
  133. if (...premature_exit...) {
  134. Py_BLOCK_THREADS
  135. PyErr_SetFromErrno(PyExc_OSError);
  136. return NULL;
  137. }
  138. An alternative is:
  139. Py_BLOCK_THREADS
  140. if (...premature_exit...) {
  141. PyErr_SetFromErrno(PyExc_OSError);
  142. return NULL;
  143. }
  144. Py_UNBLOCK_THREADS
  145. For convenience, that the value of 'errno' is restored across
  146. Py_END_ALLOW_THREADS and Py_BLOCK_THREADS.
  147. WARNING: NEVER NEST CALLS TO Py_BEGIN_ALLOW_THREADS AND
  148. Py_END_ALLOW_THREADS!!!
  149. The function PyEval_InitThreads() should be called only from
  150. init_thread() in "_threadmodule.c".
  151. Note that not yet all candidates have been converted to use this
  152. mechanism!
  153. */
  154. PyAPI_FUNC(PyThreadState *) PyEval_SaveThread(void);
  155. PyAPI_FUNC(void) PyEval_RestoreThread(PyThreadState *);
  156. PyAPI_FUNC(int) PyEval_ThreadsInitialized(void);
  157. PyAPI_FUNC(void) PyEval_InitThreads(void);
  158. #ifndef Py_LIMITED_API
  159. PyAPI_FUNC(void) _PyEval_FiniThreads(void);
  160. #endif /* !Py_LIMITED_API */
  161. PyAPI_FUNC(void) PyEval_AcquireLock(void) Py_DEPRECATED(3.2);
  162. PyAPI_FUNC(void) PyEval_ReleaseLock(void) /* Py_DEPRECATED(3.2) */;
  163. PyAPI_FUNC(void) PyEval_AcquireThread(PyThreadState *tstate);
  164. PyAPI_FUNC(void) PyEval_ReleaseThread(PyThreadState *tstate);
  165. PyAPI_FUNC(void) PyEval_ReInitThreads(void);
  166. #ifndef Py_LIMITED_API
  167. PyAPI_FUNC(void) _PyEval_SetSwitchInterval(unsigned long microseconds);
  168. PyAPI_FUNC(unsigned long) _PyEval_GetSwitchInterval(void);
  169. #endif
  170. #ifndef Py_LIMITED_API
  171. PyAPI_FUNC(Py_ssize_t) _PyEval_RequestCodeExtraIndex(freefunc);
  172. #endif
  173. #define Py_BEGIN_ALLOW_THREADS { \
  174. PyThreadState *_save; \
  175. _save = PyEval_SaveThread();
  176. #define Py_BLOCK_THREADS PyEval_RestoreThread(_save);
  177. #define Py_UNBLOCK_THREADS _save = PyEval_SaveThread();
  178. #define Py_END_ALLOW_THREADS PyEval_RestoreThread(_save); \
  179. }
  180. #ifndef Py_LIMITED_API
  181. PyAPI_FUNC(int) _PyEval_SliceIndex(PyObject *, Py_ssize_t *);
  182. PyAPI_FUNC(int) _PyEval_SliceIndexNotNone(PyObject *, Py_ssize_t *);
  183. PyAPI_FUNC(void) _PyEval_SignalAsyncExc(void);
  184. #endif
  185. /* Masks and values used by FORMAT_VALUE opcode. */
  186. #define FVC_MASK 0x3
  187. #define FVC_NONE 0x0
  188. #define FVC_STR 0x1
  189. #define FVC_REPR 0x2
  190. #define FVC_ASCII 0x3
  191. #define FVS_MASK 0x4
  192. #define FVS_HAVE_SPEC 0x4
  193. #ifdef __cplusplus
  194. }
  195. #endif
  196. #endif /* !Py_CEVAL_H */