[Python-Dev] [Python-checkins] cpython: Change decoders to use Unicode API instead of Py_UNICODE. (original) (raw)

Victor Stinner victor.stinner at haypocalc.com
Wed Nov 9 11:15:25 CET 2011


First of all, thanks for having upgraded this huge part (codecs) to the new Unicode API!

+static int +unicodewiden(PyObject **punicode, int maxchar) +{ + PyObject *result; + assert(PyUnicodeISREADY(*punicode)); + if (maxchar <= PyUnicodeMAXCHARVALUE(*punicode)) + return 0; + result = PyUnicodeNew(PyUnicodeGETLENGTH(*punicode), + maxchar); + if (result == NULL) + return -1; + PyUnicodeCopyCharacters(result, 0, *punicode, 0, + PyUnicodeGETLENGTH(*punicode)); + PyDECREF(*punicode); + *punicode = result; + return 0; +}

PyUnicode_CopyCharacters() result must be checked. If you are sure that the call cannot fail, use copy_characters() which uses assertions in debug mode ( and no check in release mode).

-#ifndef DONTMAKERESULTREADY - if (PyUnicodeREADYREPLACE(&v)) { - PyDECREF(v); - return NULL; - } -#endif

Why did you remove this call from PyUnicode_DecodeRawUnicodeEscape(), _PyUnicode_DecodeUnicodeInternal(), PyUnicode_DecodeASCII() and PyUnicode_DecodeCharmap()? It may reuse latin1 characters singletons to share a little bit more memory (there is already a special case for empty string).

"_PyUnicode_READY_REPLACE" is maybe not the best name :-)

Victor



More information about the Python-Dev mailing list