Skip to content

Commit 25d3447

Browse files
committed
Brian Hooper <brian_takashi@hotmail.com>:
Here's a patch which changes modsupport to add 'u' and 'u#', to support building Unicode objects from a null-terminated Py_UNICODE *, and a Py_UNICODE * with length, respectively. [Conversion from 'U' to 'u' by Fred, based on python-dev comments.] Note that the use of None for NULL values of the Py_UNICODE* value is still in; I'm not sure of the conclusion on that issue.
1 parent 6664bb8 commit 25d3447

1 file changed

Lines changed: 31 additions & 1 deletion

File tree

Python/modsupport.c

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,15 @@ do_mklist(p_format, p_va, endchar, n)
232232
return v;
233233
}
234234

235+
static int
236+
_ustrlen(Py_UNICODE *u)
237+
{
238+
int i = 0;
239+
Py_UNICODE *v = u;
240+
while (*v != 0) { i++; v++; }
241+
return i;
242+
}
243+
235244
static PyObject *
236245
do_mktuple(p_format, p_va, endchar, n)
237246
char **p_format;
@@ -295,7 +304,28 @@ do_mkvalue(p_format, p_va)
295304
case 'L':
296305
return PyLong_FromLongLong((LONG_LONG)va_arg(*p_va, LONG_LONG));
297306
#endif
298-
307+
case 'u':
308+
{
309+
PyObject *v;
310+
Py_UNICODE *u = va_arg(*p_va, Py_UNICODE *);
311+
int n;
312+
if (**p_format == '#') {
313+
++*p_format;
314+
n = va_arg(*p_va, int);
315+
}
316+
else
317+
n = -1;
318+
if (u == NULL) {
319+
v = Py_None;
320+
Py_INCREF(v);
321+
}
322+
else {
323+
if (n < 0)
324+
n = _ustrlen(u);
325+
v = PyUnicode_FromUnicode(u, n);
326+
}
327+
return v;
328+
}
299329
case 'f':
300330
case 'd':
301331
return PyFloat_FromDouble(

0 commit comments

Comments
 (0)