From 907796d43f99e923f1c0dc4da1944b171ab546fd Mon Sep 17 00:00:00 2001 From: Bas van Beek Date: Sat, 10 Oct 2020 14:20:13 +0200 Subject: [PATCH 1/6] ENH: Allow `ctypeslib.load_library` to take any path-like object --- numpy/ctypeslib.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/numpy/ctypeslib.py b/numpy/ctypeslib.py index e8f7750fe5ff..39463af04d46 100644 --- a/numpy/ctypeslib.py +++ b/numpy/ctypeslib.py @@ -97,10 +97,10 @@ def load_library(libname, loader_path): Parameters ---------- - libname : str + libname : path-like Name of the library, which can have 'lib' as a prefix, but without an extension. - loader_path : str + loader_path : path-like Where the library can be found. Returns @@ -119,6 +119,10 @@ def load_library(libname, loader_path): warnings.warn("All features of ctypes interface may not work " "with ctypes < 1.0.1", stacklevel=2) + # Convert path-like objects into strings + libname = os.fsdecode(libname) + loader_path = os.fsdecode(loader_path) + ext = os.path.splitext(libname)[1] if not ext: # Try to load library with platform-specific name, otherwise From a9edc0433e2efff6695c2d22a1cd0cad325556ae Mon Sep 17 00:00:00 2001 From: Bas van Beek Date: Sat, 10 Oct 2020 14:20:42 +0200 Subject: [PATCH 2/6] TST: Updated the `ctypeslib.load_library` tests --- numpy/tests/test_ctypeslib.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/numpy/tests/test_ctypeslib.py b/numpy/tests/test_ctypeslib.py index af3730df1cc9..1ea0837008b7 100644 --- a/numpy/tests/test_ctypeslib.py +++ b/numpy/tests/test_ctypeslib.py @@ -1,6 +1,7 @@ import sys import pytest import weakref +from pathlib import Path import numpy as np from numpy.ctypeslib import ndpointer, load_library, as_array @@ -37,13 +38,15 @@ reason="Known to fail on cygwin") class TestLoadLibrary: def test_basic(self): - try: - # Should succeed - load_library('_multiarray_umath', np.core._multiarray_umath.__file__) - except ImportError as e: - msg = ("ctypes is not available on this python: skipping the test" - " (import error was: %s)" % str(e)) - print(msg) + loader_path = np.core._multiarray_umath.__file__ + + out1 = load_library('_multiarray_umath', loader_path) + out2 = load_library(Path('_multiarray_umath'), loader_path) + out3 = load_library('_multiarray_umath', Path(loader_path)) + out4 = load_library(b'_multiarray_umath', loader_path) + + assert isinstance(out1, ctypes.CDLL) + assert out1 is out2 is out3 is out4 def test_basic2(self): # Regression for #801: load_library with a full library name From 3271a08baf9500f0112be24e7573d9ae2945f847 Mon Sep 17 00:00:00 2001 From: Bas van Beek Date: Sat, 10 Oct 2020 15:43:31 +0200 Subject: [PATCH 3/6] DOC: Added a `versionchanged` note --- numpy/ctypeslib.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/numpy/ctypeslib.py b/numpy/ctypeslib.py index 39463af04d46..741b545d9f6e 100644 --- a/numpy/ctypeslib.py +++ b/numpy/ctypeslib.py @@ -95,6 +95,10 @@ def load_library(libname, loader_path): plus the fact Windows will just load the first library it finds with that name. NumPy supplies the load_library function as a convenience. + .. versionchanged:: 1.20.0 + Allow libname and loader_path to take any + :term:`python:path-like object`. + Parameters ---------- libname : path-like From 104c81888760e2f5d6460e667e8092d03dcf70a0 Mon Sep 17 00:00:00 2001 From: Bas van Beek Date: Sat, 10 Oct 2020 15:43:49 +0200 Subject: [PATCH 4/6] DOC: Fixed a formatting issue --- numpy/ctypeslib.py | 1 + 1 file changed, 1 insertion(+) diff --git a/numpy/ctypeslib.py b/numpy/ctypeslib.py index 741b545d9f6e..a456d5963708 100644 --- a/numpy/ctypeslib.py +++ b/numpy/ctypeslib.py @@ -89,6 +89,7 @@ def _dummy(*args, **kwds): def load_library(libname, loader_path): """ It is possible to load a library using + >>> lib = ctypes.cdll[] # doctest: +SKIP But there are cross-platform considerations, such as library file extensions, From d690cf60beef0739f6d7c15e778c37e528afe58f Mon Sep 17 00:00:00 2001 From: Bas van Beek Date: Sat, 10 Oct 2020 15:48:27 +0200 Subject: [PATCH 5/6] REL: Added a release note --- doc/release/upcoming_changes/17530.improvements.rst | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 doc/release/upcoming_changes/17530.improvements.rst diff --git a/doc/release/upcoming_changes/17530.improvements.rst b/doc/release/upcoming_changes/17530.improvements.rst new file mode 100644 index 000000000000..07a23f0e5e6b --- /dev/null +++ b/doc/release/upcoming_changes/17530.improvements.rst @@ -0,0 +1,5 @@ +`ctypeslib.load_library` can now take any path-like object +----------------------------------------------------------------------- +All parameters in the can now take any :term:`python:path-like object`. +This includes the likes of strings, bytes and objects implementing the +:meth:`__fspath__` protocol. From b3dbd784014a22a95163c5b8c80ab3fe40e96ec7 Mon Sep 17 00:00:00 2001 From: Bas van Beek Date: Sat, 10 Oct 2020 16:21:03 +0200 Subject: [PATCH 6/6] MAINT: Fixed a filename: `*.improvements.rst` -> `*.improvement.rst` --- .../{17530.improvements.rst => 17530.improvement.rst} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename doc/release/upcoming_changes/{17530.improvements.rst => 17530.improvement.rst} (100%) diff --git a/doc/release/upcoming_changes/17530.improvements.rst b/doc/release/upcoming_changes/17530.improvement.rst similarity index 100% rename from doc/release/upcoming_changes/17530.improvements.rst rename to doc/release/upcoming_changes/17530.improvement.rst