From 29db317478280ee08b47c80129f1520b746dd2e1 Mon Sep 17 00:00:00 2001 From: Florent Gallaire Date: Thu, 11 Jun 2026 03:44:52 +0200 Subject: [PATCH] bool: bitwise ops use real bitwise operators and check both operands (2 | True returned 2) --- www/src/py_int.js | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/www/src/py_int.js b/www/src/py_int.js index d3c3fbac7..85b67174f 100644 --- a/www/src/py_int.js +++ b/www/src/py_int.js @@ -966,29 +966,33 @@ bool.$factory = function() { } /* bool start */ +// Guard BOTH operands: these slots are also reached through the reflected +// dunders (bool.__ror__ & co, selected by the subclass-priority dispatch for +// `int OP bool`), where self is the int. With the guard on `other` only, +// `2 | True` hit the JS LOGICAL `self || other` and returned 2. _b_.bool.nb_and = function(self, other) { - if ($B.$isinstance(other, bool)) { - return self && other - } else if ($B.$isinstance(other, int)) { - return int.nb_and(int_value(self), other) + if (typeof self == 'boolean' && typeof other == 'boolean') { + return (self & other) ? true : false + } else if ($B.$isinstance(self, int) && $B.$isinstance(other, int)) { + return int.nb_and(int_value(self), int_value(other)) } return _b_.NotImplemented } _b_.bool.nb_xor = function(self, other) { - if ($B.$isinstance(other, bool)) { - return self ^ other ? true : false - } else if ($B.$isinstance(other, int)) { - return int.nb_xor(int_value(self), other) + if (typeof self == 'boolean' && typeof other == 'boolean') { + return (self ^ other) ? true : false + } else if ($B.$isinstance(self, int) && $B.$isinstance(other, int)) { + return int.nb_xor(int_value(self), int_value(other)) } return _b_.NotImplemented } _b_.bool.nb_or = function(self, other) { - if ($B.$isinstance(other, bool)) { - return self || other - } else if ($B.$isinstance(other, int)) { - return int.nb_or(int_value(self), other) + if (typeof self == 'boolean' && typeof other == 'boolean') { + return (self | other) ? true : false + } else if ($B.$isinstance(self, int) && $B.$isinstance(other, int)) { + return int.nb_or(int_value(self), int_value(other)) } return _b_.NotImplemented }