summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTim Peters <tim.peters@gmail.com>2004-08-30 17:36:46 (GMT)
committerTim Peters <tim.peters@gmail.com>2004-08-30 17:36:46 (GMT)
commitd311538a93f91d94963b4fdaac8e66e3598df64e (patch)
tree1ea8a331b8b913512d613ea2b18ddaa967af5ba5
parent9b279a8df4165fa7b36eaf191b017990d7d497f5 (diff)
downloadcpython-d311538a93f91d94963b4fdaac8e66e3598df64e.zip
cpython-d311538a93f91d94963b4fdaac8e66e3598df64e.tar.gz
cpython-d311538a93f91d94963b4fdaac8e66e3598df64e.tar.bz2
win32_urandom(): There's no need to copy the generated byte string, so
don't.
-rw-r--r--Modules/posixmodule.c28
1 files changed, 11 insertions, 17 deletions
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c
index 3eb1ee8..306bce5 100644
--- a/Modules/posixmodule.c
+++ b/Modules/posixmodule.c
@@ -7239,9 +7239,8 @@ static HCRYPTPROV hCryptProv = 0;
static PyObject*
win32_urandom(PyObject *self, PyObject *args)
{
- int howMany = 0;
- unsigned char* bytes = NULL;
- PyObject* returnVal = NULL;
+ int howMany;
+ PyObject* result;
/* Read arguments */
if (! PyArg_ParseTuple(args, "i:urandom", &howMany))
@@ -7282,21 +7281,16 @@ win32_urandom(PyObject *self, PyObject *args)
}
/* Allocate bytes */
- bytes = (unsigned char*)PyMem_Malloc(howMany);
- if (bytes == NULL)
- return PyErr_NoMemory();
-
- /* Get random data */
- if (! pCryptGenRandom(hCryptProv, howMany, bytes)) {
- PyMem_Free(bytes);
- return win32_error("CryptGenRandom", NULL);
+ result = PyString_FromStringAndSize(NULL, howMany);
+ if (result != NULL) {
+ /* Get random data */
+ if (! pCryptGenRandom(hCryptProv, howMany, (unsigned char*)
+ PyString_AS_STRING(result))) {
+ Py_DECREF(result);
+ return win32_error("CryptGenRandom", NULL);
+ }
}
-
- /* Build return value */
- returnVal = PyString_FromStringAndSize(bytes, howMany);
- PyMem_Free(bytes);
-
- return returnVal;
+ return result;
}
#endif