diff options
| -rw-r--r-- | numpy/lib/npyio.py | 5 | ||||
| -rw-r--r-- | numpy/lib/tests/test_loadtxt.py | 11 |
2 files changed, 16 insertions, 0 deletions
diff --git a/numpy/lib/npyio.py b/numpy/lib/npyio.py index be313d104..63fffffbc 100644 --- a/numpy/lib/npyio.py +++ b/numpy/lib/npyio.py @@ -922,6 +922,11 @@ def _read(fname, *, delimiter=',', comment='#', quote='"', comments = None else: # assume comments are a sequence of strings + if "" in comment: + raise ValueError( + "comments cannot be an empty string. Use comments=None to " + "disable comments." + ) comments = tuple(comment) comment = None if len(comments) == 0: diff --git a/numpy/lib/tests/test_loadtxt.py b/numpy/lib/tests/test_loadtxt.py index 5a34267ed..b1e867157 100644 --- a/numpy/lib/tests/test_loadtxt.py +++ b/numpy/lib/tests/test_loadtxt.py @@ -958,3 +958,14 @@ def test_datetime_parametric_unit_discovery( a = np.loadtxt(fname, dtype=unitless_dtype) assert a.dtype == expected.dtype assert_equal(a, expected) + + +def test_control_character_empty(): + with pytest.raises(TypeError, match="Text reading control character must"): + np.loadtxt(StringIO("1 2 3"), delimiter="") + with pytest.raises(TypeError, match="Text reading control character must"): + np.loadtxt(StringIO("1 2 3"), quotechar="") + with pytest.raises(ValueError, match="comments cannot be an empty string"): + np.loadtxt(StringIO("1 2 3"), comments="") + with pytest.raises(ValueError, match="comments cannot be an empty string"): + np.loadtxt(StringIO("1 2 3"), comments=["#", ""]) |
