[Python-checkins] cpython: Implements issue #9951: Adds a hex() method to bytes, bytearray, & memoryview.

gregory.p.smith python-checkins at python.org
Sun Apr 26 01:22:36 CEST 2015


https://hg.python.org/cpython/rev/c9f1630cf2b1
changeset:   95798:c9f1630cf2b1
user:        Gregory P. Smith <greg at krypto.org>
date:        Sat Apr 25 23:22:26 2015 +0000
summary:
  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.

files:
  Doc/library/stdtypes.rst           |  37 ++++++++++++++++++
  Doc/whatsnew/3.5.rst               |   3 +
  Lib/test/test_bytes.py             |   8 +++
  Lib/test/test_doctest.py           |   2 +-
  Makefile.pre.in                    |   2 +
  Misc/ACKS                          |   1 +
  Misc/NEWS                          |   1 +
  Modules/sha1module.c               |  23 +----------
  Modules/sha256module.c             |  23 +----------
  Modules/sha512module.c             |  23 +----------
  Objects/bytearrayobject.c          |  15 +++++++
  Objects/bytesobject.c              |  16 +++++++
  Objects/memoryobject.c             |  14 ++++++
  PCbuild/pythoncore.vcxproj         |   2 +
  PCbuild/pythoncore.vcxproj.filters |   6 ++
  15 files changed, 112 insertions(+), 64 deletions(-)


diff --git a/Doc/library/stdtypes.rst b/Doc/library/stdtypes.rst
--- a/Doc/library/stdtypes.rst
+++ b/Doc/library/stdtypes.rst
@@ -2289,6 +2289,19 @@
    >>> bytes.fromhex('2Ef0 F1f2  ')
    b'.\xf0\xf1\xf2'
 
