comparison contrib/python-zstandard/tests/common.py @ 37495:b1fb341d8a61

zstandard: vendor python-zstandard 0.9.0 This was just released. It features a number of goodies. More info at https://gregoryszorc.com/blog/2018/04/09/release-of-python-zstandard-0.9/. The clang-format ignore list was updated to reflect the new source of files. The project contains a vendored copy of zstandard 1.3.4. The old version was 1.1.3. One of the changes between those versions is that zstandard is now dual licensed BSD + GPLv2 and the patent rights grant has been removed. Good riddance. The API should be backwards compatible. So no changes in core should be needed. However, there were a number of changes in the library that we'll want to adapt to. Those will be addressed in subsequent commits. Differential Revision: https://phab.mercurial-scm.org/D3198
author Gregory Szorc <gregory.szorc@gmail.com>
date Mon, 09 Apr 2018 10:13:29 -0700
parents e0dc40530c5a
children 675775c33ab6
comparison
equal deleted inserted replaced
37494:1ce7a55b09d1 37495:b1fb341d8a61
1 import imp
1 import inspect 2 import inspect
2 import io 3 import io
3 import os 4 import os
4 import types 5 import types
5 6
7 try:
8 import hypothesis
9 except ImportError:
10 hypothesis = None
11
6 12
7 def make_cffi(cls): 13 def make_cffi(cls):
8 """Decorator to add CFFI versions of each test method.""" 14 """Decorator to add CFFI versions of each test method."""
9 15
16 # The module containing this class definition should
17 # `import zstandard as zstd`. Otherwise things may blow up.
18 mod = inspect.getmodule(cls)
19 if not hasattr(mod, 'zstd'):
20 raise Exception('test module does not contain "zstd" symbol')
21
22 if not hasattr(mod.zstd, 'backend'):
23 raise Exception('zstd symbol does not have "backend" attribute; did '
24 'you `import zstandard as zstd`?')
25
26 # If `import zstandard` already chose the cffi backend, there is nothing
27 # for us to do: we only add the cffi variation if the default backend
28 # is the C extension.
29 if mod.zstd.backend == 'cffi':
30 return cls
31
32 old_env = dict(os.environ)
33 os.environ['PYTHON_ZSTANDARD_IMPORT_POLICY'] = 'cffi'
10 try: 34 try:
11 import zstd_cffi 35 try:
12 except ImportError: 36 mod_info = imp.find_module('zstandard')
13 return cls 37 mod = imp.load_module('zstandard_cffi', *mod_info)
38 except ImportError:
39 return cls
40 finally:
41 os.environ.clear()
42 os.environ.update(old_env)
43
44 if mod.backend != 'cffi':
45 raise Exception('got the zstandard %s backend instead of cffi' % mod.backend)
14 46
15 # If CFFI version is available, dynamically construct test methods 47 # If CFFI version is available, dynamically construct test methods
16 # that use it. 48 # that use it.
17 49
18 for attr in dir(cls): 50 for attr in dir(cls):
27 59
28 # Replace the "zstd" symbol with the CFFI module instance. Then copy 60 # Replace the "zstd" symbol with the CFFI module instance. Then copy
29 # the function object and install it in a new attribute. 61 # the function object and install it in a new attribute.
30 if isinstance(fn, types.FunctionType): 62 if isinstance(fn, types.FunctionType):
31 globs = dict(fn.__globals__) 63 globs = dict(fn.__globals__)
32 globs['zstd'] = zstd_cffi 64 globs['zstd'] = mod
33 new_fn = types.FunctionType(fn.__code__, globs, name, 65 new_fn = types.FunctionType(fn.__code__, globs, name,
34 fn.__defaults__, fn.__closure__) 66 fn.__defaults__, fn.__closure__)
35 new_method = new_fn 67 new_method = new_fn
36 else: 68 else:
37 globs = dict(fn.__func__.func_globals) 69 globs = dict(fn.__func__.func_globals)
38 globs['zstd'] = zstd_cffi 70 globs['zstd'] = mod
39 new_fn = types.FunctionType(fn.__func__.func_code, globs, name, 71 new_fn = types.FunctionType(fn.__func__.func_code, globs, name,
40 fn.__func__.func_defaults, 72 fn.__func__.func_defaults,
41 fn.__func__.func_closure) 73 fn.__func__.func_closure)
42 new_method = types.UnboundMethodType(new_fn, fn.im_self, 74 new_method = types.UnboundMethodType(new_fn, fn.im_self,
43 fn.im_class) 75 fn.im_class)
84 _source_files.append(data) 116 _source_files.append(data)
85 except OSError: 117 except OSError:
86 pass 118 pass
87 119
88 return _source_files 120 return _source_files
121
122
123 def generate_samples():
124 inputs = [
125 b'foo',
126 b'bar',
127 b'abcdef',
128 b'sometext',
129 b'baz',
130 ]
131
132 samples = []
133
134 for i in range(128):
135 samples.append(inputs[i % 5])
136 samples.append(inputs[i % 5] * (i + 3))
137 samples.append(inputs[-(i % 5)] * (i + 2))
138
139 return samples
140
141
142 if hypothesis:
143 default_settings = hypothesis.settings()
144 hypothesis.settings.register_profile('default', default_settings)
145
146 ci_settings = hypothesis.settings(max_examples=2500,
147 max_iterations=2500)
148 hypothesis.settings.register_profile('ci', ci_settings)
149
150 hypothesis.settings.load_profile(
151 os.environ.get('HYPOTHESIS_PROFILE', 'default'))