Skip to content

Commit

Permalink
Fix UnicodeEncodeError exception handling (#160)
Browse files Browse the repository at this point in the history
When build_cache() encounters a UnicodeEncodeError, the original code
did not escape unprintable chars and caused the cache build to fail.
This patch fixes the code so that the culprit is correctly printed in
debug mode.
  • Loading branch information
Marc Zuo committed May 7, 2024
1 parent a5577d2 commit 2ce1752
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/dmenu_extended/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -786,11 +786,11 @@ def cache_save(self, items, path):
for item in items:
clean = True
for char in item:
if char not in string.printable:
if not str.isprintable(char):
clean = False
foundError = True
if self.debug:
print("Culprit: " + item)
print("Culprit: " + item.encode('unicode_escape').decode("utf-8"))
if clean:
tmp.append(item)
if foundError:
Expand All @@ -802,7 +802,7 @@ def cache_save(self, items, path):
print("Offending items have been excluded from cache")
with open(path, "wb") as f:
for item in tmp:
f.write(item + "\n")
f.write(item.encode('unicode_escape') + b"\n")
return 2
else:
if self.debug:
Expand Down

0 comments on commit 2ce1752

Please sign in to comment.