From bd918d874f4eb459b9c2f9af6e8e994b6b19d264 Mon Sep 17 00:00:00 2001
From: Dan Callahan <danc@element.io>
Date: Fri, 14 May 2021 10:58:52 +0100
Subject: [PATCH] Simplify exception handling in is_ascii. (#9985)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

We can get away with just catching UnicodeError here.

    â‹®
    +-- ValueError
    |    +-- UnicodeError
    |         +-- UnicodeDecodeError
    |         +-- UnicodeEncodeError
    |         +-- UnicodeTranslateError
    â‹®

https://docs.python.org/3/library/exceptions.html#exception-hierarchy

Signed-off-by: Dan Callahan <danc@element.io>
---
 changelog.d/9985.misc       | 1 +
 synapse/util/stringutils.py | 4 +---
 2 files changed, 2 insertions(+), 3 deletions(-)
 create mode 100644 changelog.d/9985.misc

diff --git a/changelog.d/9985.misc b/changelog.d/9985.misc
new file mode 100644
index 0000000000..97bd747f26
--- /dev/null
+++ b/changelog.d/9985.misc
@@ -0,0 +1 @@
+Simplify a few helper functions.
diff --git a/synapse/util/stringutils.py b/synapse/util/stringutils.py
index 40cd51a8ca..f029432191 100644
--- a/synapse/util/stringutils.py
+++ b/synapse/util/stringutils.py
@@ -55,9 +55,7 @@ def random_string_with_symbols(length: int) -> str:
 def is_ascii(s: bytes) -> bool:
     try:
         s.decode("ascii").encode("ascii")
-    except UnicodeDecodeError:
-        return False
-    except UnicodeEncodeError:
+    except UnicodeError:
         return False
     return True
 
-- 
GitLab