Thanks to visit codestin.com
Credit goes to github.com

Skip to content

bpo-40286: Remove C implementation of Random.randbytes() #19797

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Lib/random.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,12 @@ def setstate(self, state):
## ---- Methods below this point do not need to be overridden when
## ---- subclassing for the purpose of using a different core generator.

## -------------------- bytes methods ---------------------

def randbytes(self, n):
"""Generate n random bytes."""
return self.getrandbits(n * 8).to_bytes(n, 'little')

## -------------------- pickle support -------------------

# Issue 17489: Since __reduce__ was defined to fix #759889 this is no
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Remove ``_random.Random.randbytes()``: the C implementation of
``randbytes()``. Implement the method in Python to ease subclassing:
``randbytes()`` now directly reuses ``getrandbits()``.
45 changes: 0 additions & 45 deletions Modules/_randommodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -513,50 +513,6 @@ _random_Random_getrandbits_impl(RandomObject *self, int k)
return result;
}

/*[clinic input]

_random.Random.randbytes

self: self(type="RandomObject *")
n: Py_ssize_t
/

Generate n random bytes.
[clinic start generated code]*/

static PyObject *
_random_Random_randbytes_impl(RandomObject *self, Py_ssize_t n)
/*[clinic end generated code: output=67a28548079a17ea input=7ba658a24150d233]*/
{
if (n < 0) {
PyErr_SetString(PyExc_ValueError,
"number of bytes must be non-negative");
return NULL;
}

PyObject *bytes = PyBytes_FromStringAndSize(NULL, n);
if (bytes == NULL) {
return NULL;
}
uint8_t *ptr = (uint8_t *)PyBytes_AS_STRING(bytes);

for (; n; ptr += 4, n -= 4) {
uint32_t word = genrand_uint32(self);
#if PY_BIG_ENDIAN
/* Convert to little endian */
word = _Py_bswap32(word);
#endif
if (n < 4) {
/* Drop least significant bits */
memcpy(ptr, (uint8_t *)&word + (4 - n), n);
break;
}
memcpy(ptr, &word, 4);
}

return bytes;
}

static PyObject *
random_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
Expand Down Expand Up @@ -586,7 +542,6 @@ static PyMethodDef random_methods[] = {
_RANDOM_RANDOM_GETSTATE_METHODDEF
_RANDOM_RANDOM_SETSTATE_METHODDEF
_RANDOM_RANDOM_GETRANDBITS_METHODDEF
_RANDOM_RANDOM_RANDBYTES_METHODDEF
{NULL, NULL} /* sentinel */
};

Expand Down
43 changes: 1 addition & 42 deletions Modules/clinic/_randommodule.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.