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:
parent
3358e5a30a
commit
a7c8579b13
|
@ -13,6 +13,9 @@ else: # pragma: no cover
|
||||||
|
|
||||||
def to_atomic(amount):
|
def to_atomic(amount):
|
||||||
"""Convert Monero decimal to atomic integer of piconero."""
|
"""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)
|
return int(amount * 10**12)
|
||||||
|
|
||||||
def from_atomic(amount):
|
def from_atomic(amount):
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
from decimal import Decimal
|
from decimal import Decimal
|
||||||
|
import sys
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
from monero.numbers import to_atomic, from_atomic, as_monero, PaymentID
|
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(to_atomic(Decimal('0.000000000001')), 1)
|
||||||
self.assertEqual(from_atomic(1), Decimal('0.000000000001'))
|
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):
|
def test_rounding(self):
|
||||||
self.assertEqual(to_atomic(Decimal('1.0000000000004')), 1000000000000)
|
self.assertEqual(to_atomic(Decimal('1.0000000000004')), 1000000000000)
|
||||||
self.assertEqual(as_monero(Decimal('1.0000000000014')), Decimal('1.000000000001'))
|
self.assertEqual(as_monero(Decimal('1.0000000000014')), Decimal('1.000000000001'))
|
||||||
|
|
Loading…
Reference in New Issue