From 27ecd4b0ed2b065683c89a6fa5cba73859326914 Mon Sep 17 00:00:00 2001 From: Jason Carver Date: Wed, 27 Nov 2019 12:25:11 -0800 Subject: [PATCH] Mock dependencies that are excluded in readthedocs It seems preferable to import just fastecdsa. But if you do that, then some kind of side-effect doesn't happen, which means that `sec1` is not available as an attribute on `fastecdsa.encoding`. So we specifically mock the sub-modules as well. --- docs/conf.py | 16 ++++++++++++++++ libp2p/crypto/key_exchange.py | 4 +++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/docs/conf.py b/docs/conf.py index 9038ad0..ebff89b 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -286,3 +286,19 @@ doctest_default_flags = (0 | doctest.IGNORE_EXCEPTION_DETAIL | doctest.NORMALIZE_WHITESPACE ) + +# -- Mocked dependencies ---------------------------------------- + +# Mock out dependencies that are unbuildable on readthedocs, as recommended here: +# https://docs.readthedocs.io/en/rel/faq.html#i-get-import-errors-on-libraries-that-depend-on-c-modules + +import sys +from unittest.mock import MagicMock + +# Add new modules to mock here (it should be the same list as those excluded in setup.py) +MOCK_MODULES = [ + "fastecdsa", + "fastecdsa.encoding", + "fastecdsa.encoding.sec1", +] +sys.modules.update((mod_name, MagicMock()) for mod_name in MOCK_MODULES) diff --git a/libp2p/crypto/key_exchange.py b/libp2p/crypto/key_exchange.py index 17ec75a..2af4030 100644 --- a/libp2p/crypto/key_exchange.py +++ b/libp2p/crypto/key_exchange.py @@ -1,12 +1,14 @@ from typing import Callable, Tuple, cast -from fastecdsa.encoding.util import int_bytelen +from fastecdsa.encoding import util from libp2p.crypto.ecc import ECCPrivateKey, ECCPublicKey, create_new_key_pair from libp2p.crypto.keys import PublicKey SharedKeyGenerator = Callable[[bytes], bytes] +int_bytelen = util.int_bytelen + def create_ephemeral_key_pair(curve_type: str) -> Tuple[PublicKey, SharedKeyGenerator]: """Facilitates ECDH key exchange."""