summaryrefslogtreecommitdiffstats
path: root/Modules/clinic/_randommodule.c.h
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@python.org>2020-04-17 17:05:35 (GMT)
committerGitHub <noreply@github.com>2020-04-17 17:05:35 (GMT)
commit9f5fe7910f4a1bf5a425837d4915e332b945eb7b (patch)
tree5f652699332e33a81cf6baa5ff5b6fecadea1903 /Modules/clinic/_randommodule.c.h
parent22386bb4ef740ee92d34c87b8cb90d681423a853 (diff)
downloadcpython-9f5fe7910f4a1bf5a425837d4915e332b945eb7b.zip
cpython-9f5fe7910f4a1bf5a425837d4915e332b945eb7b.tar.gz
cpython-9f5fe7910f4a1bf5a425837d4915e332b945eb7b.tar.bz2
bpo-40286: Add randbytes() method to random.Random (GH-19527)
Add random.randbytes() function and random.Random.randbytes() method to generate random bytes. Modify secrets.token_bytes() to use SystemRandom.randbytes() rather than calling directly os.urandom(). Rename also genrand_int32() to genrand_uint32(), since it returns an unsigned 32-bit integer, not a signed integer. The _random module is now built with Py_BUILD_CORE_MODULE defined.
Diffstat (limited to 'Modules/clinic/_randommodule.c.h')
-rw-r--r--Modules/clinic/_randommodule.c.h43
1 files changed, 42 insertions, 1 deletions
diff --git a/Modules/clinic/_randommodule.c.h b/Modules/clinic/_randommodule.c.h
index a467811..dda78f6 100644
--- a/Modules/clinic/_randommodule.c.h
+++ b/Modules/clinic/_randommodule.c.h
@@ -114,4 +114,45 @@ _random_Random_getrandbits(RandomObject *self, PyObject *arg)
exit:
return return_value;
}
-/*[clinic end generated code: output=a7feb0c9c8d1b627 input=a9049054013a1b77]*/
+
+PyDoc_STRVAR(_random_Random_randbytes__doc__,
+"randbytes($self, n, /)\n"
+"--\n"
+"\n"
+"Generate n random bytes.");
+
+#define _RANDOM_RANDOM_RANDBYTES_METHODDEF \
+ {"randbytes", (PyCFunction)_random_Random_randbytes, METH_O, _random_Random_randbytes__doc__},
+
+static PyObject *
+_random_Random_randbytes_impl(RandomObject *self, Py_ssize_t n);
+
+static PyObject *
+_random_Random_randbytes(RandomObject *self, PyObject *arg)
+{
+ PyObject *return_value = NULL;
+ Py_ssize_t n;
+
+ if (PyFloat_Check(arg)) {
+ PyErr_SetString(PyExc_TypeError,
+ "integer argument expected, got float" );
+ goto exit;
+ }
+ {
+ Py_ssize_t ival = -1;
+ PyObject *iobj = PyNumber_Index(arg);
+ if (iobj != NULL) {
+ ival = PyLong_AsSsize_t(iobj);
+ Py_DECREF(iobj);
+ }
+ if (ival == -1 && PyErr_Occurred()) {
+ goto exit;
+ }
+ n = ival;
+ }
+ return_value = _random_Random_randbytes_impl(self, n);
+
+exit:
+ return return_value;
+}
+/*[clinic end generated code: output=e515c651860c4001 input=a9049054013a1b77]*/