From 72e56f5fba73bdce0497e98de873f68f03351cf8 Mon Sep 17 00:00:00 2001 From: Marc Zuo Date: Mon, 3 Jun 2024 00:48:22 -0400 Subject: [PATCH] Fix UnicodeEncodeError exception handling (#160) (#163) * 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> --- pyproject.toml | 2 +- src/dmenu_extended/main.py | 9 ++++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index b62cead..98d7ec6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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" }, ] diff --git a/src/dmenu_extended/main.py b/src/dmenu_extended/main.py index 96374f4..45b53a4 100755 --- a/src/dmenu_extended/main.py +++ b/src/dmenu_extended/main.py @@ -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: @@ -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: