]> granicus.if.org Git - python/commitdiff
Optimize ascii(str): don't encode/decode repr if repr is already ASCII
authorVictor Stinner <victor.stinner@gmail.com>
Sun, 14 Apr 2013 16:44:10 +0000 (18:44 +0200)
committerVictor Stinner <victor.stinner@gmail.com>
Sun, 14 Apr 2013 16:44:10 +0000 (18:44 +0200)
Objects/object.c
Objects/unicodeobject.c

index fd1fd256ba75b49920b35259c83253c34ddbae6a..79f1c8a8356afaec0f46bf3da41ce967d0a34f70 100644 (file)
@@ -451,6 +451,9 @@ PyObject_ASCII(PyObject *v)
     if (repr == NULL)
         return NULL;
 
+    if (PyUnicode_IS_ASCII(repr))
+        return repr;
+
     /* repr is guaranteed to be a PyUnicode object by PyObject_Repr */
     ascii = _PyUnicode_AsASCIIString(repr, "backslashreplace");
     Py_DECREF(repr);
index 99628cad726e25546a13a1235a2924195d03b315..0996afbbb8f57cf2b05b485f6db7e8b1c038bd59 100644 (file)
@@ -6499,7 +6499,7 @@ _PyUnicode_AsASCIIString(PyObject *unicode, const char *errors)
         return NULL;
     /* Fast path: if it is an ASCII-only string, construct bytes object
        directly. Else defer to above function to raise the exception. */
-    if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128)
+    if (PyUnicode_IS_ASCII(unicode))
         return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode),
                                          PyUnicode_GET_LENGTH(unicode));
     return unicode_encode_ucs1(unicode, errors, 128);