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