8000 WIP: Remove fragile use of `__array_interface__` in ctypeslib.as_array by eric-wieser · Pull Request #10970 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

WIP: Remove fragile use of __array_interface__ in ctypeslib.as_array #10970

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 7, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
TST: Add tests for numpy.ctypeslib.as_array
  • Loading branch information
tynn authored and eric-wieser committed Apr 25, 2018
commit 1ec8f6d93ea150d79cfea6515ffe02f6d78753b2
26 changes: 24 additions & 2 deletions numpy/tests/test_ctypeslib.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blank line after imports is standard.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

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)))
0