Skip to content

Commit

Permalink
BUG: use TypeError (not OSError) when read_csv expects file path name…
Browse files Browse the repository at this point in the history
… or file-like object
  • Loading branch information
mwtoews committed Sep 7, 2021
1 parent 1ab776e commit 4f019ad
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 4 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.4.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@ I/O
- Column headers are dropped when constructing a :class:`DataFrame` from a sqlalchemy's ``Row`` object (:issue:`40682`)
- Bug in unpickling a :class:`Index` with object dtype incorrectly inferring numeric dtypes (:issue:`43188`)
- Bug in :func:`read_csv` where reading multi-header input with unequal lengths incorrectly raising uncontrolled ``IndexError`` (:issue:`43102`)
- Bug in :func:`read_csv`, changed exception class when expecting a file path name or file-like object from ``OSError`` to ``TypeError`` (:issue:`43366`)

Period
^^^^^^
Expand Down
4 changes: 0 additions & 4 deletions pandas/_libs/parsers.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -606,10 +606,6 @@ cdef class TextReader:
cdef:
void *ptr

if not hasattr(source, "read"):
raise IOError(f'Expected file path name or file-like object, '
f'got {type(source)} type')

ptr = new_rd_source(source)
self.parser.source = ptr
self.parser.cb_io = &buffer_rd_bytes
Expand Down
4 changes: 4 additions & 0 deletions pandas/io/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,10 @@ def get_handle(
isinstance(ioargs.filepath_or_buffer, str) or ioargs.should_close
)

if "r" in ioargs.mode and not hasattr(handle, "read"):
raise TypeError('Expected file path name or file-like object, '
f'got {type(ioargs.filepath_or_buffer)} type')

handles.reverse() # close the most recently added buffer first
if ioargs.should_close:
assert not isinstance(ioargs.filepath_or_buffer, str)
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/io/parser/common/test_common_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,14 @@ def test_raise_on_sep_with_delim_whitespace(all_parsers):
parser.read_csv(StringIO(data), sep=r"\s", delim_whitespace=True)


def test_read_filepath_or_buffer(all_parsers):
# see gh-43366
parser = all_parsers

with pytest.raises(TypeError, match="Expected file path name or file-like"):
parser.read_csv(filepath_or_buffer=b'input')


@xfail_pyarrow
@pytest.mark.parametrize("delim_whitespace", [True, False])
def test_single_char_leading_whitespace(all_parsers, delim_whitespace):
Expand Down

0 comments on commit 4f019ad

Please sign in to comment.