Example #1
0
 def test_buffer_info(self):
     try:
         from _numpypy import multiarray as np
     except ImportError:
         skip('pypy built without _numpypy')
     module = self.import_module(name='buffer_test')
     get_buffer_info = module.get_buffer_info
     raises(ValueError, get_buffer_info, np.arange(5)[::2], ('SIMPLE', ))
     arr = np.zeros((1, 10), order='F')
     shape, strides = get_buffer_info(arr, ['F_CONTIGUOUS'])
     assert strides[0] == 8
     arr = np.zeros((10, 1), order='C')
     shape, strides = get_buffer_info(arr, ['C_CONTIGUOUS'])
     assert strides[-1] == 8
     dt1 = np.dtype([('a', 'b'), ('b', 'i'), ('sub0', np.dtype('b,i')),
                     ('sub1', np.dtype('b,i')), ('sub2', np.dtype('b,i')),
                     ('sub3', np.dtype('b,i')), ('sub4', np.dtype('b,i')),
                     ('sub5', np.dtype('b,i')), ('sub6', np.dtype('b,i')),
                     ('sub7', np.dtype('b,i')), ('c', 'i')], )
     x = np.arange(dt1.itemsize, dtype='int8').view(dt1)
     # pytest can catch warnings from v2.8 and up, we ship 2.5
     import warnings
     warnings.filterwarnings("error")
     try:
         try:
             y = get_buffer_info(x, ['SIMPLE'])
         except UserWarning as e:
             pass
         else:
             assert False ,"PyPy-specific UserWarning not raised" \
                       " on too long format string"
     finally:
         warnings.resetwarnings()
Example #2
0
    def test_buffer_info(self):
        try:
            from _numpypy import multiarray as np
        except ImportError:
            skip("pypy built without _numpypy")
        module = self.import_module(name="buffer_test")
        get_buffer_info = module.get_buffer_info
        raises(ValueError, get_buffer_info, np.arange(5)[::2], ("SIMPLE",))
        arr = np.zeros((1, 10), order="F")
        shape, strides = get_buffer_info(arr, ["F_CONTIGUOUS"])
        assert strides[0] == 8
        arr = np.zeros((10, 1), order="C")
        shape, strides = get_buffer_info(arr, ["C_CONTIGUOUS"])
        assert strides[-1] == 8
        dt1 = np.dtype(
            [
                ("a", "b"),
                ("b", "i"),
                ("sub0", np.dtype("b,i")),
                ("sub1", np.dtype("b,i")),
                ("sub2", np.dtype("b,i")),
                ("sub3", np.dtype("b,i")),
                ("sub4", np.dtype("b,i")),
                ("sub5", np.dtype("b,i")),
                ("sub6", np.dtype("b,i")),
                ("sub7", np.dtype("b,i")),
                ("c", "i"),
            ]
        )
        x = np.arange(dt1.itemsize, dtype="int8").view(dt1)
        # pytest can catch warnings from v2.8 and up, we ship 2.5
        import warnings

        warnings.filterwarnings("error")
        try:
            try:
                y = get_buffer_info(x, ["SIMPLE"])
            except UserWarning as e:
                pass
            else:
                assert False, "PyPy-specific UserWarning not raised" " on too long format string"
        finally:
            warnings.resetwarnings()
Example #3
0
 def test_buffer_info(self):
     try:
         from _numpypy import multiarray as np
     except ImportError:
         skip('pypy built without _numpypy')
     module = self.import_module(name='buffer_test')
     get_buffer_info = module.get_buffer_info
     raises(ValueError, get_buffer_info, np.arange(5)[::2], ('SIMPLE', ))
     arr = np.zeros((1, 10), order='F')
     shape, strides = get_buffer_info(arr, ['F_CONTIGUOUS'])
     assert strides[0] == 8
     arr = np.zeros((10, 1), order='C')
     shape, strides = get_buffer_info(arr, ['C_CONTIGUOUS'])
     assert strides[-1] == 8
     dt1 = np.dtype([('a', 'b'), ('b', 'i'), ('sub0', np.dtype('b,i')),
                     ('sub1', np.dtype('b,i')), ('sub2', np.dtype('b,i')),
                     ('sub3', np.dtype('b,i')), ('sub4', np.dtype('b,i')),
                     ('sub5', np.dtype('b,i')), ('sub6', np.dtype('b,i')),
                     ('sub7', np.dtype('b,i')), ('c', 'i')], )
     x = np.arange(dt1.itemsize, dtype='int8').view(dt1)
