Commit 4cd60175 authored by Mark Dickinson's avatar Mark Dickinson

Simplify random_seed to use _PyLong_AsByteArray. Closes issue #16496.

parent 1724bb19
...@@ -168,9 +168,9 @@ init_genrand(RandomObject *self, unsigned long s) ...@@ -168,9 +168,9 @@ init_genrand(RandomObject *self, unsigned long s)
/* init_key is the array for initializing keys */ /* init_key is the array for initializing keys */
/* key_length is its length */ /* key_length is its length */
static PyObject * static PyObject *
init_by_array(RandomObject *self, unsigned long init_key[], unsigned long key_length) init_by_array(RandomObject *self, unsigned long init_key[], size_t key_length)
{ {
unsigned int i, j, k; /* was signed in the original code. RDH 12/16/2002 */ size_t i, j, k; /* was signed in the original code. RDH 12/16/2002 */
unsigned long *mt; unsigned long *mt;
mt = self->state; mt = self->state;
...@@ -207,14 +207,11 @@ static PyObject * ...@@ -207,14 +207,11 @@ static PyObject *
random_seed(RandomObject *self, PyObject *args) random_seed(RandomObject *self, PyObject *args)
{ {
PyObject *result = NULL; /* guilty until proved innocent */ PyObject *result = NULL; /* guilty until proved innocent */
PyObject *masklower = NULL;
PyObject *thirtytwo = NULL;
PyObject *n = NULL; PyObject *n = NULL;
unsigned long *new_key, *key = NULL; unsigned long *key = NULL;
unsigned long keymax; /* # of allocated slots in key */ unsigned char *key_as_bytes = NULL;
unsigned long keyused; /* # of used slots in key */ size_t bits, keyused, i;
int err; int res;
PyObject *arg = NULL; PyObject *arg = NULL;
if (!PyArg_UnpackTuple(args, "seed", 0, 1, &arg)) if (!PyArg_UnpackTuple(args, "seed", 0, 1, &arg))
...@@ -243,69 +240,43 @@ random_seed(RandomObject *self, PyObject *args) ...@@ -243,69 +240,43 @@ random_seed(RandomObject *self, PyObject *args)
if (n == NULL) if (n == NULL)
goto Done; goto Done;
/* Now split n into 32-bit chunks, from the right. Each piece is /* Now split n into 32-bit chunks, from the right. */
* stored into key, which has a capacity of keymax chunks, of which bits = _PyLong_NumBits(n);
* keyused are filled. Alas, the repeated shifting makes this a if (bits == (size_t)-1 && PyErr_Occurred())
* quadratic-time algorithm; we'd really like to use
* _PyLong_AsByteArray here, but then we'd have to break into the
* long representation to figure out how big an array was needed
* in advance.
*/
keymax = 8; /* arbitrary; grows later if needed */
keyused = 0;
key = (unsigned long *)PyMem_Malloc(keymax * sizeof(*key));
if (key == NULL)
goto Done; goto Done;
masklower = PyLong_FromUnsignedLong(0xffffffffU); /* Figure out how many 32-bit chunks this gives us. */
if (masklower == NULL) keyused = bits == 0 ? 1 : (bits - 1) / 32 + 1;
goto Done;
thirtytwo = PyLong_FromLong(32L);
if (thirtytwo == NULL)
goto Done;
while ((err=PyObject_IsTrue(n))) {
PyObject *newn;
PyObject *pychunk;
unsigned long chunk;
if (err == -1) /* Convert seed to byte sequence. */
goto Done; key_as_bytes = (unsigned char *)PyMem_Malloc((size_t)4 * keyused);
pychunk = PyNumber_And(n, masklower); if (key_as_bytes == NULL)
if (pychunk == NULL)
goto Done; goto Done;
chunk = PyLong_AsUnsignedLong(pychunk); res = _PyLong_AsByteArray((PyLongObject *)n,
Py_DECREF(pychunk); key_as_bytes, keyused * 4,
if (chunk == (unsigned long)-1 && PyErr_Occurred()) 1, /* little-endian */
goto Done; 0); /* unsigned */
newn = PyNumber_Rshift(n, thirtytwo); if (res == -1) {
if (newn == NULL) PyMem_Free(key_as_bytes);
goto Done;
Py_DECREF(n);
n = newn;
if (keyused >= keymax) {
unsigned long bigger = keymax << 1;
if ((bigger >> 1) != keymax ||
bigger > PY_SSIZE_T_MAX / sizeof(*key)) {
PyErr_NoMemory();
goto Done; goto Done;
} }
new_key = (unsigned long *)PyMem_Realloc(key,
bigger * sizeof(*key)); /* Fill array of unsigned longs from byte sequence. */
if (new_key == NULL) key = (unsigned long *)PyMem_Malloc(sizeof(unsigned long) * keyused);
if (key == NULL) {
PyMem_Free(key_as_bytes);
goto Done; goto Done;
key = new_key;
keymax = bigger;
} }
assert(keyused < keymax); for (i = 0; i < keyused; i++) {
key[keyused++] = chunk; key[i] =
((unsigned long)key_as_bytes[4*i + 0] << 0) +
((unsigned long)key_as_bytes[4*i + 1] << 8) +
((unsigned long)key_as_bytes[4*i + 2] << 16) +
((unsigned long)key_as_bytes[4*i + 3] << 24);
} }
PyMem_Free(key_as_bytes);
if (keyused == 0)
key[keyused++] = 0UL;
result = init_by_array(self, key, keyused); result = init_by_array(self, key, keyused);
Done: Done:
Py_XDECREF(masklower);
Py_XDECREF(thirtytwo);
Py_XDECREF(n); Py_XDECREF(n);
PyMem_Free(key); PyMem_Free(key);
return result; return result;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment