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
0 commit comments