8000 bpo-44154: optimize Fraction pickling by skirpichev · Pull Request #26186 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-44154: optimize Fraction pickling #26186

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 1 commit into from
May 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
bpo-44154: optimize Fraction pickling
  • Loading branch information
skirpichev committed May 17, 2021
commit 94e21e762e447153515549005589e4be6096e6eb
2 changes: 1 addition & 1 deletion Lib/fractions.py
Original file line number Diff line number Diff line change
Expand Up @@ -735,7 +735,7 @@ def __bool__(a):
# support for pickling, copy, and deepcopy

def __reduce__(self):
return (self.__class__, (str(self),))
return (self.__class__, (self._numerator, self._denominator))

def __copy__(self):
if type(self) == Fraction:
Expand Down
4 changes: 3 additions & 1 deletion Lib/test/test_fractions.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import sys
import unittest
from copy import copy, deepcopy
import pickle
from pickle import dumps, loads
F = fractions.Fraction

Expand Down Expand Up @@ -691,7 +692,8 @@ def testApproximateCos1(self):
def test_copy_deepcopy_pickle(self):
r = F(13, 7)
dr = DummyFraction(13, 7)
self.assertEqual(r, loads(dumps(r)))
for proto in range(0, pickle.HIGHEST_PROTOCOL + 1):
self.assertEqual(r, loads(dumps(r, proto)))
self.assertEqual(id(r), id(copy(r)))
self.assertEqual(id(r), id(deepcopy(r)))
self.assertNotEqual(id(dr), id(copy(dr)))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Optimize :class:`fractions.Fraction` pickling for large components.
0