Skip to content
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

split PyCell and PyClassObject concepts #3917

Merged
merged 5 commits into from
Mar 3, 2024

Conversation

davidhewitt
Copy link
Member

Motivated by #3915 (comment)

I was staring at that problem and it felt like a step in the right direction was to carry out the proposed refactoring to split the PyCell GIL Ref and PyClassObject class object layout concepts. That should make it easier for us to get rid of PyCell later by decoupling PyO3's internal implementation from that type.

This probably wants a little cleaning up and I'll rebase it after #3915, I just wanted to push it now so that we don't forget this for 0.21.

(The first commit extends a test case to make it fail on main but pass after the cleaning up here.)

Copy link
Member Author

@davidhewitt davidhewitt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I tidied this up and pushed a third commit where I tried to get rid of a bit more code which was referencing PyCell.

@Icxolu, want to see what you think of this PR? I guess #3916 will conflict with this, as you note, hopefully in general though this PR will just remove a lot of the #[allow(deprcated)] calls which otherwise needed to be added in #3916.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are probably some module reorganisations worth considering, like why this is here rather than under the normal impl_ submodule.

But for now I wanted to focus on the actual diff and just relocated code which should be "internal" from src/pycell.rs into here.

<<T::BaseType as PyClassImpl>::PyClassMutability as GetBorrowChecker<T::BaseType>>::borrow_checker(&cell.ob_base)
}
}

/// Base layout of PyClassObject.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is all basically lifted from src/pycell.rs and renamed.

@davidhewitt davidhewitt marked this pull request as ready for review March 1, 2024 23:51
Copy link
Contributor

@Icxolu Icxolu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This definitely feels right to do. It makes a clean cut between internals and userfacing types and makes deprecating PyCell much nicer. After going through this I think I've also spotted the problem that you mentioned in #3915, good catch.

I found a few style nits, otherwise this looks good to me (but haven't really dealt with this low level CPython interface, so I can't really verify these interops).

I think it makes sense to wait with #3916 until after this, otherwise we would add (and then remove) alot of unnessesary #[allow(deprecated)]

mem::forget(owned.try_borrow()?);
Ok(RefGuard(owned.clone().unbind()))
let bound = obj.downcast::<T>()?;
bound.get_class_object().borrow_checker().try_borrow()?;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see the probem now, bound.try_borrow currently clones the Bound, so we leak that ref count increment. I guess this could be changed back, once we can store a &Bound in PyRef(mut), right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes exactly, and potentially we could change it back later. I wonder whether a better follow-up would be to instead change the way this borrow checker works to return a borrow "token" which then needs to be handed back to the borrow checker later (or otherwise panic / abort on drop, maybe just in debug mode). Then what I've written here would be robust and the justification to change back to forget a PyRef seems weaker.

src/instance.rs Outdated Show resolved Hide resolved
src/instance.rs Show resolved Hide resolved
src/pycell/impl_.rs Outdated Show resolved Hide resolved
src/pyclass_init.rs Outdated Show resolved Hide resolved
@davidhewitt
Copy link
Member Author

Thanks for the review! I've tidied up for all those points, and to unblock you with #3916 I will merge this now 👍

@davidhewitt davidhewitt added this pull request to the merge queue Mar 3, 2024
Merged via the queue into PyO3:main with commit 2e56f65 Mar 3, 2024
41 of 42 checks passed
@davidhewitt davidhewitt deleted the pyclass-object branch March 3, 2024 07:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CI-skip-changelog Skip checking changelog entry
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants