/
opt
/
imh-python
/
lib
/
python3.9
/
site-packages
/
numpy
/
core
/
tests
/
/opt/imh-python/lib/python3.9/site-packages/numpy/core/tests
mkdir
upload
Name
Size
Mode
Actions
data/
-
0755
rm
examples/
-
0755
rm
__pycache__/
-
0755
rm
test_abc.py
2220
0644
edit
dl
rm
test_api.py
22995
0644
edit
dl
rm
test_argparse.py
1969
0644
edit
dl
rm
test_arraymethod.py
3244
0644
edit
dl
rm
test_arrayprint.py
40462
0644
edit
dl
rm
test_array_coercion.py
34379
0644
edit
dl
rm
test_array_interface.py
7774
0644
edit
dl
rm
test_casting_floatingpoint_errors.py
5063
0644
edit
dl
rm
test_casting_unittests.py
34298
0644
edit
dl
rm
test_conversion_utils.py
6559
0644
edit
dl
rm
test_cpu_dispatcher.py
1521
0644
edit
dl
rm
test_cpu_features.py
14858
0644
edit
dl
rm
test_custom_dtypes.py
9401
0644
edit
dl
rm
test_cython.py
3444
0644
edit
dl
rm
test_datetime.py
116211
0644
edit
dl
rm
test_defchararray.py
24997
0644
edit
dl
rm
test_deprecations.py
31076
0644
edit
dl
rm
test_dlpack.py
3522
0644
edit
dl
rm
test_dtype.py
75451
0644
edit
dl
rm
test_einsum.py
53712
0644
edit
dl
rm
test_errstate.py
2219
0644
edit
dl
rm
test_extint128.py
5643
0644
edit
dl
rm
test_function_base.py
15595
0644
edit
dl
rm
test_getlimits.py
6718
0644
edit
dl
rm
test_half.py
24226
0644
edit
dl
rm
test_hashtable.py
1011
0644
edit
dl
rm
test_indexerrors.py
5130
0644
edit
dl
rm
test_indexing.py
54314
0644
edit
dl
rm
test_item_selection.py
6458
0644
edit
dl
rm
test_limited_api.py
1172
0644
edit
dl
rm
test_longdouble.py
13905
0644
edit
dl
rm
test_machar.py
1067
0644
edit
dl
rm
test_memmap.py
7477
0644
edit
dl
rm
test_mem_overlap.py
29086
0644
edit
dl
rm
test_mem_policy.py
16780
0644
edit
dl
rm
test_multiarray.py
379827
0644
edit
dl
rm
test_nditer.py
130818
0644
edit
dl
rm
test_nep50_promotions.py
8840
0644
edit
dl
rm
test_numeric.py
136543
0644
edit
dl
rm
test_numerictypes.py
21687
0644
edit
dl
rm
test_overrides.py
26080
0644
edit
dl
rm
test_print.py
6837
0644
edit
dl
rm
test_protocols.py
1168
0644
edit
dl
rm
test_records.py
20269
0644
edit
dl
rm
test_regression.py
91595
0644
edit
dl
rm
test_scalarbuffer.py
5580
0644
edit
dl
rm
test_scalarinherit.py
2368
0644
edit
dl
rm
test_scalarmath.py
43247
0644
edit
dl
rm
test_scalarprint.py
18771
0644
edit
dl
rm
test_scalar_ctors.py
6115
0644
edit
dl
rm
test_scalar_methods.py
7541
0644
edit
dl
rm
test_shape_base.py
29723
0644
edit
dl
rm
test_simd.py
48696
0644
edit
dl
rm
test_simd_module.py
3805
0644
edit
dl
rm
test_strings.py
3835
0644
edit
dl
rm
test_ufunc.py
124800
0644
edit
dl
rm
test_umath.py
185601
0644
edit
dl
rm
test_umath_accuracy.py
3897
0644
edit
dl
rm
test_umath_complex.py
23243
0644
edit
dl
rm
test_unicode.py
12775
0644
edit
dl
rm
test__exceptions.py
2846
0644
edit
dl
rm
_locales.py
2206
0644
edit
dl
rm
__init__.py
0
0644
edit
dl
rm
Edit:
/opt/imh-python/lib/python3.9/site-packages/numpy/core/tests/test_array_interface.py
(7774B)
import sys import pytest import numpy as np from numpy.testing import extbuild @pytest.fixture def get_module(tmp_path): """ Some codes to generate data and manage temporary buffers use when sharing with numpy via the array interface protocol. """ if not sys.platform.startswith('linux'): pytest.skip('link fails on cygwin') prologue = ''' #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); } ''' functions = [ ("new_array_struct", "METH_VARARGS", """ 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; """) ] more_init = "import_array();" try: import array_interface_testing return array_interface_testing except ImportError: pass # if it does not exist, build and load it return extbuild.build_and_import_extension('array_interface_testing', functions, prologue=prologue, include_dirs=[np.get_include()], build_dir=tmp_path, more_init=more_init) # FIXME: numpy.testing.extbuild uses `numpy.distutils`, so this won't work on # Python 3.12 and up. @pytest.mark.skipif(sys.version_info >= (3, 12), reason="no numpy.distutils") @pytest.mark.slow def test_cstruct(get_module): class data_source: """ 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. """ def __init__(self, size, value): self.size = size self.value = value @property def __array_struct__(self): return get_module.new_array_struct(self.size, self.value) # write to the same stream as the C code stderr = sys.__stderr__ # used to validate the shared data. expected_value = -3.1415 multiplier = -10000.0 # create some data to share with numpy via the array interface # assign the data an expected value. stderr.write(' ---- create an object to share data ---- \n') buf = data_source(256, expected_value) stderr.write(' ---- OK!\n\n') # share the data stderr.write(' ---- share data via the array interface protocol ---- \n') arr = np.array(buf, copy=False) stderr.write('arr.__array_interface___ = %s\n' % ( str(arr.__array_interface__))) stderr.write('arr.base = %s\n' % (str(arr.base))) stderr.write(' ---- OK!\n\n') # release the source of the shared data. this will not release the data # that was shared with numpy, that is done in the PyCapsule destructor. stderr.write(' ---- destroy the object that shared data ---- \n') buf = None stderr.write(' ---- OK!\n\n') # check that we got the expected data. If the PyCapsule destructor we # defined was prematurely called then this test will fail because our # destructor sets the elements of the array to NaN before free'ing the # buffer. Reading the values here may also cause a SEGV assert np.allclose(arr, expected_value) # read the data. If the PyCapsule destructor we defined was prematurely # called then reading the values here may cause a SEGV and will be reported # as invalid reads by valgrind stderr.write(' ---- read shared data ---- \n') stderr.write('arr = %s\n' % (str(arr))) stderr.write(' ---- OK!\n\n') # write to the shared buffer. If the shared data was prematurely deleted # this will may cause a SEGV and valgrind will report invalid writes stderr.write(' ---- modify shared data ---- \n') arr *= multiplier expected_value *= multiplier stderr.write('arr.__array_interface___ = %s\n' % ( str(arr.__array_interface__))) stderr.write('arr.base = %s\n' % (str(arr.base))) stderr.write(' ---- OK!\n\n') # read the data. If the shared data was prematurely deleted this # will may cause a SEGV and valgrind will report invalid reads stderr.write(' ---- read modified shared data ---- \n') stderr.write('arr = %s\n' % (str(arr))) stderr.write(' ---- OK!\n\n') # check that we got the expected data. If the PyCapsule destructor we # defined was prematurely called then this test will fail because our # destructor sets the elements of the array to NaN before free'ing the # buffer. Reading the values here may also cause a SEGV assert np.allclose(arr, expected_value) # free the shared data, the PyCapsule destructor should run here stderr.write(' ---- free shared data ---- \n') arr = None stderr.write(' ---- OK!\n\n')
Save
cmd:
run