Skip to content
This repository was archived by the owner on Feb 2, 2024. It is now read-only.

Commit 1aa9bc3

Browse files
Add test for user exception in @unbox
1 parent f10cbf3 commit 1aa9bc3

File tree

1 file changed

+51
-2
lines changed

1 file changed

+51
-2
lines changed

sdc/tests/test_hpat_jit.py

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@
3333
import pandas as pd
3434
from sdc import *
3535
from numba.typed import Dict
36-
from numba.extending import (overload_method, overload, models, register_model, intrinsic)
36+
from numba.extending import (overload_method, overload, models, register_model,
37+
intrinsic, unbox, typeof_impl, make_attribute_wrapper,
38+
NativeValue)
3739
from numba.special import literally
3840
from numba.typing import signature
3941
from numba import cgutils
@@ -488,7 +490,54 @@ def test_impl(a):
488490
return d.lit(a)
489491

490492
jtest = numba.njit(test_impl)
491-
test_impl(5)
493+
self.assertEqual(jtest(5), 5)
494+
495+
@unittest.expectedFailure
496+
def test_unbox_with_exception(self):
497+
class Dummy:
498+
def __init__(self, a=0):
499+
self.a = a
500+
501+
class DummyType(numba.types.Type):
502+
def __init__(self):
503+
super().__init__(name="dummy")
504+
505+
@register_model(DummyType)
506+
class DummyTypeModel(models.StructModel):
507+
def __init__(self, dmm, fe_type):
508+
members = [('a', numba.types.int64)]
509+
super().__init__(dmm, fe_type, members)
510+
511+
@unbox(DummyType)
512+
def unbox_dummy(typ, val, c):
513+
context = c.context
514+
builder = c.builder
515+
516+
a_obj = c.pyapi.object_getattr_string(val, "a")
517+
a_value = c.pyapi.long_as_longlong(a_obj)
518+
519+
dmm = cgutils.create_struct_proxy(typ)(context, builder)
520+
521+
with builder.if_then(a_value):
522+
context.call_conv.return_user_exc(
523+
builder, ValueError,
524+
("exception!",)
525+
)
526+
527+
dmm.a = a_value
528+
return NativeValue(dmm._getvalue())
529+
530+
make_attribute_wrapper(DummyType, 'a', 'a')
531+
532+
def test_impl(d):
533+
return d.a
534+
535+
@typeof_impl.register(Dummy)
536+
def typeof_dummy(val, c):
537+
return DummyType()
538+
539+
jtest = numba.njit(test_impl)
540+
print(jtest(Dummy(0)))
492541

493542

494543
if __name__ == "__main__":

0 commit comments

Comments
 (0)