+A reverse conversion function exists to transform a bytes object into its
+hexadecimal representation.
+
+.. method:: bytes.hex()
+
+   Return a string object containing two hexadecimal digits for each
+   byte in the instance.
+
+   >>> b'\xf0\xf1\xf2'.hex()
+   'f0f1f2'
+
+   .. versionadded:: 3.5
+
 Since bytes objects are sequences of integers (akin to a tuple), for a bytes
 object *b*, ``b[0]`` will be an integer, while ``b[0:1]`` will be a bytes
 object of length 1.  (This contrasts with text strings, where both indexing
@@ -2344,6 +2357,19 @@
    >>> bytearray.fromhex('2Ef0 F1f2  ')
    bytearray(b'.\xf0\xf1\xf2')
 
+A reverse conversion function exists to transform a bytearray object into its
+hexadecimal representation.
+
+.. method:: bytearray.hex()
+
+   Return a string object containing two hexadecimal digits for each
+   byte in the instance.
+
+   >>> bytearray(b'\xf0\xf1\xf2').hex()
+   'f0f1f2'
+
+   .. versionadded:: 3.5
+
 Since bytearray objects are sequences of integers (akin to a list), for a
 bytearray object *b*, ``b[0]`` will be an integer, while ``b[0:1]`` will be
 a bytearray object of length 1.  (This contrasts with text strings, where
@@ -3458,6 +3484,17 @@
       supports all format strings, including those that are not in
       :mod:`struct` module syntax.
 
+   .. method:: hex()
+
+      Return a string object containing two hexadecimal digits for each
+      byte in the buffer. ::
+
+         >>> m = memoryview(b"abc")
+         >>> m.hex()
+         '616263'
+
+      .. versionadded:: 3.5
+
    .. method:: tolist()
 
       Return the data in the buffer as a list of elements. ::
diff --git a/Doc/whatsnew/3.5.rst b/Doc/whatsnew/3.5.rst
--- a/Doc/whatsnew/3.5.rst
+++ b/Doc/whatsnew/3.5.rst
@@ -80,6 +80,9 @@
 
 * ``bytes % args``, ``bytearray % args``: :pep:`461` - Adding ``%`` formatting
   to bytes and bytearray
+* ``b'\xf0\x9f\x90\x8d'.hex()``, ``bytearray(b'\xf0\x9f\x90\x8d').hex()``,
+  ``memoryview(b'\xf0\x9f\x90\x8d').hex()``: :issue:`9951` - A ``hex`` method
+  has been added to bytes, bytearray, and memoryview.
 
 Implementation improvements:
 
diff --git a/Lib/test/test_bytes.py b/Lib/test/test_bytes.py
--- a/Lib/test/test_bytes.py
+++ b/Lib/test/test_bytes.py
@@ -301,6 +301,14 @@
         self.assertRaises(ValueError, self.type2test.fromhex, '\x00')
         self.assertRaises(ValueError, self.type2test.fromhex, '12   \x00   34')
 
+    def test_hex(self):
+        self.assertRaises(TypeError, self.type2test.hex)
+        self.assertRaises(TypeError, self.type2test.hex, 1)
+        self.assertEquals(self.type2test(b"").hex(), "")
+        self.assertEquals(bytearray([0x1a, 0x2b, 0x30]).hex(), '1a2b30')
+        self.assertEquals(self.type2test(b"\x1a\x2b\x30").hex(), '1a2b30')
+        self.assertEquals(memoryview(b"\x1a\x2b\x30").hex(), '1a2b30')
+
     def test_join(self):
         self.assertEqual(self.type2test(b"").join([]), b"")
         self.assertEqual(self.type2test(b"").join([b""]), b"")
diff --git a/Lib/test/test_doctest.py b/Lib/test/test_doctest.py
--- a/Lib/test/test_doctest.py
+++ b/Lib/test/test_doctest.py
@@ -659,7 +659,7 @@
 
     >>> import builtins
     >>> tests = doctest.DocTestFinder().find(builtins)
-    >>> 790 < len(tests) < 800 # approximate number of objects with docstrings
+    >>> 790 < len(tests) < 810 # approximate number of objects with docstrings
     True
     >>> real_tests = [t for t in tests if len(t.examples) > 0]
     >>> len(real_tests) # objects that actually have doctests
diff --git a/Makefile.pre.in b/Makefile.pre.in
--- a/Makefile.pre.in
+++ b/Makefile.pre.in
@@ -402,6 +402,7 @@
 		Python/getopt.o \
 		Python/pystrcmp.o \
 		Python/pystrtod.o \
+		Python/pystrhex.o \
 		Python/dtoa.o \
 		Python/formatter_unicode.o \
 		Python/fileutils.o \
@@ -919,6 +920,7 @@
 		$(srcdir)/Include/pystate.h \
 		$(srcdir)/Include/pystrcmp.h \
 		$(srcdir)/Include/pystrtod.h \
+		$(srcdir)/Include/pystrhex.h \
 		$(srcdir)/Include/pythonrun.h \
 		$(srcdir)/Include/pythread.h \
 		$(srcdir)/Include/pytime.h \
diff --git a/Misc/ACKS b/Misc/ACKS
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -1547,6 +1547,7 @@
 Robert Xiao
 Florent Xicluna
 Hirokazu Yamamoto
+Arnon Yaari
 Ka-Ping Yee
 Jason Yeo
 EungJun Yi
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -11,6 +11,7 @@
 -----------------
 
 - Issue #24022: Fix tokenizer crash when processing undecodable source code.
+- Issue #9951: Added a hex() method to bytes, bytearray, and memoryview.
 
 Library
 -------
diff --git a/Modules/sha1module.c b/Modules/sha1module.c
--- a/Modules/sha1module.c
+++ b/Modules/sha1module.c
@@ -18,6 +18,7 @@
 
 #include "Python.h"
 #include "hashlib.h"
+#include "pystrhex.h"
 
 /*[clinic input]
 module _sha1
@@ -364,32 +365,12 @@
 {
     unsigned char digest[SHA1_DIGESTSIZE];
     struct sha1_state temp;
-    PyObject *retval;
-    Py_UCS1 *hex_digest;
-    int i, j;
 
     /* Get the raw (binary) digest value */
     temp = self->hash_state;
     sha1_done(&temp, digest);
 
-    /* Create a new string */
-    retval = PyUnicode_New(SHA1_DIGESTSIZE * 2, 127);
-    if (!retval)
-            return NULL;
-    hex_digest = PyUnicode_1BYTE_DATA(retval);
-
-    /* Make hex version of the digest */
-    for(i=j=0; i<SHA1_DIGESTSIZE; i++) {
-        unsigned char c;
-        c = (digest[i] >> 4) & 0xf;
-        hex_digest[j++] = Py_hexdigits[c];
-        c = (digest[i] & 0xf);
-        hex_digest[j++] = Py_hexdigits[c];
-    }
-#ifdef Py_DEBUG
-    assert(_PyUnicode_CheckConsistency(retval, 1));
-#endif
-    return retval;
+    return _Py_strhex((const char *)digest, SHA1_DIGESTSIZE);
 }
 
 /*[clinic input]
diff --git a/Modules/sha256module.c b/Modules/sha256module.c
--- a/Modules/sha256module.c
+++ b/Modules/sha256module.c
@@ -19,6 +19,7 @@
 #include "Python.h"
 #include "structmember.h"
 #include "hashlib.h"
+#include "pystrhex.h"
 
 /*[clinic input]
 module _sha256
@@ -454,32 +455,12 @@
 {
     unsigned char digest[SHA_DIGESTSIZE];
     SHAobject temp;
-    PyObject *retval;
-    Py_UCS1 *hex_digest;
-    int i, j;
 
     /* Get the raw (binary) digest value */
     SHAcopy(self, &temp);
     sha_final(digest, &temp);
 
-    /* Create a new string */
-    retval = PyUnicode_New(self->digestsize * 2, 127);
-    if (!retval)
-            return NULL;
-    hex_digest = PyUnicode_1BYTE_DATA(retval);
-
-    /* Make hex version of the digest */
-    for(i=j=0; i<self->digestsize; i++) {
-        unsigned char c;
-        c = (digest[i] >> 4) & 0xf;
-        hex_digest[j++] = Py_hexdigits[c];
-        c = (digest[i] & 0xf);
-        hex_digest[j++] = Py_hexdigits[c];
-    }
-#ifdef Py_DEBUG
-    assert(_PyUnicode_CheckConsistency(retval, 1));
-#endif
-    return retval;
+    return _Py_strhex((const char *)digest, self->digestsize);
 }
 
 /*[clinic input]
diff --git a/Modules/sha512module.c b/Modules/sha512module.c
--- a/Modules/sha512module.c
+++ b/Modules/sha512module.c
@@ -19,6 +19,7 @@
 #include "Python.h"
 #include "structmember.h"
 #include "hashlib.h"
+#include "pystrhex.h"
 
 /*[clinic input]
 module _sha512
@@ -521,32 +522,12 @@
 {
     unsigned char digest[SHA_DIGESTSIZE];
     SHAobject temp;
-    PyObject *retval;
-    Py_UCS1 *hex_digest;
-    int i, j;
 
     /* Get the raw (binary) digest value */
     SHAcopy(self, &temp);
     sha512_final(digest, &temp);
 
-    /* Create a new string */
-    retval = PyUnicode_New(self->digestsize * 2, 127);
-    if (!retval)
-            return NULL;
-    hex_digest = PyUnicode_1BYTE_DATA(retval);
-
-    /* Make hex version of the digest */
-    for (i=j=0; i<self->digestsize; i++) {
-        unsigned char c;
-        c = (digest[i] >> 4) & 0xf;
-        hex_digest[j++] = Py_hexdigits[c];
-        c = (digest[i] & 0xf);
-        hex_digest[j++] = Py_hexdigits[c];
-    }
-#ifdef Py_DEBUG
-    assert(_PyUnicode_CheckConsistency(retval, 1));
-#endif
-    return retval;
+    return _Py_strhex((const char *)digest, self->digestsize);
 }
 
 /*[clinic input]
diff --git a/Objects/bytearrayobject.c b/Objects/bytearrayobject.c
--- 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 @@
     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_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
--- 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 @@
     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 @@
      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
--- 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 @@
 }
 
 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 @@
 "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 @@
 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},
diff --git a/PCbuild/pythoncore.vcxproj b/PCbuild/pythoncore.vcxproj
--- a/PCbuild/pythoncore.vcxproj
+++ b/PCbuild/pythoncore.vcxproj
@@ -147,6 +147,7 @@
     <ClInclude Include="..\Include\pystate.h" />
     <ClInclude Include="..\Include\pystrcmp.h" />
     <ClInclude Include="..\Include\pystrtod.h" />
+    <ClInclude Include="..\Include\pystrhex.h" />
     <ClInclude Include="..\Include\dtoa.h" />
     <ClInclude Include="..\Include\Python-ast.h" />
     <ClInclude Include="..\Include\Python.h" />
@@ -376,6 +377,7 @@
     <ClCompile Include="..\Python\pytime.c" />
     <ClCompile Include="..\Python\pystate.c" />
     <ClCompile Include="..\Python\pystrcmp.c" />
+    <ClCompile Include="..\Python\pystrhex.c" />
     <ClCompile Include="..\Python\pystrtod.c" />
     <ClCompile Include="..\Python\dtoa.c" />
     <ClCompile Include="..\Python\Python-ast.c" />
diff --git a/PCbuild/pythoncore.vcxproj.filters b/PCbuild/pythoncore.vcxproj.filters
--- a/PCbuild/pythoncore.vcxproj.filters
+++ b/PCbuild/pythoncore.vcxproj.filters
@@ -243,6 +243,9 @@
     <ClInclude Include="..\Include\pystrtod.h">
       <Filter>Include</Filter>
     </ClInclude>
+    <ClInclude Include="..\Include\pystrhex.h">
+      <Filter>Include</Filter>
+    </ClInclude>
     <ClInclude Include="..\Include\dtoa.h">
       <Filter>Include</Filter>
     </ClInclude>
@@ -908,6 +911,9 @@
     <ClCompile Include="..\Python\pystrcmp.c">
       <Filter>Python</Filter>
     </ClCompile>
+    <ClCompile Include="..\Python\pystrhex.c">
+      <Filter>Python</Filter>
+    </ClCompile>
     <ClCompile Include="..\Python\pystrtod.c">
       <Filter>Python</Filter>
     </ClCompile>

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list