Index: Include/unicodeobject.h
===================================================================
--- Include/unicodeobject.h (révision 70384)
+++ Include/unicodeobject.h (copie de travail)
@@ -1489,6 +1489,11 @@
Py_ssize_t *count,
int append_zero_char);
+/* Number of column needed to represent the string in the current locale. -1
+ is returned in case of an error. */
+
+PyAPI_FUNC(int) PyUnicode_Width(PyObject *self);
+
/* === Characters Type APIs =============================================== */
/* Helper array used by Py_UNICODE_ISSPACE(). */
Index: Objects/unicodeobject.c
===================================================================
--- Objects/unicodeobject.c (révision 70384)
+++ Objects/unicodeobject.c (copie de travail)
@@ -8552,6 +8552,46 @@
PyDoc_STRVAR(sizeof__doc__,
"S.__sizeof__() -> size of S in memory, in bytes");
+int
+PyUnicode_Width(PyObject *unicode)
+{
+ Py_ssize_t len;
+ wchar_t *buf;
+ int res, width;
+
+ width = len = PyUnicode_GET_SIZE(unicode);
+
+ buf = PyMem_New(wchar_t, len);
+ if (!buf) {
+ PyErr_NoMemory();
+ goto error;
+ }
+ if (PyUnicode_AsWideChar((PyUnicodeObject*)unicode, buf, len) == -1)
+ goto error;
+
+#if defined(MS_WINDOWS)
+ res = WideCharToMultiByte(CP_ACP, 0, buf, len,
+ NULL, 0, NULL, NULL);
+ if (res == FALSE) {
+ PyErr_SetString(PyExc_ValueError, "Unable to compute string width");
+ goto error;
+ }
+#else
+ res = wcswidth(buf, len);
+ if (res == -1) {
+ PyErr_SetFromErrno(PyExc_IOError);
+ goto error;
+ }
+#endif
+ width = res;
+ goto finally;
+error:
+ width = -1;
+finally:
+ if (buf) PyMem_FREE(buf);
+ return width;
+}
+
static PyObject *
unicode_getnewargs(PyUnicodeObject *v)
{