diff options
author | Sebastian Berg <sebastian@sipsolutions.net> | 2022-01-13 20:06:16 -0600 |
---|---|---|
committer | Sebastian Berg <sebastian@sipsolutions.net> | 2022-01-14 20:07:07 -0600 |
commit | 73940d6b08a5b3690799bfdd68bb514ee1445b16 (patch) | |
tree | 467a2dd04053d531f731c7f43139f5aa21c40bf4 /numpy/lib/tests/test_io.py | |
parent | 08fa5ce7b6fb4d4a01923931f88d78592fc69165 (diff) | |
download | numpy-73940d6b08a5b3690799bfdd68bb514ee1445b16.tar.gz |
MAINT,TST,BUG: Simplify streamer init, fix issues, and add tests
Diffstat (limited to 'numpy/lib/tests/test_io.py')
-rw-r--r-- | numpy/lib/tests/test_io.py | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/numpy/lib/tests/test_io.py b/numpy/lib/tests/test_io.py index fd27fd671..7ec44045f 100644 --- a/numpy/lib/tests/test_io.py +++ b/numpy/lib/tests/test_io.py @@ -3347,3 +3347,48 @@ def test_loadtxt_unicode_whitespace_stripping_complex(dtype): def test_loadtxt_bad_complex(dtype, field): with pytest.raises(ValueError): np.loadtxt([field + "\n"], dtype=dtype, delimiter=",") + + +def test_loadtxt_iterator_fails_getting_next_line(): + class BadSequence: + def __len__(self): + return 100 + + def __getitem__(self, item): + if item == 50: + raise RuntimeError("Bad things happened!") + return f"{item}, {item+1}" + + with pytest.raises(RuntimeError, match="Bad things happened!"): + np.loadtxt(BadSequence(), dtype=int, delimiter=",") + + +class TestCReaderUnitTests: + # These are internal tests for path that should not be possible to hit + # unless things go very very wrong somewhere. + def test_not_an_filelike(self): + with pytest.raises(AttributeError, match=".*read"): + np.core._multiarray_umath._load_from_filelike( + object(), dtype=np.dtype("i"), filelike=True) + + def test_filelike_read_fails(self): + # Can only be reached if loadtxt opens the file, so it is hard to do + # via the public interface (although maybe not impossible considering + # the current "DataClass" backing). + class BadFileLike: + counter = 0 + def read(self, size): + self.counter += 1 + if self.counter > 20: + raise RuntimeError("Bad bad bad!") + return "1,2,3\n" + + with pytest.raises(RuntimeError, match="Bad bad bad!"): + np.core._multiarray_umath._load_from_filelike( + BadFileLike(), dtype=np.dtype("i"), filelike=True) + + def test_not_an_iter(self): + with pytest.raises(TypeError, + match="error reading from object, expected an iterable"): + np.core._multiarray_umath._load_from_filelike( + object(), dtype=np.dtype("i"), filelike=False) |