From b6abae0a323509598458fe2f78c6a73045ef3889 Mon Sep 17 00:00:00 2001 From: Joe Marshall Date: Wed, 30 Aug 2023 10:22:30 +0100 Subject: [PATCH 1/5] patches for asynchronous functionality to work on pyodide --- setup.py | 5 +++-- web3/__init__.py | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/setup.py b/setup.py index 40d37901d1..8b2baeb13e 100644 --- a/setup.py +++ b/setup.py @@ -68,7 +68,8 @@ url="https://github.com/ethereum/web3.py", include_package_data=True, install_requires=[ - "aiohttp>=3.7.4.post0", + "aiohttp>=3.7.4.post0;sys_platform!='emscripten'", + "micropip;sys_platform=='emscripten'", "eth-abi>=4.0.0", "eth-account>=0.8.0", "eth-hash[pycryptodome]>=0.5.1", @@ -81,7 +82,7 @@ "pywin32>=223;platform_system=='Windows'", "requests>=2.16.0", "typing-extensions>=4.0.1", - "websockets>=10.0.0", + "websockets>=10.0.0;sys_platform!='emscripten'", "pyunormalize>=15.0.0", ], python_requires=">=3.7.2", diff --git a/web3/__init__.py b/web3/__init__.py index ada63485ea..77ab1b5e3f 100644 --- a/web3/__init__.py +++ b/web3/__init__.py @@ -1,6 +1,42 @@ from eth_account import Account # noqa: E402, import pkg_resources +if sys.platform=="emscripten": + # asynchronous connections and websockets aren't supported on + # emscripten yet. + # We mock the aiohttp and websockets module so that things import okay + import micropip + micropip.add_mock_package("aiohttp","1.0.0",modules={ + "aiohttp":''' +class ClientSession: + def __init__(self,*args,**argv): + raise NotImplementedError("Async web3 functions aren't supported on pyodide yet") +class ClientResponse: + def __init__(self,*args,**argv): + raise NotImplementedError("Async web3 functions aren't supported on pyodide yet") +class ClientTimeout: + def __init__(self,*args,**argv): + raise NotImplementedError("Async web3 functions aren't supported on pyodide yet") +'''} ) + # mock websockets + micropip.add_mock_package("websockets","1.0.0",modules={"websockets":'',"websockets.legacy":'', + "websockets.legacy.client":''' +class WebSocketClientProtocol: + def __init__(self,*args,**argv): + raise NotImplementedError("Websockets aren't supported on pyodide yet") +''',"websockets.client":''' +def connect(*args,**argv): + raise NotImplementedError("Websockets aren't supported on pyodide yet") +''',"websockets.exceptions":''' +class WebSocketException: + def __init__(self,*args,**argv): + raise NotImplementedError("Websockets aren't supported on pyodide yet") +class ConnectionClosedOK: + def __init__(self,*args,**argv): + raise NotImplementedError("Websockets aren't supported on pyodide yet") +'''} ) + + from web3.main import ( AsyncWeb3, Web3, From 3dbe76eb4e38926186d3e0b6c68dc488be03238a Mon Sep 17 00:00:00 2001 From: Joe Marshall Date: Wed, 30 Aug 2023 10:44:13 +0100 Subject: [PATCH 2/5] patch requests so that it works on pyodide --- setup.py | 4 +++- web3/__init__.py | 4 ++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 8b2baeb13e..d4fddbd635 100644 --- a/setup.py +++ b/setup.py @@ -69,7 +69,6 @@ include_package_data=True, install_requires=[ "aiohttp>=3.7.4.post0;sys_platform!='emscripten'", - "micropip;sys_platform=='emscripten'", "eth-abi>=4.0.0", "eth-account>=0.8.0", "eth-hash[pycryptodome]>=0.5.1", @@ -84,6 +83,9 @@ "typing-extensions>=4.0.1", "websockets>=10.0.0;sys_platform!='emscripten'", "pyunormalize>=15.0.0", + # pyodide includes - unversioned to use the one from pyodide distribution + "micropip;sys_platform=='emscripten'", + "pyodide-http;sys_platform=='emscripten'" ], python_requires=">=3.7.2", extras_require=extras_require, diff --git a/web3/__init__.py b/web3/__init__.py index 77ab1b5e3f..edf372fcdf 100644 --- a/web3/__init__.py +++ b/web3/__init__.py @@ -1,7 +1,11 @@ from eth_account import Account # noqa: E402, import pkg_resources +import sys if sys.platform=="emscripten": + # pyodide has a built in patcher which makes the requests module work + import pyodide_http + pyodide_http.patch_all() # asynchronous connections and websockets aren't supported on # emscripten yet. # We mock the aiohttp and websockets module so that things import okay From 44d3317fa54700116f98f27ddaa2a3d229a6e86f Mon Sep 17 00:00:00 2001 From: Joe Marshall Date: Thu, 31 Aug 2023 11:01:38 +0100 Subject: [PATCH 3/5] linting --- web3/__init__.py | 38 +++++++++++++++++++++++++++----------- 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/web3/__init__.py b/web3/__init__.py index edf372fcdf..fb60ac284d 100644 --- a/web3/__init__.py +++ b/web3/__init__.py @@ -2,16 +2,21 @@ import pkg_resources import sys -if sys.platform=="emscripten": - # pyodide has a built in patcher which makes the requests module work +if sys.platform == "emscripten": + # pyodide has a built in patcher which makes the requests module work import pyodide_http + pyodide_http.patch_all() # asynchronous connections and websockets aren't supported on - # emscripten yet. + # emscripten yet. # We mock the aiohttp and websockets module so that things import okay import micropip - micropip.add_mock_package("aiohttp","1.0.0",modules={ - "aiohttp":''' + + micropip.add_mock_package( + "aiohttp", + "1.0.0", + modules={ + "aiohttp": """ class ClientSession: def __init__(self,*args,**argv): raise NotImplementedError("Async web3 functions aren't supported on pyodide yet") @@ -21,24 +26,35 @@ def __init__(self,*args,**argv): class ClientTimeout: def __init__(self,*args,**argv): raise NotImplementedError("Async web3 functions aren't supported on pyodide yet") -'''} ) +""" + }, + ) # mock websockets - micropip.add_mock_package("websockets","1.0.0",modules={"websockets":'',"websockets.legacy":'', - "websockets.legacy.client":''' + micropip.add_mock_package( + "websockets", + "1.0.0", + modules={ + "websockets": "", + "websockets.legacy": "", + "websockets.legacy.client": """ class WebSocketClientProtocol: def __init__(self,*args,**argv): raise NotImplementedError("Websockets aren't supported on pyodide yet") -''',"websockets.client":''' +""", + "websockets.client": """ def connect(*args,**argv): raise NotImplementedError("Websockets aren't supported on pyodide yet") -''',"websockets.exceptions":''' +""", + "websockets.exceptions": """ class WebSocketException: def __init__(self,*args,**argv): raise NotImplementedError("Websockets aren't supported on pyodide yet") class ConnectionClosedOK: def __init__(self,*args,**argv): raise NotImplementedError("Websockets aren't supported on pyodide yet") -'''} ) +""", + }, + ) from web3.main import ( From a51bcce011a20fafbee2796dc5118f84f3fc5d74 Mon Sep 17 00:00:00 2001 From: Joe Marshall Date: Fri, 1 Sep 2023 10:42:06 +0100 Subject: [PATCH 4/5] lint --- web3/__init__.py | 48 +++++++++++++++++++++++++++++------------------- 1 file changed, 29 insertions(+), 19 deletions(-) diff --git a/web3/__init__.py b/web3/__init__.py index fb60ac284d..25f485027f 100644 --- a/web3/__init__.py +++ b/web3/__init__.py @@ -4,60 +4,70 @@ if sys.platform == "emscripten": # pyodide has a built in patcher which makes the requests module work - import pyodide_http + from pyodide_http import patch_all - pyodide_http.patch_all() + patch_all() # asynchronous connections and websockets aren't supported on # emscripten yet. # We mock the aiohttp and websockets module so that things import okay - import micropip + from micropip import add_mock_package - micropip.add_mock_package( + add_mock_package( "aiohttp", "1.0.0", modules={ "aiohttp": """ -class ClientSession: +class __NotImplemented: def __init__(self,*args,**argv): - raise NotImplementedError("Async web3 functions aren't supported on pyodide yet") -class ClientResponse: - def __init__(self,*args,**argv): - raise NotImplementedError("Async web3 functions aren't supported on pyodide yet") -class ClientTimeout: - def __init__(self,*args,**argv): - raise NotImplementedError("Async web3 functions aren't supported on pyodide yet") + raise NotImplementedError( + "Async web3 functions aren't supported on pyodide yet" + ) +class ClientSession(__NotImplemented): + pass +class ClientResponse(__NotImplemented): + pass +class ClientTimeout(__NotImplemented): + pass """ }, ) # mock websockets - micropip.add_mock_package( + add_mock_package( "websockets", "1.0.0", modules={ "websockets": "", "websockets.legacy": "", "websockets.legacy.client": """ -class WebSocketClientProtocol: +class WebSocketClientProtocol def __init__(self,*args,**argv): - raise NotImplementedError("Websockets aren't supported on pyodide yet") + raise NotImplementedError( + "Websockets aren't supported on pyodide yet" + ) """, "websockets.client": """ def connect(*args,**argv): - raise NotImplementedError("Websockets aren't supported on pyodide yet") + raise NotImplementedError( + "Websockets aren't supported on pyodide yet" + ) """, "websockets.exceptions": """ class WebSocketException: def __init__(self,*args,**argv): - raise NotImplementedError("Websockets aren't supported on pyodide yet") + raise NotImplementedError( + "Websockets aren't supported on pyodide yet" + ) class ConnectionClosedOK: def __init__(self,*args,**argv): - raise NotImplementedError("Websockets aren't supported on pyodide yet") + raise NotImplementedError( + "Websockets aren't supported on pyodide yet" + ) """, }, ) -from web3.main import ( +from web3.main import ( # noqa: E402 AsyncWeb3, Web3, ) From 10164fa0d40164acffe63a56fa2584dd29090c49 Mon Sep 17 00:00:00 2001 From: Joe Marshall Date: Wed, 6 Sep 2023 10:22:56 +0100 Subject: [PATCH 5/5] lint --- setup.py | 22 +++++---------------- web3/__init__.py | 50 ++++++++++++++++++------------------------------ 2 files changed, 24 insertions(+), 48 deletions(-) diff --git a/setup.py b/setup.py index d4fddbd635..6654660ba7 100644 --- a/setup.py +++ b/setup.py @@ -1,14 +1,8 @@ #!/usr/bin/env python -from setuptools import ( - find_packages, - setup, -) +from setuptools import find_packages, setup extras_require = { - "tester": [ - "eth-tester[py-evm]==v0.9.1-b.1", - "py-geth>=3.11.0", - ], + "tester": ["eth-tester[py-evm]==v0.9.1-b.1", "py-geth>=3.11.0"], "linter": [ "black>=22.1.0", "flake8==3.8.3", @@ -18,11 +12,7 @@ "types-requests>=2.26.1", "types-protobuf==3.19.13", ], - "docs": [ - "sphinx>=5.3.0", - "sphinx_rtd_theme>=1.0.0", - "towncrier>=21,<22", - ], + "docs": ["sphinx>=5.3.0", "sphinx_rtd_theme>=1.0.0", "towncrier>=21,<22"], "dev": [ "bumpversion", "flaky>=3.7.0", @@ -40,9 +30,7 @@ "when-changed>=0.3.0", "build>=0.9.0", ], - "ipfs": [ - "ipfshttpclient==0.8.0a2", - ], + "ipfs": ["ipfshttpclient==0.8.0a2"], } extras_require["dev"] = ( @@ -85,7 +73,7 @@ "pyunormalize>=15.0.0", # pyodide includes - unversioned to use the one from pyodide distribution "micropip;sys_platform=='emscripten'", - "pyodide-http;sys_platform=='emscripten'" + "pyodide-http;sys_platform=='emscripten'", ], python_requires=">=3.7.2", extras_require=extras_require, diff --git a/web3/__init__.py b/web3/__init__.py index 25f485027f..12c1b16fbb 100644 --- a/web3/__init__.py +++ b/web3/__init__.py @@ -36,14 +36,18 @@ class ClientTimeout(__NotImplemented): "websockets", "1.0.0", modules={ - "websockets": "", - "websockets.legacy": "", - "websockets.legacy.client": """ -class WebSocketClientProtocol + "websockets": """ +class __NotImplemented: def __init__(self,*args,**argv): raise NotImplementedError( - "Websockets aren't supported on pyodide yet" + "Async web3 functions aren't supported on pyodide yet" ) +""", + "websockets.legacy": "", + "websockets.legacy.client": """ +from websockets import __NotImplemented +class WebSocketClientProtocol(__NotImplemented): + pass """, "websockets.client": """ def connect(*args,**argv): @@ -52,37 +56,21 @@ def connect(*args,**argv): ) """, "websockets.exceptions": """ -class WebSocketException: - def __init__(self,*args,**argv): - raise NotImplementedError( - "Websockets aren't supported on pyodide yet" - ) -class ConnectionClosedOK: - def __init__(self,*args,**argv): - raise NotImplementedError( - "Websockets aren't supported on pyodide yet" - ) +from websockets import __NotImplemented +class WebSocketException(__NotImplemented): + pass +class ConnectionClosedOK(__NotImplemented): + pass """, }, ) -from web3.main import ( # noqa: E402 - AsyncWeb3, - Web3, -) -from web3.providers.async_rpc import ( # noqa: E402 - AsyncHTTPProvider, -) -from web3.providers.eth_tester import ( # noqa: E402 - EthereumTesterProvider, -) -from web3.providers.ipc import ( # noqa: E402 - IPCProvider, -) -from web3.providers.rpc import ( # noqa: E402 - HTTPProvider, -) +from web3.main import AsyncWeb3, Web3 # noqa: E402 +from web3.providers.async_rpc import AsyncHTTPProvider # noqa: E402 +from web3.providers.eth_tester import EthereumTesterProvider # noqa: E402 +from web3.providers.ipc import IPCProvider # noqa: E402 +from web3.providers.rpc import HTTPProvider # noqa: E402 from web3.providers.websocket import ( # noqa: E402 WebsocketProvider, WebsocketProviderV2,