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.

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