From 1ec8f6d93ea150d79cfea6515ffe02f6d78753b2 Mon Sep 17 00:00:00 2001 From: tynn Date: Sat, 1 Jul 2017 20:33:45 +0200 Subject: [PATCH 1/4] TST: Add tests for numpy.ctypeslib.as_array --- numpy/tests/test_ctypeslib.py | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/numpy/tests/test_ctypeslib.py b/numpy/tests/test_ctypeslib.py index 0f0d6dbc4891..508a348c145a 100644 --- a/numpy/tests/test_ctypeslib.py +++ b/numpy/tests/test_ctypeslib.py @@ -4,9 +4,9 @@ import pytest import numpy as np -from numpy.ctypeslib import ndpointer, load_library +from numpy.ctypeslib import ndpointer, load_library, as_array from numpy.distutils.misc_util import get_shared_lib_extension -from numpy.testing import assert_, assert_raises +from numpy.testing import assert_, assert_array_equal, assert_raises try: cdll = None @@ -113,3 +113,25 @@ def test_cache(self): a1 = ndpointer(dtype=np.float64) a2 = ndpointer(dtype=np.float64) assert_(a1 == a2) + +class TestAsArray(object): + @pytest.mark.skipif(not _HAS_CTYPE, + reason="ctypes not available on this python installation") + def test_array(self): + from ctypes import c_int + at = c_int * 2 + a = as_array(at(1, 2)) + assert_(a.shape == (2,)) + assert_array_equal(a, np.array([1, 2])) + a = as_array((at * 3)(at(1, 2), at(3, 4), at(5, 6))) + assert_(a.shape == (3, 2)) + assert_array_equal(a, np.array([[1, 2], [3, 4], [5, 6]])) + + @pytest.mark.skipif(not _HAS_CTYPE, + reason="ctypes not available on this python installation") + def test_pointer(self): + from ctypes import c_int, cast, POINTER + p = cast((c_int * 10)(*range(10)), POINTER(c_int)) + a = as_array(p, (10,)) + assert_(a.shape == (10,)) + assert_array_equal(a, np.array(range(10))) From 58e40ce3f270a84faa45ff29bcbce5dfea2a0e74 Mon Sep 17 00:00:00 2001 From: Eric Wieser Date: Tue, 24 Apr 2018 21:48:53 -0700 Subject: [PATCH 2/4] MAINT: Use assert_equal --- numpy/tests/test_ctypeslib.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/numpy/tests/test_ctypeslib.py b/numpy/tests/test_ctypeslib.py index 508a348c145a..725d868d79ee 100644 --- a/numpy/tests/test_ctypeslib.py +++ b/numpy/tests/test_ctypeslib.py @@ -6,7 +6,7 @@ import numpy as np from numpy.ctypeslib import ndpointer, load_library, as_array from numpy.distutils.misc_util import get_shared_lib_extension -from numpy.testing import assert_, assert_array_equal, assert_raises +from numpy.testing import assert_, assert_array_equal, assert_raises, assert_equal try: cdll = None @@ -121,10 +121,10 @@ def test_array(self): from ctypes import c_int at = c_int * 2 a = as_array(at(1, 2)) - assert_(a.shape == (2,)) + assert_equal(a.shape, (2,)) assert_array_equal(a, np.array([1, 2])) a = as_array((at * 3)(at(1, 2), at(3, 4), at(5, 6))) - assert_(a.shape == (3, 2)) + assert_equal(a.shape, (3, 2)) assert_array_equal(a, np.array([[1, 2], [3, 4], [5, 6]])) @pytest.mark.skipif(not _HAS_CTYPE, @@ -133,5 +133,5 @@ def test_pointer(self): from ctypes import c_int, cast, POINTER p = cast((c_int * 10)(*range(10)), POINTER(c_int)) a = as_array(p, (10,)) - assert_(a.shape == (10,)) + assert_equal(a.shape, (10,)) assert_array_equal(a, np.array(range(10))) From 4ec8930f0b60d7fff059d0f005ed2a0efbf41fb7 Mon Sep 17 00:00:00 2001 From: Eric Wieser Date: Tue, 24 Apr 2018 21:51:23 -0700 Subject: [PATCH 3/4] MAINT: Pull repeated decorators up to their containing class --- numpy/tests/test_ctypeslib.py | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/numpy/tests/test_ctypeslib.py b/numpy/tests/test_ctypeslib.py index 725d868d79ee..fd4336fad02f 100644 --- a/numpy/tests/test_ctypeslib.py +++ b/numpy/tests/test_ctypeslib.py @@ -21,11 +21,12 @@ except ImportError: _HAS_CTYPE = False + +@pytest.mark.skipif(not _HAS_CTYPE, + reason="ctypes not available in this python") +@pytest.mark.skipif(sys.platform == 'cygwin', + reason="Known to fail on cygwin") class TestLoadLibrary(object): - @pytest.mark.skipif(not _HAS_CTYPE, - reason="ctypes not available in this python") - @pytest.mark.skipif(sys.platform == 'cygwin', - reason="Known to fail on cygwin") def test_basic(self): try: # Should succeed @@ -35,10 +36,6 @@ def test_basic(self): " (import error was: %s)" % str(e)) print(msg) - @pytest.mark.skipif(not _HAS_CTYPE, - reason="ctypes not available in this python") - @pytest.mark.skipif(sys.platform == 'cygwin', - reason="Known to fail on cygwin") def test_basic2(self): # Regression for #801: load_library with a full library name # (including extension) does not work. @@ -54,6 +51,7 @@ def test_basic2(self): " (import error was: %s)" % str(e)) print(msg) + class TestNdpointer(object): def test_dtype(self): dt = np.intc @@ -114,9 +112,10 @@ def test_cache(self): a2 = ndpointer(dtype=np.float64) assert_(a1 == a2) + +@pytest.mark.skipif(not _HAS_CTYPE, + reason="ctypes not available on this python installation") class TestAsArray(object): - @pytest.mark.skipif(not _HAS_CTYPE, - reason="ctypes not available on this python installation") def test_array(self): from ctypes import c_int at = c_int * 2 @@ -127,8 +126,6 @@ def test_array(self): assert_equal(a.shape, (3, 2)) assert_array_equal(a, np.array([[1, 2], [3, 4], [5, 6]])) - @pytest.mark.skipif(not _HAS_CTYPE, - reason="ctypes not available on this python installation") def test_pointer(self): from ctypes import c_int, cast, POINTER p = cast((c_int * 10)(*range(10)), POINTER(c_int)) From 8a8be508d2b2ce719de94209286293973c278b46 Mon Sep 17 00:00:00 2001 From: Eric Wieser Date: Tue, 24 Apr 2018 21:34:40 -0700 Subject: [PATCH 4/4] BUG: Remove fragile use of __array_interface__ in ctypeslib.as_array Everything behaves a lot better if we let the array constructor handle it, which will use the ctypes PEP3118 support. Bugs this fixes: * Stale state being attached to pointer objects (fixes gh-2671, closes gh-6214) * Weird failure modes on structured arrays (fixes-10978) * A regression in gh-10882 (fixes gh-10968) --- doc/release/1.15.0-notes.rst | 5 ++ numpy/ctypeslib.py | 133 ++++++++-------------------------- numpy/tests/test_ctypeslib.py | 48 ++++++++++-- 3 files changed, 77 insertions(+), 109 deletions(-) diff --git a/doc/release/1.15.0-notes.rst b/doc/release/1.15.0-notes.rst index 49e8ab22d77e..3ea51dca8ef5 100644 --- a/doc/release/1.15.0-notes.rst +++ b/doc/release/1.15.0-notes.rst @@ -97,6 +97,11 @@ available, but will not be maintained. The standard testing utilities, the nose specific functions `import_nose` and `raises`. Those functions are not used in numpy, but are kept for downstream compatibility. +Numpy no longer monkey-patches ``ctypes`` with ``__array_interface__`` +---------------------------------------------------------------------- +Previously numpy added ``__array_interface__`` attributes to all the integer +types from ``ctypes``. + ``np.ma.notmasked_contiguous`` and ``np.ma.flatnotmasked_contiguous`` always return lists ----------------------------------------------------------------------------------------- This was always the documented behavior, but in reality the result used to be diff --git a/numpy/ctypeslib.py b/numpy/ctypeslib.py index b8457c78b2c5..9d71adbdb6f6 100644 --- a/numpy/ctypeslib.py +++ b/numpy/ctypeslib.py @@ -319,120 +319,47 @@ def ndpointer(dtype=None, ndim=None, shape=None, flags=None): _pointer_type_cache[(dtype, shape, ndim, num)] = klass return klass -if ctypes is not None: - ct = ctypes - ################################################################ - # simple types - - # maps the numpy typecodes like '