Skip to content

Fix issue #36271 to disambiguate json string #36273

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 10 commits into from
16 changes: 16 additions & 0 deletions pandas/io/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@

from pandas.core.dtypes.common import is_file_like

from pandas._libs.json import loads

lzma = import_lzma()


Expand Down Expand Up @@ -153,6 +155,19 @@ def urlopen(*args, **kwargs):
return urllib.request.urlopen(*args, **kwargs)


def is_json(url: FilePathOrBuffer) -> bool:
"""
Returns true if the given string looks like
something json.loads can handle
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we cannot do this generally as its completely non-performant

"""
try:
loads(url)

return True
except:
return False
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can be simplified return json_pattern.match(url) is not None

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, I committed your suggestion



def is_fsspec_url(url: FilePathOrBuffer) -> bool:
"""
Returns true if the given URL looks like
Expand All @@ -161,6 +176,7 @@ def is_fsspec_url(url: FilePathOrBuffer) -> bool:
return (
isinstance(url, str)
and "://" in url
and not is_json(url)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For pandas maintainers: should this check only be done within read_json (which will presumably call json.loads anyway, repeating the work) ? Are there any other similar cases that the string passed might be handled as a path, or decoded directly, that I might have missed?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

read_json doesn't call json.loads

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tbachlechner if you really think t his out to be caught then i suppose a 'better' check instead of and "://" in url could be done (e.g. a regex)

and not url.startswith(("http://", "https://"))
)

Expand Down
2 changes: 2 additions & 0 deletions pandas/tests/io/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,3 +417,5 @@ def test_is_fsspec_url():
assert not icom.is_fsspec_url("random:pandas/somethingelse.com")
assert not icom.is_fsspec_url("/local/path")
assert not icom.is_fsspec_url("relative/local/path")
# Ensure json string is not interpreted as URL
assert not icom.is_fsspec_url('{"json": "text ://"}')