Skip to content
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

ENH: Allow ctypeslib.load_library to take any path-like object #17530

Merged
merged 6 commits into from Oct 9, 2021

Conversation

BvB93
Copy link
Member

@BvB93 BvB93 commented Oct 10, 2020

This PR allows ctypeslib.load_library() take take any path-like object,
which includes the likes of strings, bytes and any object implementing the __fspath__ protocol.

@@ -37,13 +38,15 @@
reason="Known to fail on cygwin")
class TestLoadLibrary:
def test_basic(self):
try:
Copy link
Member Author

Choose a reason for hiding this comment

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

Note that the try / except clause is redundant due to the pytest fixture:

@pytest.mark.skipif(ctypes is None,
reason="ctypes not available in this python")
@pytest.mark.skipif(sys.platform == 'cygwin',
reason="Known to fail on cygwin")
class TestLoadLibrary:

@@ -97,10 +97,10 @@ def load_library(libname, loader_path):

Parameters
----------
libname : str
libname : path-like
Copy link
Member

Choose a reason for hiding this comment

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

Are paths with / actually allowed here? If not, this looks like it should remain a str

Copy link
Member Author

Choose a reason for hiding this comment

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

It seems to be, yes.
What happens when defining libpath is, roughly speaking, calling os.path.join on loader_path and libname the input parameters.

numpy/numpy/ctypeslib.py

Lines 144 to 148 in 4afd82d

for ln in libname_ext:
libpath = os.path.join(libdir, ln)
if os.path.exists(libpath):
try:
return ctypes.cdll[libpath]

Copy link
Member

Choose a reason for hiding this comment

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

Hmm, to me that implies the path component should always be in loader_path, and libname should always be a string.

Copy link
Member Author

Choose a reason for hiding this comment

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

Perhaps, though os.path.join is not required to join a path and a file name;
the joining of two paths is also perfectly acceptable.

In [1]: import os

In [2]: os.path.join('a/b/c/', 'd/e/f')
Out[2]: 'a/b/c/d/e/f'

Besides, why should it remain a string if a / is not allowed?
The likes of pathlib.Path are just generally useful objects, regardless of whether it is used to represent a path- or file-name.

@charris charris added 56 - Needs Release Note. Needs an entry in doc/release/upcoming_changes component: numpy.ctypes labels Oct 10, 2020
@charris
Copy link
Member

charris commented Oct 10, 2020

Needs a release note.

@BvB93
Copy link
Member Author

BvB93 commented Oct 10, 2020

Needs a release note.

See d690cf6.

@BvB93 BvB93 removed the 56 - Needs Release Note. Needs an entry in doc/release/upcoming_changes label Oct 11, 2020
@charris
Copy link
Member

charris commented Jan 25, 2021

@eric-wieser Look good to you?

Base automatically changed from master to main March 4, 2021 02:05
@charris
Copy link
Member

charris commented Mar 30, 2021

close/reopen

@charris charris closed this Mar 30, 2021
@charris charris reopened this Mar 30, 2021
@charris
Copy link
Member

charris commented Apr 19, 2021

@eric-wieser Ping.

@charris charris merged commit 83d8df9 into numpy:main Oct 9, 2021
@charris
Copy link
Member

charris commented Oct 9, 2021

Thanks Bas.

@BvB93 BvB93 deleted the fspath branch October 9, 2021 20:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants