You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

230 lines
8.1 KiB

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