Skip to content

bpo-35900: Add a state_setter arg to save_reduce #12588

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

Merged
merged 29 commits into from
May 8, 2019

Conversation

pierreglaser
Copy link
Contributor

@pierreglaser pierreglaser commented Mar 27, 2019

Second PR of bpo-35900.

What

The aim of this PR is to allow users to specify custom state setting methods for types they have no control on. This is done via adding a state_setter keyword argument in Pickler.save_reduce, expected to be a callable. This callable will be called at unpickling time with the following signature state_setter(obj, state, slotstate).

Why

A practical use case is the one of cloudpickle that provides extended serialization procedures, especially for functions and classes. In cloudpickle, functions and classes are reconstructed from scratch at unpickling time. As for other built-in types reconstructed from scratch (list, dicts...), circular-references are handled by initalizing a skeleton-object, and then updating it in a state-setting phase. This state-setting phase currently only relies on potential __getstate__ methods, or fallbacks to a default procedure, and this is simply not enough to properly reconstruct functions and classes, hence this proposal.

See cloudpipe/cloudpickle#253 for more details.

  • implementaion
  • unit-tests

Notes

dependencies

This PR comes alongside #12499 . Without it, leveraging #12499 changes won't work for functions and classes.

cross-protocol/version usage

An interrogation that remains is whether using this new API with a protocol < 5 should raise an error at pickling time. If not, unpickling will not succeed if the pickle string is loaded using a python < 3.8. This may be surprising for users as one can assume that the point of using low protocols is to ensure retro-compatibility with older python versions. On the other side, this API will probably be of interested for advanced user only, that would understand why using a new API with an old protocol. As of today, this is still an open question.

https://bugs.python.org/issue35900

Copy link
Contributor

@ogrisel ogrisel left a comment

Choose a reason for hiding this comment

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

Here is a batch of comments:

@pierreglaser
Copy link
Contributor Author

ping @pitrou

Copy link
Contributor

@ogrisel ogrisel left a comment

Choose a reason for hiding this comment

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

LGTM!

@pierreglaser
Copy link
Contributor Author

OK @pitrou @ncoghlan how does this look to you now?

@ncoghlan
Copy link
Contributor

I fixed a couple of small error message issues, and a typo in the news entry, and this otherwise looks OK to me.

However, I'll leave the final review to @pitrou :)

Copy link
Member

@pitrou pitrou left a comment

Choose a reason for hiding this comment

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

Looks mostly good to me. Just some nits.

@bedevere-bot
Copy link

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@pierreglaser
Copy link
Contributor Author

pierreglaser commented May 8, 2019

I have made the requested changes; please review again
The current test failures are about SSL certificates. Seems unrelated - I'll push an empty commit in a few hours.

@bedevere-bot
Copy link

Thanks for making the requested changes!

@pitrou: please review the changes made to this pull request.

Copy link
Member

@pitrou pitrou left a comment

Choose a reason for hiding this comment

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

Sorry, one issue I overlooked.

@bedevere-bot
Copy link

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@pitrou
Copy link
Member

pitrou commented May 8, 2019

I can reproduce the cert failures on git master, so they're unrelated.

@pitrou
Copy link
Member

pitrou commented May 8, 2019

The test failure was fixed on master, can you merge / rebase?

@pierreglaser pierreglaser force-pushed the give-a-state-setter-to-save-reduce branch from 1539f68 to 3f2fdb2 Compare May 8, 2019 18:22
@pitrou pitrou merged commit 65d98d0 into python:master May 8, 2019
@pierreglaser
Copy link
Contributor Author

Thanks a lot @pitrou!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants