test_interval_example.py 8.66 KB
Newer Older
dugupeiwen's avatar
dugupeiwen committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
"""
This test is used by `docs/source/extending/interval-example.rst`.

The "magictoken" comments are used as markers for the beginning and ending of
example code.
"""
import unittest


class IntervalExampleTest(unittest.TestCase):

    def test_interval_class_usage(self):
        # magictoken.interval_py_class.begin
        class Interval(object):
            """
            A half-open interval on the real number line.
            """
            def __init__(self, lo, hi):
                self.lo = lo
                self.hi = hi

            def __repr__(self):
                return 'Interval(%f, %f)' % (self.lo, self.hi)

            @property
            def width(self):
                return self.hi - self.lo
        # magictoken.interval_py_class.end

        # magictoken.interval_type_class.begin
        from numba import types

        class IntervalType(types.Type):
            def __init__(self):
                super(IntervalType, self).__init__(name='Interval')

        interval_type = IntervalType()
        # magictoken.interval_type_class.end

        # magictoken.interval_typeof_register.begin
        from numba.extending import typeof_impl

        @typeof_impl.register(Interval)
        def typeof_index(val, c):
            return interval_type
        # magictoken.interval_typeof_register.end

        # magictoken.numba_type_register.begin
        from numba.extending import as_numba_type

        as_numba_type.register(Interval, interval_type)
        # magictoken.numba_type_register.end

        # magictoken.numba_type_callable.begin
        from numba.extending import type_callable

        @type_callable(Interval)
        def type_interval(context):
            def typer(lo, hi):
                if isinstance(lo, types.Float) and isinstance(hi, types.Float):
                    return interval_type
            return typer
        # magictoken.numba_type_callable.end

        # magictoken.interval_model.begin
        from numba.extending import models, register_model

        @register_model(IntervalType)
        class IntervalModel(models.StructModel):
            def __init__(self, dmm, fe_type):
                members = [('lo', types.float64),
                           ('hi', types.float64),]
                models.StructModel.__init__(self, dmm, fe_type, members)
        # magictoken.interval_model.end

        # magictoken.interval_attribute_wrapper.begin
        from numba.extending import make_attribute_wrapper

        make_attribute_wrapper(IntervalType, 'lo', 'lo')
        make_attribute_wrapper(IntervalType, 'hi', 'hi')
        # magictoken.interval_attribute_wrapper.end

        # magictoken.interval_overload_attribute.begin
        from numba.extending import overload_attribute

        @overload_attribute(IntervalType, "width")
        def get_width(interval):
            def getter(interval):
                return interval.hi - interval.lo
            return getter
        # magictoken.interval_overload_attribute.end

        # magictoken.interval_lower_builtin.begin
        from numba.extending import lower_builtin
        from numba.core import cgutils

        @lower_builtin(Interval, types.Float, types.Float)
        def impl_interval(context, builder, sig, args):
            typ = sig.return_type
            lo, hi = args
            interval = cgutils.create_struct_proxy(typ)(context, builder)
            interval.lo = lo
            interval.hi = hi
            return interval._getvalue()
        # magictoken.interval_lower_builtin.end

        # magictoken.interval_unbox.begin
        from numba.extending import unbox, NativeValue
        from contextlib import ExitStack

        @unbox(IntervalType)
        def unbox_interval(typ, obj, c):
            """
            Convert a Interval object to a native interval structure.
            """
            is_error_ptr = cgutils.alloca_once_value(c.builder, cgutils.false_bit)
            interval = cgutils.create_struct_proxy(typ)(c.context, c.builder)

            with ExitStack() as stack:
                lo_obj = c.pyapi.object_getattr_string(obj, "lo")
                with cgutils.early_exit_if_null(c.builder, stack, lo_obj):
                    c.builder.store(cgutils.true_bit, is_error_ptr)
                lo_native = c.unbox(types.float64, lo_obj)
                c.pyapi.decref(lo_obj)
                with cgutils.early_exit_if(c.builder, stack, lo_native.is_error):
                    c.builder.store(cgutils.true_bit, is_error_ptr)

                hi_obj = c.pyapi.object_getattr_string(obj, "hi")
                with cgutils.early_exit_if_null(c.builder, stack, hi_obj):
                    c.builder.store(cgutils.true_bit, is_error_ptr)
                hi_native = c.unbox(types.float64, hi_obj)
                c.pyapi.decref(hi_obj)
                with cgutils.early_exit_if(c.builder, stack, hi_native.is_error):
                    c.builder.store(cgutils.true_bit, is_error_ptr)

                interval.lo = lo_native.value
                interval.hi = hi_native.value

            return NativeValue(interval._getvalue(), is_error=c.builder.load(is_error_ptr))
        # magictoken.interval_unbox.end

        # magictoken.interval_box.begin
        from numba.extending import box

        @box(IntervalType)
        def box_interval(typ, val, c):
            """
            Convert a native interval structure to an Interval object.
            """
            ret_ptr = cgutils.alloca_once(c.builder, c.pyapi.pyobj)
            fail_obj = c.pyapi.get_null_object()

            with ExitStack() as stack:
                interval = cgutils.create_struct_proxy(typ)(c.context, c.builder, value=val)
                lo_obj = c.box(types.float64, interval.lo)
                with cgutils.early_exit_if_null(c.builder, stack, lo_obj):
                    c.builder.store(fail_obj, ret_ptr)

                hi_obj = c.box(types.float64, interval.hi)
                with cgutils.early_exit_if_null(c.builder, stack, hi_obj):
                    c.pyapi.decref(lo_obj)
                    c.builder.store(fail_obj, ret_ptr)

                class_obj = c.pyapi.unserialize(c.pyapi.serialize_object(Interval))
                with cgutils.early_exit_if_null(c.builder, stack, class_obj):
                    c.pyapi.decref(lo_obj)
                    c.pyapi.decref(hi_obj)
                    c.builder.store(fail_obj, ret_ptr)

                # NOTE: The result of this call is not checked as the clean up
                # has to occur regardless of whether it is successful. If it
                # fails `res` is set to NULL and a Python exception is set.
                res = c.pyapi.call_function_objargs(class_obj, (lo_obj, hi_obj))
                c.pyapi.decref(lo_obj)
                c.pyapi.decref(hi_obj)
                c.pyapi.decref(class_obj)
                c.builder.store(res, ret_ptr)

            return c.builder.load(ret_ptr)
        # magictoken.interval_box.end

        # magictoken.interval_usage.begin
        from numba import njit

        @njit
        def inside_interval(interval, x):
            return interval.lo <= x < interval.hi

        @njit
        def interval_width(interval):
            return interval.width

        @njit
        def sum_intervals(i, j):
            return Interval(i.lo + j.lo, i.hi + j.hi)
        # magictoken.interval_usage.end

        def check_equal_intervals(x, y):
            self.assertIsInstance(x, Interval)
            self.assertIsInstance(y, Interval)
            self.assertEquals(x.lo, y.lo)
            self.assertEquals(x.hi, y.hi)

        a = Interval(2, 3)
        b = Interval(4, 5)
        c = Interval(6, 8)

        # Test box-unbox
        return_func = njit(lambda x: x)
        check_equal_intervals(a, return_func(a))

        # Test .width attribute
        self.assertEqual(a.width, interval_width(a))

        # Test exceptions
        class NotAFloat:
            def __float__(self):
                raise RuntimeError("I am not a float")

        # TODO: This should produce a `RuntimeError`, but the `unbox` handler for `float` ignores
        # the error raised by `__float__`, leading to a subsequent `TypeError` cause by passing
        # `NULL` to `PyFloat_AsDouble`.
        # This isn't the fault of the `Interval` extension that is being testing
        # in this file.
        with self.assertRaises(TypeError):
            interval_width(Interval(2, NotAFloat()))

        bad_interval = Interval(1, 2)
        del bad_interval.hi

        with self.assertRaises(AttributeError):
            interval_width(bad_interval)

        # Test .lo and .hi usage
        self.assertFalse(inside_interval(a, 5))

        # Test native Interval constructor
        check_equal_intervals(c, sum_intervals(a, b))


if __name__ == '__main__':
    unittest.main()