Example #4
0
    def test_ufunc(self):
        from _numpypy.multiarray import arange
        mod = self.import_extension('foo', [
                ("create_ufunc_basic",  "METH_NOARGS",
                """
                PyUFuncGenericFunction funcs[] = {&double_times2, &int_times2};
                char types[] = { NPY_DOUBLE,NPY_DOUBLE, NPY_INT, NPY_INT };
                void *array_data[] = {NULL, NULL};
                PyObject * retval;
                retval = PyUFunc_FromFuncAndData(funcs,
                                    array_data, types, 2, 1, 1, PyUFunc_None,
                                    "times2", "times2_docstring", 0);
                return retval;
                """
                ),
                ("create_ufunc_signature", "METH_NOARGS",
                """
                PyUFuncGenericFunction funcs[] = {&double_times2, &int_times2};
                char types[] = { NPY_DOUBLE,NPY_DOUBLE, NPY_INT, NPY_INT };
                void *array_data[] = {NULL, NULL};
                PyObject * retval;
                retval = PyUFunc_FromFuncAndDataAndSignature(funcs,
                                    array_data, types, 2, 1, 1, PyUFunc_None,
                                    "times2", "times2_docstring", 0, "()->()");
                return retval;
                """),
                ("create_float_ufunc_3x3", "METH_NOARGS",
                """
                PyUFuncGenericFunction funcs[] = {&float_func_with_sig_3x3};
                char types[] = { NPY_FLOAT,NPY_FLOAT};
                void *array_data[] = {NULL, NULL};
                return PyUFunc_FromFuncAndDataAndSignature(funcs,
                                    array_data, types, 1, 1, 1, PyUFunc_None,
                                    "float_3x3", 
                                    "a ufunc that tests a more complicated signature", 
                                    0, "(m,m)->(m,m)");
                """),
                ], prologue='''
                #include "numpy/ndarraytypes.h"
                #include "pypy_numpy.h"
                /*#include <numpy/ufuncobject.h> generated by numpy setup.py*/
                typedef void (*PyUFuncGenericFunction)
                            (char **args,
                             npy_intp *dimensions,
                             npy_intp *strides,
                             void *innerloopdata);
                #define PyUFunc_None -1
                void double_times2(char **args, npy_intp *dimensions,
                              npy_intp* steps, void* data)
                {
                    npy_intp i;
                    npy_intp n;
                    char *in, *out;
                    npy_intp in_step, out_step;
                    double tmp;
                    n = dimensions[0];
                    in = args[0]; out=args[1];
                    in_step = steps[0]; out_step = steps[1];

                    for (i = 0; i < n; i++) {
                        /*BEGIN main ufunc computation*/
                        tmp = *(double *)in;
                        tmp *=2.0;
                        *((double *)out) = tmp;
                        /*END main ufunc computation*/

                        in += in_step;
                        out += out_step;
                    };
                };
                void int_times2(char **args, npy_intp *dimensions,
                              npy_intp* steps, void* data)
                {
                    npy_intp i;
                    npy_intp n = dimensions[0];
                    char *in = args[0], *out=args[1];
                    npy_intp in_step = steps[0], out_step = steps[1];
                    int tmp;
                    for (i = 0; i < n; i++) {
                        /*BEGIN main ufunc computation*/
                        tmp = *(int *)in;
                        tmp *=2.0;
                        *((int *)out) = tmp;
                        /*END main ufunc computation*/

                        in += in_step;
                        out += out_step;
                    };
                };
                void float_func_with_sig_3x3(char ** args, npy_intp * dimensions,
                              npy_intp* steps, void* data)
                {
                    int target_dims[] = {1, 3};
                    int target_steps[] = {0, 0, 12, 4, 12, 4};
                    int res = 0;
                    int i;
                    for (i=0; i<sizeof(target_dims)/sizeof(int); i++)
                        if (dimensions[i] != target_dims[i])
                            res += 1;
                    for (i=0; i<sizeof(target_steps)/sizeof(int); i++)
                        if (steps[i] != target_steps[i])
                            res += +10;
                    *((float *)args[1]) = res;
                };
                            
                ''')
        sq = arange(18, dtype="float32").reshape(2,3,3)
        float_ufunc = mod.create_float_ufunc_3x3()
        out = float_ufunc(sq)
        assert out[0, 0, 0] == 0

        times2 = mod.create_ufunc_basic()
        arr = arange(12, dtype='i').reshape(3, 4)
        out = times2(arr, extobj=[0, 0, None])
        assert (out == arr * 2).all()

        times2prime = mod.create_ufunc_signature()
        out = times2prime(arr, sig='d->d', extobj=[0, 0, None])
        assert (out == arr * 2).all()
