Simplify exception handling in is_ascii. (#9985)

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>
code_spécifique_watcha
Dan Callahan 4 years ago committed by GitHub
parent 498084228b
commit bd918d874f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      changelog.d/9985.misc
  2. 4
      synapse/util/stringutils.py

@ -0,0 +1 @@
Simplify a few helper functions.

@ -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

Loading…
Cancel
Save