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
Conversation
@@ -37,13 +38,15 @@ | |||
reason="Known to fail on cygwin") | |||
class TestLoadLibrary: | |||
def test_basic(self): | |||
try: |
There was a problem hiding this comment.
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:
numpy/numpy/tests/test_ctypeslib.py
Lines 35 to 39 in a9edc04
@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 |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
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] |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
Needs a release note. |
See d690cf6. |
@eric-wieser Look good to you? |
close/reopen |
@eric-wieser Ping. |
Thanks Bas. |
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.