Skip to content

Commit 4735195

Browse files
committed
gh-129813, PEP 782: Use PyBytesWriter in _hashopenssl
Replace PyBytes_FromStringAndSize(NULL, size) with the new public PyBytesWriter API.
1 parent 32b2c5d commit 4735195

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

Modules/_hashopenssl.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1629,7 +1629,6 @@ pbkdf2_hmac_impl(PyObject *module, const char *hash_name,
16291629
{
16301630
_hashlibstate *state = get_hashlib_state(module);
16311631
PyObject *key_obj = NULL;
1632-
char *key;
16331632
long dklen;
16341633
int retval;
16351634

@@ -1682,24 +1681,24 @@ pbkdf2_hmac_impl(PyObject *module, const char *hash_name,
16821681
goto end;
16831682
}
16841683

1685-
key_obj = PyBytes_FromStringAndSize(NULL, dklen);
1686-
if (key_obj == NULL) {
1687-
goto end;
1684+
PyBytesWriter *writer = PyBytesWriter_Create(dklen);
1685+
if (writer == NULL) {
1686+
return NULL;
16881687
}
1689-
key = PyBytes_AS_STRING(key_obj);
16901688

16911689
Py_BEGIN_ALLOW_THREADS
16921690
retval = PKCS5_PBKDF2_HMAC((const char *)password->buf, (int)password->len,
16931691
(const unsigned char *)salt->buf, (int)salt->len,
16941692
iterations, digest, dklen,
1695-
(unsigned char *)key);
1693+
PyBytesWriter_GetData(writer));
16961694
Py_END_ALLOW_THREADS
16971695

16981696
if (!retval) {
1699-
Py_CLEAR(key_obj);
1697+
PyBytesWriter_Discard(writer);
17001698
notify_ssl_error_occurred_in(Py_STRINGIFY(PKCS5_PBKDF2_HMAC));
17011699
goto end;
17021700
}
1701+
key_obj = PyBytesWriter_Finish(writer);
17031702

17041703
end:
17051704
if (digest != NULL) {

0 commit comments

Comments
 (0)