1.1 --- a/lib/operator/comparison.py Tue Nov 29 17:25:54 2016 +0100
1.2 +++ b/lib/operator/comparison.py Tue Nov 29 17:27:31 2016 +0100
1.3 @@ -3,7 +3,7 @@
1.4 """
1.5 Operator support.
1.6
1.7 -Copyright (C) 2010, 2013, 2015 Paul Boddie <paul@boddie.org.uk>
1.8 +Copyright (C) 2010, 2013, 2015, 2016 Paul Boddie <paul@boddie.org.uk>
1.9
1.10 This program is free software; you can redistribute it and/or modify it under
1.11 the terms of the GNU General Public License as published by the Free Software
1.12 @@ -25,13 +25,10 @@
1.13 # lambda functions. Thus, the appropriate methods are defined locally, but no
1.14 # attempt to obtain them is made until the generic function is called.
1.15
1.16 -# NOTE: The compiler should make it possible for the following functions to call
1.17 -# NOTE: the generic operator implementations with no additional call overhead.
1.18 -
1.19 # Comparison functions.
1.20
1.21 def eq(a, b):
1.22 - return binary_op(a, b, lambda a: a.__eq__, lambda b: b.__eq__)
1.23 + return binary_op(a, b, lambda a: a.__eq__, lambda b: b.__eq__, False)
1.24
1.25 def ge(a, b):
1.26 return binary_op(a, b, lambda a: a.__ge__, lambda b: b.__le__)
1.27 @@ -46,6 +43,6 @@
1.28 return binary_op(a, b, lambda a: a.__lt__, lambda b: b.__gt__)
1.29
1.30 def ne(a, b):
1.31 - return binary_op(a, b, lambda a: a.__ne__, lambda b: b.__ne__)
1.32 + return binary_op(a, b, lambda a: a.__ne__, lambda b: b.__ne__, True)
1.33
1.34 # vim: tabstop=4 expandtab shiftwidth=4
2.1 --- a/lib/operator/core.py Tue Nov 29 17:25:54 2016 +0100
2.2 +++ b/lib/operator/core.py Tue Nov 29 17:27:31 2016 +0100
2.3 @@ -24,7 +24,7 @@
2.4
2.5 from native import _is as is_, _is_not as is_not
2.6
2.7 -def binary_op(a, b, left_accessor, right_accessor):
2.8 +def binary_op(a, b, left_accessor, right_accessor, default=None):
2.9
2.10 """
2.11 A single parameterised function providing the binary operator mechanism for
2.12 @@ -57,11 +57,14 @@
2.13 return result
2.14
2.15 # Where no methods were available, or if neither method could support the
2.16 - # operation, raise an exception.
2.17 + # operation, raise an exception or provide a default result.
2.18
2.19 - raise TypeError
2.20 + if default is None:
2.21 + raise TypeError
2.22 + else:
2.23 + return default
2.24
2.25 -def unary_op(a, accessor):
2.26 +def unary_op(a, accessor, default=None):
2.27
2.28 """
2.29 A single parameterised function providing the unary operator mechanism for
2.30 @@ -81,9 +84,12 @@
2.31 return result
2.32
2.33 # Where no method was available, or if the method could not support the
2.34 - # operation, raise an exception.
2.35 + # operation, raise an exception or provide a default result.
2.36
2.37 - raise TypeError
2.38 + if default is None:
2.39 + raise TypeError
2.40 + else:
2.41 + return default
2.42
2.43 def augassign(a, b, augmented_accessor, left_accessor, right_accessor):
2.44