(self)
| 2924 | @pytest.mark.skipif(not hasattr(ct, "pythonapi"), |
| 2925 | reason="`ctypes.pythonapi` required for capsule unpacking.") |
| 2926 | def test_loop_access(self): |
| 2927 | # This is a basic test for the full strided loop access |
| 2928 | data_t = ct.ARRAY(ct.c_char_p, 2) |
| 2929 | dim_t = ct.ARRAY(ct.c_ssize_t, 1) |
| 2930 | strides_t = ct.ARRAY(ct.c_ssize_t, 2) |
| 2931 | strided_loop_t = ct.CFUNCTYPE( |
| 2932 | ct.c_int, ct.c_void_p, data_t, dim_t, strides_t, ct.c_void_p) |
| 2933 | |
| 2934 | class call_info_t(ct.Structure): |
| 2935 | _fields_ = [ |
| 2936 | ("strided_loop", strided_loop_t), |
| 2937 | ("context", ct.c_void_p), |
| 2938 | ("auxdata", ct.c_void_p), |
| 2939 | ("requires_pyapi", ct.c_byte), |
| 2940 | ("no_floatingpoint_errors", ct.c_byte), |
| 2941 | ] |
| 2942 | |
| 2943 | i4 = np.dtype("i4") |
| 2944 | dt, call_info_obj = np.negative._resolve_dtypes_and_context((i4, i4)) |
| 2945 | assert dt == (i4, i4) # can be used without casting |
| 2946 | |
| 2947 | # Fill in the rest of the information: |
| 2948 | np.negative._get_strided_loop(call_info_obj) |
| 2949 | |
| 2950 | ct.pythonapi.PyCapsule_GetPointer.restype = ct.c_void_p |
| 2951 | call_info = ct.pythonapi.PyCapsule_GetPointer( |
| 2952 | ct.py_object(call_info_obj), |
| 2953 | ct.c_char_p(b"numpy_1.24_ufunc_call_info")) |
| 2954 | |
| 2955 | call_info = ct.cast(call_info, ct.POINTER(call_info_t)).contents |
| 2956 | |
| 2957 | arr = np.arange(10, dtype=i4) |
| 2958 | call_info.strided_loop( |
| 2959 | call_info.context, |
| 2960 | data_t(arr.ctypes.data, arr.ctypes.data), |
| 2961 | arr.ctypes.shape, # is a C-array with 10 here |
| 2962 | strides_t(arr.ctypes.strides[0], arr.ctypes.strides[0]), |
| 2963 | call_info.auxdata) |
| 2964 | |
| 2965 | # We just directly called the negative inner-loop in-place: |
| 2966 | assert_array_equal(arr, -np.arange(10, dtype=i4)) |
| 2967 | |
| 2968 | @pytest.mark.parametrize("strides", [1, (1, 2, 3), (1, "2")]) |
| 2969 | def test__get_strided_loop_errors_bad_strides(self, strides): |
nothing calls this directly
no test coverage detected