summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarcello Stanisci <stanisci.m@gmail.com>2019-02-05 15:32:56 +0100
committerMarcello Stanisci <stanisci.m@gmail.com>2019-03-29 18:00:53 +0100
commiteadac43aad616839f56067e278ac586fa940d558 (patch)
treecc4721cb9c36014f3a7210cfe3e5b80665a3c903
parenta3e7f9d9ea77f5b3ca7b8547f3854eb149e1c6c8 (diff)
downloadbank-eadac43aad616839f56067e278ac586fa940d558.tar.gz
bank-eadac43aad616839f56067e278ac586fa940d558.tar.bz2
bank-eadac43aad616839f56067e278ac586fa940d558.zip
Hot-addressing #5542.
-rw-r--r--talerbank/app/amount.py61
1 files changed, 25 insertions, 36 deletions
diff --git a/talerbank/app/amount.py b/talerbank/app/amount.py
index 458fdea..959dab4 100644
--- a/talerbank/app/amount.py
+++ b/talerbank/app/amount.py
@@ -5,23 +5,25 @@
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
-# version 2.1 of the License, or (at your option) any later version.
+# version 2.1 of the License, or (at your option) any later
+# version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-# Lesser General Public License for more details.
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
-# License along with this library; if not, write to the Free Software
-# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+# License along with this library; if not, write to the Free
+# Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+# Boston, MA 02110-1301 USA
#
# @author Marcello Stanisci
# @version 0.1
# @repository https://git.taler.net/copylib.git/
# This code is "copylib", it is versioned under the Git repository
-# mentioned above, and it is meant to be manually copied into any project
-# which might need it.
+# mentioned above, and it is meant to be manually copied into
+# any project which might need it.
##
@@ -41,26 +43,6 @@ class CurrencyMismatch(Exception):
super(CurrencyMismatch, self).__init__(
"%s vs %s" % (curr1, curr2))
-
-##
-# Exception class to raise when an amount object
-# is getting a value "too big"; the threshold here is
-# the maximum value that the wallet can handle, which
-# is 2^53 - 1.
-class AmountOverflow(Exception):
- hint = "Overflowing amount"
-
- ##
- # Init constructor.
- #
- # @param self the object itself
- # @param part whether the exception occurred in a 'value'
- # or in a 'fraction' part of an amount.
- def __init__(self, part) -> None:
- super(BadFormatAmount, self).__init__(
- "This part exceedes 2^53 -1: " + part)
-
-
##
# Exception class to raise when a amount string is not valid.
class BadFormatAmount(Exception):
@@ -75,9 +57,18 @@ class BadFormatAmount(Exception):
super(BadFormatAmount, self).__init__(
"Bad format amount: " + faulty_str)
-
##
# Main Amount class.
+class NumberTooBig(Exception):
+ def __init__(self) -> None:
+ super(BadFormatAmount, self).__init__(
+ "Number given is too big!")
+
+class NegativeNumber(Exception):
+ def __init__(self) -> None:
+ super(BadFormatAmount, self).__init__(
+ "Negative number given as value and/or fraction!")
+
class Amount:
##
# How many "fraction" units make one "value" unit of currency
@@ -102,17 +93,14 @@ class Amount:
# @param value integer part the amount
# @param fraction fractional part of the amount
def __init__(self, currency, value=0, fraction=0) -> None:
-
- # NOTE: the following, not-so-good, assertions got
- # eliminated in the _stable_ branch due to a recent
- # hot-fix. So this file is going to have to be manually
- # deconflicted.
- assert value >= 0 and fraction >= 0
+ if value < 0 or fraction < 0:
+ raise NegativeNumber()
self.value = value
self.fraction = fraction
self.currency = currency
self.__normalize()
- assert self.value <= Amount._max_value()
+ if self.value > Amount._max_value():
+ raise NumberTooBig()
##
# Normalize amount. It means it makes sure that the
@@ -241,7 +229,8 @@ class Amount:
# @param pretty if True, put the currency in the last position and
# omit the colon.
def stringify(self, ndigits: int, pretty=False) -> str:
- assert ndigits > 0
+ if ndigits <= 0:
+ raise BadFormatAmount("ndigits must be > 0")
tmp = self.fraction
fraction_str = ""
while ndigits > 0: