test_conversion.py 7.37 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
import array
import gc
import itertools
import sys

import numpy as np

import unittest
from numba.core.compiler import compile_isolated, Flags
from numba import jit
from numba.core import types
from numba.tests.support import TestCase
from numba.np import numpy_support


def identity(x):
    return x

def addition(x, y):
    return x + y

def equality(x, y):
    return x == y

def foobar(x, y, z):
    return x


class TestConversion(TestCase):
    """
    Testing Python to Native conversion
    """

    def test_complex_identity(self):
        pyfunc = identity
        cres = compile_isolated(pyfunc, [types.complex64],
                                return_type=types.complex64)

        xs = [1.0j, (1+1j), (-1-1j), (1+0j)]
        for x in xs:
            self.assertEqual(cres.entry_point(x), x)
        for x in np.complex64(xs):
            self.assertEqual(cres.entry_point(x), x)

        cres = compile_isolated(pyfunc, [types.complex128],
                                return_type=types.complex128)

        xs = [1.0j, (1+1j), (-1-1j), (1+0j)]
        for x in xs:
            self.assertEqual(cres.entry_point(x), x)
        for x in np.complex128(xs):
            self.assertEqual(cres.entry_point(x), x)

    def test_complex_addition(self):
        pyfunc = addition
        cres = compile_isolated(pyfunc, [types.complex64, types.complex64],
                                return_type=types.complex64)

        xs = [1.0j, (1+1j), (-1-1j), (1+0j)]
        for x in xs:
            y = x
            self.assertEqual(cres.entry_point(x, y), x + y)
        for x in np.complex64(xs):
            y = x
            self.assertEqual(cres.entry_point(x, y), x + y)


        cres = compile_isolated(pyfunc, [types.complex128, types.complex128],
                                return_type=types.complex128)

        xs = [1.0j, (1+1j), (-1-1j), (1+0j)]
        for x in xs:
            y = x
            self.assertEqual(cres.entry_point(x, y), x + y)
        for x in np.complex128(xs):
            y = x
            self.assertEqual(cres.entry_point(x, y), x + y)

    def test_boolean_as_int(self):
        pyfunc = equality
        cres = compile_isolated(pyfunc, [types.boolean, types.intp])
        cfunc = cres.entry_point

        xs = True, False
        ys = -1, 0, 1

        for xs, ys in itertools.product(xs, ys):
            self.assertEqual(pyfunc(xs, ys), cfunc(xs, ys))

    def test_boolean_as_float(self):
        pyfunc = equality
        cres = compile_isolated(pyfunc, [types.boolean, types.float64])
        cfunc = cres.entry_point

        xs = True, False
        ys = -1, 0, 1

        for xs, ys in itertools.product(xs, ys):
            self.assertEqual(pyfunc(xs, ys), cfunc(xs, ys))

    def test_boolean_eq_boolean(self):
        pyfunc = equality
        cres = compile_isolated(pyfunc, [types.boolean, types.boolean])
        cfunc = cres.entry_point

        xs = True, False
        ys = True, False

        for xs, ys in itertools.product(xs, ys):
            self.assertEqual(pyfunc(xs, ys), cfunc(xs, ys))

    # test when a function parameters are jitted as unsigned types
    # the function is called with negative parameters the Python error 
    # that it generates is correctly handled -- a Python error is returned to the user
    # For more info, see the comment in Include/longobject.h for _PyArray_AsByteArray 
    # which PyLong_AsUnsignedLongLong calls
    def test_negative_to_unsigned(self):
        def f(x):
            return x
        with self.assertRaises(OverflowError):
            jit('uintp(uintp)', nopython=True)(f)(-5)

    # test the switch logic in callwraper.py:build_wrapper() works for more than one argument
    # and where the error occurs 
    def test_multiple_args_negative_to_unsigned(self): 
        pyfunc = foobar
        cres = compile_isolated(pyfunc, [types.uint64, types.uint64, types.uint64],
                                return_type=types.uint64)
        cfunc = cres.entry_point
        test_fail_args = ((-1, 0, 1), (0, -1, 1), (0, 1, -1))
        with self.assertRaises(OverflowError):
            for a, b, c in test_fail_args:
                cfunc(a, b, c)

    # test switch logic of callwraper.py:build_wrapper() with records as function parameters
    def test_multiple_args_records(self): 
        pyfunc = foobar

        mystruct_dt = np.dtype([('p', np.float64),
                           ('row', np.float64),
                           ('col', np.float64)])
        mystruct = numpy_support.from_dtype(mystruct_dt)

        cres = compile_isolated(pyfunc, [mystruct[:], types.uint64, types.uint64],
                                return_type=mystruct[:])
        cfunc = cres.entry_point

        st1 = np.recarray(3, dtype=mystruct_dt)

        st1.p = np.arange(st1.size) + 1
        st1.row = np.arange(st1.size) + 1
        st1.col = np.arange(st1.size) + 1

        with self.assertRefCount(st1):
            test_fail_args = ((st1, -1, 1), (st1, 1, -1))

            for a, b, c in test_fail_args:
                with self.assertRaises(OverflowError):
                    cfunc(a, b, c)

            del test_fail_args, a, b, c
            gc.collect()

    # test switch logic of callwraper.py:build_wrapper() with no function parameters
    def test_with_no_parameters(self):
        def f():
            pass 
        self.assertEqual(f(), jit('()', nopython=True)(f)())

    def check_argument_cleanup(self, typ, obj):
        """
        Check that argument cleanup doesn't leak references.
        """
        def f(x, y):
            pass

        def _objects(obj):
            objs = [obj]
            if isinstance(obj, tuple):
                for v in obj:
                    objs += _objects(v)
            return objs

        objects = _objects(obj)

        cres = compile_isolated(f, (typ, types.uint32))
        with self.assertRefCount(*objects):
            cres.entry_point(obj, 1)
        with self.assertRefCount(*objects):
            with self.assertRaises(OverflowError):
                cres.entry_point(obj, -1)

        cres = compile_isolated(f, (types.uint32, typ))
        with self.assertRefCount(*objects):
            cres.entry_point(1, obj)
        with self.assertRefCount(*objects):
            with self.assertRaises(OverflowError):
                cres.entry_point(-1, obj)

    def test_cleanup_buffer(self):
        mem = memoryview(bytearray(b"xyz"))
        self.check_argument_cleanup(types.Buffer(types.intc, 1, 'C'), mem)

    def test_cleanup_record(self):
        dtype = np.dtype([('x', np.float64), ('y', np.float64)])
        recarr = np.zeros(1, dtype=dtype)
        self.check_argument_cleanup(numpy_support.from_dtype(dtype), recarr[0])

    def test_cleanup_tuple(self):
        mem = memoryview(bytearray(b"xyz"))
        tp = types.UniTuple(types.Buffer(types.intc, 1, 'C'), 2)
        self.check_argument_cleanup(tp, (mem, mem))

    def test_cleanup_optional(self):
        mem = memoryview(bytearray(b"xyz"))
        tp = types.Optional(types.Buffer(types.intc, 1, 'C'))
        self.check_argument_cleanup(tp, mem)

    def test_stringliteral_to_unicode(self):
        # See issue #6907, explicit signature on bar() takes a unicode_type but
        # the call to bar() in foo() is with a StringLiteral

        @jit(types.void(types.unicode_type), nopython=True)
        def bar(string):
            pass

        @jit(types.void(), nopython=True)
        def foo2():
            bar("literal string")


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