From 58c2a996afd13f729ec5d2aed77151c8e799548b Mon Sep 17 00:00:00 2001 From: Aaron Meurer Date: Tue, 2 Mar 2021 16:57:03 -0700 Subject: Make sure the array API ndarray object cannot wrap an array scalar --- numpy/_array_api/_array_object.py | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'numpy/_array_api') diff --git a/numpy/_array_api/_array_object.py b/numpy/_array_api/_array_object.py index 32a7bc9a8..b78405860 100644 --- a/numpy/_array_api/_array_object.py +++ b/numpy/_array_api/_array_object.py @@ -19,6 +19,8 @@ from enum import IntEnum from ._types import Optional, PyCapsule, Tuple, Union, array from ._creation_functions import asarray +import numpy as np + class ndarray: # Use a custom constructor instead of __init__, as manually initializing # this class is not supported API. @@ -34,6 +36,10 @@ class ndarray: """ obj = super().__new__(cls) + # Note: The spec does not have array scalars, only shape () arrays. + if isinstance(x, np.generic): + # x[...] converts an array scalar to a shape () array. + x = x[...] obj._array = x return obj -- cgit v1.2.1