From 4f6a6920862068c77460cc09c0fe04915d101499 Mon Sep 17 00:00:00 2001 From: Charles Cooper Date: Sun, 24 Oct 2021 08:12:26 -0700 Subject: [PATCH 1/2] Change use of getcontext() to fresh Context() https://github.com/vyperlang/vyper/pull/2479 disabled changes to the global decimal context to preserve compile-time constant semantics. This commit changes use of the global getcontext to a local context. It shouldn't change the behavior of the current code (since it didn't depend on modifying the global context, just having a local Context with the correct precision). --- brownie/convert/datatypes.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/brownie/convert/datatypes.py b/brownie/convert/datatypes.py index e9be3773f..dfbea6760 100644 --- a/brownie/convert/datatypes.py +++ b/brownie/convert/datatypes.py @@ -1,7 +1,7 @@ #!/usr/bin/python3 from copy import deepcopy -from decimal import Decimal, getcontext +from decimal import Context, Decimal from typing import Any, Dict, ItemsView, KeysView, List, Optional, Sequence, TypeVar, Union import eth_utils @@ -182,11 +182,11 @@ def _to_fixed(value: Any) -> Decimal: except TypeError: pass try: - ctx = getcontext() + ctx = Context() ctx.prec = 100 return Decimal(value, context=ctx) - except Exception: - raise TypeError(f"Cannot convert {type(value).__name__} '{value}' to decimal.") + except Exception as e: + raise TypeError(f"Cannot convert {type(value).__name__} '{value}' to decimal.") from e class EthAddress(str): From 32323ddce681a46775b2692ef44f8cff091d9824 Mon Sep 17 00:00:00 2001 From: Charles Cooper Date: Wed, 27 Oct 2021 06:57:16 -0700 Subject: [PATCH 2/2] fix use of Context previous commit made the change to getcontext to be local. but this is only correct if vyper is imported (which sets context precision). this commit sets the context precision correctly in the case where vyper is not imported. --- brownie/convert/datatypes.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/brownie/convert/datatypes.py b/brownie/convert/datatypes.py index dfbea6760..1c5ffbd2d 100644 --- a/brownie/convert/datatypes.py +++ b/brownie/convert/datatypes.py @@ -1,9 +1,14 @@ #!/usr/bin/python3 from copy import deepcopy -from decimal import Context, Decimal +from decimal import Decimal, getcontext from typing import Any, Dict, ItemsView, KeysView, List, Optional, Sequence, TypeVar, Union +try: + from vyper.exceptions import DecimalOverrideException +except ImportError: + DecimalOverrideException = BaseException # regular catch blocks shouldn't catch + import eth_utils from hexbytes import HexBytes @@ -182,9 +187,13 @@ def _to_fixed(value: Any) -> Decimal: except TypeError: pass try: - ctx = Context() - ctx.prec = 100 - return Decimal(value, context=ctx) + try: + # until vyper v0.3.1 we can mess with the precision + ctx = getcontext() + ctx.prec = 78 + except DecimalOverrideException: + pass # vyper set the precision, do nothing. + return Decimal(value) except Exception as e: raise TypeError(f"Cannot convert {type(value).__name__} '{value}' to decimal.") from e