Check type of amounts passed to conversion, avoid MemoryError when a string is being concatenated 10^12 times. Close#48

This commit is contained in:
Michał Sałaban 2019-04-04 13:06:04 +02:00
parent 3358e5a30a
commit a7c8579b13
2 changed files with 12 additions and 0 deletions

View File

@ -13,6 +13,9 @@ else: # pragma: no cover
def to_atomic(amount):
"""Convert Monero decimal to atomic integer of piconero."""
if not isinstance(amount, (Decimal, float) + _integer_types):
raise ValueError("Amount '{}' doesn't have numeric type. Only Decimal, int, long and "
"float (not recommended) are accepted as amounts.")
return int(amount * 10**12)
def from_atomic(amount):

View File

@ -1,4 +1,5 @@
from decimal import Decimal
import sys
import unittest
from monero.numbers import to_atomic, from_atomic, as_monero, PaymentID
@ -12,6 +13,14 @@ class NumbersTestCase(unittest.TestCase):
self.assertEqual(to_atomic(Decimal('0.000000000001')), 1)
self.assertEqual(from_atomic(1), Decimal('0.000000000001'))
def test_numeric_types(self):
"Only check if conversion of given type succeeds or fails."
self.assertTrue(to_atomic(1))
self.assertTrue(to_atomic(1.0))
if hasattr(sys, 'maxint'): # Python 2.x
self.assertTrue(to_atomic(sys.maxint + 1))
self.assertRaises(ValueError, to_atomic, '1')
def test_rounding(self):
self.assertEqual(to_atomic(Decimal('1.0000000000004')), 1000000000000)
self.assertEqual(as_monero(Decimal('1.0000000000014')), Decimal('1.000000000001'))