Trait that contains methods
| 31 | |
| 32 | /// Trait that contains methods |
| 33 | pub trait ObjectProtocol: PythonObject { |
| 34 | /// Determines whether this object has the given attribute. |
| 35 | /// This is equivalent to the Python expression 'hasattr(self, attr_name)'. |
| 36 | #[inline] |
| 37 | fn hasattr<N>(&self, py: Python, attr_name: N) -> PyResult<bool> |
| 38 | where |
| 39 | N: ToPyObject, |
| 40 | { |
| 41 | attr_name.with_borrowed_ptr(py, |attr_name| unsafe { |
| 42 | Ok(ffi::PyObject_HasAttr(self.as_ptr(), attr_name) != 0) |
| 43 | }) |
| 44 | } |
| 45 | |
| 46 | /// Retrieves an attribute value. |
| 47 | /// This is equivalent to the Python expression 'self.attr_name'. |
| 48 | #[inline] |
| 49 | fn getattr<N>(&self, py: Python, attr_name: N) -> PyResult<PyObject> |
| 50 | where |
| 51 | N: ToPyObject, |
| 52 | { |
| 53 | attr_name.with_borrowed_ptr(py, |attr_name| unsafe { |
| 54 | err::result_from_owned_ptr(py, ffi::PyObject_GetAttr(self.as_ptr(), attr_name)) |
| 55 | }) |
| 56 | } |
| 57 | |
| 58 | /// Sets an attribute value. |
| 59 | /// This is equivalent to the Python expression 'self.attr_name = value'. |
| 60 | #[inline] |
| 61 | fn setattr<N, V>(&self, py: Python, attr_name: N, value: V) -> PyResult<()> |
| 62 | where |
| 63 | N: ToPyObject, |
| 64 | V: ToPyObject, |
| 65 | { |
| 66 | attr_name.with_borrowed_ptr(py, move |attr_name| { |
| 67 | value.with_borrowed_ptr(py, |value| unsafe { |
| 68 | err::error_on_minusone(py, ffi::PyObject_SetAttr(self.as_ptr(), attr_name, value)) |
| 69 | }) |
| 70 | }) |
| 71 | } |
| 72 | |
| 73 | /// Deletes an attribute. |
| 74 | /// This is equivalent to the Python expression 'del self.attr_name'. |
| 75 | #[inline] |
| 76 | fn delattr<N>(&self, py: Python, attr_name: N) -> PyResult<()> |
| 77 | where |
| 78 | N: ToPyObject, |
| 79 | { |
| 80 | attr_name.with_borrowed_ptr(py, |attr_name| unsafe { |
| 81 | err::error_on_minusone(py, ffi::PyObject_DelAttr(self.as_ptr(), attr_name)) |
| 82 | }) |
| 83 | } |
| 84 | |
| 85 | /// Compares two Python objects. |
| 86 | /// |
| 87 | /// On Python 2, this is equivalent to the Python expression 'cmp(self, other)'. |
| 88 | /// |
| 89 | /// On Python 3, this is equivalent to: |
| 90 | /// ```ignore |
no outgoing calls
no test coverage detected