Is there a way (without eval) to access object.__setattr__ #2321
Unanswered
samuelcolvin
asked this question in
Questions
Replies: 2 comments 6 replies
-
I've found a work around, what I needed was pub fn force_setattr<N, V>(obj: &PyObject, py: Python<'_>, attr_name: N, value: V) -> PyResult<()>
where
N: ToPyObject,
V: ToPyObject,
{
attr_name.with_borrowed_ptr(py, move |attr_name| {
value.with_borrowed_ptr(py, |value| unsafe {
error_on_minusone(py, ffi::PyObject_GenericSetAttr(obj.as_ptr(), attr_name, value))
})
})
}
// Defined here as it's not exported by pyo3
#[inline]
fn error_on_minusone(py: Python<'_>, result: c_int) -> PyResult<()> {
if result != -1 {
Ok(())
} else {
Err(PyErr::fetch(py))
}
} |
Beta Was this translation helpful? Give feedback.
6 replies
-
You can get it from let setattr = PyModule::import(py, "builtins")?
.getattr("object")?
.getattr("__setattr__")?; |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I use
object.__setattr__
to set attributes while bypassing a custom__setattr__
method, see here.My Current logic for doing this in rust is:
I'm wondering if there is a cleaner and more performant way of getting access to
object.__setattr__
or a similar method?The
setattr
method doesn't work as it doesn't bypass the setattr method on the class/instance in question.Beta Was this translation helpful? Give feedback.
All reactions