MCPcopy Create free account
hub / github.com/PyO3/pyo3 / test_pyfunction_is_not_static

Function test_pyfunction_is_not_static

tests/test_pyfunction.rs:812–848  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

810#[test]
811#[cfg(any(not(PyPy), Py_3_12))]
812fn test_pyfunction_is_not_static() {
813 use pyo3::ffi;
814
815 // one function for each calling convention
816 #[pyfunction]
817 fn no_args() {}
818
819 #[pyfunction]
820 fn positional(_arg: i32) {}
821
822 #[pyfunction]
823 #[pyo3(signature = (**_kwargs))]
824 fn keywords(_kwargs: Option<&Bound<'_, types::PyDict>>) {}
825
826 Python::attach(|py| {
827 let module = PyModule::new(py, "test_module").unwrap();
828 for function in [
829 wrap_pyfunction!(no_args, &module).unwrap(),
830 wrap_pyfunction!(positional, &module).unwrap(),
831 wrap_pyfunction!(keywords, &module).unwrap(),
832 ] {
833 let name = function.getattr("__name__").unwrap();
834 // `METH_STATIC` and `METH_CLASS` are only for methods of a class
835 let flags = unsafe { ffi::PyCFunction_GetFlags(function.as_ptr()) };
836 assert_eq!(
837 flags & (ffi::METH_STATIC | ffi::METH_CLASS),
838 0,
839 "`{name}` has flags {flags:#x}"
840 );
841 // `METH_STATIC` would make `__self__` `None`
842 assert!(
843 function.getattr("__self__").unwrap().is(&module),
844 "`{name}.__self__` is not the module"
845 );
846 }
847 });
848}

Callers

nothing calls this directly

Calls 2

getattrMethod · 0.45
as_ptrMethod · 0.45

Tested by

no test coverage detected