Example #5
0
 def test_buffer_protocol_capi(self):
     foo = self.import_extension('foo', [("get_len", "METH_VARARGS", """
             Py_buffer view;
             PyObject* obj = PyTuple_GetItem(args, 0);
             long ret, vlen;
             memset(&view, 0, sizeof(Py_buffer));
             ret = PyObject_GetBuffer(obj, &view, PyBUF_FULL_RO);
             if (ret != 0)
                 return NULL;
             vlen = view.len / view.itemsize;
             PyBuffer_Release(&view);
             return PyLong_FromLong(vlen);
          """),
                                         ("test_buffer", "METH_VARARGS", """
             Py_buffer* view = NULL;
             PyObject* obj = PyTuple_GetItem(args, 0);
             PyObject* memoryview = PyMemoryView_FromObject(obj);
             if (memoryview == NULL)
                 return NULL;
             view = PyMemoryView_GET_BUFFER(memoryview);
             Py_DECREF(memoryview);
             return PyLong_FromLong(view->len / view->itemsize);
         """),
                                         ("test_contiguous", "METH_O", """
             Py_buffer* view;
             PyObject * memoryview;
             void * buf = NULL;
             int ret;
             Py_ssize_t len;
             memoryview = PyMemoryView_FromObject(args);
             if (memoryview == NULL)
                 return NULL;
             view = PyMemoryView_GET_BUFFER(memoryview);
             Py_DECREF(memoryview);
             len = view->len;
             if (len == 0)
                 return NULL;
             buf = malloc(len);
             ret = PyBuffer_ToContiguous(buf, view, view->len, 'A');
             if (ret != 0)
             {
                 free(buf);
                 return NULL;
             }
             ret = PyBuffer_FromContiguous(view, buf, view->len, 'A');
             free(buf);
             if (ret != 0)
                 return NULL;
              Py_RETURN_NONE;
          """),
                                         ("get_contiguous", "METH_O", """
            return PyMemoryView_GetContiguous(args, PyBUF_READ, 'C');
         """)])
     module = self.import_module(name='buffer_test')
     arr = module.PyMyArray(10)
     ten = foo.get_len(arr)
     assert ten == 10
     ten = foo.get_len('1234567890')
     assert ten == 10
     ten = foo.test_buffer(arr)
     assert ten == 10
     foo.test_contiguous(arr)
     contig = foo.get_contiguous(arr)
     foo.test_contiguous(contig)
     try:
         from _numpypy import multiarray as np
     except ImportError:
         skip('pypy built without _numpypy')
     a = np.arange(20)[::2]
     skip('not implemented yet')
     contig = foo.get_contiguous(a)
     foo.test_contiguous(contig)
