From a054b63bace07521522c5774d0770e790ba7ec13 Mon Sep 17 00:00:00 2001 From: holger krekel Date: Tue, 5 Oct 2010 17:21:27 +0200 Subject: [PATCH] introduce py.builtin.any --HG-- branch : trunk --- CHANGELOG | 1 + py/__init__.py | 1 + py/_builtin.py | 9 +++++++++ testing/root/test_builtin.py | 4 ++++ 4 files changed, 15 insertions(+) diff --git a/CHANGELOG b/CHANGELOG index 395100378..2e5499610 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -11,6 +11,7 @@ Changes between 1.3.4 and 1.4.0.dev0 - major refactoring of internal collection handling - majorly reduce py.test core code, shift function/python testing to own plugin - fix issue88 (finding custom test nodes from command line arg) +- introduce py.builtin.any() Changes between 1.3.3 and 1.3.4 ================================================== diff --git a/py/__init__.py b/py/__init__.py index 4a55d189d..36a565c91 100644 --- a/py/__init__.py +++ b/py/__init__.py @@ -102,6 +102,7 @@ py.apipkg.initpkg(__name__, dict( 'enumerate' : '._builtin:enumerate', 'reversed' : '._builtin:reversed', 'sorted' : '._builtin:sorted', + 'any' : '._builtin:any', 'set' : '._builtin:set', 'frozenset' : '._builtin:frozenset', 'BaseException' : '._builtin:BaseException', diff --git a/py/_builtin.py b/py/_builtin.py index a356db044..336a2034a 100644 --- a/py/_builtin.py +++ b/py/_builtin.py @@ -35,6 +35,15 @@ except NameError: def __length_hint__(self): return self.remaining +try: + any = any +except NameError: + def any(iterable): + for x in iterable: + if x: + return True + return False + try: sorted = sorted except NameError: diff --git a/testing/root/test_builtin.py b/testing/root/test_builtin.py index bf659122f..f3c165c41 100644 --- a/testing/root/test_builtin.py +++ b/testing/root/test_builtin.py @@ -8,6 +8,10 @@ def test_enumerate(): for i,x in enumerate(l): assert i == x +def test_any(): + assert not py.builtin.any([0,False, None]) + assert py.builtin.any([0,False, None,1]) + def test_BaseException(): assert issubclass(IndexError, py.builtin.BaseException) assert issubclass(Exception, py.builtin.BaseException)