zmc
2023-10-12 ed135d79df12a2466b52dae1a82326941211dcc9
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
U
O±d„ã@sFddlZddlZddlZddlmZejdd„ƒZejj    dd„ƒZ
dS)éN)ÚextbuildcCsdtj d¡st d¡d}dg}d}zddl}|WStk
rFYnXtjd||t     
¡g||d    S)
z€ Some codes to generate data and manage temporary buffers use when
    sharing with numpy via the array interface protocol.
    Úlinuxzlink fails on cygwina$
        #include <Python.h>
        #define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
        #include <numpy/arrayobject.h>
        #include <stdio.h>
        #include <math.h>
 
        NPY_NO_EXPORT
        void delete_array_struct(PyObject *cap) {
 
            /* get the array interface structure */
            PyArrayInterface *inter = (PyArrayInterface*)
                PyCapsule_GetPointer(cap, NULL);
 
            /* get the buffer by which data was shared */
            double *ptr = (double*)PyCapsule_GetContext(cap);
 
            /* for the purposes of the regression test set the elements
               to nan */
            for (npy_intp i = 0; i < inter->shape[0]; ++i)
                ptr[i] = nan("");
 
            /* free the shared buffer */
            free(ptr);
 
            /* free the array interface structure */
            free(inter->shape);
            free(inter);
 
            fprintf(stderr, "delete_array_struct\ncap = %ld inter = %ld"
                " ptr = %ld\n", (long)cap, (long)inter, (long)ptr);
        }
        )Únew_array_structZ METH_VARARGSa}
 
            long long n_elem = 0;
            double value = 0.0;
 
            if (!PyArg_ParseTuple(args, "Ld", &n_elem, &value)) {
                Py_RETURN_NONE;
            }
 
            /* allocate and initialize the data to share with numpy */
            long long n_bytes = n_elem*sizeof(double);
            double *data = (double*)malloc(n_bytes);
 
            if (!data) {
                PyErr_Format(PyExc_MemoryError,
                    "Failed to malloc %lld bytes", n_bytes);
 
                Py_RETURN_NONE;
            }
 
            for (long long i = 0; i < n_elem; ++i) {
                data[i] = value;
            }
 
            /* calculate the shape and stride */
            int nd = 1;
 
            npy_intp *ss = (npy_intp*)malloc(2*nd*sizeof(npy_intp));
            npy_intp *shape = ss;
            npy_intp *stride = ss + nd;
 
            shape[0] = n_elem;
            stride[0] = sizeof(double);
 
            /* construct the array interface */
            PyArrayInterface *inter = (PyArrayInterface*)
                malloc(sizeof(PyArrayInterface));
 
            memset(inter, 0, sizeof(PyArrayInterface));
 
            inter->two = 2;
            inter->nd = nd;
            inter->typekind = 'f';
            inter->itemsize = sizeof(double);
            inter->shape = shape;
            inter->strides = stride;
            inter->data = data;
            inter->flags = NPY_ARRAY_WRITEABLE | NPY_ARRAY_NOTSWAPPED |
                           NPY_ARRAY_ALIGNED | NPY_ARRAY_C_CONTIGUOUS;
 
            /* package into a capsule */
            PyObject *cap = PyCapsule_New(inter, NULL, delete_array_struct);
 
            /* save the pointer to the data */
            PyCapsule_SetContext(cap, data);
 
            fprintf(stderr, "new_array_struct\ncap = %ld inter = %ld"
                " ptr = %ld\n", (long)cap, (long)inter, (long)data);
 
            return cap;
        zimport_array();rNÚarray_interface_testing)ÚprologueÚ include_dirsÚ    build_dirÚ    more_init) ÚsysÚplatformÚ
startswithÚpytestÚskiprÚ ImportErrorrZbuild_and_import_extensionÚnpZ get_include)Ztmp_pathrZ    functionsr    r©rú\d:\z\workplace\vscode\pyvenv\venv\Lib\site-packages\numpy/core/tests/test_array_interface.pyÚ
get_modules$ 
#ÿ@ûrcsvG‡fdd„dƒ}tj}d}d}| d¡|d|ƒ}| d¡| d¡tj|d    d
}| d t|jƒ¡| d t|jƒ¡| d¡| d ¡d}| d¡t ||¡s°t    ‚| d¡| dt|ƒ¡| d¡| d¡||9}||9}| d t|jƒ¡| d t|jƒ¡| d¡| d¡| dt|ƒ¡| d¡t ||¡sZt    ‚| d¡d}| d¡dS)Ncs(eZdZdZdd„Ze‡fdd„ƒZdS)z!test_cstruct.<locals>.data_sourceaJ
        This class is for testing the timing of the PyCapsule destructor
        invoked when numpy release its reference to the shared data as part of
        the numpy array interface protocol. If the PyCapsule destructor is
        called early the shared data is freed and invalid memory accesses will
        occur.
        cSs||_||_dS©N)ÚsizeÚvalue)ÚselfrrrrrÚ__init__sz*test_cstruct.<locals>.data_source.__init__csˆ |j|j¡Sr)rrr)r©rrrÚ__array_struct__“sz2test_cstruct.<locals>.data_source.__array_struct__N)Ú__name__Ú
__module__Ú __qualname__Ú__doc__rÚpropertyrrrrrÚ data_source†sr goƒÀÊ!    ÀgˆÃÀz+ ---- create an object to share data ---- 
éz  ---- OK!
 
z8 ---- share data via the array interface protocol ---- 
F)Úcopyzarr.__array_interface___ = %s
zarr.base = %s
z0 ---- destroy the object that shared data ---- 
z ---- read shared data ---- 
z    arr = %s
z ---- modify shared data ---- 
z& ---- read modified shared data ---- 
z ---- free shared data ---- 
)
r
Ú
__stderr__ÚwriterÚarrayÚstrZ__array_interface__ÚbaseZallcloseÚAssertionError)rr ÚstderrZexpected_valueZ
multiplierÚbufZarrrrrÚ test_cstructƒsH
 
 
 
ÿ
 
 
 
 
 
ÿ
 
 
 
r+) r
r ÚnumpyrZ numpy.testingrZfixturerÚmarkZslowr+rrrrÚ<module>s 
{