diff options
| author | Ross Barnowski <rossbar@berkeley.edu> | 2022-01-11 12:05:00 -0800 |
|---|---|---|
| committer | Sebastian Berg <sebastian@sipsolutions.net> | 2022-01-14 20:07:07 -0600 |
| commit | 3ca9f5a2a252e020a44a355f4fc8114d91ea3423 (patch) | |
| tree | 460d6d61109ce2a54c5f3dfd6c8ac6404a566950 /numpy/lib | |
| parent | 530c954316680f234b6926d0188843bca56de90d (diff) | |
| download | numpy-3ca9f5a2a252e020a44a355f4fc8114d91ea3423.tar.gz | |
Add warning on empty file + tests.
Diffstat (limited to 'numpy/lib')
| -rw-r--r-- | numpy/lib/npyio.py | 13 | ||||
| -rw-r--r-- | numpy/lib/tests/test_io.py | 6 |
2 files changed, 9 insertions, 10 deletions
diff --git a/numpy/lib/npyio.py b/numpy/lib/npyio.py index 3b7b3bb30..6e660dce1 100644 --- a/numpy/lib/npyio.py +++ b/numpy/lib/npyio.py @@ -1062,12 +1062,13 @@ def _read(fname, *, delimiter=',', comment='#', quote='"', arr = _ensure_ndmin_ndarray(arr, ndmin=ndmin) - if arr.shape[0] == 0: - warnings.warn( - f'loadtxt: input contained no data: "{fname}"', - category=UserWarning, - stacklevel=2 - ) + if arr.shape: + if arr.shape[0] == 0: + warnings.warn( + f'loadtxt: input contained no data: "{fname}"', + category=UserWarning, + stacklevel=2 + ) if unpack: # Handle unpack like np.loadtxt. diff --git a/numpy/lib/tests/test_io.py b/numpy/lib/tests/test_io.py index 4a5cc1ad8..b4ca5b74b 100644 --- a/numpy/lib/tests/test_io.py +++ b/numpy/lib/tests/test_io.py @@ -919,8 +919,7 @@ class TestLoadTxt(LoadTxtBase): assert_array_equal(x, a) def test_empty_file(self): - with suppress_warnings() as sup: - sup.filter(message="loadtxt: Empty input file:") + with pytest.warns(UserWarning, match="input contained no data"): c = TextIO() x = np.loadtxt(c) assert_equal(x.shape, (0,)) @@ -1098,8 +1097,7 @@ class TestLoadTxt(LoadTxtBase): assert_(x.shape == (3,)) # Test ndmin kw with empty file. - with suppress_warnings() as sup: - sup.filter(message="loadtxt: Empty input file:") + with pytest.warns(UserWarning, match="input contained no data"): f = TextIO() assert_(np.loadtxt(f, ndmin=2).shape == (0, 1,)) assert_(np.loadtxt(f, ndmin=1).shape == (0,)) |
