[Python-checkins] cpython: PyLocale_strxfrm() uses the new Unicode API

victor.stinner python-checkins at python.org
Thu Sep 29 23:51:16 CEST 2011


http://hg.python.org/cpython/rev/771064b4466f
changeset:   72537:771064b4466f
user:        Victor Stinner <victor.stinner at haypocalc.com>
date:        Thu Sep 29 23:32:06 2011 +0200
summary:
  PyLocale_strxfrm() uses the new Unicode API

files:
  Modules/_localemodule.c |  39 +++++++++++-----------------
  1 files changed, 15 insertions(+), 24 deletions(-)


diff --git a/Modules/_localemodule.c b/Modules/_localemodule.c
--- a/Modules/_localemodule.c
+++ b/Modules/_localemodule.c
@@ -271,31 +271,22 @@
 static PyObject*
 PyLocale_strxfrm(PyObject* self, PyObject* args)
 {
-    Py_UNICODE *s0;
-    Py_ssize_t n0;
-    wchar_t *s, *buf = NULL;
-    size_t n1, n2;
+    PyObject *str;
+    Py_ssize_t n1;
+    wchar_t *s = NULL, *buf = NULL;
+    size_t n2;
     PyObject *result = NULL;
-#if Py_UNICODE_SIZE != SIZEOF_WCHAR_T
-    Py_ssize_t i;
-#endif
 
-    if (!PyArg_ParseTuple(args, "u#:strxfrm", &s0, &n0))
+    if (!PyArg_ParseTuple(args, "U:strxfrm", &str))
         return NULL;
 
-#if Py_UNICODE_SIZE == SIZEOF_WCHAR_T
-    s = (wchar_t *) s0;
-#else
-    s = PyMem_Malloc((n0+1)*sizeof(wchar_t));
-    if (!s)
-        return PyErr_NoMemory();
-    for (i=0; i<=n0; i++)
-        s[i] = s0[i];
-#endif
+    s = PyUnicode_AsWideCharString(str, &n1);
+    if (s == NULL)
+        goto exit;
 
     /* assume no change in size, first */
-    n1 = wcslen(s) + 1;
-    buf = PyMem_Malloc(n1*sizeof(wchar_t));
+    n1 = n1 + 1;
+    buf = PyMem_Malloc(n1 * sizeof(wchar_t));
     if (!buf) {
         PyErr_NoMemory();
         goto exit;
@@ -311,11 +302,11 @@
         n2 = wcsxfrm(buf, s, n2+1);
     }
     result = PyUnicode_FromWideChar(buf, n2);
- exit:
-    if (buf) PyMem_Free(buf);
-#if Py_UNICODE_SIZE != SIZEOF_WCHAR_T
-    PyMem_Free(s);
-#endif
+exit:
+    if (buf)
+        PyMem_Free(buf);
+    if (s)
+        PyMem_Free(s);
     return result;
 }
 #endif

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list