summaryrefslogtreecommitdiffstats
path: root/Python/Dependencies/future-0.18.2/tests/test_future/test_requests.py
diff options
context:
space:
mode:
authoryum <yum.food.vr@gmail.com>2022-12-17 17:26:16 -0800
committeryum <yum.food.vr@gmail.com>2022-12-17 17:26:16 -0800
commit4d836989720523cd0363927e3e066f56b9dc445c (patch)
treef7a9ff7cb50eda1ff29e91c78067dcc5e0ce6233 /Python/Dependencies/future-0.18.2/tests/test_future/test_requests.py
parentda754e9cf5b192239826aa1619e1ada3c98daa45 (diff)
Check in `future` package
I hit some issues installing Whisper and had to embed this package. I haven't taken the time to deeply understand what's going on. I think that embedded Python follows different rules about resolving module paths than regular system Python. Basically, `future`'s setup.py has a line like `import src`, where `src` is a module inside future (like `future/src/__init__.py`). This doesn't work unless we put that directory on the search path.
Diffstat (limited to 'Python/Dependencies/future-0.18.2/tests/test_future/test_requests.py')
-rw-r--r--Python/Dependencies/future-0.18.2/tests/test_future/test_requests.py107
1 files changed, 107 insertions, 0 deletions
diff --git a/Python/Dependencies/future-0.18.2/tests/test_future/test_requests.py b/Python/Dependencies/future-0.18.2/tests/test_future/test_requests.py
new file mode 100644
index 0000000..e362a1f
--- /dev/null
+++ b/Python/Dependencies/future-0.18.2/tests/test_future/test_requests.py
@@ -0,0 +1,107 @@
+"""
+Tests for whether the standard library hooks in ``future`` are compatible with
+the ``requests`` package.
+"""
+
+from __future__ import absolute_import, unicode_literals, print_function
+from future import standard_library
+from future.tests.base import unittest, CodeHandler
+import textwrap
+import sys
+import os
+import io
+
+
+# Don't import requests first. This avoids the problem we want to expose:
+# with standard_library.suspend_hooks():
+# try:
+# import requests
+# except ImportError:
+# requests = None
+
+
+class write_module(object):
+ """
+ A context manager to streamline the tests. Creates a temp file for a
+ module designed to be imported by the ``with`` block, then removes it
+ afterwards.
+ """
+ def __init__(self, code, tempdir):
+ self.code = code
+ self.tempdir = tempdir
+
+ def __enter__(self):
+ print('Creating {0}test_imports_future_stdlib.py ...'.format(self.tempdir))
+ with io.open(self.tempdir + 'test_imports_future_stdlib.py', 'wt',
+ encoding='utf-8') as f:
+ f.write(textwrap.dedent(self.code))
+ sys.path.insert(0, self.tempdir)
+ return self
+
+ def __exit__(self, exc_type, exc_val, exc_tb):
+ """
+ If an exception occurred, we leave the file for inspection.
+ """
+ sys.path.remove(self.tempdir)
+ if exc_type is None:
+ # No exception occurred
+ os.remove(self.tempdir + 'test_imports_future_stdlib.py')
+ try:
+ os.remove(self.tempdir + 'test_imports_future_stdlib.pyc')
+ except OSError:
+ pass
+
+
+class TestRequests(CodeHandler):
+ """
+ This class tests whether the requests module conflicts with the
+ standard library import hooks, as in issue #19.
+ """
+ def test_remove_hooks_then_requests(self):
+ code = """
+ from future import standard_library
+ standard_library.install_hooks()
+
+ import builtins
+ import http.client
+ import html.parser
+ """
+ with write_module(code, self.tempdir):
+ import test_imports_future_stdlib
+ standard_library.remove_hooks()
+ try:
+ import requests
+ except ImportError:
+ print("Requests doesn't seem to be available. Skipping requests test ...")
+ else:
+ r = requests.get('http://google.com')
+ self.assertTrue(r)
+ self.assertTrue(True)
+
+
+ def test_requests_cm(self):
+ """
+ Tests whether requests can be used importing standard_library modules
+ previously with the hooks context manager
+ """
+ code = """
+ from future import standard_library
+ with standard_library.hooks():
+ import builtins
+ import html.parser
+ import http.client
+ """
+ with write_module(code, self.tempdir):
+ import test_imports_future_stdlib
+ try:
+ import requests
+ except ImportError:
+ print("Requests doesn't seem to be available. Skipping requests test ...")
+ else:
+ r = requests.get('http://google.com')
+ self.assertTrue(r)
+ self.assertTrue(True)
+
+
+if __name__ == '__main__':
+ unittest.main()