1
#define NPY_NO_DEPRECATED_API NPY_API_VERSION
2

3
#include <Python.h>
4
#include <numpy/arrayobject.h>
5
#include <numpy/ufuncobject.h>
6
#include "numpy/npy_3kcompat.h"
7
#include <math.h>
8
#include <structmember.h>
9

10

11
static PyMethodDef TestMethods[] = {
12
        {NULL, NULL, 0, NULL}
13
};
14

15

16
static void
17 1
inplace_add(char **args, npy_intp const *dimensions, npy_intp const *steps, void *data)
18
{
19
    npy_intp i;
20 1
    npy_intp n = dimensions[0];
21 1
    char *in1 = args[0];
22 1
    char *in2 = args[1];
23 1
    npy_intp in1_step = steps[0];
24 1
    npy_intp in2_step = steps[1];
25

26 1
    for (i = 0; i < n; i++) {
27 1
        (*(long *)in1) = *(long*)in1 + *(long*)in2;
28 1
        in1 += in1_step;
29 1
        in2 += in2_step;
30
    }
31 1
}
32

33

34
/*This a pointer to the above function*/
35
PyUFuncGenericFunction funcs[1] = {&inplace_add};
36

37
/* These are the input and return dtypes of logit.*/
38
static char types[2] = {NPY_LONG, NPY_LONG};
39

40
static void *data[1] = {NULL};
41

42
static struct PyModuleDef moduledef = {
43
    PyModuleDef_HEAD_INIT,
44
    "_operand_flag_tests",
45
    NULL,
46
    -1,
47
    TestMethods,
48
    NULL,
49
    NULL,
50
    NULL,
51
    NULL
52
};
53

54 1
PyMODINIT_FUNC PyInit__operand_flag_tests(void)
55
{
56 1
    PyObject *m = NULL;
57
    PyObject *ufunc;
58

59 1
    m = PyModule_Create(&moduledef);
60 1
    if (m == NULL) {
61
        goto fail;
62
    }
63

64 1
    import_array();
65 1
    import_umath();
66

67 1
    ufunc = PyUFunc_FromFuncAndData(funcs, data, types, 1, 2, 0,
68
                                    PyUFunc_None, "inplace_add",
69
                                    "inplace_add_docstring", 0);
70

71
    /*
72
     * Set flags to turn off buffering for first input operand,
73
     * so that result can be written back to input operand.
74
     */
75 1
    ((PyUFuncObject*)ufunc)->op_flags[0] = NPY_ITER_READWRITE;
76 1
    ((PyUFuncObject*)ufunc)->iter_flags = NPY_ITER_REDUCE_OK;
77 1
    PyModule_AddObject(m, "inplace_add", (PyObject*)ufunc);
78

79 1
    return m;
80

81 0
fail:
82 0
    if (!PyErr_Occurred()) {
83 0
        PyErr_SetString(PyExc_RuntimeError,
84
                        "cannot load _operand_flag_tests module.");
85
    }
86
    if (m) {
87
        Py_DECREF(m);
88
        m = NULL;
89
    }
90
    return m;
91
}

Read our documentation on viewing source code .

Loading