diff 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
line wrap: on
line diff
--- 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'))