Skip to content

Commit 8ebad36

Browse files
committed
gh-139122: Format UUIDs directly into the result string
1 parent ce35192 commit 8ebad36

1 file changed

Lines changed: 6 additions & 2 deletions

File tree

‎Modules/_uuidmodule.c‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1452,7 +1452,11 @@ Uuid_str(PyObject *self)
14521452
uuidobject *uuid = uuidobject_CAST(self);
14531453

14541454
// UUID string format: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx (36 chars)
1455-
char str[36];
1455+
PyObject *result = PyUnicode_New(36, 127);
1456+
if (result == NULL) {
1457+
return NULL;
1458+
}
1459+
char *str = (char *)PyUnicode_1BYTE_DATA(result);
14561460

14571461
for (int i = 0; i < 4; i++) {
14581462
byte_to_hex(uuid->bytes[i], &str[i * 2]);
@@ -1478,7 +1482,7 @@ Uuid_str(PyObject *self)
14781482
byte_to_hex(uuid->bytes[i], &str[24 + (i - 10) * 2]);
14791483
}
14801484

1481-
return PyUnicode_FromStringAndSize(str, sizeof(str));
1485+
return result;
14821486
}
14831487

14841488
static PyObject *

0 commit comments

Comments
 (0)