From 803941a53ae96cbd25d36b1a0e784582e1f51a62 Mon Sep 17 00:00:00 2001 From: RunasSudo Date: Wed, 30 Dec 2020 02:32:36 +1100 Subject: [PATCH] Raise Exception in Python when attempting to operate on incompatible types, as this will be invalid in JS --- pyRCV2/numbers/fixed_py.py | 18 ++++++++++++++++++ pyRCV2/numbers/int_py.py | 18 ++++++++++++++++++ pyRCV2/numbers/native_py.py | 18 ++++++++++++++++++ pyRCV2/numbers/rational_py.py | 18 ++++++++++++++++++ 4 files changed, 72 insertions(+) diff --git a/pyRCV2/numbers/fixed_py.py b/pyRCV2/numbers/fixed_py.py index fbbfe4e..e92b72e 100644 --- a/pyRCV2/numbers/fixed_py.py +++ b/pyRCV2/numbers/fixed_py.py @@ -44,23 +44,41 @@ class Fixed: return Rational(self.impl) def __add__(self, other): + if not isinstance(other, Fixed): + raise ValueError('Attempt to operate on incompatible types') return Fixed(self.impl + other.impl) def __sub__(self, other): + if not isinstance(other, Fixed): + raise ValueError('Attempt to operate on incompatible types') return Fixed(self.impl - other.impl) def __mul__(self, other): + if not isinstance(other, Fixed): + raise ValueError('Attempt to operate on incompatible types') return Fixed(self.impl * other.impl) def __truediv__(self, other): + if not isinstance(other, Fixed): + raise ValueError('Attempt to operate on incompatible types') return Fixed(self.impl / other.impl) def __eq__(self, other): + if not isinstance(other, Fixed): + raise ValueError('Attempt to operate on incompatible types') return self.impl == other.impl def __gt__(self, other): + if not isinstance(other, Fixed): + raise ValueError('Attempt to operate on incompatible types') return self.impl > other.impl def __ge__(self, other): + if not isinstance(other, Fixed): + raise ValueError('Attempt to operate on incompatible types') return self.impl >= other.impl def __lt__(self, other): + if not isinstance(other, Fixed): + raise ValueError('Attempt to operate on incompatible types') return self.impl < other.impl def __le__(self, other): + if not isinstance(other, Fixed): + raise ValueError('Attempt to operate on incompatible types') return self.impl <= other.impl def __floor__(self): diff --git a/pyRCV2/numbers/int_py.py b/pyRCV2/numbers/int_py.py index c8bcf8d..92e087f 100644 --- a/pyRCV2/numbers/int_py.py +++ b/pyRCV2/numbers/int_py.py @@ -37,23 +37,41 @@ class NativeInt: return Rational(self.impl) def __add__(self, other): + if not isinstance(other, NativeInt): + raise ValueError('Attempt to operate on incompatible types') return NativeInt(self.impl + other.impl) def __sub__(self, other): + if not isinstance(other, NativeInt): + raise ValueError('Attempt to operate on incompatible types') return NativeInt(self.impl - other.impl) def __mul__(self, other): + if not isinstance(other, NativeInt): + raise ValueError('Attempt to operate on incompatible types') return NativeInt(self.impl * other.impl) def __truediv__(self, other): + if not isinstance(other, NativeInt): + raise ValueError('Attempt to operate on incompatible types') return NativeInt(self.impl / other.impl) def __eq__(self, other): + if not isinstance(other, NativeInt): + raise ValueError('Attempt to operate on incompatible types') return self.impl == other.impl def __gt__(self, other): + if not isinstance(other, NativeInt): + raise ValueError('Attempt to operate on incompatible types') return self.impl > other.impl def __ge__(self, other): + if not isinstance(other, NativeInt): + raise ValueError('Attempt to operate on incompatible types') return self.impl >= other.impl def __lt__(self, other): + if not isinstance(other, NativeInt): + raise ValueError('Attempt to operate on incompatible types') return self.impl < other.impl def __le__(self, other): + if not isinstance(other, NativeInt): + raise ValueError('Attempt to operate on incompatible types') return self.impl <= other.impl def __floor__(self): diff --git a/pyRCV2/numbers/native_py.py b/pyRCV2/numbers/native_py.py index 2d3aee0..b4ee3a2 100644 --- a/pyRCV2/numbers/native_py.py +++ b/pyRCV2/numbers/native_py.py @@ -37,23 +37,41 @@ class Native: return Rational(self.impl) def __add__(self, other): + if not isinstance(other, Native): + raise ValueError('Attempt to operate on incompatible types') return Native(self.impl + other.impl) def __sub__(self, other): + if not isinstance(other, Native): + raise ValueError('Attempt to operate on incompatible types') return Native(self.impl - other.impl) def __mul__(self, other): + if not isinstance(other, Native): + raise ValueError('Attempt to operate on incompatible types') return Native(self.impl * other.impl) def __truediv__(self, other): + if not isinstance(other, Native): + raise ValueError('Attempt to operate on incompatible types') return Native(self.impl / other.impl) def __eq__(self, other): + if not isinstance(other, Native): + raise ValueError('Attempt to operate on incompatible types') return self.impl == other.impl def __gt__(self, other): + if not isinstance(other, Native): + raise ValueError('Attempt to operate on incompatible types') return self.impl > other.impl def __ge__(self, other): + if not isinstance(other, Native): + raise ValueError('Attempt to operate on incompatible types') return self.impl >= other.impl def __lt__(self, other): + if not isinstance(other, Native): + raise ValueError('Attempt to operate on incompatible types') return self.impl < other.impl def __le__(self, other): + if not isinstance(other, Native): + raise ValueError('Attempt to operate on incompatible types') return self.impl <= other.impl def __floor__(self): diff --git a/pyRCV2/numbers/rational_py.py b/pyRCV2/numbers/rational_py.py index 1e02ecc..4720c3f 100644 --- a/pyRCV2/numbers/rational_py.py +++ b/pyRCV2/numbers/rational_py.py @@ -46,23 +46,41 @@ class Rational: return Num(self.impl.numerator) / Num(self.impl.denominator) def __add__(self, other): + if not isinstance(other, Rational): + raise ValueError('Attempt to operate on incompatible types') return Rational(self.impl + other.impl) def __sub__(self, other): + if not isinstance(other, Rational): + raise ValueError('Attempt to operate on incompatible types') return Rational(self.impl - other.impl) def __mul__(self, other): + if not isinstance(other, Rational): + raise ValueError('Attempt to operate on incompatible types') return Rational(self.impl * other.impl) def __truediv__(self, other): + if not isinstance(other, Rational): + raise ValueError('Attempt to operate on incompatible types') return Rational(self.impl / other.impl) def __eq__(self, other): + if not isinstance(other, Rational): + raise ValueError('Attempt to operate on incompatible types') return self.impl == other.impl def __gt__(self, other): + if not isinstance(other, Rational): + raise ValueError('Attempt to operate on incompatible types') return self.impl > other.impl def __ge__(self, other): + if not isinstance(other, Rational): + raise ValueError('Attempt to operate on incompatible types') return self.impl >= other.impl def __lt__(self, other): + if not isinstance(other, Rational): + raise ValueError('Attempt to operate on incompatible types') return self.impl < other.impl def __le__(self, other): + if not isinstance(other, Rational): + raise ValueError('Attempt to operate on incompatible types') return self.impl <= other.impl def __floor__(self):