-
Notifications
You must be signed in to change notification settings - Fork 784
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PyObject -> &'py PyAny #1041
Comments
Are you able to update to pyO3 This returns |
Thanks, is there a conversion that works for primitive types as well as |
You could use However I think a safe API should be trivial to add, so I'll add this for |
Thanks, this one-liner is working nicely. |
I don't know what you need precisely, but doesn't |
@kngwyu I think if you do that the lifetime isn't long enough to return from the function. (The reference is owned by the intermediary |
Indeed, |
Indeed, the API I'm considering is |
@davidhewitt |
I was thinking that |
So that is only for making things one-liner? |
It avoids an Or rather, it moves the unsafety out of user code and into |
As an alternative, how about: impl<'p> Python<'p> {
pub fn register<T: crate::IntoPy<PyObject>>(self, value: T) -> &'p PyAny {
unsafe { self.from_owned_ptr(value.into_py(self).into_ptr()) }
}
} ? @1tgr let obj: PyObject = EmptyClassWithNew {}.into_py(py);
let any = obj.as_ref(py); And this is the intended path to |
@kngwyu the lifetime on the reference returned by FYI, the context is a serde serializer that turns a Rust struct into a Python dict. The serializer returns |
Yeah I considered that too. I think the nice thing about it being on
Though |
@1tgr @davidhewitt |
@davidhewitt keen on having only one way to do something if possible to keep things simple |
I'm implementing a function within an API that's expected to return
&'py PyAny
. The function will serialize a Rust object to a Python object and return a reference to it, bound to the GIL lifetime ofPython<'py>
.It would be easy to implement a function that returns
PyObject
, and there are various types in PyO3 that will return&'py PySomething
, egPyBool::new
,PyDict::new
. However I don't see a route to expose Rust objects generically as&'py PyAny
.Conceptually I think I want to: allocate an object on the Python heap; move my instance
T
into it; add the new Python object to the auto-release pool; return a reference to the Python object.The closest I could see was:
PyObject::as_ref
(but that returns a reference bound to the lifetime of&self
, not to&'py
); or something involving*mut ffi::PyObject
and unsafePython::from_owned_ptr
.In case it's relevant, I'm working against PyO3 v0.8.5.
The text was updated successfully, but these errors were encountered: