Open
Description
Describe the issue:
If genfromtxt
is given an encoding
with a bad type (e.g. encoding=int
, which is nonsense, of course), the error message in the "outer" exception is wrong:
In [25]: np.genfromtxt('data.csv', encoding=int)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
~/mc39numpy/lib/python3.9/site-packages/numpy/lib/npyio.py in genfromtxt(fname, dtype, comments, delimiter, skip_header, skip_footer, converters, missing_values, filling_values, usecols, names, excludelist, deletechars, replace_space, autostrip, case_sensitive, defaultfmt, unpack, usemask, loose, invalid_raise, max_rows, encoding, like)
1812 if isinstance(fname, str):
-> 1813 fid = np.lib._datasource.open(fname, 'rt', encoding=encoding)
1814 fid_ctx = contextlib.closing(fid)
~/mc39numpy/lib/python3.9/site-packages/numpy/lib/_datasource.py in open(path, mode, destpath, encoding, newline)
192 ds = DataSource(destpath)
--> 193 return ds.open(path, mode, encoding=encoding, newline=newline)
194
~/mc39numpy/lib/python3.9/site-packages/numpy/lib/_datasource.py in open(self, path, mode, encoding, newline)
528 mode.replace("+", "")
--> 529 return _file_openers[ext](found, mode=mode,
530 encoding=encoding, newline=newline)
TypeError: open() argument 'encoding' must be str or None, not type
The above exception was the direct cause of the following exception:
TypeError Traceback (most recent call last)
<ipython-input-25-b2096b5fdc92> in <module>
----> 1 np.genfromtxt('data.csv', encoding=int)
~/mc39numpy/lib/python3.9/site-packages/numpy/lib/npyio.py in genfromtxt(fname, dtype, comments, delimiter, skip_header, skip_footer, converters, missing_values, filling_values, usecols, names, excludelist, deletechars, replace_space, autostrip, case_sensitive, defaultfmt, unpack, usemask, loose, invalid_raise, max_rows, encoding, like)
1818 fhd = iter(fid)
1819 except TypeError as e:
-> 1820 raise TypeError(
1821 f"fname must be a string, filehandle, list of strings,\n"
1822 f"or generator. Got {type(fname)} instead."
TypeError: fname must be a string, filehandle, list of strings,
or generator. Got <class 'str'> instead.
The problem is that the try/except
statement at
Lines 1809 to 1823 in 79b381f
doesn't take into account that a
TypeError
can be raised for reasons other than a bad filename.
Reproduce the code example:
See above.
Error message:
See above.
NumPy/Python version information:
1.22.0.dev0+1698.g79b381f36 3.9.7 (default, Sep 16 2021, 13:09:58)