Skip to content

gh-93627: Align Python implementation of pickle with C implementation of pickle #103035

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 45 commits into from
Sep 10, 2023

Conversation

eendebakpt
Copy link
Contributor

@eendebakpt eendebakpt commented Mar 25, 2023

In this PR we align the python and C implementation of pickle module. In this PR we have chosen to modify all implementations to follow the c implementation of pickle conventions: if a method like __reduce_ex_ or __reduce__ is set to None, a TypeError is raised.

Since the behaviour of the python and c implemenations is not equal, any PR aliging the two cannot be fully backwards compatible. The impact seems limited:

  • No changes are required to the tests (although a few tests are added to verify the new behaviour)
  • The changes impact cases that seem rare (e.g. setting the __reduce_ex__ attribute of a class to None, instead of creating a method)

Notes:

  • Changes to align the copy module have been removed from the PR.

@erlend-aasland erlend-aasland marked this pull request as draft March 28, 2023 08:15
@erlend-aasland erlend-aasland changed the title Draft: gh-93627: Align python implementation of pickle with c implementation of pickle gh-93627: Align Python implementation of pickle with C implementation of pickle Mar 28, 2023
@eendebakpt eendebakpt marked this pull request as ready for review April 2, 2023 19:33
@eendebakpt
Copy link
Contributor Author

@serhiy-storchaka Will you be able to review the PR?

@eendebakpt eendebakpt requested a review from Eclips4 April 27, 2023 19:41
Copy link
Member

@Eclips4 Eclips4 left a comment

Choose a reason for hiding this comment

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

LGTM

@JelleZijlstra JelleZijlstra self-requested a review April 28, 2023 13:21
@CAM-Gerlach CAM-Gerlach added the stdlib Python modules in the Lib dir label Apr 28, 2023
def test_pickle_invalid_reduction_method(self):
c = C_None_reduce_ex()
with self.assertRaises(TypeError):
pickle.dumps(c)
Copy link
Member

Choose a reason for hiding this comment

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

What implementation does it test?

The tests in this module is organized in special way. There are abstract classes which test with the specified implementation specified by class attributes, and there are concrete subclasses which specialize tests for Python and C implementations. It would be better to rewrite your tests in conformance with existing style.

Copy link
Contributor Author

@eendebakpt eendebakpt Aug 28, 2023

Choose a reason for hiding this comment

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

@serhiy-storchaka I moved the tests to be part of AbstractPickleTests, AbstractPicklerUnpicklerObjectTests and AbstractDispatchTableTests. These are included in concrete tests both inside and outside the has_c_implementation section of test_pickle.py.

@serhiy-storchaka serhiy-storchaka self-requested a review September 10, 2023 06:47
Copy link
Member

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

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

LGTM.

@serhiy-storchaka serhiy-storchaka merged commit 85a5d3d into python:main Sep 10, 2023
@serhiy-storchaka
Copy link
Member

Thank you for your contribution @eendebakpt. Do you mind to align also the copy module with the pickle module? It is more complex and dangerous change, because we may need to change the order of some operations which is different in copy and pickle modules.

@eendebakpt
Copy link
Contributor Author

@serhiy-storchaka If aligning the copy of pickle module is still blocking #91610 I will have a look at the alignment. However, Jelle Zijlstra remarked in #103035 (comment) that the alignment might break some existing code, so we reverted these changes for this PR. @JelleZijlstra was your remark only about the __copy__ or also about the None/NoValue for the other attributes (e.g. __reduce_ex__, __reduce__)?

@eendebakpt
Copy link
Contributor Author

@serhiy-storchaka @JelleZijlstra I created #109498 to get a feeling for the impact of aligning the behaviour of the copy module with the pickle module. Maybe more needs to be aligned, but the PR seems quite straightforward so far.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants