1515import struct
1616import threading
1717import gc
18+ import warnings
19+
20+ def pickle_deprecated (testfunc ):
21+ """ Run the test three times.
22+ First, verify that a Deprecation Warning is raised.
23+ Second, run normally but with DeprecationWarnings temporarily disabled.
24+ Third, run with warnings promoted to errors.
25+ """
26+ def inner (self ):
27+ with self .assertWarns (DeprecationWarning ):
28+ testfunc (self )
29+ with warnings .catch_warnings ():
30+ warnings .simplefilter ("ignore" ,category = DeprecationWarning )
31+ testfunc (self )
32+ with warnings .catch_warnings ():
33+ warnings .simplefilter ("error" ,category = DeprecationWarning )
34+ with self .assertRaises ((DeprecationWarning ,AssertionError ,SystemError )):
35+ testfunc (self )
36+
37+ return inner
1838
1939maxsize = support .MAX_Py_ssize_t
2040minsize = - maxsize - 1
@@ -124,6 +144,7 @@ def expand(it, i=0):
124144c = expand (compare [took :])
125145self .assertEqual (a ,c );
126146
147+ @pickle_deprecated
127148def test_accumulate (self ):
128149self .assertEqual (list (accumulate (range (10 ))),# one positional arg
129150 [0 ,1 ,3 ,6 ,10 ,15 ,21 ,28 ,36 ,45 ])
@@ -220,6 +241,7 @@ def test_chain_from_iterable(self):
220241self .assertRaises (TypeError ,list ,chain .from_iterable ([2 ,3 ]))
221242self .assertEqual (list (islice (chain .from_iterable (repeat (range (5 ))),2 )), [0 ,1 ])
222243
244+ @pickle_deprecated
223245def test_chain_reducible (self ):
224246for oper in [copy .deepcopy ]+ picklecopiers :
225247it = chain ('abc' ,'def' )
@@ -233,6 +255,7 @@ def test_chain_reducible(self):
233255for proto in range (pickle .HIGHEST_PROTOCOL + 1 ):
234256self .pickletest (proto ,chain ('abc' ,'def' ),compare = list ('abcdef' ))
235257
258+ @pickle_deprecated
236259def test_chain_setstate (self ):
237260self .assertRaises (TypeError ,chain ().__setstate__ , ())
238261self .assertRaises (TypeError ,chain ().__setstate__ , [])
@@ -246,6 +269,7 @@ def test_chain_setstate(self):
246269it .__setstate__ ((iter (['abc' ,'def' ]),iter (['ghi' ])))
247270self .assertEqual (list (it ), ['ghi' ,'a' ,'b' ,'c' ,'d' ,'e' ,'f' ])
248271
272+ @pickle_deprecated
249273def test_combinations (self ):
250274self .assertRaises (TypeError ,combinations ,'abc' )# missing r argument
251275self .assertRaises (TypeError ,combinations ,'abc' ,2 ,1 )# too many arguments
@@ -269,7 +293,6 @@ def test_combinations(self):
269293self .assertEqual (list (op (testIntermediate )),
270294 [(0 ,1 ,3 ), (0 ,2 ,3 ), (1 ,2 ,3 )])
271295
272-
273296def combinations1 (iterable ,r ):
274297'Pure python version shown in the docs'
275298pool = tuple (iterable )
@@ -337,6 +360,7 @@ def test_combinations_tuple_reuse(self):
337360self .assertEqual (len (set (map (id ,combinations ('abcde' ,3 )))),1 )
338361self .assertNotEqual (len (set (map (id ,list (combinations ('abcde' ,3 ))))),1 )
339362
363+ @pickle_deprecated
340364def test_combinations_with_replacement (self ):
341365cwr = combinations_with_replacement
342366self .assertRaises (TypeError ,cwr ,'abc' )# missing r argument
@@ -425,6 +449,7 @@ def test_combinations_with_replacement_tuple_reuse(self):
425449self .assertEqual (len (set (map (id ,cwr ('abcde' ,3 )))),1 )
426450self .assertNotEqual (len (set (map (id ,list (cwr ('abcde' ,3 ))))),1 )
427451
452+ @pickle_deprecated
428453def test_permutations (self ):
429454self .assertRaises (TypeError ,permutations )# too few arguments
430455self .assertRaises (TypeError ,permutations ,'abc' ,2 ,1 )# too many arguments
@@ -531,6 +556,7 @@ def test_combinatorics(self):
531556self .assertEqual (comb ,list (filter (set (perm ).__contains__ ,cwr )))# comb: cwr that is a perm
532557self .assertEqual (comb ,sorted (set (cwr )& set (perm )))# comb: both a cwr and a perm
533558
559+ @pickle_deprecated
534560def test_compress (self ):
535561self .assertEqual (list (compress (data = 'ABCDEF' ,selectors = [1 ,0 ,1 ,0 ,1 ,1 ])),list ('ACEF' ))
536562self .assertEqual (list (compress ('ABCDEF' , [1 ,0 ,1 ,0 ,1 ,1 ])),list ('ACEF' ))
@@ -564,7 +590,7 @@ def test_compress(self):
564590next (testIntermediate )
565591self .assertEqual (list (op (testIntermediate )),list (result2 ))
566592
567-
593+ @ pickle_deprecated
568594def test_count (self ):
569595self .assertEqual (lzip ('abc' ,count ()), [('a' ,0 ), ('b' ,1 ), ('c' ,2 )])
570596self .assertEqual (lzip ('abc' ,count (3 )), [('a' ,3 ), ('b' ,4 ), ('c' ,5 )])
@@ -613,6 +639,7 @@ def test_count(self):
613639#check proper internal error handling for large "step' sizes
614640count (1 ,maxsize + 5 );sys .exc_info ()
615641
642+ @pickle_deprecated
616643def test_count_with_stride (self ):
617644self .assertEqual (lzip ('abc' ,count (2 ,3 )), [('a' ,2 ), ('b' ,5 ), ('c' ,8 )])
618645self .assertEqual (lzip ('abc' ,count (start = 2 ,step = 3 )),
@@ -675,6 +702,7 @@ def test_cycle(self):
675702self .assertRaises (TypeError ,cycle ,5 )
676703self .assertEqual (list (islice (cycle (gen3 ()),10 )), [0 ,1 ,2 ,0 ,1 ,2 ,0 ,1 ,2 ,0 ])
677704
705+ @pickle_deprecated
678706def test_cycle_copy_pickle (self ):
679707# check copy, deepcopy, pickle
680708c = cycle ('abc' )
@@ -711,6 +739,7 @@ def test_cycle_copy_pickle(self):
711739d = pickle .loads (p )# rebuild the cycle object
712740self .assertEqual (take (20 ,d ),list ('cdeabcdeabcdeabcdeab' ))
713741
742+ @pickle_deprecated
714743def test_cycle_unpickle_compat (self ):
715744testcases = [
716745b'citertools\n cycle\n (c__builtin__\n iter\n ((lI1\n aI2\n aI3\n atRI1\n btR((lI1\n aI0\n tb.' ,
@@ -742,6 +771,7 @@ def test_cycle_unpickle_compat(self):
742771it = pickle .loads (t )
743772self .assertEqual (take (10 ,it ), [2 ,3 ,1 ,2 ,3 ,1 ,2 ,3 ,1 ,2 ])
744773
774+ @pickle_deprecated
745775def test_cycle_setstate (self ):
746776# Verify both modes for restoring state
747777
@@ -778,6 +808,7 @@ def test_cycle_setstate(self):
778808self .assertRaises (TypeError ,cycle ('' ).__setstate__ , ())
779809self .assertRaises (TypeError ,cycle ('' ).__setstate__ , ([],))
780810
811+ @pickle_deprecated
781812def test_groupby (self ):
782813# Check whether it accepts arguments correctly
783814self .assertEqual ([],list (groupby ([])))
@@ -935,6 +966,7 @@ def test_filter(self):
935966c = filter (isEven ,range (6 ))
936967self .pickletest (proto ,c )
937968
969+ @pickle_deprecated
938970def test_filterfalse (self ):
939971self .assertEqual (list (filterfalse (isEven ,range (6 ))), [1 ,3 ,5 ])
940972self .assertEqual (list (filterfalse (None , [0 ,1 ,0 ,2 ,0 ])), [0 ,0 ,0 ])
@@ -965,6 +997,7 @@ def test_zip(self):
965997lzip ('abc' ,'def' ))
966998
967999@support .impl_detail ("tuple reuse is specific to CPython" )
1000+ @pickle_deprecated
9681001def test_zip_tuple_reuse (self ):
9691002ids = list (map (id ,zip ('abc' ,'def' )))
9701003self .assertEqual (min (ids ),max (ids ))
@@ -1040,6 +1073,7 @@ def test_zip_longest_tuple_reuse(self):
10401073ids = list (map (id ,list (zip_longest ('abc' ,'def' ))))
10411074self .assertEqual (len (dict .fromkeys (ids )),len (ids ))
10421075
1076+ @pickle_deprecated
10431077def test_zip_longest_pickling (self ):
10441078for proto in range (pickle .HIGHEST_PROTOCOL + 1 ):
10451079self .pickletest (proto ,zip_longest ("abc" ,"def" ))
@@ -1186,6 +1220,7 @@ def test_product_tuple_reuse(self):
11861220self .assertEqual (len (set (map (id ,product ('abc' ,'def' )))),1 )
11871221self .assertNotEqual (len (set (map (id ,list (product ('abc' ,'def' ))))),1 )
11881222
1223+ @pickle_deprecated
11891224def test_product_pickling (self ):
11901225# check copy, deepcopy, pickle
11911226for args ,result in [
@@ -1201,6 +1236,7 @@ def test_product_pickling(self):
12011236for proto in range (pickle .HIGHEST_PROTOCOL + 1 ):
12021237self .pickletest (proto ,product (* args ))
12031238
1239+ @pickle_deprecated
12041240def test_product_issue_25021 (self ):
12051241# test that indices are properly clamped to the length of the tuples
12061242p = product ((1 ,2 ),(3 ,))
@@ -1211,6 +1247,7 @@ def test_product_issue_25021(self):
12111247p .__setstate__ ((0 ,0 ,0x1000 ))# will access tuple element 1 if not clamped
12121248self .assertRaises (StopIteration ,next ,p )
12131249
1250+ @pickle_deprecated
12141251def test_repeat (self ):
12151252self .assertEqual (list (repeat (object = 'a' ,times = 3 )), ['a' ,'a' ,'a' ])
12161253self .assertEqual (lzip (range (3 ),repeat ('a' )),
@@ -1243,6 +1280,7 @@ def test_repeat_with_negative_times(self):
12431280self .assertEqual (repr (repeat ('a' ,times = - 1 )),"repeat('a', 0)" )
12441281self .assertEqual (repr (repeat ('a' ,times = - 2 )),"repeat('a', 0)" )
12451282
1283+ @pickle_deprecated
12461284def test_map (self ):
12471285self .assertEqual (list (map (operator .pow ,range (3 ),range (1 ,7 ))),
12481286 [0 ** 1 ,1 ** 2 ,2 ** 3 ])
@@ -1273,6 +1311,7 @@ def test_map(self):
12731311c = map (tupleize ,'abc' ,count ())
12741312self .pickletest (proto ,c )
12751313
1314+ @pickle_deprecated
12761315def test_starmap (self ):
12771316self .assertEqual (list (starmap (operator .pow ,zip (range (3 ),range (1 ,7 )))),
12781317 [0 ** 1 ,1 ** 2 ,2 ** 3 ])
@@ -1300,6 +1339,7 @@ def test_starmap(self):
13001339c = starmap (operator .pow ,zip (range (3 ),range (1 ,7 )))
13011340self .pickletest (proto ,c )
13021341
1342+ @pickle_deprecated
13031343def test_islice (self ):
13041344for args in [# islice(args) should agree with range(args)
13051345 (10 ,20 ,3 ),
@@ -1394,6 +1434,7 @@ def __index__(self):
13941434self .assertEqual (list (islice (range (100 ),IntLike (10 ),IntLike (50 ),IntLike (5 ))),
13951435list (range (10 ,50 ,5 )))
13961436
1437+ @pickle_deprecated
13971438def test_takewhile (self ):
13981439data = [1 ,3 ,5 ,20 ,2 ,4 ,6 ,8 ]
13991440self .assertEqual (list (takewhile (underten ,data )), [1 ,3 ,5 ])
@@ -1414,6 +1455,7 @@ def test_takewhile(self):
14141455for proto in range (pickle .HIGHEST_PROTOCOL + 1 ):
14151456self .pickletest (proto ,takewhile (underten ,data ))
14161457
1458+ @pickle_deprecated
14171459def test_dropwhile (self ):
14181460data = [1 ,3 ,5 ,20 ,2 ,4 ,6 ,8 ]
14191461self .assertEqual (list (dropwhile (underten ,data )), [20 ,2 ,4 ,6 ,8 ])
@@ -1431,6 +1473,7 @@ def test_dropwhile(self):
14311473for proto in range (pickle .HIGHEST_PROTOCOL + 1 ):
14321474self .pickletest (proto ,dropwhile (underten ,data ))
14331475
1476+ @pickle_deprecated
14341477def test_tee (self ):
14351478n = 200
14361479
@@ -1732,6 +1775,7 @@ class TestExamples(unittest.TestCase):
17321775def test_accumulate (self ):
17331776self .assertEqual (list (accumulate ([1 ,2 ,3 ,4 ,5 ])), [1 ,3 ,6 ,10 ,15 ])
17341777
1778+ @pickle_deprecated
17351779def test_accumulate_reducible (self ):
17361780# check copy, deepcopy, pickle
17371781data = [1 ,2 ,3 ,4 ,5 ]
@@ -1747,6 +1791,7 @@ def test_accumulate_reducible(self):
17471791self .assertEqual (list (copy .deepcopy (it )),accumulated [1 :])
17481792self .assertEqual (list (copy .copy (it )),accumulated [1 :])
17491793
1794+ @pickle_deprecated
17501795def test_accumulate_reducible_none (self ):
17511796# Issue #25718: total is None
17521797it = accumulate ([None ,None ,None ],operator .is_ )