summaryrefslogtreecommitdiff
path: root/numpy/lib
diff options
context:
space:
mode:
authorCharles Harris <charlesr.harris@gmail.com>2021-08-16 15:16:03 -0600
committerGitHub <noreply@github.com>2021-08-16 15:16:03 -0600
commitcde69925a74cafa5c7d3dc3c820f75835c514fb2 (patch)
treeb231291406fd87abc2a0a36d12e09f2ebe57e214 /numpy/lib
parent9e06c2cc1e83b0ca9869bd1bb0ff5dceb993d12d (diff)
parentce1188384ad6b2b0e3f2e673e89ff2b84262e038 (diff)
downloadnumpy-cde69925a74cafa5c7d3dc3c820f75835c514fb2.tar.gz
Merge pull request #19680 from BvB93/fromregex
ENH: Allow `np.fromregex` to accept `os.PathLike` implementations
Diffstat (limited to 'numpy/lib')
-rw-r--r--numpy/lib/npyio.py6
-rw-r--r--numpy/lib/npyio.pyi4
-rw-r--r--numpy/lib/tests/test_io.py6
3 files changed, 11 insertions, 5 deletions
diff --git a/numpy/lib/npyio.py b/numpy/lib/npyio.py
index a593af65e..7a594f25b 100644
--- a/numpy/lib/npyio.py
+++ b/numpy/lib/npyio.py
@@ -1484,8 +1484,11 @@ def fromregex(file, regexp, dtype, encoding=None):
Parameters
----------
- file : str or file
+ file : path or file
Filename or file object to read.
+
+ .. versionchanged:: 1.22.0
+ Now accepts `os.PathLike` implementations.
regexp : str or regexp
Regular expression used to parse the file.
Groups in the regular expression correspond to fields in the dtype.
@@ -1535,6 +1538,7 @@ def fromregex(file, regexp, dtype, encoding=None):
"""
own_fh = False
if not hasattr(file, "read"):
+ file = os.fspath(file)
file = np.lib._datasource.open(file, 'rt', encoding=encoding)
own_fh = True
diff --git a/numpy/lib/npyio.pyi b/numpy/lib/npyio.pyi
index 264ceef14..de6bc3ded 100644
--- a/numpy/lib/npyio.pyi
+++ b/numpy/lib/npyio.pyi
@@ -182,14 +182,14 @@ def savetxt(
@overload
def fromregex(
- file: str | IO[Any],
+ file: str | os.PathLike[str] | IO[Any],
regexp: str | bytes | Pattern[Any],
dtype: _DTypeLike[_SCT],
encoding: None | str = ...
) -> NDArray[_SCT]: ...
@overload
def fromregex(
- file: str | IO[Any],
+ file: str | os.PathLike[str] | IO[Any],
regexp: str | bytes | Pattern[Any],
dtype: DTypeLike,
encoding: None | str = ...
diff --git a/numpy/lib/tests/test_io.py b/numpy/lib/tests/test_io.py
index 02a9789a7..11f2b7d4d 100644
--- a/numpy/lib/tests/test_io.py
+++ b/numpy/lib/tests/test_io.py
@@ -1229,9 +1229,11 @@ class Testfromregex:
a = np.array([(1312,), (1534,), (4444,)], dtype=dt)
assert_array_equal(x, a)
- def test_record_unicode(self):
+ @pytest.mark.parametrize("path_type", [str, Path])
+ def test_record_unicode(self, path_type):
utf8 = b'\xcf\x96'
- with temppath() as path:
+ with temppath() as str_path:
+ path = path_type(str_path)
with open(path, 'wb') as f:
f.write(b'1.312 foo' + utf8 + b' \n1.534 bar\n4.444 qux')