From 8cb6569fe14ba8e57ab1a2bea68594747852a9d1 Mon Sep 17 00:00:00 2001 From: "Gregory P. Smith" Date: Sat, 25 Apr 2015 23:22:26 +0000 Subject: 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. --- Objects/memoryobject.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'Objects/memoryobject.c') diff --git a/Objects/memoryobject.c b/Objects/memoryobject.c index b6951ab88e..54fd05f6f6 100644 --- a/Objects/memoryobject.c +++ b/Objects/memoryobject.c @@ -1,6 +1,7 @@ /* Memoryview object implementation */ #include "Python.h" +#include "pystrhex.h" #include @@ -2157,6 +2158,14 @@ memory_tobytes(PyMemoryViewObject *self, PyObject *dummy) return bytes; } +static PyObject * +memory_hex(PyMemoryViewObject *self, PyObject *dummy) +{ + Py_buffer *src = VIEW_ADDR(self); + CHECK_RELEASED(self); + return _Py_strhex(src->buf, src->len); +} + static PyObject * memory_repr(PyMemoryViewObject *self) { @@ -3061,6 +3070,10 @@ PyDoc_STRVAR(memory_tobytes_doc, "tobytes($self, /)\n--\n\ \n\ Return the data in the buffer as a byte string."); +PyDoc_STRVAR(memory_hex_doc, +"hex($self, /)\n--\n\ +\n\ +Return the data in the buffer as a string of hexadecimal numbers."); PyDoc_STRVAR(memory_tolist_doc, "tolist($self, /)\n--\n\ \n\ @@ -3073,6 +3086,7 @@ Cast a memoryview to a new format or shape."); static PyMethodDef memory_methods[] = { {"release", (PyCFunction)memory_release, METH_NOARGS, memory_release_doc}, {"tobytes", (PyCFunction)memory_tobytes, METH_NOARGS, memory_tobytes_doc}, + {"hex", (PyCFunction)memory_hex, METH_NOARGS, memory_hex_doc}, {"tolist", (PyCFunction)memory_tolist, METH_NOARGS, memory_tolist_doc}, {"cast", (PyCFunction)memory_cast, METH_VARARGS|METH_KEYWORDS, memory_cast_doc}, {"__enter__", memory_enter, METH_NOARGS, NULL}, -- cgit v1.2.1