diff contrib/python-zstandard/tests/common.py @ 43999: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
line wrap: on
line diff
--- a/contrib/python-zstandard/tests/common.py	Fri Dec 27 18:54:57 2019 -0500
+++ b/contrib/python-zstandard/tests/common.py	Sat Dec 28 09:55:45 2019 -0800
@@ -3,6 +3,7 @@
 import io
 import os
 import types
+import unittest
 
 try:
     import hypothesis
@@ -10,39 +11,46 @@
     hypothesis = None
 
 
+class TestCase(unittest.TestCase):
+    if not getattr(unittest.TestCase, "assertRaisesRegex", False):
+        assertRaisesRegex = unittest.TestCase.assertRaisesRegexp
+
+
 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'):
+    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 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':
+    if mod.zstd.backend == "cffi":
         return cls
 
     old_env = dict(os.environ)
-    os.environ['PYTHON_ZSTANDARD_IMPORT_POLICY'] = 'cffi'
+    os.environ["PYTHON_ZSTANDARD_IMPORT_POLICY"] = "cffi"
     try:
         try:
-            mod_info = imp.find_module('zstandard')
-            mod = imp.load_module('zstandard_cffi', *mod_info)
+            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 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.
@@ -52,27 +60,31 @@
         if not inspect.ismethod(fn) and not inspect.isfunction(fn):
             continue
 
-        if not fn.__name__.startswith('test_'):
+        if not fn.__name__.startswith("test_"):
             continue
 
-        name = '%s_cffi' % fn.__name__
+        name = "%s_cffi" % fn.__name__
 
         # Replace the "zstd" symbol with the CFFI module instance. Then copy
         # the function object and install it in a new attribute.
         if isinstance(fn, types.FunctionType):
             globs = dict(fn.__globals__)
-            globs['zstd'] = mod
-            new_fn = types.FunctionType(fn.__code__, globs, name,
-                                        fn.__defaults__, fn.__closure__)
+            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'] = mod
-            new_fn = types.FunctionType(fn.__func__.func_code, globs, name,
-                                        fn.__func__.func_defaults,
-                                        fn.__func__.func_closure)
-            new_method = types.UnboundMethodType(new_fn, fn.im_self,
-                                                 fn.im_class)
+            globs["zstd"] = mod
+            new_fn = types.FunctionType(
+                fn.__func__.func_code,
+                globs,
+                name,
+                fn.__func__.func_defaults,
+                fn.__func__.func_closure,
+            )
+            new_method = types.UnboundMethodType(new_fn, fn.im_self, fn.im_class)
 
         setattr(cls, name, new_method)
 
@@ -84,6 +96,7 @@
 
     This allows us to access written data after close().
     """
+
     def __init__(self, *args, **kwargs):
         super(NonClosingBytesIO, self).__init__(*args, **kwargs)
         self._saved_buffer = None
@@ -135,7 +148,7 @@
         dirs[:] = list(sorted(dirs))
         for f in sorted(files):
             try:
-                with open(os.path.join(root, f), 'rb') as fh:
+                with open(os.path.join(root, f), "rb") as fh:
                     data = fh.read()
                     if data:
                         _source_files.append(data)
@@ -154,11 +167,11 @@
 
 def generate_samples():
     inputs = [
-        b'foo',
-        b'bar',
-        b'abcdef',
-        b'sometext',
-        b'baz',
+        b"foo",
+        b"bar",
+        b"abcdef",
+        b"sometext",
+        b"baz",
     ]
 
     samples = []
@@ -173,13 +186,12 @@
 
 if hypothesis:
     default_settings = hypothesis.settings(deadline=10000)
-    hypothesis.settings.register_profile('default', default_settings)
+    hypothesis.settings.register_profile("default", default_settings)
 
     ci_settings = hypothesis.settings(deadline=20000, max_examples=1000)
-    hypothesis.settings.register_profile('ci', ci_settings)
+    hypothesis.settings.register_profile("ci", ci_settings)
 
     expensive_settings = hypothesis.settings(deadline=None, max_examples=10000)
-    hypothesis.settings.register_profile('expensive', expensive_settings)
+    hypothesis.settings.register_profile("expensive", expensive_settings)
 
-    hypothesis.settings.load_profile(
-        os.environ.get('HYPOTHESIS_PROFILE', 'default'))
+    hypothesis.settings.load_profile(os.environ.get("HYPOTHESIS_PROFILE", "default"))