Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

msgpack: support decimals with negative scale #298

Merged
merged 2 commits into from
Jun 30, 2023
Merged
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
3 changes: 0 additions & 3 deletions .github/workflows/testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@ jobs:
# "This page is taking too long to load." error. Thus we use
# pairwise testing.
include:
- tarantool: '2.11'
python: '3.11'
msgpack-deps: 'msgpack-python==0.4.0'
- tarantool: '2.11'
python: '3.11'
msgpack-deps: 'msgpack==0.5.0'
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- Allow to require specific server protocol version and features (#267).

### Changed
- Drop `msgpack-python` support. Use `msgpack` instead.

### Fixed
- Parsing of E-notation Tarantool decimals with positive exponent (PR #298).

## 1.0.0 - 2023-04-17

### Changed
Expand Down
19 changes: 16 additions & 3 deletions tarantool/msgpack_ext/decimal.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@

from decimal import Decimal

import msgpack

from tarantool.error import MsgpackError, MsgpackWarning, warn

EXT_ID = 1
Expand Down Expand Up @@ -353,7 +355,12 @@ def decode(data, _):
:raise: :exc:`~tarantool.error.MsgpackError`
"""

scale = data[0]
# A decimal starts with mp_int or mp_uint followed by raw bytes.
unpacker = msgpack.Unpacker()
unpacker.feed(data)

scale = unpacker.unpack()
scale_size = unpacker.tell()

sign = get_str_sign(data[-1] & 0x0f)

Expand All @@ -362,7 +369,7 @@ def decode(data, _):

add_str_digit(data[-1] >> 4, digits_reverted, scale)

for i in range(len(data) - 2, 0, -1):
for i in range(len(data) - 2, scale_size - 1, -1):
add_str_digit(data[i] & 0x0f, digits_reverted, scale)
add_str_digit(data[i] >> 4, digits_reverted, scale)

Expand All @@ -372,6 +379,12 @@ def decode(data, _):

digits_reverted.append(sign)

str_repr = ''.join(digits_reverted[::-1])
digits = digits_reverted[::-1]

# Add trailing zeroes in case of a negative scale
for i in range(0, -1 * scale):
add_str_digit(0, digits, scale)

str_repr = ''.join(digits)

return Decimal(str_repr)
33 changes: 33 additions & 0 deletions test/suites/test_decimal.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,39 @@ def setUp(self):
b'\x09\x87\x65\x43\x21\x98\x76\x54\x32\x1d'),
'tarantool': "decimal.new('-1234567891234567890.0987654321987654321')",
},
'decimal_exponent_1': {
'python': decimal.Decimal('1e33'),
'msgpack': (b'\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00'
b'\x00\x00\x00\x00\x00\x00\x00\x0c'),
'tarantool': "decimal.new('1e33')",
},
'decimal_exponent_2': {
'python': decimal.Decimal('1.2345e33'),
'msgpack': (b'\x00\x01\x23\x45\x00\x00\x00\x00\x00\x00\x00'
b'\x00\x00\x00\x00\x00\x00\x00\x0c'),
'tarantool': "decimal.new('1.2345e33')",
},
'decimal_exponent_3': {
'python': decimal.Decimal('1.2345e2'),
'msgpack': (b'\x02\x12\x34\x5c'),
'tarantool': "decimal.new('1.2345e2')",
},
'decimal_exponent_4': {
'python': decimal.Decimal('1.2345e4'),
'msgpack': (b'\x00\x12\x34\x5c'),
'tarantool': "decimal.new('1.2345e4')",
},
'decimal_exponent_5': {
'python': decimal.Decimal('-1e33'),
'msgpack': (b'\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00'
b'\x00\x00\x00\x00\x00\x00\x00\x0d'),
'tarantool': "decimal.new('-1e33')",
},
'decimal_exponent_6': {
'python': decimal.Decimal('1e-33'),
'msgpack': (b'\x21\x1c'),
DifferentialOrange marked this conversation as resolved.
Show resolved Hide resolved
'tarantool': "decimal.new('1e-33')",
},
}

def test_msgpack_decode(self):
Expand Down