Skip to content

Commit 5eb7c30

Browse files
committed
builtin cover: fix handling of invalid glob ranges with Python 3.10.5+
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
1 parent f2c2f5e commit 5eb7c30

2 files changed

Lines changed: 14 additions & 20 deletions

File tree

‎quodlibet/util/cover/built_in.py‎

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -100,24 +100,24 @@ def cover(self):
100100
base = self.song('~dirname')
101101
images = []
102102

103+
def safe_glob(*args, **kwargs):
104+
try:
105+
return glob.glob(*args, **kwargs)
106+
except sre_constants.error:
107+
# https://github.com/python/cpython/issues/89973
108+
# old glob would fail with invalid ranges, newer one
109+
# handles it correctly.
110+
return []
111+
103112
if config.getboolean("albumart", "force_filename"):
104113
score = 100
105114
for filename in config.get("albumart", "filename").split(","):
106115
# Remove white space to avoid confusion (e.g. "name, name2")
107116
filename = filename.strip()
108117

109118
escaped_path = os.path.join(glob.escape(base), filename)
110-
try:
111-
for path in glob.glob(escaped_path):
112-
images.append((score, path))
113-
except sre_constants.error:
114-
# Use literal filename if globbing causes errors
115-
path = os.path.join(base, filename)
116-
117-
# We check this here, so we can search for alternative
118-
# files in case no preferred file was found.
119-
if os.path.isfile(path):
120-
images.append((score, path))
119+
for path in safe_glob(escaped_path):
120+
images.append((score, path))
121121

122122
# So names and patterns at the start are preferred
123123
score -= 1

‎tests/test_util_cover.py‎

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -105,15 +105,9 @@ def test_invalid_glob(self):
105105
config.set("albumart", "force_filename", str(True))
106106
config.set("albumart", "filename", "[a-2].jpg")
107107

108-
# Should match
109-
f = self.add_file("[a-2].jpg")
110-
assert path_equal(
111-
os.path.abspath(self._find_cover(self.song).name), f)
112-
113-
# Should not crash
114-
f = self.add_file("test.jpg")
115-
assert not path_equal(
116-
os.path.abspath(self._find_cover(self.song).name), f)
108+
# Invalid glob range, should not match anything
109+
self.add_file("a.jpg")
110+
assert self._find_cover(self.song) is None
117111

118112
def test_invalid_glob_path(self):
119113
config.set("albumart", "force_filename", str(True))

0 commit comments

Comments
 (0)