Skip to content

Commit 9fa73b3

Browse files
committed
words: fix alphabet_sort exception handling for py3.14
As of python/cpython#121395, the format of the list.index exception has changed and parsing it like this no longer works. Relying on implementation details like this is always risky, so this solution should be more future-proof.
1 parent 4cf05de commit 9fa73b3

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

domdf_python_tools/words.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -171,14 +171,13 @@ def alpha_sort(
171171

172172
alphabet_ = list(alphabet)
173173

174-
try:
175-
return sorted(iterable, key=lambda attr: [alphabet_.index(letter) for letter in attr], reverse=reverse)
176-
except ValueError as e:
177-
m = re.match(r"'(.*)' is not in list", str(e))
178-
if m:
179-
raise ValueError(f"The character {m.group(1)!r} was not found in the alphabet.") from None
180-
else: # pragma: no cover
181-
raise e
174+
def _alphabet_index(letter: str) -> int:
175+
try:
176+
return alphabet_.index(letter)
177+
except ValueError:
178+
raise ValueError(f"The character {letter!r} was not found in the alphabet.") from None
179+
180+
return sorted(iterable, key=lambda attr: [_alphabet_index(letter) for letter in attr], reverse=reverse)
182181

183182

184183
class Font(Dict[str, str]):

0 commit comments

Comments
 (0)