mirror of
https://github.com/python/cpython.git
synced 2025-08-29 13:15:11 +00:00
bpo-44154: optimize Fraction pickling (GH-26186)
This commit is contained in:
parent
b3f65e819f
commit
b102dd598d
3 changed files with 5 additions and 2 deletions
|
@ -735,7 +735,7 @@ class Fraction(numbers.Rational):
|
||||||
# support for pickling, copy, and deepcopy
|
# support for pickling, copy, and deepcopy
|
||||||
|
|
||||||
def __reduce__(self):
|
def __reduce__(self):
|
||||||
return (self.__class__, (str(self),))
|
return (self.__class__, (self._numerator, self._denominator))
|
||||||
|
|
||||||
def __copy__(self):
|
def __copy__(self):
|
||||||
if type(self) == Fraction:
|
if type(self) == Fraction:
|
||||||
|
|
|
@ -10,6 +10,7 @@ import functools
|
||||||
import sys
|
import sys
|
||||||
import unittest
|
import unittest
|
||||||
from copy import copy, deepcopy
|
from copy import copy, deepcopy
|
||||||
|
import pickle
|
||||||
from pickle import dumps, loads
|
from pickle import dumps, loads
|
||||||
F = fractions.Fraction
|
F = fractions.Fraction
|
||||||
|
|
||||||
|
@ -691,7 +692,8 @@ class FractionTest(unittest.TestCase):
|
||||||
def test_copy_deepcopy_pickle(self):
|
def test_copy_deepcopy_pickle(self):
|
||||||
r = F(13, 7)
|
r = F(13, 7)
|
||||||
dr = DummyFraction(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(copy(r)))
|
||||||
self.assertEqual(id(r), id(deepcopy(r)))
|
self.assertEqual(id(r), id(deepcopy(r)))
|
||||||
self.assertNotEqual(id(dr), id(copy(dr)))
|
self.assertNotEqual(id(dr), id(copy(dr)))
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
Optimize :class:`fractions.Fraction` pickling for large components.
|
Loading…
Add table
Add a link
Reference in a new issue