@@ -675,6 +675,7 @@ def test_cycle(self):
675
675
self .assertRaises (TypeError , cycle , 5 )
676
676
self .assertEqual (list (islice (cycle (gen3 ()),10 )), [0 ,1 ,2 ,0 ,1 ,2 ,0 ,1 ,2 ,0 ])
677
677
678
+ def test_cycle_copy_pickle (self ):
678
679
# check copy, deepcopy, pickle
679
680
c = cycle ('abc' )
680
681
self .assertEqual (next (c ), 'a' )
@@ -710,6 +711,37 @@ def test_cycle(self):
710
711
d = pickle .loads (p ) # rebuild the cycle object
711
712
self .assertEqual (take (20 , d ), list ('cdeabcdeabcdeabcdeab' ))
712
713
714
+ def test_cycle_unpickle_compat (self ):
715
+ testcases = [
716
+ b'citertools\n cycle\n (c__builtin__\n iter\n ((lI1\n aI2\n aI3\n atRI1\n btR((lI1\n aI0\n tb.' ,
717
+ b'citertools\n cycle\n (c__builtin__\n iter\n (](K\x01 K\x02 K\x03 etRK\x01 btR(]K\x01 aK\x00 tb.' ,
718
+ b'\x80 \x02 citertools\n cycle\n c__builtin__\n iter\n ](K\x01 K\x02 K\x03 e\x85 RK\x01 b\x85 R]K\x01 aK\x00 \x86 b.' ,
719
+ b'\x80 \x03 citertools\n cycle\n cbuiltins\n iter\n ](K\x01 K\x02 K\x03 e\x85 RK\x01 b\x85 R]K\x01 aK\x00 \x86 b.' ,
720
+ b'\x80 \x04 \x95 =\x00 \x00 \x00 \x00 \x00 \x00 \x00 \x8c \t itertools\x8c \x05 cycle\x93 \x8c \x08 builtins\x8c \x04 iter\x93 ](K\x01 K\x02 K\x03 e\x85 RK\x01 b\x85 R]K\x01 aK\x00 \x86 b.' ,
721
+
722
+ b'citertools\n cycle\n (c__builtin__\n iter\n ((lp0\n I1\n aI2\n aI3\n atRI1\n btR(g0\n I1\n tb.' ,
723
+ b'citertools\n cycle\n (c__builtin__\n iter\n (]q\x00 (K\x01 K\x02 K\x03 etRK\x01 btR(h\x00 K\x01 tb.' ,
724
+ b'\x80 \x02 citertools\n cycle\n c__builtin__\n iter\n ]q\x00 (K\x01 K\x02 K\x03 e\x85 RK\x01 b\x85 Rh\x00 K\x01 \x86 b.' ,
725
+ b'\x80 \x03 citertools\n cycle\n cbuiltins\n iter\n ]q\x00 (K\x01 K\x02 K\x03 e\x85 RK\x01 b\x85 Rh\x00 K\x01 \x86 b.' ,
726
+ b'\x80 \x04 \x95 <\x00 \x00 \x00 \x00 \x00 \x00 \x00 \x8c \t itertools\x8c \x05 cycle\x93 \x8c \x08 builtins\x8c \x04 iter\x93 ]\x94 (K\x01 K\x02 K\x03 e\x85 RK\x01 b\x85 Rh\x00 K\x01 \x86 b.' ,
727
+
728
+ b'citertools\n cycle\n (c__builtin__\n iter\n ((lI1\n aI2\n aI3\n atRI1\n btR((lI1\n aI00\n tb.' ,
729
+ b'citertools\n cycle\n (c__builtin__\n iter\n (](K\x01 K\x02 K\x03 etRK\x01 btR(]K\x01 aI00\n tb.' ,
730
+ b'\x80 \x02 citertools\n cycle\n c__builtin__\n iter\n ](K\x01 K\x02 K\x03 e\x85 RK\x01 b\x85 R]K\x01 a\x89 \x86 b.' ,
731
+ b'\x80 \x03 citertools\n cycle\n cbuiltins\n iter\n ](K\x01 K\x02 K\x03 e\x85 RK\x01 b\x85 R]K\x01 a\x89 \x86 b.' ,
732
+ b'\x80 \x04 \x95 <\x00 \x00 \x00 \x00 \x00 \x00 \x00 \x8c \t itertools\x8c \x05 cycle\x93 \x8c \x08 builtins\x8c \x04 iter\x93 ](K\x01 K\x02 K\x03 e\x85 RK\x01 b\x85 R]K\x01 a\x89 \x86 b.' ,
733
+
734
+ b'citertools\n cycle\n (c__builtin__\n iter\n ((lp0\n I1\n aI2\n aI3\n atRI1\n btR(g0\n I01\n tb.' ,
735
+ b'citertools\n cycle\n (c__builtin__\n iter\n (]q\x00 (K\x01 K\x02 K\x03 etRK\x01 btR(h\x00 I01\n tb.' ,
736
+ b'\x80 \x02 citertools\n cycle\n c__builtin__\n iter\n ]q\x00 (K\x01 K\x02 K\x03 e\x85 RK\x01 b\x85 Rh\x00 \x88 \x86 b.' ,
737
+ b'\x80 \x03 citertools\n cycle\n cbuiltins\n iter\n ]q\x00 (K\x01 K\x02 K\x03 e\x85 RK\x01 b\x85 Rh\x00 \x88 \x86 b.' ,
738
+ b'\x80 \x04 \x95 ;\x00 \x00 \x00 \x00 \x00 \x00 \x00 \x8c \t itertools\x8c \x05 cycle\x93 \x8c \x08 builtins\x8c \x04 iter\x93 ]\x94 (K\x01 K\x02 K\x03 e\x85 RK\x01 b\x85 Rh\x00 \x88 \x86 b.' ,
739
+ ]
740
+ assert len (testcases ) == 20
741
+ for t in testcases :
742
+ it = pickle .loads (t )
743
+ self .assertEqual (take (10 , it ), [2 , 3 , 1 , 2 , 3 , 1 , 2 , 3 , 1 , 2 ])
744
+
713
745
def test_cycle_setstate (self ):
714
746
# Verify both modes for restoring state
715
747
@@ -739,6 +771,10 @@ def test_cycle_setstate(self):
739
771
c .__setstate__ ((tuple ('defg' ), 0 ))
740
772
take (20 , c )
741
773
774
+ # The second argument in the setstate tuple must be an int
775
+ with self .assertRaises (TypeError ):
776
+ cycle ('defg' ).__setstate__ ((list ('abcdefg' ), 'x' ))
777
+
742
778
self .assertRaises (TypeError , cycle ('' ).__setstate__ , ())
743
779
self .assertRaises (TypeError , cycle ('' ).__setstate__ , ([],))
744
780
0 commit comments