methodobject.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #ifndef Py_CPYTHON_METHODOBJECT_H
  2. # error "this header file must not be included directly"
  3. #endif
  4. // PyCFunctionObject structure
  5. typedef struct {
  6. PyObject_HEAD
  7. PyMethodDef *m_ml; /* Description of the C function to call */
  8. PyObject *m_self; /* Passed as 'self' arg to the C func, can be NULL */
  9. PyObject *m_module; /* The __module__ attribute, can be anything */
  10. PyObject *m_weakreflist; /* List of weak references */
  11. vectorcallfunc vectorcall;
  12. } PyCFunctionObject;
  13. #define _PyCFunctionObject_CAST(func) \
  14. (assert(PyCFunction_Check(func)), \
  15. _Py_CAST(PyCFunctionObject*, (func)))
  16. // PyCMethodObject structure
  17. typedef struct {
  18. PyCFunctionObject func;
  19. PyTypeObject *mm_class; /* Class that defines this method */
  20. } PyCMethodObject;
  21. #define _PyCMethodObject_CAST(func) \
  22. (assert(PyCMethod_Check(func)), \
  23. _Py_CAST(PyCMethodObject*, (func)))
  24. PyAPI_DATA(PyTypeObject) PyCMethod_Type;
  25. #define PyCMethod_CheckExact(op) Py_IS_TYPE((op), &PyCMethod_Type)
  26. #define PyCMethod_Check(op) PyObject_TypeCheck((op), &PyCMethod_Type)
  27. /* Static inline functions for direct access to these values.
  28. Type checks are *not* done, so use with care. */
  29. static inline PyCFunction PyCFunction_GET_FUNCTION(PyObject *func) {
  30. return _PyCFunctionObject_CAST(func)->m_ml->ml_meth;
  31. }
  32. #define PyCFunction_GET_FUNCTION(func) PyCFunction_GET_FUNCTION(_PyObject_CAST(func))
  33. static inline PyObject* PyCFunction_GET_SELF(PyObject *func_obj) {
  34. PyCFunctionObject *func = _PyCFunctionObject_CAST(func_obj);
  35. if (func->m_ml->ml_flags & METH_STATIC) {
  36. return _Py_NULL;
  37. }
  38. return func->m_self;
  39. }
  40. #define PyCFunction_GET_SELF(func) PyCFunction_GET_SELF(_PyObject_CAST(func))
  41. static inline int PyCFunction_GET_FLAGS(PyObject *func) {
  42. return _PyCFunctionObject_CAST(func)->m_ml->ml_flags;
  43. }
  44. #define PyCFunction_GET_FLAGS(func) PyCFunction_GET_FLAGS(_PyObject_CAST(func))
  45. static inline PyTypeObject* PyCFunction_GET_CLASS(PyObject *func_obj) {
  46. PyCFunctionObject *func = _PyCFunctionObject_CAST(func_obj);
  47. if (func->m_ml->ml_flags & METH_METHOD) {
  48. return _PyCMethodObject_CAST(func)->mm_class;
  49. }
  50. return _Py_NULL;
  51. }
  52. #define PyCFunction_GET_CLASS(func) PyCFunction_GET_CLASS(_PyObject_CAST(func))