https://github.com/python/cpython/commit/f62b495f792b579f610a7fbf93c8e4c53ccf1369
commit: f62b495f792b579f610a7fbf93c8e4c53ccf1369
branch: main
author: Victor Stinner <[email protected]>
committer: vstinner <[email protected]>
date: 2025-09-15T22:53:03+02:00
summary:
gh-129813, PEP 782: Use PyBytesWriter in _sha3 (#138923)
Replace PyBytes_FromStringAndSize(NULL, size) with the new public
PyBytesWriter API.
files:
M Modules/sha3module.c
diff --git a/Modules/sha3module.c b/Modules/sha3module.c
index 47fe5e57e6ddd9..14c543b86415d5 100644
--- a/Modules/sha3module.c
+++ b/Modules/sha3module.c
@@ -509,14 +509,19 @@ _sha3_shake_128_digest_impl(SHA3object *self, Py_ssize_t
length)
if (length == 0) {
return Py_GetConstant(Py_CONSTANT_EMPTY_BYTES);
}
-
CHECK_HACL_UINT32_T_LENGTH(length);
- PyObject *digest = PyBytes_FromStringAndSize(NULL, length);
- uint8_t *buffer = (uint8_t *)PyBytes_AS_STRING(digest);
+
+ PyBytesWriter *writer = PyBytesWriter_Create(length);
+ if (writer == NULL) {
+ return NULL;
+ }
+ uint8_t *buffer = (uint8_t *)PyBytesWriter_GetData(writer);
+
HASHLIB_ACQUIRE_LOCK(self);
(void)Hacl_Hash_SHA3_squeeze(self->hash_state, buffer, (uint32_t)length);
HASHLIB_RELEASE_LOCK(self);
- return digest;
+
+ return PyBytesWriter_Finish(writer);
}
@@ -540,8 +545,8 @@ _sha3_shake_128_hexdigest_impl(SHA3object *self, Py_ssize_t
length)
if (length == 0) {
return Py_GetConstant(Py_CONSTANT_EMPTY_STR);
}
-
CHECK_HACL_UINT32_T_LENGTH(length);
+
uint8_t *buffer = PyMem_Malloc(length);
if (buffer == NULL) {
return PyErr_NoMemory();
@@ -550,6 +555,7 @@ _sha3_shake_128_hexdigest_impl(SHA3object *self, Py_ssize_t
length)
HASHLIB_ACQUIRE_LOCK(self);
(void)Hacl_Hash_SHA3_squeeze(self->hash_state, buffer, (uint32_t)length);
HASHLIB_RELEASE_LOCK(self);
+
PyObject *digest = _Py_strhex((const char *)buffer, length);
PyMem_Free(buffer);
return digest;
_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]