summaryrefslogtreecommitdiffstats
path: root/Objects/bytearrayobject.c
diff options
context:
space:
mode:
authorGregory P. Smith <greg@krypto.org>2015-04-25 23:22:26 (GMT)
committerGregory P. Smith <greg@krypto.org>2015-04-25 23:22:26 (GMT)
commit8cb6569fe14ba8e57ab1a2bea68594747852a9d1 (patch)
tree4391a41ff833b66e6482f5abbf7f0714f23ccf67 /Objects/bytearrayobject.c
parent644adc6adaecf5249de68211f70c0825a36fe6f7 (diff)
downloadcpython-8cb6569fe14ba8e57ab1a2bea68594747852a9d1.zip
cpython-8cb6569fe14ba8e57ab1a2bea68594747852a9d1.tar.gz
cpython-8cb6569fe14ba8e57ab1a2bea68594747852a9d1.tar.bz2
Implements issue #9951: Adds a hex() method to bytes, bytearray, & memoryview.
Also updates a few internal implementations of the same thing to use the new built-in code. Contributed by Arnon Yaari.
Diffstat (limited to 'Objects/bytearrayobject.c')
-rw-r--r--Objects/bytearrayobject.c15
1 files changed, 15 insertions, 0 deletions
diff --git a/Objects/bytearrayobject.c b/Objects/bytearrayobject.c
index d76f15f..14444a2 100644
--- a/Objects/bytearrayobject.c
+++ b/Objects/bytearrayobject.c
@@ -5,6 +5,7 @@
#include "structmember.h"
#include "bytes_methods.h"
#include "bytesobject.h"
+#include "pystrhex.h"
/*[clinic input]
class bytearray "PyByteArrayObject *" "&PyByteArray_Type"
@@ -2872,6 +2873,19 @@ bytearray_fromhex_impl(PyObject*cls, PyObject *string)
return NULL;
}
+PyDoc_STRVAR(hex__doc__,
+"B.hex() -> string\n\
+\n\
+Create a string of hexadecimal numbers from a bytearray object.\n\
+Example: bytearray([0xb9, 0x01, 0xef]).hex() -> 'b901ef'.");
+
+static PyObject *
+bytearray_hex(PyBytesObject *self)
+{
+ char* argbuf = PyByteArray_AS_STRING(self);
+ Py_ssize_t arglen = PyByteArray_GET_SIZE(self);
+ return _Py_strhex(argbuf, arglen);
+}
static PyObject *
_common_reduce(PyByteArrayObject *self, int proto)
@@ -3002,6 +3016,7 @@ bytearray_methods[] = {
BYTEARRAY_EXTEND_METHODDEF
{"find", (PyCFunction)bytearray_find, METH_VARARGS, find__doc__},
BYTEARRAY_FROMHEX_METHODDEF
+ {"hex", (PyCFunction)bytearray_hex, METH_NOARGS, hex__doc__},
{"index", (PyCFunction)bytearray_index, METH_VARARGS, index__doc__},
BYTEARRAY_INSERT_METHODDEF
{"isalnum", (PyCFunction)stringlib_isalnum, METH_NOARGS,