Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions backtrader/lineroot.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,13 @@ def _makeoperationown(self, operation, _ownerskip=None):

if not math.isfinite(value):
return False
return value != 0.0
# bool() for the same reason as in __nonzero__:
# numpy.float64 subclasses float, so it reaches
# this branch and `!= 0.0` yields numpy.bool_.
# This path returns a value rather than going
# through the __bool__ protocol, so a leak here
# would propagate silently instead of raising.
return bool(value != 0.0)
return bool(value)
return False
except Exception:
Expand All @@ -265,7 +271,9 @@ def _makeoperationown(self, operation, _ownerskip=None):

if not math.isfinite(value):
return False
return value != 0.0
# See the note above: guard against numpy.bool_ leaking
# out of this non-__bool__ return path.
return bool(value != 0.0)
return bool(value)
return False
except Exception:
Expand Down Expand Up @@ -607,7 +615,14 @@ def __nonzero__(self):

if not math.isfinite(value):
return False
return value != 0.0
# bool() is required, not cosmetic: numpy.float64
# subclasses float, so numpy scalars reach this branch
# and `np.float64 != 0.0` yields numpy.bool_, which
# CPython rejects from __bool__. Such scalars come from
# PandasData and survive only in QBuffer/exactbars mode,
# where lines are backed by a deque rather than
# array.array("d") (which coerces them to float).
return bool(value != 0.0)
return bool(value)
return False
if hasattr(self, "__getitem__") and hasattr(self, "__len__"):
Expand All @@ -621,7 +636,10 @@ def __nonzero__(self):

if not math.isfinite(value):
return False
return value != 0.0
# See the note above: bool() guards against numpy.float64
# reaching the isinstance(value, float) branch and making
# `!=` return numpy.bool_.
return bool(value != 0.0)
return bool(value)
return False
# Fallback: if no data available, return False
Expand Down
Loading
Loading