# HG changeset patch # User Paul Boddie # Date 1480436851 -3600 # Node ID d16731ea85682b0ee1dd95e675bf630dfaafb9be # Parent 661a64c3c23729f5ca867ace14b253a1c1069f1e Introduced default results for certain operators instead of raising TypeError. Removed a reminder comment that should be placed elsewhere. diff -r 661a64c3c237 -r d16731ea8568 lib/operator/comparison.py --- a/lib/operator/comparison.py Tue Nov 29 17:25:54 2016 +0100 +++ b/lib/operator/comparison.py Tue Nov 29 17:27:31 2016 +0100 @@ -3,7 +3,7 @@ """ Operator support. -Copyright (C) 2010, 2013, 2015 Paul Boddie +Copyright (C) 2010, 2013, 2015, 2016 Paul Boddie This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software @@ -25,13 +25,10 @@ # lambda functions. Thus, the appropriate methods are defined locally, but no # attempt to obtain them is made until the generic function is called. -# NOTE: The compiler should make it possible for the following functions to call -# NOTE: the generic operator implementations with no additional call overhead. - # Comparison functions. def eq(a, b): - return binary_op(a, b, lambda a: a.__eq__, lambda b: b.__eq__) + return binary_op(a, b, lambda a: a.__eq__, lambda b: b.__eq__, False) def ge(a, b): return binary_op(a, b, lambda a: a.__ge__, lambda b: b.__le__) @@ -46,6 +43,6 @@ return binary_op(a, b, lambda a: a.__lt__, lambda b: b.__gt__) def ne(a, b): - return binary_op(a, b, lambda a: a.__ne__, lambda b: b.__ne__) + return binary_op(a, b, lambda a: a.__ne__, lambda b: b.__ne__, True) # vim: tabstop=4 expandtab shiftwidth=4 diff -r 661a64c3c237 -r d16731ea8568 lib/operator/core.py --- a/lib/operator/core.py Tue Nov 29 17:25:54 2016 +0100 +++ b/lib/operator/core.py Tue Nov 29 17:27:31 2016 +0100 @@ -24,7 +24,7 @@ from native import _is as is_, _is_not as is_not -def binary_op(a, b, left_accessor, right_accessor): +def binary_op(a, b, left_accessor, right_accessor, default=None): """ A single parameterised function providing the binary operator mechanism for @@ -57,11 +57,14 @@ return result # Where no methods were available, or if neither method could support the - # operation, raise an exception. + # operation, raise an exception or provide a default result. - raise TypeError + if default is None: + raise TypeError + else: + return default -def unary_op(a, accessor): +def unary_op(a, accessor, default=None): """ A single parameterised function providing the unary operator mechanism for @@ -81,9 +84,12 @@ return result # Where no method was available, or if the method could not support the - # operation, raise an exception. + # operation, raise an exception or provide a default result. - raise TypeError + if default is None: + raise TypeError + else: + return default def augassign(a, b, augmented_accessor, left_accessor, right_accessor):