summaryrefslogtreecommitdiff
path: root/Objects/bytesobject.c
diff options
context:
space:
mode:
Diffstat (limited to 'Objects/bytesobject.c')
-rw-r--r--Objects/bytesobject.c16
1 files changed, 16 insertions, 0 deletions
diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c
index d981e0ee69..d2b52c77b3 100644
--- a/Objects/bytesobject.c
+++ b/Objects/bytesobject.c
@@ -5,6 +5,7 @@
#include "Python.h"
#include "bytes_methods.h"
+#include "pystrhex.h"
#include <stddef.h>
/*[clinic input]
@@ -3036,6 +3037,20 @@ bytes_fromhex_impl(PyTypeObject *type, PyObject *string)
return NULL;
}
+PyDoc_STRVAR(hex__doc__,
+"B.hex() -> string\n\
+\n\
+Create a string of hexadecimal numbers from a bytes object.\n\
+Example: b'\\xb9\\x01\\xef'.hex() -> 'b901ef'.");
+
+static PyObject *
+bytes_hex(PyBytesObject *self)
+{
+ char* argbuf = PyBytes_AS_STRING(self);
+ Py_ssize_t arglen = PyBytes_GET_SIZE(self);
+ return _Py_strhex(argbuf, arglen);
+}
+
static PyObject *
bytes_getnewargs(PyBytesObject *v)
{
@@ -3057,6 +3072,7 @@ bytes_methods[] = {
expandtabs__doc__},
{"find", (PyCFunction)bytes_find, METH_VARARGS, find__doc__},
BYTES_FROMHEX_METHODDEF
+ {"hex", (PyCFunction)bytes_hex, METH_NOARGS, hex__doc__},
{"index", (PyCFunction)bytes_index, METH_VARARGS, index__doc__},
{"isalnum", (PyCFunction)stringlib_isalnum, METH_NOARGS,
_Py_isalnum__doc__},