diff options
author | mattip <matti.picus@gmail.com> | 2019-08-28 10:01:41 +0300 |
---|---|---|
committer | Charles Harris <charlesr.harris@gmail.com> | 2019-09-04 17:44:56 -0600 |
commit | 992a20e9d9cf8779b956a04ea42299efd45615a4 (patch) | |
tree | d83b434316b08685e3192790057f16d948faa4cf | |
parent | 4ea7bca8e09f854f2aabc73f0c04a9a4068dc49a (diff) | |
download | numpy-992a20e9d9cf8779b956a04ea42299efd45615a4.tar.gz |
BUG: test, fix regression in converting to ctypes
-rw-r--r-- | numpy/core/tests/test_regression.py | 27 | ||||
-rw-r--r-- | numpy/ctypeslib.py | 17 |
2 files changed, 37 insertions, 7 deletions
diff --git a/numpy/core/tests/test_regression.py b/numpy/core/tests/test_regression.py index 3b9ca7246..62ac2b310 100644 --- a/numpy/core/tests/test_regression.py +++ b/numpy/core/tests/test_regression.py @@ -2455,3 +2455,30 @@ class TestRegression(object): __array_interface__ = {} np.array([T()]) + + def test_2d__array__shape(self): + class T(object): + def __array__(self): + return np.ndarray(shape=(0,0)) + + # Make sure __array__ is used instead of Sequence methods. + def __iter__(self): + return iter([]) + + def __getitem__(self, idx): + raise AssertionError("__getitem__ was called") + + def __len__(self): + return 0 + + + t = T() + #gh-13659, would raise in broadcasting [x=t for x in result] + np.array([t]) + + def test_to_ctypes(self): + #gh-14214 + arr = np.zeros((2 ** 31 + 1,), 'b') + assert(arr.size * arr.itemsize > 2 ** 31) + c_arr = np.ctypeslib.as_ctypes(arr) + assert_equal(c_arr._length_, arr.size) diff --git a/numpy/ctypeslib.py b/numpy/ctypeslib.py index 535ea768b..5001ba931 100644 --- a/numpy/ctypeslib.py +++ b/numpy/ctypeslib.py @@ -92,11 +92,11 @@ else: # Adapted from Albert Strasheim def load_library(libname, loader_path): """ - It is possible to load a library using - >>> lib = ctypes.cdll[<full_path_name>] + It is possible to load a library using + >>> lib = ctypes.cdll[<full_path_name>] # doctest: +SKIP But there are cross-platform considerations, such as library file extensions, - plus the fact Windows will just load the first library it finds with that name. + plus the fact Windows will just load the first library it finds with that name. NumPy supplies the load_library function as a convenience. Parameters @@ -110,12 +110,12 @@ else: Returns ------- ctypes.cdll[libpath] : library object - A ctypes library object + A ctypes library object Raises ------ OSError - If there is no library with the expected extension, or the + If there is no library with the expected extension, or the library is defective and cannot be loaded. """ if ctypes.__version__ < '1.0.1': @@ -535,7 +535,10 @@ if ctypes is not None: if readonly: raise TypeError("readonly arrays unsupported") - dtype = _dtype((ai["typestr"], ai["shape"])) - result = as_ctypes_type(dtype).from_address(addr) + # can't use `_dtype((ai["typestr"], ai["shape"]))` here, as it overflows + # dtype.itemsize (gh-14214) + ctype_scalar = as_ctypes_type(ai["typestr"]) + result_type = _ctype_ndarray(ctype_scalar , ai['shape']) + result = result_type.from_address(addr) result.__keep = obj return result |