/python27-sys/src/methodobject.rs

https://github.com/dgrunwald/rust-cpython · Rust · 121 lines · 82 code · 15 blank · 24 comment · 2 complexity · 8ce09d89d534d9d3792dcf75aa4783a9 MD5 · raw file

  1. use core::ptr;
  2. use libc::{c_char, c_int};
  3. use crate::object::{PyObject, PyTypeObject, Py_TYPE};
  4. #[cfg_attr(windows, link(name = "pythonXY"))]
  5. extern "C" {
  6. pub static mut PyCFunction_Type: PyTypeObject;
  7. }
  8. #[inline(always)]
  9. pub unsafe fn PyCFunction_Check(op: *mut PyObject) -> c_int {
  10. let u: *mut PyTypeObject = &mut PyCFunction_Type;
  11. (Py_TYPE(op) == u) as c_int
  12. }
  13. pub type PyCFunction =
  14. unsafe extern "C" fn(slf: *mut PyObject, args: *mut PyObject) -> *mut PyObject;
  15. pub type PyCFunctionWithKeywords = unsafe extern "C" fn(
  16. slf: *mut PyObject,
  17. args: *mut PyObject,
  18. kwds: *mut PyObject,
  19. ) -> *mut PyObject;
  20. pub type PyNoArgsFunction = unsafe extern "C" fn(slf: *mut PyObject) -> *mut PyObject;
  21. #[cfg_attr(windows, link(name = "pythonXY"))]
  22. extern "C" {
  23. pub fn PyCFunction_GetFunction(f: *mut PyObject) -> Option<PyCFunction>;
  24. pub fn PyCFunction_GetSelf(f: *mut PyObject) -> *mut PyObject;
  25. pub fn PyCFunction_GetFlags(f: *mut PyObject) -> c_int;
  26. pub fn PyCFunction_Call(
  27. f: *mut PyObject,
  28. args: *mut PyObject,
  29. kwds: *mut PyObject,
  30. ) -> *mut PyObject;
  31. }
  32. #[repr(C)]
  33. #[derive(Copy)]
  34. pub struct PyMethodDef {
  35. pub ml_name: *const c_char,
  36. pub ml_meth: Option<PyCFunction>,
  37. pub ml_flags: c_int,
  38. pub ml_doc: *const c_char,
  39. }
  40. impl Clone for PyMethodDef {
  41. #[inline]
  42. fn clone(&self) -> PyMethodDef {
  43. *self
  44. }
  45. }
  46. /* Flag passed to newmethodobject */
  47. pub const METH_OLDARGS: c_int = 0x0000;
  48. pub const METH_VARARGS: c_int = 0x0001;
  49. pub const METH_KEYWORDS: c_int = 0x0002;
  50. /* METH_NOARGS and METH_O must not be combined with the flags above. */
  51. pub const METH_NOARGS: c_int = 0x0004;
  52. pub const METH_O: c_int = 0x0008;
  53. /* METH_CLASS and METH_STATIC are a little different; these control
  54. the construction of methods for a class. These cannot be used for
  55. functions in modules. */
  56. pub const METH_CLASS: c_int = 0x0010;
  57. pub const METH_STATIC: c_int = 0x0020;
  58. /* METH_COEXIST allows a method to be entered eventhough a slot has
  59. already filled the entry. When defined, the flag allows a separate
  60. method, "__contains__" for example, to coexist with a defined
  61. slot like sq_contains. */
  62. pub const METH_COEXIST: c_int = 0x0040;
  63. #[repr(C)]
  64. #[derive(Copy, Clone)]
  65. pub struct PyMethodChain {
  66. pub methods: *mut PyMethodDef,
  67. pub link: *mut PyMethodChain,
  68. }
  69. /*
  70. #[repr(C)]
  71. #[derive(Copy)]
  72. struct PyCFunctionObject {
  73. #[cfg(py_sys_config="Py_TRACE_REFS")]
  74. pub _ob_next: *mut PyObject,
  75. #[cfg(py_sys_config="Py_TRACE_REFS")]
  76. pub _ob_prev: *mut PyObject,
  77. pub ob_refcnt: Py_ssize_t,
  78. pub ob_type: *mut PyTypeObject,
  79. pub m_ml: *mut PyMethodDef,
  80. pub m_self: *mut PyObject,
  81. pub m_module: *mut PyObject,
  82. }
  83. */
  84. #[cfg_attr(windows, link(name = "pythonXY"))]
  85. extern "C" {
  86. pub fn Py_FindMethod(
  87. methods: *mut PyMethodDef,
  88. slf: *mut PyObject,
  89. name: *const c_char,
  90. ) -> *mut PyObject;
  91. pub fn PyCFunction_NewEx(
  92. ml: *mut PyMethodDef,
  93. slf: *mut PyObject,
  94. module: *mut PyObject,
  95. ) -> *mut PyObject;
  96. pub fn Py_FindMethodInChain(
  97. chain: *mut PyMethodChain,
  98. slf: *mut PyObject,
  99. name: *const c_char,
  100. ) -> *mut PyObject;
  101. pub fn PyCFunction_ClearFreeList() -> c_int;
  102. }
  103. #[inline(always)]
  104. pub unsafe fn PyCFunction_New(ml: *mut PyMethodDef, slf: *mut PyObject) -> *mut PyObject {
  105. PyCFunction_NewEx(ml, slf, ptr::null_mut())
  106. }