From 7f17ee1b35928ce16dcba66aac4cdec7b1f539cd Mon Sep 17 00:00:00 2001 From: Ben Darnell Date: Sat, 14 May 2011 16:58:23 -0700 Subject: [PATCH] Slightly faster utf8 function --- tornado/escape.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/tornado/escape.py b/tornado/escape.py index ee4ffb68..42c5ad76 100644 --- a/tornado/escape.py +++ b/tornado/escape.py @@ -92,13 +92,12 @@ def url_unescape(value): return _unicode(urllib.unquote_plus(value)) +_UTF8_TYPES = (bytes, type(None)) def utf8(value): - if value is None: - return None - if isinstance(value, unicode): - return value.encode("utf-8") - assert isinstance(value, bytes) - return value + if isinstance(value, _UTF8_TYPES): + return value + assert isinstance(value, unicode) + return value.encode("utf-8") # I originally used the regex from