From: Victor Stinner Date: Sun, 14 Apr 2013 16:44:10 +0000 (+0200) Subject: Optimize ascii(str): don't encode/decode repr if repr is already ASCII X-Git-Tag: v3.4.0a1~920 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=af03757d20a13b4090d06e0a198122be194aa6b0;p=python Optimize ascii(str): don't encode/decode repr if repr is already ASCII --- diff --git a/Objects/object.c b/Objects/object.c index fd1fd256ba..79f1c8a835 100644 --- a/Objects/object.c +++ b/Objects/object.c @@ -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); diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index 99628cad72..0996afbbb8 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -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);