Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit af03757

Browse files
committed
Optimize ascii(str): don't encode/decode repr if repr is already ASCII
1 parent 322cc74 commit af03757

2 files changed

Lines changed: 4 additions & 1 deletion

File tree

Objects/object.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,9 @@ PyObject_ASCII(PyObject *v)
451451
if (repr == NULL)
452452
return NULL;
453453

454+
if (PyUnicode_IS_ASCII(repr))
455+
return repr;
456+
454457
/* repr is guaranteed to be a PyUnicode object by PyObject_Repr */
455458
ascii = _PyUnicode_AsASCIIString(repr, "backslashreplace");
456459
Py_DECREF(repr);

Objects/unicodeobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6499,7 +6499,7 @@ _PyUnicode_AsASCIIString(PyObject *unicode, const char *errors)
64996499
return NULL;
65006500
/* Fast path: if it is an ASCII-only string, construct bytes object
65016501
directly. Else defer to above function to raise the exception. */
6502-
if (PyUnicode_MAX_CHAR_VALUE(unicode) < 128)
6502+
if (PyUnicode_IS_ASCII(unicode))
65036503
return PyBytes_FromStringAndSize(PyUnicode_DATA(unicode),
65046504
PyUnicode_GET_LENGTH(unicode));
65056505
return unicode_encode_ucs1(unicode, errors, 128);

0 commit comments

Comments
 (0)