-
Notifications
You must be signed in to change notification settings - Fork 782
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
Allow #[new]
to return existing objects
#2384
Comments
This is an important feature for me, either supporting this or having full blown support for PEP 435 enums would be great. I specifically need full emulation/support for an IntFlag-like type. |
@kszlim I agree with you. While I think this will not make it into the 0.18 release, I would very much like to address this soon. |
Do you know if fully supporting an IntFlag like type is possible from pyo3? It seems like it'd require allowing returning references from rust into python or multiple ownership of the same pointer, ideally IntFlags would be lazily created and be singletons and have the |
I think the problem really is limited to #[pyclass]
pub struct Color { .. }
#[pyfunction]
pub fn red(py: Python) -> Py<Color> {
static RED: GILOnceCell<Py<Color>> = GILOnceCell:new();
RED.get_or_init(py, || Color { .. }).clone()
} should work. (I think that a (The problem with |
@adamreichold does that Color.red() is Color.red() I'm under the impression that the clone will make that not true. |
Cloning a
Sounds like |
Indeed. Thank you! @kszlim So you better end up using |
late reply, but it's my understanding that the id produced won't be identical, ie, so even if the underlying object is the same, since it's another pointer to that same object, the |
No - "another pointer to the same object" means the pointer must point to the same object, i.e. the address/ |
ah, thanks you're right, i just tested it. Guess I just need this issue to close for my code to work, thanks! |
Is a potential solution here:
|
If ^^ sounds plausible, I'm happy to take a pass at working up a PR for it. |
Sorry for the slow reply. The above sounds sort of on the lines, though I was hoping it might be simpler to just allow any |
Hmm, how would that work -- would the proc-macro have to recognize the
different return type syntactically.
…On Sun, Jul 2, 2023 at 11:04 AM David Hewitt ***@***.***> wrote:
Sorry for the slow reply. The above sounds sort of on the lines, though I
was hoping it might be simpler to just allow any T: IntoPy<PyObject> be a
valid return type from #[new]. I'm trying to dabble around with that idea
occasionally though haven't quite proven it works.
—
Reply to this email directly, view it on GitHub
<#2384 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAAAGBF3C3AZ6AKHWTM4K33XOGEWRANCNFSM5WD45JEA>
.
You are receiving this because you commented.Message ID:
***@***.***>
--
All that is necessary for evil to succeed is for good people to do nothing.
|
I don't think it'd be necessary to solve the return type syntactically, my current thinking is that it should be possible to use autoref specialization somewhere in the macro implementation to handle the different code paths. Just needs some playing around to see if it can work... |
I don't think it's possible for
#[new]
in PyO3 to return an existing object (at the moment), though it really should be.Originally posted by @davidhewitt in #2382 (comment)
The text was updated successfully, but these errors were encountered: