summaryrefslogtreecommitdiffstats
path: root/Objects/memoryobject.c
diff options
context:
space:
mode:
authorGregory P. Smith <greg@krypto.org>2019-05-29 18:46:58 (GMT)
committerGitHub <noreply@github.com>2019-05-29 18:46:58 (GMT)
commit0c2f9305640f7655ba0cd5f478948b2763b376b3 (patch)
treeeb5b39614be93083e883f7aeb6f3397d8d8b89c2 /Objects/memoryobject.c
parentaacc77fbd77640a8f03638216fa09372cc21673d (diff)
downloadcpython-0c2f9305640f7655ba0cd5f478948b2763b376b3.zip
cpython-0c2f9305640f7655ba0cd5f478948b2763b376b3.tar.gz
cpython-0c2f9305640f7655ba0cd5f478948b2763b376b3.tar.bz2
bpo-22385: Support output separators in hex methods. (#13578)
* bpo-22385: Support output separators in hex methods. Also in binascii.hexlify aka b2a_hex. The underlying implementation behind all hex generation in CPython uses the same pystrhex.c implementation. This adds support to bytes, bytearray, and memoryview objects. The binascii module functions exist rather than being slated for deprecation because they return bytes rather than requiring an intermediate step through a str object. This change was inspired by MicroPython which supports sep in its binascii implementation (and does not yet support the .hex methods). https://bugs.python.org/issue22385
Diffstat (limited to 'Objects/memoryobject.c')
-rw-r--r--Objects/memoryobject.c45
1 files changed, 37 insertions, 8 deletions
diff --git a/Objects/memoryobject.c b/Objects/memoryobject.c
index 6bbb413..3955c58 100644
--- a/Objects/memoryobject.c
+++ b/Objects/memoryobject.c
@@ -7,6 +7,12 @@
#include "pystrhex.h"
#include <stddef.h>
+/*[clinic input]
+class memoryview "PyMemoryViewObject *" "&PyMemoryView_Type"
+[clinic start generated code]*/
+/*[clinic end generated code: output=da39a3ee5e6b4b0d input=e2e49d2192835219]*/
+
+#include "clinic/memoryobject.c.h"
/****************************************************************************/
/* ManagedBuffer Object */
@@ -2160,8 +2166,33 @@ memory_tobytes(PyMemoryViewObject *self, PyObject *args, PyObject *kwds)
return bytes;
}
+/*[clinic input]
+memoryview.hex
+
+ sep: object = NULL
+ An optional single character or byte to separate hex bytes.
+ bytes_per_sep: int = 1
+ How many bytes between separators. Positive values count from the
+ right, negative values count from the left.
+
+Return the data in the buffer as a str of hexadecimal numbers.
+
+Example:
+>>> value = memoryview(b'\xb9\x01\xef')
+>>> value.hex()
+'b901ef'
+>>> value.hex(':')
+'b9:01:ef'
+>>> value.hex(':', 2)
+'b9:01ef'
+>>> value.hex(':', -2)
+'b901:ef'
+[clinic start generated code]*/
+
static PyObject *
-memory_hex(PyMemoryViewObject *self, PyObject *dummy)
+memoryview_hex_impl(PyMemoryViewObject *self, PyObject *sep,
+ int bytes_per_sep)
+/*[clinic end generated code: output=430ca760f94f3ca7 input=539f6a3a5fb56946]*/
{
Py_buffer *src = VIEW_ADDR(self);
PyObject *bytes;
@@ -2170,7 +2201,7 @@ memory_hex(PyMemoryViewObject *self, PyObject *dummy)
CHECK_RELEASED(self);
if (MV_C_CONTIGUOUS(self->flags)) {
- return _Py_strhex(src->buf, src->len);
+ return _Py_strhex_with_sep(src->buf, src->len, sep, bytes_per_sep);
}
bytes = PyBytes_FromStringAndSize(NULL, src->len);
@@ -2182,7 +2213,9 @@ memory_hex(PyMemoryViewObject *self, PyObject *dummy)
return NULL;
}
- ret = _Py_strhex(PyBytes_AS_STRING(bytes), PyBytes_GET_SIZE(bytes));
+ ret = _Py_strhex_with_sep(
+ PyBytes_AS_STRING(bytes), PyBytes_GET_SIZE(bytes),
+ sep, bytes_per_sep);
Py_DECREF(bytes);
return ret;
@@ -3090,10 +3123,6 @@ When order is 'C' or 'F', the data of the original array is converted to C or\n\
Fortran order. For contiguous views, 'A' returns an exact copy of the physical\n\
memory. In particular, in-memory Fortran order is preserved. For non-contiguous\n\
views, the data is converted to C first. order=None is the same as order='C'.");
-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\
@@ -3110,7 +3139,7 @@ Return a readonly version of the memoryview.");
static PyMethodDef memory_methods[] = {
{"release", (PyCFunction)memory_release, METH_NOARGS, memory_release_doc},
{"tobytes", (PyCFunction)(void(*)(void))memory_tobytes, METH_VARARGS|METH_KEYWORDS, memory_tobytes_doc},
- {"hex", (PyCFunction)memory_hex, METH_NOARGS, memory_hex_doc},
+ MEMORYVIEW_HEX_METHODDEF
{"tolist", (PyCFunction)memory_tolist, METH_NOARGS, memory_tolist_doc},
{"cast", (PyCFunction)(void(*)(void))memory_cast, METH_VARARGS|METH_KEYWORDS, memory_cast_doc},
{"toreadonly", (PyCFunction)memory_toreadonly, METH_NOARGS, memory_toreadonly_doc},