ceval.h 7.4 KB

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