diff options
author | Eric Wieser <wieser.eric@gmail.com> | 2018-10-12 07:08:22 -0700 |
---|---|---|
committer | Eric Wieser <wieser.eric@gmail.com> | 2018-10-12 08:39:40 -0700 |
commit | 489362c0779bd60c688ce87baf9ecd6ac9ccf938 (patch) | |
tree | 98656881b4c774d30783b21e62b760c2905decb0 /numpy/core/memmap.py | |
parent | 28679be331b25a3ae72403e42cc6b17cffddc124 (diff) | |
download | numpy-489362c0779bd60c688ce87baf9ecd6ac9ccf938.tar.gz |
ENH: Add support for third-party path-like objects by backporting os.fspath
Diffstat (limited to 'numpy/core/memmap.py')
-rw-r--r-- | numpy/core/memmap.py | 19 |
1 files changed, 8 insertions, 11 deletions
diff --git a/numpy/core/memmap.py b/numpy/core/memmap.py index 8269f537f..f5cc68bb9 100644 --- a/numpy/core/memmap.py +++ b/numpy/core/memmap.py @@ -3,7 +3,7 @@ from __future__ import division, absolute_import, print_function import numpy as np from .numeric import uint8, ndarray, dtype from numpy.compat import ( - long, basestring, is_pathlib_path, contextlib_nullcontext + long, basestring, os_fspath, contextlib_nullcontext, is_pathlib_path ) __all__ = ['memmap'] @@ -218,10 +218,8 @@ class memmap(ndarray): if hasattr(filename, 'read'): f_ctx = contextlib_nullcontext(filename) - elif is_pathlib_path(filename): - f_ctx = filename.open(('r' if mode == 'c' else mode)+'b') else: - f_ctx = open(filename, ('r' if mode == 'c' else mode)+'b') + f_ctx = open(os_fspath(filename), ('r' if mode == 'c' else mode)+'b') with f_ctx as fid: fid.seek(0, 2) @@ -268,14 +266,13 @@ class memmap(ndarray): self.offset = offset self.mode = mode - if isinstance(filename, basestring): - self.filename = os.path.abspath(filename) - elif is_pathlib_path(filename): + if is_pathlib_path(filename): + # special case - if we were constructed with a pathlib.path, + # then filename is a path object, not a string self.filename = filename.resolve() - # py3 returns int for TemporaryFile().name - elif (hasattr(filename, "name") and - isinstance(filename.name, basestring)): - self.filename = os.path.abspath(filename.name) + elif hasattr(fid, "name") and isinstance(fid.name, basestring): + # py3 returns int for TemporaryFile().name + self.filename = os.path.abspath(fid.name) # same as memmap copies (e.g. memmap + 1) else: self.filename = None |