-
-
Notifications
You must be signed in to change notification settings - Fork 11k
ENH: Improve support for pathlib.Path objects in load functions #11348
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
Conversation
Add support for pathlib.Path when "mmap_mode" is specified in np.load "Closes numpy#11342". Add support for pathlib.Path in np.core.records.fromfile.
Worth noting that this only matters on python < 3.6 edit: Not true, apologies. Would be good to use |
@eric-wieser |
numpy/core/records.py
Outdated
@@ -767,6 +767,9 @@ def fromfile(fd, dtype=None, shape=None, offset=0, formats=None, | |||
if isinstance(fd, str): | |||
name = 1 | |||
fd = open(fd, 'rb') | |||
elif is_pathlib_path(fd): | |||
8000 name = 1 | |||
fd = fd.open('rb') |
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.
curious, what happens when fd is a unicode string on python2?
Not sure how using
Backporting On another topic, we don't accept unicode (pre-python3) or bytes (post-python3)? |
Yes, it seems like this is the current situation. I would suggest using from numpy.compat import isfileobj, is_pathlib_path
if isfileobj(fd):
pass # the file is already open
elif is_pathlib_path(fd):
# new pathlib code
else:
# previous code If there are any unicode errors caused by previous code, then at least numpy is not responsible for resolving them. [EDIT]: This also applies to the memmap code https://github.com/numpy/numpy/pull/11348/files/c4e5aa10ab630a26019af621ee77d532c99cb076#diff-177e218db0a99e24a32dbb379623d744R755. |
There are conflicts and the release note change is for 1.15, not 1.16 |
@mattip Could you please give me your opinion on my suggestion in my previous comment? I will have a look at the conflicts once it is clear how to proceed. |
Your suggestion looks good to me. Please go ahead with it. |
The implementation of Cutting out this comment in place of a PR at #12157 |
I suppose I will wait for #12157 then. At least the test functions might be useful. |
@@ -2268,6 +2268,7 @@ def test_save_load_memmap(self): | |||
np.save(path, a) | |||
data = np.load(path, mmap_mode='r') | |||
assert_array_equal(data, a) | |||
del data |
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.
What's the rationale for this line? A comment might be nice
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.
This is required such that numpy closes the mem-mapped file. Otherwise you will get weird errors on Windows.
#12157 has been merged |
Add support for pathlib.Path when "mmap_mode" is specified in np.load "Closes numpy#11342". Add support for pathlib.Path in np.core.records.fromfile.
It seems like it screwed up and used "pull" instead of "rebase" somewhere along the way of getting the changes from #12157 into this feature branch. |
Looks like that did the trick. I guess that's a github bug? |
numpy/lib/format.py
Outdated
if is_pathlib_path(filename): | ||
fp = filename.open(mode+'b') | ||
else: | ||
fp = open(filename, mode+'b') |
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.
Better as
from numpy.compat import os_fspath
fp = open(os_fspath(filename), mode+'b')
Since this handles PurePath
and third-party pathlike objects
numpy/lib/format.py
Outdated
if is_pathlib_path(filename): | ||
fp = filename.open('rb') | ||
else: | ||
fp = open(filename, 'rb') |
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.
Same comment here
I think I covered everything. I am not sure though, if I edited the release notes correctly (please check). And I don't know why numpy.core.tests.test_records.TestPathUsage.test_tofile_fromfile fails on Shippable/Python2.7 (it works on my Ubuntu machine). |
The release note should be on the upcoming |
OK, got it. Checks also seem to pass. Ready to merge. I would suggest a squash-merge. |
Thanks @paulmueller , @eric-wieser |
I misused the squash merge feature of github so the message got doubled. |
E097 | a[5] = (0.5,10,'abcde') | |
a.newbyteorder('<') | ||
with path.open("wb") as fd: | ||
a.tofile(fd) |
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.
ndarray.tofile()
discards information on endianness and most recent ARM features switchable endianness (bi-endian)
I wonder if this is contributing to #12330.
Certainly this test has just started failing stochastically on shippable / ARM for 2.7 / 3.7, so it may not have been stable in the first place given how recently this was merged.
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.
This wasn't the problem -- I'm preparing the patch now in the linked PR, which is related to testing precision and np.empty() as far as I can tell.
Add support for pathlib.Path when "mmap_mode" is specified
in np.load "Closes #11342".
Add support for pathlib.Path in np.core.records.fromfile.