Example #6
0
    def test_ufunc(self):
        from _numpypy.multiarray import arange
        mod = self.import_extension('foo', [
            ("create_ufunc_basic", "METH_NOARGS", """
                PyUFuncGenericFunction funcs[] = {&double_times2, &int_times2};
                char types[] = { NPY_DOUBLE,NPY_DOUBLE, NPY_INT, NPY_INT };
                void *array_data[] = {NULL, NULL};
                PyObject * retval;
                retval = PyUFunc_FromFuncAndData(funcs,
                                    array_data, types, 2, 1, 1, PyUFunc_None,
                                    "times2", "times2_docstring", 0);
                return retval;
                """),
            ("create_ufunc_signature", "METH_NOARGS", """
                PyUFuncGenericFunction funcs[] = {&double_times2, &int_times2};
                char types[] = { NPY_DOUBLE,NPY_DOUBLE, NPY_INT, NPY_INT };
                void *array_data[] = {NULL, NULL};
                PyObject * retval;
                retval = PyUFunc_FromFuncAndDataAndSignature(funcs,
                                    array_data, types, 2, 1, 1, PyUFunc_None,
                                    "times2", "times2_docstring", 0, "()->()");
                return retval;
                """),
            ("create_float_ufunc_3x3", "METH_NOARGS", """
                PyUFuncGenericFunction funcs[] = {&float_func_with_sig_3x3};
                char types[] = { NPY_FLOAT,NPY_FLOAT};
                void *array_data[] = {NULL, NULL};
                return PyUFunc_FromFuncAndDataAndSignature(funcs,
                                    array_data, types, 1, 1, 1, PyUFunc_None,
                                    "float_3x3", 
                                    "a ufunc that tests a more complicated signature", 
                                    0, "(m,m)->(m,m)");
                """),
        ],
                                    prologue='''
                #include "numpy/ndarraytypes.h"
                /*#include <numpy/ufuncobject.h> generated by numpy setup.py*/
                typedef void (*PyUFuncGenericFunction)
                            (char **args,
                             npy_intp *dimensions,
                             npy_intp *strides,
                             void *innerloopdata);
                #define PyUFunc_None -1
                void double_times2(char **args, npy_intp *dimensions,
                              npy_intp* steps, void* data)
                {
                    npy_intp i;
                    npy_intp n;
                    char *in, *out;
                    npy_intp in_step, out_step;
                    double tmp;
                    n = dimensions[0];
                    in = args[0]; out=args[1];
                    in_step = steps[0]; out_step = steps[1];

                    for (i = 0; i < n; i++) {
                        /*BEGIN main ufunc computation*/
                        tmp = *(double *)in;
                        tmp *=2.0;
                        *((double *)out) = tmp;
                        /*END main ufunc computation*/

                        in += in_step;
                        out += out_step;
                    };
                };
                void int_times2(char **args, npy_intp *dimensions,
                              npy_intp* steps, void* data)
                {
                    npy_intp i;
                    npy_intp n = dimensions[0];
                    char *in = args[0], *out=args[1];
                    npy_intp in_step = steps[0], out_step = steps[1];
                    int tmp;
                    for (i = 0; i < n; i++) {
                        /*BEGIN main ufunc computation*/
                        tmp = *(int *)in;
                        tmp *=2.0;
                        *((int *)out) = tmp;
                        /*END main ufunc computation*/

                        in += in_step;
                        out += out_step;
                    };
                };
                void float_func_with_sig_3x3(char ** args, npy_intp * dimensions,
                              npy_intp* steps, void* data)
                {
                    int target_dims[] = {1, 3};
                    int target_steps[] = {0, 0, 12, 4, 12, 4};
                    int res = 0;
                    int i;
                    for (i=0; i<sizeof(target_dims)/sizeof(int); i++)
                        if (dimensions[i] != target_dims[i])
                            res += 1;
                    for (i=0; i<sizeof(target_steps)/sizeof(int); i++)
                        if (steps[i] != target_steps[i])
                            res += +10;
                    *((float *)args[1]) = res;
                };
                            
                ''')
        sq = arange(18, dtype="float32").reshape(2, 3, 3)
        float_ufunc = mod.create_float_ufunc_3x3()
        out = float_ufunc(sq)
        assert out[0, 0, 0] == 0

        times2 = mod.create_ufunc_basic()
        arr = arange(12, dtype='i').reshape(3, 4)
        out = times2(arr, extobj=[0, 0, None])
        assert (out == arr * 2).all()

        times2prime = mod.create_ufunc_signature()
        out = times2prime(arr, sig='(d)->(d)', extobj=[0, 0, None])
        assert (out == arr * 2).all()