/python3-sys/src/methodobject.rs

https://github.com/dgrunwald/rust-cpython · Rust · 123 lines · 94 code · 20 blank · 9 comment · 3 complexity · 8bd09a6466d751365d5e45c62c5b6d20 MD5 · raw file

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