funcobject.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /* Function object interface */
  2. #ifndef Py_LIMITED_API
  3. #ifndef Py_FUNCOBJECT_H
  4. #define Py_FUNCOBJECT_H
  5. #ifdef __cplusplus
  6. extern "C" {
  7. #endif
  8. #define COMMON_FIELDS(PREFIX) \
  9. PyObject *PREFIX ## globals; \
  10. PyObject *PREFIX ## builtins; \
  11. PyObject *PREFIX ## name; \
  12. PyObject *PREFIX ## qualname; \
  13. PyObject *PREFIX ## code; /* A code object, the __code__ attribute */ \
  14. PyObject *PREFIX ## defaults; /* NULL or a tuple */ \
  15. PyObject *PREFIX ## kwdefaults; /* NULL or a dict */ \
  16. PyObject *PREFIX ## closure; /* NULL or a tuple of cell objects */
  17. typedef struct {
  18. COMMON_FIELDS(fc_)
  19. } PyFrameConstructor;
  20. /* Function objects and code objects should not be confused with each other:
  21. *
  22. * Function objects are created by the execution of the 'def' statement.
  23. * They reference a code object in their __code__ attribute, which is a
  24. * purely syntactic object, i.e. nothing more than a compiled version of some
  25. * source code lines. There is one code object per source code "fragment",
  26. * but each code object can be referenced by zero or many function objects
  27. * depending only on how many times the 'def' statement in the source was
  28. * executed so far.
  29. */
  30. typedef struct {
  31. PyObject_HEAD
  32. COMMON_FIELDS(func_)
  33. PyObject *func_doc; /* The __doc__ attribute, can be anything */
  34. PyObject *func_dict; /* The __dict__ attribute, a dict or NULL */
  35. PyObject *func_weakreflist; /* List of weak references */
  36. PyObject *func_module; /* The __module__ attribute, can be anything */
  37. PyObject *func_annotations; /* Annotations, a dict or NULL */
  38. PyObject *func_typeparams; /* Tuple of active type variables or NULL */
  39. vectorcallfunc vectorcall;
  40. /* Version number for use by specializer.
  41. * Can set to non-zero when we want to specialize.
  42. * Will be set to zero if any of these change:
  43. * defaults
  44. * kwdefaults (only if the object changes, not the contents of the dict)
  45. * code
  46. * annotations
  47. * vectorcall function pointer */
  48. uint32_t func_version;
  49. /* Invariant:
  50. * func_closure contains the bindings for func_code->co_freevars, so
  51. * PyTuple_Size(func_closure) == PyCode_GetNumFree(func_code)
  52. * (func_closure may be NULL if PyCode_GetNumFree(func_code) == 0).
  53. */
  54. } PyFunctionObject;
  55. PyAPI_DATA(PyTypeObject) PyFunction_Type;
  56. #define PyFunction_Check(op) Py_IS_TYPE((op), &PyFunction_Type)
  57. PyAPI_FUNC(PyObject *) PyFunction_New(PyObject *, PyObject *);
  58. PyAPI_FUNC(PyObject *) PyFunction_NewWithQualName(PyObject *, PyObject *, PyObject *);
  59. PyAPI_FUNC(PyObject *) PyFunction_GetCode(PyObject *);
  60. PyAPI_FUNC(PyObject *) PyFunction_GetGlobals(PyObject *);
  61. PyAPI_FUNC(PyObject *) PyFunction_GetModule(PyObject *);
  62. PyAPI_FUNC(PyObject *) PyFunction_GetDefaults(PyObject *);
  63. PyAPI_FUNC(int) PyFunction_SetDefaults(PyObject *, PyObject *);
  64. PyAPI_FUNC(void) PyFunction_SetVectorcall(PyFunctionObject *, vectorcallfunc);
  65. PyAPI_FUNC(PyObject *) PyFunction_GetKwDefaults(PyObject *);
  66. PyAPI_FUNC(int) PyFunction_SetKwDefaults(PyObject *, PyObject *);
  67. PyAPI_FUNC(PyObject *) PyFunction_GetClosure(PyObject *);
  68. PyAPI_FUNC(int) PyFunction_SetClosure(PyObject *, PyObject *);
  69. PyAPI_FUNC(PyObject *) PyFunction_GetAnnotations(PyObject *);
  70. PyAPI_FUNC(int) PyFunction_SetAnnotations(PyObject *, PyObject *);
  71. PyAPI_FUNC(PyObject *) _PyFunction_Vectorcall(
  72. PyObject *func,
  73. PyObject *const *stack,
  74. size_t nargsf,
  75. PyObject *kwnames);
  76. #define _PyFunction_CAST(func) \
  77. (assert(PyFunction_Check(func)), _Py_CAST(PyFunctionObject*, func))
  78. /* Static inline functions for direct access to these values.
  79. Type checks are *not* done, so use with care. */
  80. static inline PyObject* PyFunction_GET_CODE(PyObject *func) {
  81. return _PyFunction_CAST(func)->func_code;
  82. }
  83. #define PyFunction_GET_CODE(func) PyFunction_GET_CODE(_PyObject_CAST(func))
  84. static inline PyObject* PyFunction_GET_GLOBALS(PyObject *func) {
  85. return _PyFunction_CAST(func)->func_globals;
  86. }
  87. #define PyFunction_GET_GLOBALS(func) PyFunction_GET_GLOBALS(_PyObject_CAST(func))
  88. static inline PyObject* PyFunction_GET_MODULE(PyObject *func) {
  89. return _PyFunction_CAST(func)->func_module;
  90. }
  91. #define PyFunction_GET_MODULE(func) PyFunction_GET_MODULE(_PyObject_CAST(func))
  92. static inline PyObject* PyFunction_GET_DEFAULTS(PyObject *func) {
  93. return _PyFunction_CAST(func)->func_defaults;
  94. }
  95. #define PyFunction_GET_DEFAULTS(func) PyFunction_GET_DEFAULTS(_PyObject_CAST(func))
  96. static inline PyObject* PyFunction_GET_KW_DEFAULTS(PyObject *func) {
  97. return _PyFunction_CAST(func)->func_kwdefaults;
  98. }
  99. #define PyFunction_GET_KW_DEFAULTS(func) PyFunction_GET_KW_DEFAULTS(_PyObject_CAST(func))
  100. static inline PyObject* PyFunction_GET_CLOSURE(PyObject *func) {
  101. return _PyFunction_CAST(func)->func_closure;
  102. }
  103. #define PyFunction_GET_CLOSURE(func) PyFunction_GET_CLOSURE(_PyObject_CAST(func))
  104. static inline PyObject* PyFunction_GET_ANNOTATIONS(PyObject *func) {
  105. return _PyFunction_CAST(func)->func_annotations;
  106. }
  107. #define PyFunction_GET_ANNOTATIONS(func) PyFunction_GET_ANNOTATIONS(_PyObject_CAST(func))
  108. /* The classmethod and staticmethod types lives here, too */
  109. PyAPI_DATA(PyTypeObject) PyClassMethod_Type;
  110. PyAPI_DATA(PyTypeObject) PyStaticMethod_Type;
  111. PyAPI_FUNC(PyObject *) PyClassMethod_New(PyObject *);
  112. PyAPI_FUNC(PyObject *) PyStaticMethod_New(PyObject *);
  113. #define PY_FOREACH_FUNC_EVENT(V) \
  114. V(CREATE) \
  115. V(DESTROY) \
  116. V(MODIFY_CODE) \
  117. V(MODIFY_DEFAULTS) \
  118. V(MODIFY_KWDEFAULTS)
  119. typedef enum {
  120. #define PY_DEF_EVENT(EVENT) PyFunction_EVENT_##EVENT,
  121. PY_FOREACH_FUNC_EVENT(PY_DEF_EVENT)
  122. #undef PY_DEF_EVENT
  123. } PyFunction_WatchEvent;
  124. /*
  125. * A callback that is invoked for different events in a function's lifecycle.
  126. *
  127. * The callback is invoked with a borrowed reference to func, after it is
  128. * created and before it is modified or destroyed. The callback should not
  129. * modify func.
  130. *
  131. * When a function's code object, defaults, or kwdefaults are modified the
  132. * callback will be invoked with the respective event and new_value will
  133. * contain a borrowed reference to the new value that is about to be stored in
  134. * the function. Otherwise the third argument is NULL.
  135. *
  136. * If the callback returns with an exception set, it must return -1. Otherwise
  137. * it should return 0.
  138. */
  139. typedef int (*PyFunction_WatchCallback)(
  140. PyFunction_WatchEvent event,
  141. PyFunctionObject *func,
  142. PyObject *new_value);
  143. /*
  144. * Register a per-interpreter callback that will be invoked for function lifecycle
  145. * events.
  146. *
  147. * Returns a handle that may be passed to PyFunction_ClearWatcher on success,
  148. * or -1 and sets an error if no more handles are available.
  149. */
  150. PyAPI_FUNC(int) PyFunction_AddWatcher(PyFunction_WatchCallback callback);
  151. /*
  152. * Clear the watcher associated with the watcher_id handle.
  153. *
  154. * Returns 0 on success or -1 if no watcher exists for the supplied id.
  155. */
  156. PyAPI_FUNC(int) PyFunction_ClearWatcher(int watcher_id);
  157. #ifdef __cplusplus
  158. }
  159. #endif
  160. #endif /* !Py_FUNCOBJECT_H */
  161. #endif /* Py_LIMITED_API */