@@ -225,6 +225,8 @@ def test_all(self):
225225 self .assertEqual (all (x > 42 for x in S ), True )
226226 S = [50 , 40 , 60 ]
227227 self .assertEqual (all (x > 42 for x in S ), False )
228+ S = [50 , 40 , 60 , TestFailingBool ()]
229+ self .assertEqual (all (x > 42 for x in S ), False )
228230
229231 def test_any (self ):
230232 self .assertEqual (any ([None , None , None ]), False )
@@ -238,9 +240,59 @@ def test_any(self):
238240 self .assertEqual (any ([1 , TestFailingBool ()]), True ) # Short-circuit
239241 S = [40 , 60 , 30 ]
240242 self .assertEqual (any (x > 42 for x in S ), True )
243+ S = [40 , 60 , 30 , TestFailingBool ()]
244+ self .assertEqual (any (x > 42 for x in S ), True )
241245 S = [10 , 20 , 30 ]
242246 self .assertEqual (any (x > 42 for x in S ), False )
243247
248+ def test_all_any_tuple_optimization (self ):
249+ def f_all ():
250+ return all (x - 2 for x in [1 ,2 ,3 ])
251+
252+ def f_any ():
253+ return any (x - 1 for x in [1 ,2 ,3 ])
254+
255+ def f_tuple ():
256+ return tuple (2 * x for x in [1 ,2 ,3 ])
257+
258+ funcs = [f_all , f_any , f_tuple ]
259+
260+ for f in funcs :
261+ # check that generator code object is not duplicated
262+ code_objs = [c for c in f .__code__ .co_consts if isinstance (c , type (f .__code__ ))]
263+ self .assertEqual (len (code_objs ), 1 )
264+
265+
266+ # check the overriding the builtins works
267+
268+ global all , any , tuple
269+ saved = all , any , tuple
270+ try :
271+ all = lambda x : "all"
272+ any = lambda x : "any"
273+ tuple = lambda x : "tuple"
274+
275+ overridden_outputs = [f () for f in funcs ]
276+ finally :
277+ all , any , tuple = saved
278+
279+ self .assertEqual (overridden_outputs , ['all' , 'any' , 'tuple' ])
280+
281+ # Now repeat, overriding the builtins module as well
282+ saved = all , any , tuple
283+ try :
284+ builtins .all = all = lambda x : "all"
285+ builtins .any = any = lambda x : "any"
286+ builtins .tuple = tuple = lambda x : "tuple"
287+
288+ overridden_outputs = [f () for f in funcs ]
289+ finally :
290+ all , any , tuple = saved
291+ builtins .all , builtins .any , builtins .tuple = saved
292+
293+ self .assertEqual (overridden_outputs , ['all' , 'any' , 'tuple' ])
294+
295+
244296 def test_ascii (self ):
245297 self .assertEqual (ascii ('' ), '\' \' ' )
246298 self .assertEqual (ascii (0 ), '0' )
0 commit comments