Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 3 additions & 13 deletions src-python/amazon/ion/reader_binary.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# License.
from collections import deque
from datetime import timedelta
from decimal import Decimal, localcontext
from decimal import Decimal
from enum import IntEnum
from functools import partial
from io import BytesIO
Expand Down Expand Up @@ -185,18 +185,8 @@ def _parse_decimal(buf):
exponent = _parse_var_int(buf, signed=True)
sign_bit, coefficient = _parse_signed_int_components(buf)

if coefficient == 0:
# Handle the zero cases--especially negative zero
value = Decimal((sign_bit, (0,), exponent))
else:
coefficient *= sign_bit and -1 or 1
with localcontext() as context:
# Adjusting precision for taking into account arbitrarily
# large/small numbers
context.prec = len(str(coefficient))
value = Decimal(coefficient).scaleb(exponent)

return value
# Construct the value without applying the caller's arithmetic context.
return Decimal((sign_bit, Decimal(coefficient).as_tuple().digits, exponent))


def _parse_sid_iter(data):
Expand Down
15 changes: 14 additions & 1 deletion tests/test_simpleion.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from functools import partial
from io import BytesIO, StringIO

from decimal import Decimal
from decimal import Decimal, localcontext
from itertools import chain
from math import isnan

Expand Down Expand Up @@ -948,6 +948,19 @@ def wrap_annot(content, annot_bytes):
return ivm + content


@mark.parametrize('text', ['1e3', '-1e3', '1e-3', '-1e-3', '1.2300', '0.00', '-0.00'])
@mark.parametrize('parse_eagerly', [True, False])
def test_binary_decimal_preserves_value_outside_context(text, parse_eagerly):
expected = Decimal(text)
data = dumps(expected, imports=[])
with localcontext() as context:
context.prec = 2
context.Emax = 2
context.Emin = -2
actual = loads(data, catalog=SymbolTableCatalog(), parse_eagerly=parse_eagerly)
assert actual.as_tuple() == expected.as_tuple()


def test_load_deeply_nested_annotations_binary_raises_ion_exception():
data = _build_nested_annotation_binary(3000)
buf = BytesIO(data)
Expand Down