This repository has been archived by the owner on Apr 14, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #47 from pquentin/bleach-spike-defer-backend-import
Ensure `import urllib3` can succeed even if trio and twisted aren't installed
- Loading branch information
Showing
10 changed files
with
188 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import pytest | ||
|
||
import urllib3 | ||
from urllib3.backends import Backend | ||
from urllib3._backends._loader import normalize_backend, load_backend | ||
|
||
|
||
requires_async_pool_manager = pytest.mark.skipif( | ||
not hasattr(urllib3, "AsyncPoolManager"), | ||
reason="async backends require AsyncPoolManager", | ||
) | ||
|
||
|
||
class TestNormalizeBackend(object): | ||
""" | ||
Assert that we fail correctly if we attempt to use an unknown or incompatible backend. | ||
""" | ||
def test_unknown(self): | ||
with pytest.raises(ValueError) as excinfo: | ||
normalize_backend("_unknown", async_mode=False) | ||
|
||
assert 'unknown backend specifier _unknown' == str(excinfo.value) | ||
|
||
def test_sync(self): | ||
assert normalize_backend(Backend("sync"), async_mode=False) == Backend("sync") | ||
assert normalize_backend("sync", async_mode=False) == Backend("sync") | ||
assert normalize_backend(None, async_mode=False) == Backend("sync") | ||
|
||
with pytest.raises(ValueError) as excinfo: | ||
normalize_backend(Backend("trio"), async_mode=False) | ||
assert ('trio backend needs to be run in async mode' == str(excinfo.value)) | ||
|
||
@requires_async_pool_manager | ||
def test_async(self): | ||
assert normalize_backend(Backend("trio"), async_mode=True) == Backend("trio") | ||
assert normalize_backend("twisted", async_mode=True) == Backend("twisted") | ||
|
||
with pytest.raises(ValueError) as excinfo: | ||
normalize_backend(Backend("sync"), async_mode=True) | ||
assert ('sync backend needs to be run in sync mode' == str(excinfo.value)) | ||
|
||
from twisted.internet import reactor | ||
assert ( | ||
normalize_backend(Backend("twisted", reactor=reactor), async_mode=True) | ||
== Backend("twisted", reactor=reactor)) | ||
|
||
|
||
class TestLoadBackend(object): | ||
""" | ||
Assert that we can load a normalized backend | ||
""" | ||
def test_sync(self): | ||
load_backend(normalize_backend("sync", async_mode=False)) | ||
|
||
@requires_async_pool_manager() | ||
def test_async(self): | ||
from twisted.internet import reactor | ||
load_backend(Backend("twisted", reactor=reactor)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +0,0 @@ | ||
from urllib3.packages import six | ||
from .sync_backend import SyncBackend | ||
|
||
__all__ = ['SyncBackend'] | ||
|
||
if six.PY3: | ||
from .trio_backend import TrioBackend | ||
from .twisted_backend import TwistedBackend | ||
__all__ += ['TrioBackend', 'TwistedBackend'] | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
from ..backends import Backend | ||
|
||
|
||
class Loader: | ||
|
||
def __init__(self, name, loader, is_async): | ||
self.name = name | ||
self.loader = loader | ||
self.is_async = is_async | ||
|
||
def __call__(self, *args, **kwargs): | ||
return self.loader(kwargs) | ||
|
||
|
||
def load_sync_backend(kwargs): | ||
from .sync_backend import SyncBackend | ||
return SyncBackend(**kwargs) | ||
|
||
|
||
def load_trio_backend(kwargs): | ||
from .trio_backend import TrioBackend | ||
return TrioBackend(**kwargs) | ||
|
||
|
||
def load_twisted_backend(kwargs): | ||
from .twisted_backend import TwistedBackend | ||
return TwistedBackend(**kwargs) | ||
|
||
|
||
def backend_directory(): | ||
""" | ||
We defer any heavy duty imports until the last minute. | ||
""" | ||
loaders = [ | ||
Loader( | ||
name="sync", | ||
loader=load_sync_backend, | ||
is_async=False, | ||
), | ||
Loader( | ||
name="trio", | ||
loader=load_trio_backend, | ||
is_async=True, | ||
), | ||
Loader( | ||
name="twisted", | ||
loader=load_twisted_backend, | ||
is_async=True, | ||
), | ||
] | ||
return { | ||
loader.name: loader for loader in loaders | ||
} | ||
|
||
|
||
def normalize_backend(backend, async_mode): | ||
if backend is None: | ||
backend = Backend(name="sync") # sync backend is the default | ||
elif not isinstance(backend, Backend): | ||
backend = Backend(name=backend) | ||
|
||
loaders_by_name = backend_directory() | ||
if backend.name not in loaders_by_name: | ||
raise ValueError("unknown backend specifier {}".format(backend.name)) | ||
|
||
loader = loaders_by_name[backend.name] | ||
|
||
if async_mode and not loader.is_async: | ||
raise ValueError("{} backend needs to be run in sync mode".format( | ||
loader.name)) | ||
|
||
if not async_mode and loader.is_async: | ||
raise ValueError("{} backend needs to be run in async mode".format( | ||
loader.name)) | ||
|
||
return backend | ||
|
||
|
||
def load_backend(backend): | ||
loaders_by_name = backend_directory() | ||
loader = loaders_by_name[backend.name] | ||
return loader(backend.kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
class Backend: | ||
""" | ||
Specifies the desired backend and any arguments passed to its constructor. | ||
Projects that use urllib3 can subclass this interface to expose it to users. | ||
""" | ||
def __init__(self, name, **kwargs): | ||
self.name = name | ||
self.kwargs = kwargs | ||
|
||
def __eq__(self, other): | ||
return self.name == other.name and self.kwargs == other.kwargs |