ceval.h 8.2 KB

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