MCPcopy Create free account
hub / github.com/numpy/numpy / PyArray_IntpConverter

Function PyArray_IntpConverter

numpy/core/src/multiarray/conversion_utils.c:111–211  ·  view source on GitHub ↗

NUMPY_API * Get intp chunk from sequence * * This function takes a Python sequence object and allocates and * fills in an intp array with the converted values. * * Remember to free the pointer seq.ptr when done using * PyDimMem_FREE(seq.ptr)** */

Source from the content-addressed store, hash-verified

109 * PyDimMem_FREE(seq.ptr)**
110 */
111NPY_NO_EXPORT int
112PyArray_IntpConverter(PyObject *obj, PyArray_Dims *seq)
113{
114 seq->ptr = NULL;
115 seq->len = 0;
116
117 /*
118 * When the deprecation below expires, remove the `if` statement, and
119 * update the comment for PyArray_OptionalIntpConverter.
120 */
121 if (obj == Py_None) {
122 /* Numpy 1.20, 2020-05-31 */
123 if (DEPRECATE(
124 "Passing None into shape arguments as an alias for () is "
125 "deprecated.") < 0){
126 return NPY_FAIL;
127 }
128 return NPY_SUCCEED;
129 }
130
131 PyObject *seq_obj = NULL;
132
133 /*
134 * If obj is a scalar we skip all the useless computations and jump to
135 * dimension_from_scalar as soon as possible.
136 */
137 if (!PyLong_CheckExact(obj) && PySequence_Check(obj)) {
138 seq_obj = PySequence_Fast(obj,
139 "expected a sequence of integers or a single integer.");
140 if (seq_obj == NULL) {
141 /* continue attempting to parse as a single integer. */
142 PyErr_Clear();
143 }
144 }
145
146 if (seq_obj == NULL) {
147 /*
148 * obj *might* be a scalar (if dimension_from_scalar does not fail, at
149 * the moment no check have been performed to verify this hypothesis).
150 */
151 seq->ptr = npy_alloc_cache_dim(1);
152 if (seq->ptr == NULL) {
153 PyErr_NoMemory();
154 return NPY_FAIL;
155 }
156 else {
157 seq->len = 1;
158
159 seq->ptr[0] = dimension_from_scalar(obj);
160 if (error_converting(seq->ptr[0])) {
161 /*
162 * If the error occurred is a type error (cannot convert the
163 * value to an integer) communicate that we expected a sequence
164 * or an integer from the user.
165 */
166 if (PyErr_ExceptionMatches(PyExc_TypeError)) {
167 PyErr_Format(PyExc_TypeError,
168 "expected a sequence of integers or a single "

Callers 10

array_reshapeFunction · 0.85
array_resizeFunction · 0.85
array_transposeFunction · 0.85
PyArray_ReshapeFunction · 0.85
_convert_from_tupleFunction · 0.85

Calls 4

npy_alloc_cache_dimFunction · 0.85
dimension_from_scalarFunction · 0.85
npy_free_cache_dim_objFunction · 0.85

Tested by

no test coverage detected