-
-
Notifications
You must be signed in to change notification settings - Fork 10.9k
ENH: pathlib support for fromfile(), .tofile() and .dump() #12915
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
Changes from all commits
4a94ee4
eea6b8c
3c060d5
703c59c
998daea
0933c15
9a968f3
6c8f7fb
3c618a3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,32 @@ | ||
#ifndef _NPY_ARRAY_METHODS_H_ | ||
#define _NPY_ARRAY_METHODS_H_ | ||
|
||
#include "npy_import.h" | ||
|
||
extern NPY_NO_EXPORT PyMethodDef array_methods[]; | ||
|
||
NPY_NO_EXPORT const char * | ||
npy_casting_to_string(NPY_CASTING casting); | ||
|
||
/* Pathlib support */ | ||
static inline PyObject * | ||
NpyPath_PathlikeToFspath(PyObject *file) | ||
{ | ||
static PyObject *os_PathLike = NULL; | ||
static PyObject *os_fspath = NULL; | ||
npy_cache_import("numpy.compat", "os_PathLike", &os_PathLike); | ||
if (os_PathLike == NULL) { | ||
return NULL; | ||
} | ||
npy_cache_import("numpy.compat", "os_fspath", &os_fspath); | ||
sorenrasmussenai marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (os_fspath == NULL) { | ||
return NULL; | ||
} | ||
|
||
if (!PyObject_IsInstance(file, os_PathLike)) { | ||
return file; | ||
} | ||
return PyObject_CallFunctionObjArgs(os_fspath, file, NULL); | ||
} | ||
|
||
#endif |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,14 @@ | |
|
||
from numpy.compat import pickle | ||
|
||
try: | ||
import pathlib | ||
except ImportError: | ||
try: | ||
import pathlib2 as pathlib | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not actually sure our stuff works on pathlib2, or whether our CI exercises this code path. Either way, if it turns out this test fails on systems using pathlib2, at least with the test present we'll likely get a bug report. |
||
except ImportError: | ||
pathlib = None | ||
|
||
if sys.version_info[0] >= 3: | ||
import builtins | ||
else: | ||
|
@@ -4639,6 +4647,20 @@ def test_roundtrip_filename(self): | |
y = np.fromfile(self.filename, dtype=self.dtype) | ||
assert_array_equal(y, self.x.flat) | ||
|
||
@pytest.mark.skipif(pathlib is None, reason="pathlib not found") | ||
def test_roundtrip_pathlib(self): | ||
p = pathlib.Path(self.filename) | ||
self.x.tofile(p) | ||
y = np.fromfile(p, dtype=self.dtype) | ||
assert_array_equal(y, self.x.flat) | ||
|
||
@pytest.mark.skipif(pathlib is None, reason="pathlib not found") | ||
def test_roundtrip_dump_pathlib(self): | ||
p = pathlib.Path(self.filename) | ||
self.x.dump(p) | ||
y = np.load(p, allow_pickle=True) | ||
assert_array_equal(y, self.x) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch, #13684. |
||
def test_roundtrip_binary_str(self): | ||
s = self.x.tobytes() | ||
y = np.frombuffer(s, dtype=self.dtype) | ||
|
Uh oh!
There was an error while loading. Please reload this page.