|
|
5515ae |
From 4e6fe24a702b58117e282cf8c9eb612d4e85cd83 Mon Sep 17 00:00:00 2001
|
|
|
5515ae |
From: Paolo Bonzini <pbonzini@redhat.com>
|
|
|
5515ae |
Date: Wed, 2 Nov 2022 10:24:39 +0100
|
|
|
5515ae |
Subject: [PATCH 4/9] warn on equality/inequality with different types
|
|
|
5515ae |
Content-Type: text/plain
|
|
|
5515ae |
|
|
|
5515ae |
---
|
|
|
5515ae |
mesonbuild/interpreterbase/baseobjects.py | 18 ++++++++++--------
|
|
|
5515ae |
1 file changed, 10 insertions(+), 8 deletions(-)
|
|
|
5515ae |
|
|
|
5515ae |
diff --git a/mesonbuild/interpreterbase/baseobjects.py b/mesonbuild/interpreterbase/baseobjects.py
|
|
|
5515ae |
index a65b0536d..67f294387 100644
|
|
|
5515ae |
--- a/mesonbuild/interpreterbase/baseobjects.py
|
|
|
5515ae |
+++ b/mesonbuild/interpreterbase/baseobjects.py
|
|
|
5515ae |
@@ -12,7 +12,7 @@
|
|
|
5515ae |
# See the License for the specific language governing permissions and
|
|
|
5515ae |
# limitations under the License.
|
|
|
5515ae |
|
|
|
5515ae |
-from .. import mparser
|
|
|
5515ae |
+from .. import mparser, mlog
|
|
|
5515ae |
from .exceptions import InvalidCode, InvalidArguments
|
|
|
5515ae |
from .helpers import flatten, resolve_second_level_holders
|
|
|
5515ae |
from .operator import MesonOperator
|
|
|
5515ae |
@@ -98,6 +98,9 @@ class InterpreterObject:
|
|
|
5515ae |
if op[0] is None and other is not None:
|
|
|
5515ae |
raise MesonBugException(f'The unary operator `{operator.value}` of {self.display_name()} was passed the object {other} of type {type(other).__name__}')
|
|
|
5515ae |
if op[0] is not None and not isinstance(other, op[0]):
|
|
|
5515ae |
+ if operator in (MesonOperator.EQUALS, MesonOperator.NOT_EQUALS):
|
|
|
5515ae |
+ mlog.warning(f'Trying to compare values of different types ({self.display_name()}, {type(other).__name__})')
|
|
|
5515ae |
+ return operator == MesonOperator.NOT_EQUALS
|
|
|
5515ae |
raise InvalidArguments(f'The `{operator.value}` operator of {self.display_name()} does not accept objects of type {type(other).__name__} ({other})')
|
|
|
5515ae |
return op[1](other)
|
|
|
5515ae |
if operator in self.operators:
|
|
|
5515ae |
@@ -106,12 +109,8 @@ class InterpreterObject:
|
|
|
5515ae |
|
|
|
5515ae |
# Default comparison operator support
|
|
|
5515ae |
def _throw_comp_exception(self, other: TYPE_var, opt_type: str) -> T.NoReturn:
|
|
|
5515ae |
- raise InvalidArguments(textwrap.dedent(
|
|
|
5515ae |
- f'''
|
|
|
5515ae |
- Trying to compare values of different types ({self.display_name()}, {type(other).__name__}) using {opt_type}.
|
|
|
5515ae |
- This was deprecated and undefined behavior previously and is as of 0.60.0 a hard error.
|
|
|
5515ae |
- '''
|
|
|
5515ae |
- ))
|
|
|
5515ae |
+ mlog.warning(
|
|
|
5515ae |
+ 'Trying to compare values of different types ({self.display_name()}, {type(other).__name__}) using {opt_type}.')
|
|
|
5515ae |
|
|
|
5515ae |
def op_equals(self, other: TYPE_var) -> bool:
|
|
|
5515ae |
# We use `type(...) == type(...)` here to enforce an *exact* match for comparison. We
|
|
|
5515ae |
@@ -119,11 +118,12 @@ class InterpreterObject:
|
|
|
5515ae |
# would pass because this comparison must never be true: `derived_obj == base_obj`
|
|
|
5515ae |
if type(self) != type(other):
|
|
|
5515ae |
self._throw_comp_exception(other, '==')
|
|
|
5515ae |
+ return False
|
|
|
5515ae |
return self == other
|
|
|
5515ae |
|
|
|
5515ae |
def op_not_equals(self, other: TYPE_var) -> bool:
|
|
|
5515ae |
if type(self) != type(other):
|
|
|
5515ae |
- self._throw_comp_exception(other, '!=')
|
|
|
5515ae |
+ return True
|
|
|
5515ae |
return self != other
|
|
|
5515ae |
|
|
|
5515ae |
class MesonInterpreterObject(InterpreterObject):
|
|
|
5515ae |
@@ -157,11 +157,13 @@ class ObjectHolder(InterpreterObject, T.Generic[InterpreterObjectTypeVar]):
|
|
|
5515ae |
# See the comment from InterpreterObject why we are using `type()` here.
|
|
|
5515ae |
if type(self.held_object) != type(other):
|
|
|
5515ae |
self._throw_comp_exception(other, '==')
|
|
|
5515ae |
+ return False
|
|
|
5515ae |
return self.held_object == other
|
|
|
5515ae |
|
|
|
5515ae |
def op_not_equals(self, other: TYPE_var) -> bool:
|
|
|
5515ae |
if type(self.held_object) != type(other):
|
|
|
5515ae |
self._throw_comp_exception(other, '!=')
|
|
|
5515ae |
+ return True
|
|
|
5515ae |
return self.held_object != other
|
|
|
5515ae |
|
|
|
5515ae |
def __repr__(self) -> str:
|
|
|
5515ae |
--
|
|
|
5515ae |
2.38.1
|
|
|
5515ae |
|