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

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 unicode_char() 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
<snip>
> @@ -2887,17 +2883,7 @@
>          return NULL;
>      }
>
> -    if ((Py_UCS4)ordinal < 256)
> -        return get_latin1_char((unsigned char)ordinal);
> -
> -    v = PyUnicode_New(1, ordinal);
> -    if (v == NULL)
> -        return NULL;
> -    kind = PyUnicode_KIND(v);
> -    data = PyUnicode_DATA(v);
> -    PyUnicode_WRITE(kind, data, 0, ordinal);
> -    assert(_PyUnicode_CheckConsistency(v, 1));
> -    return v;
> +    return unicode_char((Py_UCS4)ordinal);
>  }
>
>  PyObject *
> @@ -11354,17 +11340,7 @@
>      kind = PyUnicode_KIND(self);
>      data = PyUnicode_DATA(self);
>      ch = PyUnicode_READ(kind, data, index);
> -    if (ch < 256)
> -        return get_latin1_char(ch);
> -
> -    res = PyUnicode_New(1, ch);
> -    if (res == NULL)
> -        return NULL;
> -    kind = PyUnicode_KIND(res);
> -    data = PyUnicode_DATA(res);
> -    PyUnicode_WRITE(kind, data, 0, ch);
> -    assert(_PyUnicode_CheckConsistency(res, 1));
> -    return res;
> +    return unicode_char(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;
-    PyObject *res;

     if (!PyUnicode_Check(self) || PyUnicode_READY(self) == -1) {
         PyErr_BadArgument();



-- 
Zach


More information about the Python-Dev mailing list