Skip to content

Commit 23cbe15

Browse files
lesteveschettino72ogrisel
authored
FIX: Support PyPy > 3.7 (#480)
Co-authored-by: Eduardo Schettino <[email protected]> Co-authored-by: Olivier Grisel <[email protected]>
1 parent f5472e1 commit 23cbe15

File tree

5 files changed

+38
-28
lines changed

5 files changed

+38
-28
lines changed

.github/workflows/testing.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ jobs:
2929
strategy:
3030
matrix:
3131
os: [ubuntu-latest, windows-latest, macos-latest]
32-
python_version: [3.6, 3.7, 3.8, 3.9, "3.10", "3.11-dev", "pypy3"]
32+
python_version: [3.6, 3.7, 3.8, 3.9, "3.10", "3.11-dev", "pypy-3.9"]
3333
exclude:
3434
# Do not test all minor versions on all platforms, especially if they
3535
# are not the oldest/newest supported versions
@@ -41,7 +41,7 @@ jobs:
4141
python_version: 3.8
4242
# as of 4/02/2020, psutil won't build under PyPy + Windows
4343
- os: windows-latest
44-
python_version: "pypy3"
44+
python_version: "pypy-3.9"
4545
- os: macos-latest
4646
python_version: 3.6
4747
- os: macos-latest
@@ -51,7 +51,7 @@ jobs:
5151
- os: macos-latest
5252
# numpy triggers: RuntimeError: Polyfit sanity test emitted a
5353
# warning
54-
python_version: "pypy3"
54+
python_version: "pypy-3.9"
5555

5656
runs-on: ${{ matrix.os }}
5757

CHANGES.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
2.2.0 (in development)
22
======================
33

4-
- TODO document changes here.
4+
- Fix support of PyPy 3.8 and later.
5+
([issue #455](https://github.com/cloudpipe/cloudpickle/issues/455))
56

67
2.1.0
78
=====

cloudpickle/cloudpickle_fast.py

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
)
4040

4141

42-
if pickle.HIGHEST_PROTOCOL >= 5 and not PYPY:
42+
if pickle.HIGHEST_PROTOCOL >= 5:
4343
# Shorthands similar to pickle.dump/pickle.dumps
4444

4545
def dump(obj, file, protocol=None, buffer_callback=None):
@@ -566,7 +566,6 @@ class CloudPickler(Pickler):
566566
_dispatch_table[abc.abstractstaticmethod] = _classmethod_reduce
567567
_dispatch_table[abc.abstractproperty] = _property_reduce
568568

569-
570569
dispatch_table = ChainMap(_dispatch_table, copyreg.dispatch_table)
571570

572571
# function reducers are defined as instance methods of CloudPickler
@@ -642,6 +641,32 @@ def dump(self, obj):
642641
raise
643642

644643
if pickle.HIGHEST_PROTOCOL >= 5:
644+
def __init__(self, file, protocol=None, buffer_callback=None):
645+
if protocol is None:
646+
protocol = DEFAULT_PROTOCOL
647+
Pickler.__init__(
648+
self, file, protocol=protocol, buffer_callback=buffer_callback
649+
)
650+
# map functions __globals__ attribute ids, to ensure that functions
651+
# sharing the same global namespace at pickling time also share
652+
# their global namespace at unpickling time.
653+
self.globals_ref = {}
654+
self.proto = int(protocol)
655+
else:
656+
def __init__(self, file, protocol=None):
657+
if protocol is None:
658+
protocol = DEFAULT_PROTOCOL
659+
Pickler.__init__(self, file, protocol=protocol)
660+
# map functions __globals__ attribute ids, to ensure that functions
661+
# sharing the same global namespace at pickling time also share
662+
# their global namespace at unpickling time.
663+
self.globals_ref = {}
664+
assert hasattr(self, 'proto')
665+
666+
if pickle.HIGHEST_PROTOCOL >= 5 and not PYPY:
667+
# Pickler is the C implementation of the CPython pickler and therefore
668+
# we rely on reduce_override method to customize the pickler behavior.
669+
645670
# `CloudPickler.dispatch` is only left for backward compatibility - note
646671
# that when using protocol 5, `CloudPickler.dispatch` is not an
647672
# extension of `Pickler.dispatch` dictionary, because CloudPickler
@@ -662,17 +687,6 @@ def dump(self, obj):
662687
# availability of both notions coincide on CPython's pickle and the
663688
# pickle5 backport, but it may not be the case anymore when pypy
664689
# implements protocol 5
665-
def __init__(self, file, protocol=None, buffer_callback=None):
666-
if protocol is None:
667-
protocol = DEFAULT_PROTOCOL
668-
Pickler.__init__(
669-
self, file, protocol=protocol, buffer_callback=buffer_callback
670-
)
671-
# map functions __globals__ attribute ids, to ensure that functions
672-
# sharing the same global namespace at pickling time also share
673-
# their global namespace at unpickling time.
674-
self.globals_ref = {}
675-
self.proto = int(protocol)
676690

677691
def reducer_override(self, obj):
678692
"""Type-agnostic reducing callback for function and classes.
@@ -733,16 +747,6 @@ def reducer_override(self, obj):
733747
# hard-coded call to save_global when pickling meta-classes.
734748
dispatch = Pickler.dispatch.copy()
735749

736-
def __init__(self, file, protocol=None):
737-
if protocol is None:
738-
protocol = DEFAULT_PROTOCOL
739-
Pickler.__init__(self, file, protocol=protocol)
740-
# map functions __globals__ attribute ids, to ensure that functions
741-
# sharing the same global namespace at pickling time also share
742-
# their global namespace at unpickling time.
743-
self.globals_ref = {}
744-
assert hasattr(self, 'proto')
745-
746750
def _save_reduce_pickle5(self, func, args, state=None, listitems=None,
747751
dictitems=None, state_setter=None, obj=None):
748752
save = self.save

cloudpickle/compat.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@
77
from pickle5 import Pickler # noqa: F401
88
except ImportError:
99
import pickle # noqa: F401
10+
11+
# Use the Python pickler for old CPython versions
1012
from pickle import _Pickler as Pickler # noqa: F401
1113
else:
1214
import pickle # noqa: F401
15+
16+
# Pickler will the C implementation in CPython and the Python
17+
# implementation in PyPy
1318
from pickle import Pickler # noqa: F401

dev-requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ pytest
44
pytest-cov
55
psutil
66
# To test on older Python versions
7-
pickle5 >=0.0.11 ; python_version <= '3.7' and python_implementation == 'CPython'
7+
pickle5 >=0.0.11 ; python_version == '3.7' and python_implementation == 'CPython'
88
# To be able to test tornado coroutines
99
tornado
1010
# To be able to test numpy specific things

0 commit comments

Comments
 (0)