Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions fsspec/tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import io
import os.path
import random
import sys
import time
Expand All @@ -13,6 +14,7 @@
common_prefix,
get_file_extension,
get_protocol,
glob_translate,
infer_storage_options,
merge_offset_ranges,
mirror_from,
Expand Down Expand Up @@ -664,3 +666,42 @@ def test_stringify_path(path, expected):
path = fsspec.utils.stringify_path(path)

assert path == expected


@pytest.mark.parametrize(
"sep,altsep",
[
("/", None), # posix
("\\", "/"), # windows
],
)
def test_glob_translate_does_not_depend_on_the_host_os(monkeypatch, sep, altsep):
# fsspec paths always use "/", so the pattern a given glob compiles to has to be
# the same everywhere. Deriving the separators from os.path made a backslash a
# separator on Windows only.
monkeypatch.setattr(os.path, "sep", sep)
monkeypatch.setattr(os.path, "altsep", altsep)

assert glob_translate("*") == r"(?s:[^/]+)\Z"
assert glob_translate("a/b*") == r"(?s:a/b[^/]*)\Z"
# a backslash is an ordinary character in a key, not a separator
assert glob_translate("we\\ird.txt") == r"(?s:we\\ird\.txt)\Z"


def test_glob_matches_a_name_containing_a_backslash():
# A backslash is a legal character in an object store key. It must not be treated
# as a path separator, on any platform.
fs = fsspec.filesystem("memory")
for name in ["/data/plain.txt", "/data/we\\ird.txt"]:
with fs.open(name, "wb") as f:
f.write(b"x")

try:
assert sorted(fs.glob("/data/*")) == ["/data/plain.txt", "/data/we\\ird.txt"]
assert fs.glob("/data/we*") == ["/data/we\\ird.txt"]
assert sorted(fs.glob("/data/*.txt")) == [
"/data/plain.txt",
"/data/we\\ird.txt",
]
finally:
fs.store.clear()
12 changes: 7 additions & 5 deletions fsspec/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -753,12 +753,14 @@ def _translate(pat, STAR, QUESTION_MARK):
def glob_translate(pat):
# Copied from: https://github.com/python/cpython/pull/106703.
# The keyword parameters' values are fixed to:
# recursive=True, include_hidden=True, seps=None
# recursive=True, include_hidden=True, seps="/"
"""Translate a pathname with shell wildcards to a regular expression."""
if os.path.altsep:
seps = os.path.sep + os.path.altsep
else:
seps = os.path.sep
# fsspec paths always use "/" as their separator (AbstractFileSystem.sep), on every
# platform, and glob() has already put the pattern through _strip_protocol by the
# time it reaches here. Taking the separators from os.path instead would make a
# backslash a separator on Windows only, so a key containing one, which is an
# ordinary character on an object store, would stop matching there.
seps = "/"
escaped_seps = "".join(map(re.escape, seps))
any_sep = f"[{escaped_seps}]" if len(seps) > 1 else escaped_seps
not_sep = f"[^{escaped_seps}]"
Expand Down
Loading