diff options
author | Rémi Cardona <remi.cardona@free.fr> | 2014-07-18 22:52:03 +0200 |
---|---|---|
committer | Rémi Cardona <remi.cardona@free.fr> | 2014-07-18 22:52:03 +0200 |
commit | 8c0ad73dcbeda55b0a280164c397568bb1f9c1aa (patch) | |
tree | 3da49a280e653c3ae6d31e4a578bea44d5845461 | |
parent | 62d58bc48c2850a2bd24816b9773580725ef4069 (diff) | |
download | logilab-common-8c0ad73dcbeda55b0a280164c397568bb1f9c1aa.tar.gz |
Use inspect.isgeneratorfunction() (related to #264017)
Requires python >= 2.6.
-rw-r--r-- | decorators.py | 6 | ||||
-rw-r--r-- | pytest.py | 3 | ||||
-rw-r--r-- | testlib.py | 23 |
3 files changed, 8 insertions, 24 deletions
diff --git a/decorators.py b/decorators.py index 34bbd3a..f71c723 100644 --- a/decorators.py +++ b/decorators.py @@ -21,20 +21,18 @@ __docformat__ = "restructuredtext en" import sys import types from time import clock, time +from inspect import isgeneratorfunction from logilab.common.compat import callable, method_type # XXX rewrite so we can use the decorator syntax when keyarg has to be specified -def _is_generator_function(callableobj): - return callableobj.func_code.co_flags & 0x20 - class cached_decorator(object): def __init__(self, cacheattr=None, keyarg=None): self.cacheattr = cacheattr self.keyarg = keyarg def __call__(self, callableobj=None): - assert not _is_generator_function(callableobj), \ + assert not isgeneratorfunction(callableobj), \ 'cannot cache generator function: %s' % callableobj if callableobj.func_code.co_argcount == 1 or self.keyarg == 0: cache = _SingleValueCache(callableobj, self.cacheattr) @@ -115,6 +115,7 @@ import os.path as osp from time import time, clock import warnings import types +from inspect import isgeneratorfunction from logilab.common.fileutils import abspath_listdir from logilab.common import textutils @@ -888,7 +889,7 @@ class SkipAwareTextTestRunner(unittest.TextTestRunner): testname = '%s.%s' % (test.im_class.__name__, func.__name__) else: return True # Not sure when this happens - if testlib.is_generator(test) and skipgenerator: + if isgeneratorfunction(test) and skipgenerator: return self.does_match_tags(test) # Let inner tests decide at run time if self._this_is_skipped(testname): return False # this was explicitly skipped @@ -54,6 +54,7 @@ from shutil import rmtree from operator import itemgetter from ConfigParser import ConfigParser from itertools import dropwhile +from inspect import isgeneratorfunction from logilab.common.deprecation import deprecated from logilab.common.compat import builtins @@ -99,23 +100,7 @@ __all__ = ['main', 'unittest_main', 'find_tests', 'run_test', 'spawn'] DEFAULT_PREFIXES = ('test', 'regrtest', 'smoketest', 'unittest', 'func', 'validation') - -if sys.version_info >= (2, 6): - # FIXME : this does not work as expected / breaks tests on testlib - # however testlib does not work on py3k for many reasons ... - from inspect import CO_GENERATOR -else: - from compiler.consts import CO_GENERATOR - -if sys.version_info >= (3, 0): - def is_generator(function): - flags = function.__code__.co_flags - return flags & CO_GENERATOR - -else: - def is_generator(function): - flags = function.func_code.co_flags - return flags & CO_GENERATOR +is_generator = deprecated('[lgc 0.63] use inspect.isgeneratorfunction')(isgeneratorfunction) # used by unittest to count the number of relevant levels in the traceback __unittest = 1 @@ -124,7 +109,7 @@ __unittest = 1 def with_tempdir(callable): """A decorator ensuring no temporary file left when the function return Work only for temporary file create with the tempfile module""" - if is_generator(callable): + if isgeneratorfunction(callable): def proxy(*args, **kwargs): old_tmpdir = tempfile.gettempdir() new_tmpdir = tempfile.mkdtemp(prefix="temp-lgc-") @@ -589,7 +574,7 @@ class TestCase(unittest.TestCase): try: if not self.quiet_run(result, self.setUp): return - generative = is_generator(testMethod.im_func) + generative = isgeneratorfunction(testMethod) # generative tests if generative: self._proceed_generative(result, testMethod, |