Skip to content

Commit

Permalink
builtin cover: fix handling of invalid glob ranges with Python 3.10.5+
Browse files Browse the repository at this point in the history
Previously Python would raise if an invalid range was given
to glob, but with 3.10.5 they fixed it to not match anything.
python/cpython#89973

Our tests depended on the previous logic and treating the glob pattern
as a literal file name in that case.

One could argue that this is wrong since a range that doesn't contain anything
should also not match anything, so wrap glob() to make it not match for all
Python versions in that case and adjust the tests accordingly.

This should fix the Windows CI, which is currently the only job using 3.10.5
  • Loading branch information
lazka committed Jun 26, 2022
1 parent f2c2f5e commit 5eb7c30
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 20 deletions.
22 changes: 11 additions & 11 deletions quodlibet/util/cover/built_in.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,24 +100,24 @@ def cover(self):
base = self.song('~dirname')
images = []

def safe_glob(*args, **kwargs):
try:
return glob.glob(*args, **kwargs)
except sre_constants.error:
# https://github.com/python/cpython/issues/89973
# old glob would fail with invalid ranges, newer one
# handles it correctly.
return []

if config.getboolean("albumart", "force_filename"):
score = 100
for filename in config.get("albumart", "filename").split(","):
# Remove white space to avoid confusion (e.g. "name, name2")
filename = filename.strip()

escaped_path = os.path.join(glob.escape(base), filename)
try:
for path in glob.glob(escaped_path):
images.append((score, path))
except sre_constants.error:
# Use literal filename if globbing causes errors
path = os.path.join(base, filename)

# We check this here, so we can search for alternative
# files in case no preferred file was found.
if os.path.isfile(path):
images.append((score, path))
for path in safe_glob(escaped_path):
images.append((score, path))

# So names and patterns at the start are preferred
score -= 1
Expand Down
12 changes: 3 additions & 9 deletions tests/test_util_cover.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,9 @@ def test_invalid_glob(self):
config.set("albumart", "force_filename", str(True))
config.set("albumart", "filename", "[a-2].jpg")

# Should match
f = self.add_file("[a-2].jpg")
assert path_equal(
os.path.abspath(self._find_cover(self.song).name), f)

# Should not crash
f = self.add_file("test.jpg")
assert not path_equal(
os.path.abspath(self._find_cover(self.song).name), f)
# Invalid glob range, should not match anything
self.add_file("a.jpg")
assert self._find_cover(self.song) is None

def test_invalid_glob_path(self):
config.set("albumart", "force_filename", str(True))
Expand Down

0 comments on commit 5eb7c30

Please sign in to comment.