Skip to content

Commit

Permalink
Generalize TmpEnvMgr thing
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielSchiavini committed Sep 11, 2024
1 parent df5b807 commit 201c0dd
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 14 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ source venv/bin/activate
# Install dev requirements
pip install -r dev-requirements.txt
# Install prod requirements (in the pyproject.tom)
pip install .
pip install .
```

*Note: When you delete your terminal/shell, you will need to reactivate this virtual environment again each time. To exit this python virtual environment, type `deactivate`*
Expand Down
31 changes: 21 additions & 10 deletions boa/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import contextlib
import sys

import boa.explorer
from boa.contracts.base_evm_contract import BoaError
from boa.contracts.vyper.vyper_contract import check_boa_error_matches
from boa.dealer import deal
from boa.debugger import BoaDebug
from boa.environment import Env
from boa.explorer import BlockExplorer
from boa.interpret import (
from_etherscan,
load,
Expand Down Expand Up @@ -49,19 +51,18 @@ def set_env(new_env):
# Simple context manager which functions like the `open()` builtin -
# if simply called, it never calls __exit__, but if used as a context manager,
# it calls __exit__ at scope exit
class _TmpEnvMgr:
def __init__(self, new_env):
global env
self.old_env = env

set_env(new_env)
class _TemporaryContext:
def __init__(self, old_value, new_value, set_value):
self.old_value = old_value
self.set_value = set_value
set_value(new_value)

def __enter__(self):
# dummy
pass

def __exit__(self, *args):
set_env(self.old_env)
self.set_value(self.old_value)


def fork(
Expand All @@ -75,20 +76,30 @@ def fork(

new_env = Env()
new_env.fork(url=url, block_identifier=block_identifier, deprecated=False, **kwargs)
return _TmpEnvMgr(new_env)
return _TemporaryContext(env, new_env, set_env)


def set_browser_env(address=None):
"""Set the environment to use the browser's network in Jupyter/Colab"""
# import locally because jupyter is generally not installed
global env
from boa.integrations.jupyter import BrowserEnv

return _TmpEnvMgr(BrowserEnv(address))
return _TemporaryContext(env, BrowserEnv(address), set_env)


def set_network_env(url):
"""Set the environment to use a custom network URL"""
return _TmpEnvMgr(NetworkEnv.from_url(url))
global env
return _TemporaryContext(env, NetworkEnv.from_url(url), set_env)


def set_etherscan(*args, **kwargs):
def set(explorer: BlockExplorer):
boa.explorer.etherscan = explorer

explorer = BlockExplorer(*args, **kwargs)
return _TemporaryContext(boa.explorer.etherscan, explorer, set)


def reset_env():
Expand Down
3 changes: 2 additions & 1 deletion tests/integration/fork/test_from_etherscan.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@

@pytest.fixture(scope="module", autouse=True)
def api_key():
boa.explorer.etherscan.api_key = os.environ["ETHERSCAN_API_KEY"]
with boa.set_etherscan(os.environ["ETHERSCAN_API_KEY"]):
yield


@pytest.fixture(scope="module")
Expand Down
4 changes: 2 additions & 2 deletions tests/unitary/test_boa.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
def test_env_mgr_noctx():
s = boa.env
t = boa.Env()
boa._TmpEnvMgr(t)
boa._TemporaryContext(t)
assert boa.env is not s
assert boa.env is t

Expand All @@ -13,7 +13,7 @@ def test_env_mgr_with_ctx():
s = boa.env
t = boa.Env()

with boa._TmpEnvMgr(t):
with boa._TemporaryContext(t):
assert boa.env is not s
assert boa.env is t

Expand Down

0 comments on commit 201c0dd

Please sign in to comment.