Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #21052 -- Small performance optimization.

  • Loading branch information...
commit b80be68e088f56f5b2ab5371b92a478a14937e8b 1 parent e492ab8
Aymeric Augustin authored September 06, 2013

Showing 1 changed file with 4 additions and 4 deletions. Show diff stats Hide diff stats

  1. 8  django/utils/encoding.py
8  django/utils/encoding.py
@@ -60,8 +60,7 @@ def force_text(s, encoding='utf-8', strings_only=False, errors='strict'):
60 60
 
61 61
     If strings_only is True, don't convert (some) non-string-like objects.
62 62
     """
63  
-    # Handle the common case first, saves 30-40% when s is an instance of
64  
-    # six.text_type. This function gets called often in that setting.
  63
+    # Handle the common case first for performance reasons.
65 64
     if isinstance(s, six.text_type):
66 65
         return s
67 66
     if strings_only and is_protected_type(s):
@@ -115,8 +114,7 @@ def force_bytes(s, encoding='utf-8', strings_only=False, errors='strict'):
115 114
 
116 115
     If strings_only is True, don't convert (some) non-string-like objects.
117 116
     """
118  
-    if isinstance(s, six.memoryview):
119  
-        s = bytes(s)
  117
+    # Handle the common case first for performance reasons.
120 118
     if isinstance(s, bytes):
121 119
         if encoding == 'utf-8':
122 120
             return s
@@ -124,6 +122,8 @@ def force_bytes(s, encoding='utf-8', strings_only=False, errors='strict'):
124 122
             return s.decode('utf-8', errors).encode(encoding, errors)
125 123
     if strings_only and is_protected_type(s):
126 124
         return s
  125
+    if isinstance(s, six.memoryview):
  126
+        return bytes(s)
127 127
     if isinstance(s, Promise):
128 128
         return six.text_type(s).encode(encoding, errors)
129 129
     if not isinstance(s, six.string_types):

0 notes on commit b80be68

Please sign in to comment.
Something went wrong with that request. Please try again.