diff options
author | Gregory P. Smith <greg@krypto.org> | 2015-04-25 23:22:26 (GMT) |
---|---|---|
committer | Gregory P. Smith <greg@krypto.org> | 2015-04-25 23:22:26 (GMT) |
commit | 8cb6569fe14ba8e57ab1a2bea68594747852a9d1 (patch) | |
tree | 4391a41ff833b66e6482f5abbf7f0714f23ccf67 /Objects | |
parent | 644adc6adaecf5249de68211f70c0825a36fe6f7 (diff) | |
download | cpython-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')
-rw-r--r-- | Objects/bytearrayobject.c | 15 | ||||
-rw-r--r-- | Objects/bytesobject.c | 16 | ||||
-rw-r--r-- | Objects/memoryobject.c | 14 |
3 files changed, 45 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, diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c index d981e0e..d2b52c7 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__}, diff --git a/Objects/memoryobject.c b/Objects/memoryobject.c index b6951ab..54fd05f 100644 --- a/Objects/memoryobject.c +++ b/Objects/memoryobject.c @@ -1,6 +1,7 @@ /* Memoryview object implementation */ #include "Python.h" +#include "pystrhex.h" #include <stddef.h> @@ -2158,6 +2159,14 @@ memory_tobytes(PyMemoryViewObject *self, PyObject *dummy) } 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) { if (self->flags & _Py_MEMORYVIEW_RELEASED) @@ -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}, |