Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

BUG: lib: Incorrect error raised when genfromtxt is given an encoding with the wrong type. #20329

Copy link
Copy link
Open
@WarrenWeckesser

Description

@WarrenWeckesser
Issue body actions

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

numpy/numpy/lib/npyio.py

Lines 1809 to 1823 in 79b381f

try:
if isinstance(fname, os_PathLike):
fname = os_fspath(fname)
if isinstance(fname, str):
fid = np.lib._datasource.open(fname, 'rt', encoding=encoding)
fid_ctx = contextlib.closing(fid)
else:
fid = fname
fid_ctx = contextlib.nullcontext(fid)
fhd = iter(fid)
except TypeError as e:
raise TypeError(
f"fname must be a string, filehandle, list of strings,\n"
f"or generator. Got {type(fname)} instead."
) from e

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)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.