summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRémi Cardona <remi.cardona@free.fr>2014-07-18 22:52:03 +0200
committerRémi Cardona <remi.cardona@free.fr>2014-07-18 22:52:03 +0200
commit8c0ad73dcbeda55b0a280164c397568bb1f9c1aa (patch)
tree3da49a280e653c3ae6d31e4a578bea44d5845461
parent62d58bc48c2850a2bd24816b9773580725ef4069 (diff)
downloadlogilab-common-8c0ad73dcbeda55b0a280164c397568bb1f9c1aa.tar.gz
Use inspect.isgeneratorfunction() (related to #264017)
Requires python >= 2.6.
-rw-r--r--decorators.py6
-rw-r--r--pytest.py3
-rw-r--r--testlib.py23
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)
diff --git a/pytest.py b/pytest.py
index 99989ba..61fd3f7 100644
--- a/pytest.py
+++ b/pytest.py
@@ -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
diff --git a/testlib.py b/testlib.py
index 1e1fd92..1dd7064 100644
--- a/testlib.py
+++ b/testlib.py
@@ -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,