Skip to content

Commit

Permalink
Raise FileNotFoundError when a literal JSON string that looks like a …
Browse files Browse the repository at this point in the history
…json filename is passed (#15806)

- closes #13026

Authors:
  - Thomas Li (https://github.com/lithomas1)

Approvers:
  - Vyas Ramasubramani (https://github.com/vyasr)

URL: #15806
  • Loading branch information
lithomas1 authored May 22, 2024
1 parent 766fbb7 commit ad56bc3
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
8 changes: 8 additions & 0 deletions python/cudf/cudf/tests/test_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,14 @@ def test_json_lines_basic(json_input, engine):
np.testing.assert_array_equal(pd_df[pd_col], cu_df[cu_col].to_numpy())


@pytest.mark.filterwarnings("ignore:Using CPU")
@pytest.mark.parametrize("engine", ["auto", "cudf", "pandas"])
def test_nonexistent_json_correct_error(engine):
json_input = "doesnotexist.json"
with pytest.raises(FileNotFoundError):
cudf.read_json(json_input, engine=engine)


@pytest.mark.skipif(
PANDAS_VERSION < PANDAS_CURRENT_SUPPORTED_VERSION,
reason="warning not present in older pandas versions",
Expand Down
24 changes: 23 additions & 1 deletion python/cudf/cudf/utils/ioutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1718,10 +1718,32 @@ def get_reader_filepath_or_buffer(
if _is_local_filesystem(fs):
# Doing this as `read_json` accepts a json string
# path_or_data need not be a filepath like string

# helper for checking if raw text looks like a json filename
compression_extensions = [
".tar",
".tar.gz",
".tar.bz2",
".tar.xz",
".gz",
".bz2",
".zip",
".xz",
".zst",
"",
]

if len(paths):
if fs.exists(paths[0]):
path_or_data = paths if len(paths) > 1 else paths[0]
elif not allow_raw_text_input:

# raise FileNotFound if path looks like json
# following pandas
# see
# https://github.com/pandas-dev/pandas/pull/46718/files#diff-472ce5fe087e67387942e1e1c409a5bc58dde9eb8a2db6877f1a45ae4974f694R724-R729
elif not allow_raw_text_input or paths[0].lower().endswith(
tuple(f".json{c}" for c in compression_extensions)
):
raise FileNotFoundError(
f"{path_or_data} could not be resolved to any files"
)
Expand Down

0 comments on commit ad56bc3

Please sign in to comment.