|
33 | 33 | import pandas as pd
|
34 | 34 | from sdc import *
|
35 | 35 | 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) |
37 | 39 | from numba.special import literally
|
38 | 40 | from numba.typing import signature
|
39 | 41 | from numba import cgutils
|
@@ -488,7 +490,54 @@ def test_impl(a):
|
488 | 490 | return d.lit(a)
|
489 | 491 |
|
490 | 492 | 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))) |
492 | 541 |
|
493 | 542 |
|
494 | 543 | if __name__ == "__main__":
|
|
0 commit comments