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

Function PyArray_ScalarFromObject

numpy/core/src/multiarray/scalarapi.c:312–384  ·  view source on GitHub ↗

NUMPY_API * Get an Array Scalar From a Python Object * * Returns NULL if unsuccessful but error is only set if another error occurred. * Currently only Numeric-like object supported. */

Source from the content-addressed store, hash-verified

310 * Currently only Numeric-like object supported.
311 */
312NPY_NO_EXPORT PyObject *
313PyArray_ScalarFromObject(PyObject *object)
314{
315 if (DEPRECATE(
316 "PyArray_ScalarFromObject() is deprecated and scheduled for "
317 "removal. If you are using this (undocumented) function, "
318 "please notify the NumPy developers to look for solutions."
319 "(Deprecated in NumPy 1.23)") < 0) {
320 return NULL;
321 }
322
323 PyObject *ret = NULL;
324
325 if (PyArray_IsZeroDim(object)) {
326 return PyArray_ToScalar(PyArray_DATA((PyArrayObject *)object),
327 (PyArrayObject *)object);
328 }
329 /*
330 * Booleans in Python are implemented as a subclass of integers,
331 * so PyBool_Check must be called before PyLong_Check.
332 */
333 if (PyBool_Check(object)) {
334 if (object == Py_True) {
335 PyArrayScalar_RETURN_TRUE;
336 }
337 else {
338 PyArrayScalar_RETURN_FALSE;
339 }
340 }
341 else if (PyLong_Check(object)) {
342 /* Check if fits in long */
343 npy_long val_long = PyLong_AsLong(object);
344 if (!error_converting(val_long)) {
345 ret = PyArrayScalar_New(Long);
346 if (ret != NULL) {
347 PyArrayScalar_VAL(ret, Long) = val_long;
348 }
349 return ret;
350 }
351 PyErr_Clear();
352
353 /* Check if fits in long long */
354 npy_longlong val_longlong = PyLong_AsLongLong(object);
355 if (!error_converting(val_longlong)) {
356 ret = PyArrayScalar_New(LongLong);
357 if (ret != NULL) {
358 PyArrayScalar_VAL(ret, LongLong) = val_longlong;
359 }
360 return ret;
361 }
362 PyErr_Clear();
363
364 return NULL;
365 }
366 else if (PyFloat_Check(object)) {
367 ret = PyArrayScalar_New(Double);
368 if (ret != NULL) {
369 PyArrayScalar_VAL(ret, Double) = PyFloat_AS_DOUBLE(object);

Callers

nothing calls this directly

Calls 1

PyArray_DATAFunction · 0.85

Tested by

no test coverage detected