Skip to content

Commit

Permalink
mpi: Simplify code for comparisons
Browse files Browse the repository at this point in the history
  • Loading branch information
Synss committed May 11, 2019
1 parent 586db0a commit f24ff81
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 27 deletions.
1 change: 1 addition & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Misc.
* *: Format Python files with `black`
* ci: Check that the docs build in CI.
* ci: Repair test coverage measurements on coveralls.
* mpi: Simplify code for comparisons.


[0.17.1] - 2019-04-16
Expand Down
38 changes: 11 additions & 27 deletions src/mbedtls/mpi.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -190,13 +190,11 @@ cdef class MPI:
# Negative values are not supported.
return self

def __eq__(self, other):
if not all((isinstance(self, numbers.Integral),
isinstance(other, numbers.Integral))):
def __eq__(MPI self, other):
if not isinstance(other, numbers.Integral):
return NotImplemented
cdef MPI self_ = MPI(self)
cdef MPI other_ = MPI(other)
return mbedtls_mpi_cmp_mpi(&self_._ctx, &other_._ctx) == 0
return mbedtls_mpi_cmp_mpi(&self._ctx, &other_._ctx) == 0

def __float__(self):
return float(long(self))
Expand Down Expand Up @@ -239,34 +237,20 @@ cdef class MPI:
&result._ctx, &self_._ctx, &other_._ctx))
return result

def __lt__(self, other):
if not all((isinstance(self, numbers.Integral),
isinstance(other, numbers.Integral))):
def __lt__(MPI self, other):
if not isinstance(other, numbers.Integral):
return NotImplemented
cdef MPI self_ = MPI(self)
cdef MPI other_ = MPI(other)
return mbedtls_mpi_cmp_mpi(&self_._ctx, &other_._ctx) == -1
return mbedtls_mpi_cmp_mpi(&self._ctx, &other_._ctx) == -1

def __le__(self, other):
try:
other = _mpi.MPI(other)
except TypeError:
return NotImplemented
return any((self < other, self == other))
def __le__(MPI self, other):
return self < other or self == other

def __gt__(self, other):
try:
other = _mpi.MPI(other)
except TypeError:
return NotImplemented
def __gt__(MPI self, other):
return not self <= other

def __ge__(self, other):
try:
other = _mpi.MPI(other)
except TypeError:
return NotImplemented
return any((self > other, self == other))
def __ge__(MPI self, other):
return self > other or self == other

def __complex__(self):
return complex(float(self))
Expand Down

0 comments on commit f24ff81

Please sign in to comment.