[Python-Dev] [Python-checkins] cpython: add unicode_char() in unicodeobject.c to factorize code (original) (raw)

Zachary Ware zachary.ware at gmail.com
Fri Jan 3 17:27:18 CET 2014


On Fri, Jan 3, 2014 at 6:01 AM, victor.stinner <python-checkins at python.org> wrote:

http://hg.python.org/cpython/rev/d453c95def31 changeset: 88271:d453c95def31 user: Victor Stinner <victor.stinner at gmail.com> date: Fri Jan 03 12:53:47 2014 +0100 summary: add unicodechar() in unicodeobject.c to factorize code

files: Objects/unicodeobject.c | 86 ++++++++++------------------ 1 files changed, 31 insertions(+), 55 deletions(-)

diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -2887,17 +2883,7 @@ return NULL; } - if ((PyUCS4)ordinal < 256) - return getlatin1char((unsigned char)ordinal); - - v = PyUnicodeNew(1, ordinal); - if (v == NULL) - return NULL; - kind = PyUnicodeKIND(v); - data = PyUnicodeDATA(v); - PyUnicodeWRITE(kind, data, 0, ordinal); - assert(PyUnicodeCheckConsistency(v, 1)); - return v; + return unicodechar((PyUCS4)ordinal); } PyObject * @@ -11354,17 +11340,7 @@ kind = PyUnicodeKIND(self); data = PyUnicodeDATA(self); ch = PyUnicodeREAD(kind, data, index); - if (ch < 256) - return getlatin1char(ch); - - res = PyUnicodeNew(1, ch); - if (res == NULL) - return NULL; - kind = PyUnicodeKIND(res); - data = PyUnicodeDATA(res); - PyUnicodeWRITE(kind, data, 0, ch); - assert(PyUnicodeCheckConsistency(res, 1)); - return res; + return unicodechar(ch); } /* Believe it or not, this produces the same value for ASCII strings

The above-quoted parts of this changeset caused several compiler warnings due to unused variables. On 32-bit Windows:

..\Objects\unicodeobject.c(2881): warning C4101: 'kind' : unreferenced local variable [P:\ath\to\cpython\PCbuild\pythoncore.vcxproj] ..\Objects\unicodeobject.c(2879): warning C4101: 'v' : unreferenced local variable [P:\ath\to\cpython\PCbuild\pythoncore.vcxproj] ..\Objects\unicodeobject.c(2880): warning C4101: 'data' : unreferenced local variable [P:\ath\to\cpython\PCbuild\pythoncore.vcxproj] ..\Objects\unicodeobject.c(11333): warning C4101: 'res' : unreferenced local variable [P:\ath\to\cpython\PCbuild\pythoncore.vcxproj]

I believe this should fix it, but I'll leave it up to you to confirm that, Victor :)

diff -r 8a3718f31188 Objects/unicodeobject.c --- a/Objects/unicodeobject.c Fri Jan 03 15:53:20 2014 +0100 +++ b/Objects/unicodeobject.c Fri Jan 03 10:20:12 2014 -0600 @@ -2876,10 +2876,6 @@ PyObject * PyUnicode_FromOrdinal(int ordinal) { - PyObject *v; - void *data; - int kind;

 if (ordinal < 0 || ordinal > MAX_UNICODE) {
     PyErr_SetString(PyExc_ValueError,
                     "chr() arg not in range(0x110000)");

@@ -11330,7 +11326,6 @@ void *data; enum PyUnicode_Kind kind; Py_UCS4 ch;

-- Zach



More information about the Python-Dev mailing list