本文整理汇总了Python中nose.tools.make_decorator函数的典型用法代码示例。如果您正苦于以下问题:Python make_decorator函数的具体用法?Python make_decorator怎么用?Python make_decorator使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了make_decorator函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: needs_reflink_fs
def needs_reflink_fs(test):
def no_support(*args):
raise SkipTest("btrfs not supported")
def not_reflink_fs(*args):
raise SkipTest("testdir is not on reflink-capable filesystem")
if not has_feature('btrfs-support'):
return make_decorator(test)(no_support)
elif not is_on_reflink_fs(TESTDIR_NAME):
return make_decorator(test)(not_reflink_fs)
else:
return test
示例2: decorator
def decorator(func):
def timed_out(_signum, frame):
raise TestTimedOut(time_limit, frame)
def newfunc(*args, **kwargs):
try:
# Will only work on unix systems
orig_handler = signal.signal(signal.SIGALRM, timed_out)
signal.alarm(time_limit)
except AttributeError:
pass
try:
rc = func(*args, **kwargs)
finally:
try:
# Will only work on unix systems
signal.alarm(0)
signal.signal(signal.SIGALRM, orig_handler)
except AttributeError:
pass
return rc
newfunc = make_decorator(func)(newfunc)
return newfunc
示例3: dec
def dec(in_func):
if cond:
def wrapper():
raise nose.SkipTest(msg)
return make_decorator(in_func)(wrapper)
else:
return in_func
示例4: transplant_func
def transplant_func(func, module):
"""
Make a function imported from module A appear as if it is located
in module B.
>>> from pprint import pprint
>>> pprint.__module__
'pprint'
>>> pp = transplant_func(pprint, __name__)
>>> pp.__module__
'nose.util'
The original function is not modified.
>>> pprint.__module__
'pprint'
Calling the transplanted function calls the original.
>>> pp([1, 2])
[1, 2]
>>> pprint([1,2])
[1, 2]
"""
from nose.tools import make_decorator
def newfunc(*arg, **kw):
return func(*arg, **kw)
newfunc = make_decorator(func)(newfunc)
newfunc.__module__ = module
return newfunc
示例5: regex_related
def regex_related(test):
def skip_test(*args):
raise SkipTest("Regex not supported")
if not is_regex_supported():
return make_decorator(test)(skip_test)
return test
示例6: dec
def dec(in_func):
# make the wrapper function
# if the condition is True
if cond:
def inner_wrap():
# try the test anywoy
try:
in_func()
# when in fails, raises KnownFailureTest
# which is registered with nose and it will be marked
# as K in the results
except Exception:
raise KnownFailureTest()
# if it does not fail, raise KnownFailureDidNotFailTest which
# is a normal exception. This may seem counter-intuitive
# but knowing when tests that _should_ fail don't can be useful
else:
raise KnownFailureDidNotFailTest()
# use `make_decorator` from nose to make sure that the meta-data on
# the function is forwarded properly (name, teardown, setup, etc)
return make_decorator(in_func)(inner_wrap)
# if the condition is false, don't make a wrapper function
# this is effectively a no-op
else:
return in_func
示例7: decorator
def decorator(func):
# Wrap the test function to call, but with a timeout
def wrapper(*args, **kwargs):
# First, install the SIGALRM signal handler
prev_hndlr = signal.signal(signal.SIGALRM, handler)
try:
# Set up the alarm
signal.alarm(timeout)
# Run the test function
result = func(*args, **kwargs)
finally:
# Stop the alarm
signal.alarm(0)
# Restore the signal handler
signal.signal(signal.SIGALRM, prev_hndlr)
# Return the result
return result
# Replace func with wrapper
wrapper = tools.make_decorator(func)(wrapper)
return wrapper
示例8: decorate
def decorate(func):
def newfunc(*arg, **kwargs):
timer = Timer(limit, handler)
try:
log.debug('starting timer in %s for %s',
str(datetime.datetime.now()), str(limit))
timer.start()
ret = func(*arg, **kwargs)
except KeyboardInterrupt:
if timer.isAlive:
timer.cancel()
raise
except:
if timer.isAlive():
timer.cancel()
log.debug('canceled timer in %s because of failure',
str(datetime.datetime.now()))
raise
else:
raise TimeExpired, 'Time expired ( and test raised: ' \
+ str(sys.exc_info()) + ')', sys.exc_info()[2]
if timer.isAlive():
timer.cancel()
log.debug('canceled timer in %s', str(datetime.datetime.now()))
else:
log.debug('timer has expired', str(datetime.datetime.now()))
raise TimeExpired("time limit exceeded")
return ret
newfunc = make_decorator(func)(newfunc)
return newfunc
示例9: decorate
def decorate(func):
def newfunc(*arg, **kwargs):
mvpa2.seed(mvpa2._random_seed)
return func(*arg, **kwargs)
newfunc = make_decorator(func)(newfunc)
return newfunc
示例10: decorate
def decorate(func):
def newfunc(*arg, **kw):
port = random.randint(8000, 8500)
# TODO: ATM we are relying on path being local so we could
# start HTTP server in the same directory. FIX IT!
SocketServer.TCPServer.allow_reuse_address = True
httpd = SocketServer.TCPServer(("", port), SilentHTTPHandler)
server_thread = Thread(target=httpd.serve_forever)
arg, path = arg[:-1], arg[-1]
# There is a problem with Haskell on wheezy trying to
# fetch via IPv6 whenever there is a ::1 localhost entry in
# /etc/hosts. Apparently fixing that docker image reliably
# is not that straightforward, although see
# http://jasonincode.com/customizing-hosts-file-in-docker/
# so we just force to use 127.0.0.1 while on wheezy
hostname = '127.0.0.1' if on_debian_wheezy else 'localhost'
url = 'http://%s:%d/%s/' % (hostname, port, path)
lgr.debug("HTTP: serving %s under %s", path, url)
server_thread.start()
#time.sleep(1) # just give it few ticks
try:
func(*(arg + (path, url,)), **kw)
finally:
lgr.debug("HTTP: stopping server")
httpd.shutdown()
server_thread.join()
newfunc = make_decorator(func)(newfunc)
return newfunc
示例11: snapshot
def snapshot(f):
"Takes an MD5 snapshot of video for test acceptance"
@wraps(f)
def wrapper(*args, **kwargs):
filename = f(*args, **kwargs)
snapshot_name = f.func_name
print "snapshot:", snapshot_name
snapshot_filename = os.path.join(snapshot_dir, snapshot_name)
snapshot_md5_filename = snapshot_filename + '.md5'
snapshot_mov_filename = snapshot_filename + '.mov'
# Do we have an existing snapshot in snapshots directory?
if not os.path.exists(snapshot_md5_filename):
prompt_compare(snapshot_mov_filename, snapshot_md5_filename,
filename)
else:
# otherwise we have a snapshot
sample_md5 = md5sum(filename)
snapshot_md5 = file(snapshot_md5_filename).read()
if sample_md5 == snapshot_md5:
print "snapshot: success, they are the same!"
else:
prompt_compare(snapshot_mov_filename,
snapshot_md5_filename, filename)
if os.path.exists(filename):
print "snapshot: removing", filename
os.remove(filename)
wrapper = make_decorator(f)(wrapper)
return wrapper
示例12: hash_setup
def hash_setup(test):
def hash_test():
sys.stdout = output = io.BytesIO()
test_hash = test()
output.seek(0)
test_hash = hashlib.md5(output.read()).hexdigest()
output.close()
# Get the true hash
try:
true_hash_file = open(test.__name__ + '.hash', 'r')
except IOError:
# File doesn't exist because it's a new test and there's no
# true hash file
true_hash_file = open(test.__name__ + '.hash', 'w')
true_hash_file.write(test_hash + '\n')
true_hash_file.close()
true_hash_file = open(test.__name__ + '.hash', 'r')
raise NewTestError("New test found. Run again.")
true_hash = true_hash_file.read().strip()
true_hash_file.close()
assert true_hash == test_hash, 'Test "{}" failed with hash {}'.format(
test.__name__, test_hash)
return make_decorator(test)(hash_test)
示例13: search_related
def search_related(test):
def skip_test(*args):
raise SkipTest("Search not supported")
if not is_search_supported():
return make_decorator(test)(skip_test)
return test
示例14: skip
def skip(f):
""" Decorator to indicate a test should be skipped.
"""
def g(*args, **kw):
raise SkipTest()
return make_decorator(f)(g)
示例15: deprecated
def deprecated(f):
""" Decorator to indicate a test is deprecated.
"""
def g(*args, **kw):
raise DeprecatedTest()
return make_decorator(f)(g)