Skip to content
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

Fix: add utility to check whether a string is a filepath #1085

Merged
merged 8 commits into from
Sep 27, 2021
Merged
8 changes: 8 additions & 0 deletions src/awkward/_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import re
import sys
import os
import os.path
import warnings
import itertools
import numbers
Expand Down Expand Up @@ -41,6 +42,13 @@
kMaxLevels = 48


def is_file_path(x):
try:
return os.path.isfile(x)
except ValueError:
return False


def isint(x):
"""
Returns True if and only if ``x`` is an integer (including NumPy, not
Expand Down
2 changes: 1 addition & 1 deletion src/awkward/operations/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -1075,7 +1075,7 @@ def from_json(

is_path, source = ak._util.regularize_path(source)

if os.path.isfile(source):
if ak._util.is_file_path(source):
layout = ak._ext.fromjsonfile(
source,
nan_string=nan_string,
Expand Down
23 changes: 23 additions & 0 deletions tests/test_1084-from-json-win-path.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# BSD 3-Clause License; see https://github.com/scikit-hep/awkward-1.0/blob/main/LICENSE
from __future__ import absolute_import

import pytest # noqa: F401
import numpy as np # noqa: F401
import awkward as ak # noqa: F401


@pytest.mark.skipif(not ak._util.win, reason="need Windows to test this case")
def test():
if not ak._util.py27:
# Python 2.7 has differently named urllib libraries
import urllib.request
import urllib.error

url = "https://raw.githubusercontent.com/Chicago/osd-bike-routes/5f556dc/data/Bikeroutes.geojson"
try:
bikeroutes_json = urllib.request.urlopen(url).read()
except urllib.error.URLError:
pytest.skip(msg="couldn't download sample dataset")

# This shouldn't fail (see #1084)
ak.from_json(bikeroutes_json)