[Python-checkins] cpython: Optimize ascii(str): don't encode/decode repr if repr is already ASCII

victor.stinner python-checkins at python.org
Sun Apr 14 19:31:42 CEST 2013


http://hg.python.org/cpython/rev/ff89957e3204
changeset:   83383:ff89957e3204
user:        Victor Stinner <victor.stinner at gmail.com>
date:        Sun Apr 14 18:44:10 2013 +0200
summary:
  Optimize ascii(str): don't encode/decode repr if repr is already ASCII

files:
  Objects/object.c        |  3 +++
  Objects/unicodeobject.c |  2 +-
  2 files changed, 4 insertions(+), 1 deletions(-)


diff --git a/Objects/object.c b/Objects/object.c
--- a/Objects/object.c
+++ b/Objects/object.c
@@ -451,6 +451,9 @@
     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);
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -6499,7 +6499,7 @@
         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);

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list