Skip to content

Commit

Permalink
Fix UnicodeEncodeError exception handling (#160) (#163)
Browse files Browse the repository at this point in the history
* Fix UnicodeEncodeError exception handling (#160)

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.

* Incrementing version string (1.2.1 -> 1.2.2)

* Reformatted code using pyblack

---------

Co-authored-by: Marc Zuo <680830-timippos@users.noreply.gitlab.com>
  • Loading branch information
marczuo and Marc Zuo authored Jun 3, 2024
1 parent 8f5e347 commit 72e56f5
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "dmenu_extended"
version = "1.2.3"
version = "1.2.4"
authors = [
{ name="Mark Hedley Jones", email="markhedleyjones@gmail.com" },
]
Expand Down
9 changes: 6 additions & 3 deletions src/dmenu_extended/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -786,11 +786,14 @@ 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 +805,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 72e56f5

Please sign in to comment.