Mercurial > public > mercurial-scm > hg
comparison contrib/python-zstandard/tests/common.py @ 43994:de7838053207
zstandard: vendor python-zstandard 0.13.0
Version 0.13.0 of the package was just released. It contains
an upgraded zstd C library which can result in some performance
wins, official support for Python 3.8, and a blackened code base.
There were no meaningful code or functionality changes in this
release of python-zstandard: just reformatting and an upgraded
zstd library version. So the diff seems much larger than what it
is.
Files were added without modifications.
The clang-format-ignorelist file was updated to reflect a new
header file in the zstd distribution.
# no-check-commit because 3rd party code has different style guidelines
Differential Revision: https://phab.mercurial-scm.org/D7770
author | Gregory Szorc <gregory.szorc@gmail.com> |
---|---|
date | Sat, 28 Dec 2019 09:55:45 -0800 |
parents | 675775c33ab6 |
children | 5e84a96d865b |
comparison
equal
deleted
inserted
replaced
43993:873d0fecb9a3 | 43994:de7838053207 |
---|---|
1 import imp | 1 import imp |
2 import inspect | 2 import inspect |
3 import io | 3 import io |
4 import os | 4 import os |
5 import types | 5 import types |
6 import unittest | |
6 | 7 |
7 try: | 8 try: |
8 import hypothesis | 9 import hypothesis |
9 except ImportError: | 10 except ImportError: |
10 hypothesis = None | 11 hypothesis = None |
12 | |
13 | |
14 class TestCase(unittest.TestCase): | |
15 if not getattr(unittest.TestCase, "assertRaisesRegex", False): | |
16 assertRaisesRegex = unittest.TestCase.assertRaisesRegexp | |
11 | 17 |
12 | 18 |
13 def make_cffi(cls): | 19 def make_cffi(cls): |
14 """Decorator to add CFFI versions of each test method.""" | 20 """Decorator to add CFFI versions of each test method.""" |
15 | 21 |
16 # The module containing this class definition should | 22 # The module containing this class definition should |
17 # `import zstandard as zstd`. Otherwise things may blow up. | 23 # `import zstandard as zstd`. Otherwise things may blow up. |
18 mod = inspect.getmodule(cls) | 24 mod = inspect.getmodule(cls) |
19 if not hasattr(mod, 'zstd'): | 25 if not hasattr(mod, "zstd"): |
20 raise Exception('test module does not contain "zstd" symbol') | 26 raise Exception('test module does not contain "zstd" symbol') |
21 | 27 |
22 if not hasattr(mod.zstd, 'backend'): | 28 if not hasattr(mod.zstd, "backend"): |
23 raise Exception('zstd symbol does not have "backend" attribute; did ' | 29 raise Exception( |
24 'you `import zstandard as zstd`?') | 30 'zstd symbol does not have "backend" attribute; did ' |
31 "you `import zstandard as zstd`?" | |
32 ) | |
25 | 33 |
26 # If `import zstandard` already chose the cffi backend, there is nothing | 34 # 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 | 35 # for us to do: we only add the cffi variation if the default backend |
28 # is the C extension. | 36 # is the C extension. |
29 if mod.zstd.backend == 'cffi': | 37 if mod.zstd.backend == "cffi": |
30 return cls | 38 return cls |
31 | 39 |
32 old_env = dict(os.environ) | 40 old_env = dict(os.environ) |
33 os.environ['PYTHON_ZSTANDARD_IMPORT_POLICY'] = 'cffi' | 41 os.environ["PYTHON_ZSTANDARD_IMPORT_POLICY"] = "cffi" |
34 try: | 42 try: |
35 try: | 43 try: |
36 mod_info = imp.find_module('zstandard') | 44 mod_info = imp.find_module("zstandard") |
37 mod = imp.load_module('zstandard_cffi', *mod_info) | 45 mod = imp.load_module("zstandard_cffi", *mod_info) |
38 except ImportError: | 46 except ImportError: |
39 return cls | 47 return cls |
40 finally: | 48 finally: |
41 os.environ.clear() | 49 os.environ.clear() |
42 os.environ.update(old_env) | 50 os.environ.update(old_env) |
43 | 51 |
44 if mod.backend != 'cffi': | 52 if mod.backend != "cffi": |
45 raise Exception('got the zstandard %s backend instead of cffi' % mod.backend) | 53 raise Exception("got the zstandard %s backend instead of cffi" % mod.backend) |
46 | 54 |
47 # If CFFI version is available, dynamically construct test methods | 55 # If CFFI version is available, dynamically construct test methods |
48 # that use it. | 56 # that use it. |
49 | 57 |
50 for attr in dir(cls): | 58 for attr in dir(cls): |
51 fn = getattr(cls, attr) | 59 fn = getattr(cls, attr) |
52 if not inspect.ismethod(fn) and not inspect.isfunction(fn): | 60 if not inspect.ismethod(fn) and not inspect.isfunction(fn): |
53 continue | 61 continue |
54 | 62 |
55 if not fn.__name__.startswith('test_'): | 63 if not fn.__name__.startswith("test_"): |
56 continue | 64 continue |
57 | 65 |
58 name = '%s_cffi' % fn.__name__ | 66 name = "%s_cffi" % fn.__name__ |
59 | 67 |
60 # Replace the "zstd" symbol with the CFFI module instance. Then copy | 68 # Replace the "zstd" symbol with the CFFI module instance. Then copy |
61 # the function object and install it in a new attribute. | 69 # the function object and install it in a new attribute. |
62 if isinstance(fn, types.FunctionType): | 70 if isinstance(fn, types.FunctionType): |
63 globs = dict(fn.__globals__) | 71 globs = dict(fn.__globals__) |
64 globs['zstd'] = mod | 72 globs["zstd"] = mod |
65 new_fn = types.FunctionType(fn.__code__, globs, name, | 73 new_fn = types.FunctionType( |
66 fn.__defaults__, fn.__closure__) | 74 fn.__code__, globs, name, fn.__defaults__, fn.__closure__ |
75 ) | |
67 new_method = new_fn | 76 new_method = new_fn |
68 else: | 77 else: |
69 globs = dict(fn.__func__.func_globals) | 78 globs = dict(fn.__func__.func_globals) |
70 globs['zstd'] = mod | 79 globs["zstd"] = mod |
71 new_fn = types.FunctionType(fn.__func__.func_code, globs, name, | 80 new_fn = types.FunctionType( |
72 fn.__func__.func_defaults, | 81 fn.__func__.func_code, |
73 fn.__func__.func_closure) | 82 globs, |
74 new_method = types.UnboundMethodType(new_fn, fn.im_self, | 83 name, |
75 fn.im_class) | 84 fn.__func__.func_defaults, |
85 fn.__func__.func_closure, | |
86 ) | |
87 new_method = types.UnboundMethodType(new_fn, fn.im_self, fn.im_class) | |
76 | 88 |
77 setattr(cls, name, new_method) | 89 setattr(cls, name, new_method) |
78 | 90 |
79 return cls | 91 return cls |
80 | 92 |
82 class NonClosingBytesIO(io.BytesIO): | 94 class NonClosingBytesIO(io.BytesIO): |
83 """BytesIO that saves the underlying buffer on close(). | 95 """BytesIO that saves the underlying buffer on close(). |
84 | 96 |
85 This allows us to access written data after close(). | 97 This allows us to access written data after close(). |
86 """ | 98 """ |
99 | |
87 def __init__(self, *args, **kwargs): | 100 def __init__(self, *args, **kwargs): |
88 super(NonClosingBytesIO, self).__init__(*args, **kwargs) | 101 super(NonClosingBytesIO, self).__init__(*args, **kwargs) |
89 self._saved_buffer = None | 102 self._saved_buffer = None |
90 | 103 |
91 def close(self): | 104 def close(self): |
133 | 146 |
134 for root, dirs, files in os.walk(os.path.dirname(__file__)): | 147 for root, dirs, files in os.walk(os.path.dirname(__file__)): |
135 dirs[:] = list(sorted(dirs)) | 148 dirs[:] = list(sorted(dirs)) |
136 for f in sorted(files): | 149 for f in sorted(files): |
137 try: | 150 try: |
138 with open(os.path.join(root, f), 'rb') as fh: | 151 with open(os.path.join(root, f), "rb") as fh: |
139 data = fh.read() | 152 data = fh.read() |
140 if data: | 153 if data: |
141 _source_files.append(data) | 154 _source_files.append(data) |
142 except OSError: | 155 except OSError: |
143 pass | 156 pass |
152 return _source_files | 165 return _source_files |
153 | 166 |
154 | 167 |
155 def generate_samples(): | 168 def generate_samples(): |
156 inputs = [ | 169 inputs = [ |
157 b'foo', | 170 b"foo", |
158 b'bar', | 171 b"bar", |
159 b'abcdef', | 172 b"abcdef", |
160 b'sometext', | 173 b"sometext", |
161 b'baz', | 174 b"baz", |
162 ] | 175 ] |
163 | 176 |
164 samples = [] | 177 samples = [] |
165 | 178 |
166 for i in range(128): | 179 for i in range(128): |
171 return samples | 184 return samples |
172 | 185 |
173 | 186 |
174 if hypothesis: | 187 if hypothesis: |
175 default_settings = hypothesis.settings(deadline=10000) | 188 default_settings = hypothesis.settings(deadline=10000) |
176 hypothesis.settings.register_profile('default', default_settings) | 189 hypothesis.settings.register_profile("default", default_settings) |
177 | 190 |
178 ci_settings = hypothesis.settings(deadline=20000, max_examples=1000) | 191 ci_settings = hypothesis.settings(deadline=20000, max_examples=1000) |
179 hypothesis.settings.register_profile('ci', ci_settings) | 192 hypothesis.settings.register_profile("ci", ci_settings) |
180 | 193 |
181 expensive_settings = hypothesis.settings(deadline=None, max_examples=10000) | 194 expensive_settings = hypothesis.settings(deadline=None, max_examples=10000) |
182 hypothesis.settings.register_profile('expensive', expensive_settings) | 195 hypothesis.settings.register_profile("expensive", expensive_settings) |
183 | 196 |
184 hypothesis.settings.load_profile( | 197 hypothesis.settings.load_profile(os.environ.get("HYPOTHESIS_PROFILE", "default")) |
185 os.environ.get('HYPOTHESIS_PROFILE', 'default')) |