summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRémi Cardona <remi.cardona@free.fr>2014-07-28 23:02:46 +0200
committerRémi Cardona <remi.cardona@free.fr>2014-07-28 23:02:46 +0200
commita8ad07d5eeceadcc466f2d5f6e3274168b74a2a3 (patch)
tree7647069da0bd9ead74aaa4b25419e180ef18cfe1
parent7bc13c991f1d49b59e243e663406ec4c0845a2a7 (diff)
downloadlogilab-common-a8ad07d5eeceadcc466f2d5f6e3274168b74a2a3.tar.gz
[compat] Deprecate any() and all() (related to #264017)
They're builtin since python 2.5, so we can remove our implementation.
-rw-r--r--compat.py29
-rw-r--r--fileutils.py2
-rw-r--r--pytest.py1
-rw-r--r--test/unittest_compat.py120
-rw-r--r--testlib.py2
5 files changed, 6 insertions, 148 deletions
diff --git a/compat.py b/compat.py
index 92e8808..a317402 100644
--- a/compat.py
+++ b/compat.py
@@ -91,31 +91,10 @@ except ImportError:
from logilab.common.deprecation import deprecated
-# Python2.5 builtins
-try:
- any = any
- all = all
-except NameError:
- def any(iterable):
- """any(iterable) -> bool
-
- Return True if bool(x) is True for any x in the iterable.
- """
- for elt in iterable:
- if elt:
- return True
- return False
-
- def all(iterable):
- """all(iterable) -> bool
-
- Return True if bool(x) is True for all values x in the iterable.
- """
- for elt in iterable:
- if not elt:
- return False
- return True
-
+# Other projects import these from here, keep providing them for
+# backwards compat
+any = deprecated('use builtin "any"')(any)
+all = deprecated('use builtin "all"')(all)
# Python2.5 subprocess added functions and exceptions
try:
diff --git a/fileutils.py b/fileutils.py
index 7a028b1..3b04db9 100644
--- a/fileutils.py
+++ b/fileutils.py
@@ -36,7 +36,7 @@ from stat import ST_MODE, S_IWRITE
from logilab.common import STD_BLACKLIST as BASE_BLACKLIST, IGNORED_EXTENSIONS
from logilab.common.shellutils import find
from logilab.common.deprecation import deprecated
-from logilab.common.compat import FileIO, any
+from logilab.common.compat import FileIO
def first_level_directory(path):
"""Return the first level directory of a path.
diff --git a/pytest.py b/pytest.py
index ae4c221..99989ba 100644
--- a/pytest.py
+++ b/pytest.py
@@ -121,7 +121,6 @@ from logilab.common import textutils
from logilab.common import testlib, STD_BLACKLIST
# use the same unittest module as testlib
from logilab.common.testlib import unittest, start_interactive_mode
-from logilab.common.compat import any
import doctest
import unittest as unittest_legacy
diff --git a/test/unittest_compat.py b/test/unittest_compat.py
deleted file mode 100644
index 69f7a35..0000000
--- a/test/unittest_compat.py
+++ /dev/null
@@ -1,120 +0,0 @@
-# copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
-# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
-#
-# This file is part of logilab-common.
-#
-# logilab-common is free software: you can redistribute it and/or modify it under
-# the terms of the GNU Lesser General Public License as published by the Free
-# Software Foundation, either version 2.1 of the License, or (at your option) any
-# later version.
-#
-# logilab-common is distributed in the hope that it will be useful, but WITHOUT
-# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
-# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
-# details.
-#
-# You should have received a copy of the GNU Lesser General Public License along
-# with logilab-common. If not, see <http://www.gnu.org/licenses/>.
-"""provides unit tests for compat module"""
-
-from logilab.common.testlib import TestCase, unittest_main
-import sys
-import types
-from logilab.common.compat import builtins
-
-class CompatTCMixIn:
- MODNAMES = {}
- BUILTINS = []
- ALTERED_BUILTINS = {}
-
- def setUp(self):
- self.builtins_backup = {}
- self.modules_backup = {}
- self.remove_builtins()
- self.alter_builtins()
- self.remove_modules()
-
- def tearDown(self):
- for modname in self.MODNAMES:
- del sys.modules[modname]
- for funcname, func in self.builtins_backup.items():
- setattr(builtins, funcname, func)
- # delattr(builtins, 'builtin_%s' % funcname)
- for modname, mod in self.modules_backup.items():
- sys.modules[modname] = mod
- try:
- del sys.modules['logilab.common.compat']
- except KeyError:
- pass
-
- def remove_builtins(self):
- for builtin in self.BUILTINS:
- func = getattr(builtins, builtin, None)
- if func is not None:
- self.builtins_backup[builtin] = func
- delattr(builtins, builtin)
- # setattr(builtins, 'builtin_%s' % builtin, func)
- def alter_builtins(self):
- for builtin, func in self.ALTERED_BUILTINS.iteritems():
- old_func = getattr(builtins, builtin, None)
- if func is not None:
- self.builtins_backup[builtin] = old_func
- setattr(builtins, builtin, func)
- # setattr(builtins, 'builtin_%s' % builtin, func)
-
- def remove_modules(self):
- for modname in self.MODNAMES:
- if modname in sys.modules:
- self.modules_backup[modname] = sys.modules[modname]
- sys.modules[modname] = types.ModuleType('faked%s' % modname)
-
- def test_removed_builtins(self):
- """tests that builtins are actually uncallable"""
- for builtin in self.BUILTINS:
- self.assertRaises(NameError, eval, builtin, {})
-
- def test_removed_modules(self):
- """tests that builtins are actually emtpy"""
- for modname, funcnames in self.MODNAMES.items():
- import_stmt = 'from %s import %s' % (modname, ', '.join(funcnames))
- # FIXME: use __import__ instead
- code = compile(import_stmt, 'foo.py', 'exec')
- self.assertRaises(ImportError, eval, code)
-
-
-class Py25CompatTC(CompatTCMixIn, TestCase):
- BUILTINS = ('any', 'all',)
-
- def test_any(self):
- from logilab.common.compat import any
- testdata = ([], (), '', 'abc', xrange(0, 10), xrange(0, -10, -1))
- self.assertEqual(any([]), False)
- self.assertEqual(any(()), False)
- self.assertEqual(any(''), False)
- self.assertEqual(any('abc'), True)
- self.assertEqual(any(xrange(10)), True)
- self.assertEqual(any(xrange(0, -10, -1)), True)
- # python2.5's any consumes iterables
- irange = iter(range(10))
- self.assertEqual(any(irange), True)
- self.assertEqual(irange.next(), 2)
-
-
- def test_all(self):
- from logilab.common.compat import all
- testdata = ([], (), '', 'abc', xrange(0, 10), xrange(0, -10, -1))
- self.assertEqual(all([]), True)
- self.assertEqual(all(()), True)
- self.assertEqual(all(''), True)
- self.assertEqual(all('abc'), True)
- self.assertEqual(all(xrange(10)), False)
- self.assertEqual(all(xrange(0, -10, -1)), False)
- # python2.5's all consumes iterables
- irange = iter(range(10))
- self.assertEqual(all(irange), False)
- self.assertEqual(irange.next(), 1)
-
-
-
-if __name__ == '__main__':
- unittest_main()
diff --git a/testlib.py b/testlib.py
index d1a2be6..7d5f028 100644
--- a/testlib.py
+++ b/testlib.py
@@ -87,7 +87,7 @@ except ImportError:
test_support = TestSupport()
# pylint: disable=W0622
-from logilab.common.compat import any, InheritableSet, callable
+from logilab.common.compat import InheritableSet, callable
# pylint: enable=W0622
from logilab.common.debugger import Debugger, colorize_source
from logilab.common.decorators import cached, classproperty