Skip to content

Commit

Permalink
Merge pull request #7477 from stuartarchibald/fix/7474
Browse files Browse the repository at this point in the history
Fix unicode operator.eq handling of Optional types.
  • Loading branch information
sklam committed Oct 11, 2021
2 parents 8d4559a + df0858b commit 679ade8
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
20 changes: 18 additions & 2 deletions numba/cpython/unicode.py
Original file line number Diff line number Diff line change
Expand Up @@ -451,11 +451,27 @@ def len_impl(s):
def unicode_eq(a, b):
if not (a.is_internal and b.is_internal):
return
if isinstance(a, types.Optional):
check_a = a.type
else:
check_a = a
if isinstance(b, types.Optional):
check_b = b.type
else:
check_b = b
accept = (types.UnicodeType, types.StringLiteral, types.UnicodeCharSeq)
a_unicode = isinstance(a, accept)
b_unicode = isinstance(b, accept)
a_unicode = isinstance(check_a, accept)
b_unicode = isinstance(check_b, accept)
if a_unicode and b_unicode:
def eq_impl(a, b):
# handle Optionals at runtime
a_none = a is None
b_none = b is None
if a_none or b_none:
if a_none and b_none:
return True
else:
return False
# the str() is for UnicodeCharSeq, it's a nop else
a = str(a)
b = str(b)
Expand Down
22 changes: 22 additions & 0 deletions numba/tests/test_unicode.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,28 @@ def test_eq(self, flags=no_pyobj_flags):
self.assertEqual(pyfunc(1, b),
cfunc(1, b), '%s, %s' % (1, b))

def test_eq_optional(self):
# See issue #7474
@njit
def foo(pred1, pred2):
if pred1 > 0:
resolved1 = 'concrete'
else:
resolved1 = None
if pred2 < 0:
resolved2 = 'concrete'
else:
resolved2 = None

# resolved* are Optionals
if resolved1 == resolved2:
return 10
else:
return 20

for (p1, p2) in product(*((-1, 1),) * 2):
self.assertEqual(foo(p1, p2), foo.py_func(p1, p2))

def _check_ordering_op(self, usecase):
pyfunc = usecase
cfunc = njit(pyfunc)
Expand Down

0 comments on commit 679ade8

Please sign in to comment.