提交 060fc688 authored 作者: gdesjardins's avatar gdesjardins

Fixes ticket #375.

a < b < c was failing silently by generating the graph "lt(b,c)". We now forbid "boolean" comparison of the return value of __lt__, __le__, __gt__, __ge__. We cannot forbid "boolean" comparison of TensorVariables in general, since the syntax "if a:" is often used instead of "if a is not None:"
上级 73ac6dba
...@@ -892,10 +892,35 @@ class _tensor_py_operators: ...@@ -892,10 +892,35 @@ class _tensor_py_operators:
#def __complex__(self): return convert_to_complex128(self) #def __complex__(self): return convert_to_complex128(self)
#COMPARISONS #COMPARISONS
def __lt__(self,other): return lt(self, other) _is_nonzero = True
def __le__(self,other): return le(self, other) def __lt__(self,other):
def __gt__(self,other): return gt(self, other) rval = lt(self, other)
def __ge__(self,other): return ge(self, other) rval._is_nonzero=False
return rval
def __le__(self,other):
rval = le(self, other)
rval._is_nonzero=False
return rval
def __gt__(self,other):
rval = gt(self, other)
rval._is_nonzero=False
return rval
def __ge__(self,other):
rval = ge(self, other)
rval._is_nonzero=False
return rval
def __nonzero__(self):
# This is meant to prohibit stuff like a < b < c, which is internally implemented as
# (a < b) and (b < c). The trouble with this is the side-effect that checking for a
# non-NULL a by typing "if a: ..." uses the same __nonzero__ method. We want these
# both to work, but it seems impossible. Currently, all vars evaluate to nonzero
# except the return values of comparison operators, which raise this exception. If you
# can think of a better solution, go for it!
if self._is_nonzero:
return True
else:
raise TypeError("Variable does not support boolean operations.")
#BITWISE #BITWISE
def __invert__(self): return invert(self) def __invert__(self): return invert(self)
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论