summary refs log tree commit diff
path: root/synapse/util/stringutils.py
diff options
context:
space:
mode:
authorDavid Baker <dave@matrix.org>2017-04-26 11:31:55 +0100
committerDavid Baker <dave@matrix.org>2017-04-26 11:31:55 +0100
commit81804909d3a7c20330b2992cf1fed0672dd8f531 (patch)
treea6849618c8e3d2e6eb8f7136c8055c5c4b3e10fb /synapse/util/stringutils.py
parentFix get_json (diff)
parentdocument how to make IPv6 work (#2088) (diff)
downloadsynapse-81804909d3a7c20330b2992cf1fed0672dd8f531.tar.xz
Merge remote-tracking branch 'origin/develop' into dbkr/http_request_propagate_error
Diffstat (limited to 'synapse/util/stringutils.py')
-rw-r--r--synapse/util/stringutils.py14
1 files changed, 14 insertions, 0 deletions
diff --git a/synapse/util/stringutils.py b/synapse/util/stringutils.py
index a100f151d4..95a6168e16 100644
--- a/synapse/util/stringutils.py
+++ b/synapse/util/stringutils.py
@@ -40,3 +40,17 @@ def is_ascii(s):
         return False
     else:
         return True
+
+
+def to_ascii(s):
+    """Converts a string to ascii if it is ascii, otherwise leave it alone.
+
+    If given None then will return None.
+    """
+    if s is None:
+        return None
+
+    try:
+        return s.encode("ascii")
+    except UnicodeEncodeError:
+        return s