I'm stuck with some rust issues using pyo3 I could...
# development
c
I'm stuck with some rust issues using pyo3 I could use some help with, if anyone has a bone to trough I'd be happy to bite 🙂 My problem posted on SO: https://stackoverflow.com/questions/78522334/how-do-i-call-the-equiv-of-object-getattribute-self-name-using-pyo3
b
You probably need to load
object
and actually call
__getattribute
on it (and not
self
?)
c
load
object
? (I get your idea, but I've no clue how to do that.. 😎 )
just found a seemingly working thing going, but it feels very icky..
b
let obj = py.import("builtins")?.get("object")?;
c
Copy code
fn __getattribute__(
        self_: &PyCell<Self>,
        name: String,
        py: Python,
    ) -> PyResult<*mut ffi::PyObject> {
        if name == "default" {
            if let Some(_default) = &self_.extract::<Self>()?.default {
                //if let Some(_default) = &self.default {
                // panic!("in get default, with default!")
            }
            // panic!("in get default, without default!")
        }

        unsafe {
            let slf = self_.borrow_mut().into_ptr();
            let attr = name.into_py(py).into_ptr();
            let res = ffi::PyObject_GenericGetAttr(slf, attr);
            if res.is_null() {
                Err(PyErr::fetch(py))
            } else {
                Ok(res)
            }
        }
    }
b
Yeah I wouldn't do the unsafe thing, lol
ðŸĪŠ 1
c
that's copied from the pyo3 stuff... 😅
b
Technically you shouldn't call
object
, you should call
suoer()
, but idk how much that matters here
c
I've done the super route... but for some reason (could be a pyo3 bug) that lands me in recursion hell
iirc.. that or segfault.. think the super one was hitting the recursion limit..
b
Yeah if you aren't inheriting from anything, I think assuming object is ok
c
I've sent feelers out in the pyo3 discord too... see if I get any replies there..
b
The biiltins route is safer, but also slower
c
I'm not, but the instance using this is a subclass... so I imagine the super one gives me my class, rather than object.
yea, don't want this to be slow 😛
(working on the
Field
base class..)
b
Oh, maybe
__bases___
c
I can't access
__bases__
from
__getattribute__
I don't think... ðŸĪ”
anywho.. for the unsafe version, I've got this PR: https://github.com/pantsbuild/pants/pull/20949
b
You should be able to, dunder attributes bypass normal attribute lookup semantics
Note This method may still be bypassed when looking up special methods as the result of implicit invocation via language syntax or built-in functions. See Special method lookup.
(anywho...)
c
oohh... 👀