Skip to content

Commit eec8c98

Browse files
authored
gh-129813, PEP 782: Use PyBytesWriter in _hashopenssl (#138835)
Replace PyBytes_FromStringAndSize(NULL, size) with the new public PyBytesWriter API.
1 parent ea26f6d commit eec8c98

File tree

1 file changed

+11
-13
lines changed

1 file changed

+11
-13
lines changed

Modules/_hashopenssl.c

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1101,39 +1101,38 @@ _hashlib_HASHXOF_digest_impl(HASHobject *self, Py_ssize_t length)
11011101
/*[clinic end generated code: output=dcb09335dd2fe908 input=224d047da2c12a42]*/
11021102
{
11031103
EVP_MD_CTX *temp_ctx;
1104-
PyObject *retval;
11051104

11061105
if (length == 0) {
11071106
return Py_GetConstant(Py_CONSTANT_EMPTY_BYTES);
11081107
}
11091108

1110-
retval = PyBytes_FromStringAndSize(NULL, length);
1111-
if (retval == NULL) {
1109+
PyBytesWriter *writer = PyBytesWriter_Create(length);
1110+
if (writer == NULL) {
11121111
return NULL;
11131112
}
11141113

11151114
temp_ctx = py_wrapper_EVP_MD_CTX_new();
11161115
if (temp_ctx == NULL) {
1117-
Py_DECREF(retval);
1116+
PyBytesWriter_Discard(writer);
11181117
return NULL;
11191118
}
11201119

11211120
if (_hashlib_HASH_copy_locked(self, temp_ctx) < 0) {
11221121
goto error;
11231122
}
11241123
if (!EVP_DigestFinalXOF(temp_ctx,
1125-
(unsigned char*)PyBytes_AS_STRING(retval),
1124+
(unsigned char*)PyBytesWriter_GetData(writer),
11261125
length))
11271126
{
11281127
notify_ssl_error_occurred_in(Py_STRINGIFY(EVP_DigestFinalXOF));
11291128
goto error;
11301129
}
11311130

11321131
EVP_MD_CTX_free(temp_ctx);
1133-
return retval;
1132+
return PyBytesWriter_Finish(writer);
11341133

11351134
error:
1136-
Py_DECREF(retval);
1135+
PyBytesWriter_Discard(writer);
11371136
EVP_MD_CTX_free(temp_ctx);
11381137
return NULL;
11391138
}
@@ -1750,7 +1749,6 @@ _hashlib_scrypt_impl(PyObject *module, Py_buffer *password, Py_buffer *salt,
17501749
long maxmem, long dklen)
17511750
/*[clinic end generated code: output=d424bc3e8c6b9654 input=bdeac9628d07f7a1]*/
17521751
{
1753-
PyObject *key = NULL;
17541752
int retval;
17551753

17561754
if (password->len > INT_MAX) {
@@ -1791,8 +1789,8 @@ _hashlib_scrypt_impl(PyObject *module, Py_buffer *password, Py_buffer *salt,
17911789
return NULL;
17921790
}
17931791

1794-
key = PyBytes_FromStringAndSize(NULL, dklen);
1795-
if (key == NULL) {
1792+
PyBytesWriter *writer = PyBytesWriter_Create(dklen);
1793+
if (writer == NULL) {
17961794
return NULL;
17971795
}
17981796

@@ -1801,16 +1799,16 @@ _hashlib_scrypt_impl(PyObject *module, Py_buffer *password, Py_buffer *salt,
18011799
(const char *)password->buf, (size_t)password->len,
18021800
(const unsigned char *)salt->buf, (size_t)salt->len,
18031801
(uint64_t)n, (uint64_t)r, (uint64_t)p, (uint64_t)maxmem,
1804-
(unsigned char *)PyBytes_AS_STRING(key), (size_t)dklen
1802+
PyBytesWriter_GetData(writer), (size_t)dklen
18051803
);
18061804
Py_END_ALLOW_THREADS
18071805

18081806
if (!retval) {
1809-
Py_DECREF(key);
1807+
PyBytesWriter_Discard(writer);
18101808
notify_ssl_error_occurred_in(Py_STRINGIFY(EVP_PBE_scrypt));
18111809
return NULL;
18121810
}
1813-
return key;
1811+
return PyBytesWriter_Finish(writer);
18141812
}
18151813

18161814
#undef HASHLIB_SCRYPT_MAX_DKLEN

0 commit comments

Comments
 (0)