diff -r 1ce7a55b09d1 -r b1fb341d8a61 contrib/python-zstandard/tests/common.py --- a/contrib/python-zstandard/tests/common.py Sun Apr 08 01:08:43 2018 +0200 +++ b/contrib/python-zstandard/tests/common.py Mon Apr 09 10:13:29 2018 -0700 @@ -1,16 +1,48 @@ +import imp import inspect import io import os import types +try: + import hypothesis +except ImportError: + hypothesis = None + def make_cffi(cls): """Decorator to add CFFI versions of each test method.""" + # The module containing this class definition should + # `import zstandard as zstd`. Otherwise things may blow up. + mod = inspect.getmodule(cls) + if not hasattr(mod, 'zstd'): + raise Exception('test module does not contain "zstd" symbol') + + if not hasattr(mod.zstd, 'backend'): + raise Exception('zstd symbol does not have "backend" attribute; did ' + 'you `import zstandard as zstd`?') + + # If `import zstandard` already chose the cffi backend, there is nothing + # for us to do: we only add the cffi variation if the default backend + # is the C extension. + if mod.zstd.backend == 'cffi': + return cls + + old_env = dict(os.environ) + os.environ['PYTHON_ZSTANDARD_IMPORT_POLICY'] = 'cffi' try: - import zstd_cffi - except ImportError: - return cls + try: + mod_info = imp.find_module('zstandard') + mod = imp.load_module('zstandard_cffi', *mod_info) + except ImportError: + return cls + finally: + os.environ.clear() + os.environ.update(old_env) + + if mod.backend != 'cffi': + raise Exception('got the zstandard %s backend instead of cffi' % mod.backend) # If CFFI version is available, dynamically construct test methods # that use it. @@ -29,13 +61,13 @@ # the function object and install it in a new attribute. if isinstance(fn, types.FunctionType): globs = dict(fn.__globals__) - globs['zstd'] = zstd_cffi + globs['zstd'] = mod new_fn = types.FunctionType(fn.__code__, globs, name, fn.__defaults__, fn.__closure__) new_method = new_fn else: globs = dict(fn.__func__.func_globals) - globs['zstd'] = zstd_cffi + globs['zstd'] = mod new_fn = types.FunctionType(fn.__func__.func_code, globs, name, fn.__func__.func_defaults, fn.__func__.func_closure) @@ -86,3 +118,34 @@ pass return _source_files + + +def generate_samples(): + inputs = [ + b'foo', + b'bar', + b'abcdef', + b'sometext', + b'baz', + ] + + samples = [] + + for i in range(128): + samples.append(inputs[i % 5]) + samples.append(inputs[i % 5] * (i + 3)) + samples.append(inputs[-(i % 5)] * (i + 2)) + + return samples + + +if hypothesis: + default_settings = hypothesis.settings() + hypothesis.settings.register_profile('default', default_settings) + + ci_settings = hypothesis.settings(max_examples=2500, + max_iterations=2500) + hypothesis.settings.register_profile('ci', ci_settings) + + hypothesis.settings.load_profile( + os.environ.get('HYPOTHESIS_PROFILE', 'default'))