[Python-checkins] cpython: Issue9951: update _hashopenssl and md5module to use _Py_strhex().

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


https://hg.python.org/cpython/rev/955a479b31a8
changeset:   95799:955a479b31a8
user:        Gregory P. Smith <greg at krypto.org>
date:        Sat Apr 25 23:42:38 2015 +0000
summary:
  Issue9951: update _hashopenssl and md5module to use _Py_strhex().
Also update _posixsubprocess to use Py_hexdigits instead of its own constant.

files:
  Modules/_hashopenssl.c     |  22 +++-------------------
  Modules/_posixsubprocess.c |   2 +-
  Modules/md5module.c        |  23 ++---------------------
  3 files changed, 6 insertions(+), 41 deletions(-)


diff --git a/Modules/_hashopenssl.c b/Modules/_hashopenssl.c
--- a/Modules/_hashopenssl.c
+++ b/Modules/_hashopenssl.c
@@ -16,6 +16,7 @@
 #include "Python.h"
 #include "structmember.h"
 #include "hashlib.h"
+#include "pystrhex.h"
 
 
 /* EVP is the preferred interface to hashing in OpenSSL */
@@ -157,9 +158,7 @@
 {
     unsigned char digest[EVP_MAX_MD_SIZE];
     EVP_MD_CTX temp_ctx;
-    PyObject *retval;
-    char *hex_digest;
-    unsigned int i, j, digest_size;
+    unsigned int digest_size;
 
     /* Get the raw (binary) digest value */
     locked_EVP_MD_CTX_copy(&temp_ctx, self);
@@ -168,22 +167,7 @@
 
     EVP_MD_CTX_cleanup(&temp_ctx);
 
-    /* Allocate a new buffer */
-    hex_digest = PyMem_Malloc(digest_size * 2 + 1);
-    if (!hex_digest)
-        return PyErr_NoMemory();
-
-    /* Make hex version of the digest */
-    for(i=j=0; i<digest_size; 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];
-    }
-    retval = PyUnicode_FromStringAndSize(hex_digest, digest_size * 2);
-    PyMem_Free(hex_digest);
-    return retval;
+    return _Py_strhex((const char *)digest, digest_size);
 }
 
 PyDoc_STRVAR(EVP_update__doc__,
diff --git a/Modules/_posixsubprocess.c b/Modules/_posixsubprocess.c
--- a/Modules/_posixsubprocess.c
+++ b/Modules/_posixsubprocess.c
@@ -504,7 +504,7 @@
         _Py_write_noraise(errpipe_write, "OSError:", 8);
         cur = hex_errno + sizeof(hex_errno);
         while (saved_errno != 0 && cur > hex_errno) {
-            *--cur = "0123456789ABCDEF"[saved_errno % 16];
+            *--cur = Py_hexdigits[saved_errno % 16];
             saved_errno /= 16;
         }
         _Py_write_noraise(errpipe_write, cur, hex_errno + sizeof(hex_errno) - cur);
diff --git a/Modules/md5module.c b/Modules/md5module.c
--- a/Modules/md5module.c
+++ b/Modules/md5module.c
@@ -18,6 +18,7 @@
 
 #include "Python.h"
 #include "hashlib.h"
+#include "pystrhex.h"
 
 /*[clinic input]
 module _md5
@@ -387,32 +388,12 @@
 {
     unsigned char digest[MD5_DIGESTSIZE];
     struct md5_state temp;
-    PyObject *retval;
-    Py_UCS1 *hex_digest;
-    int i, j;
 
     /* Get the raw (binary) digest value */
     temp = self->hash_state;
     md5_done(&temp, digest);
 
-    /* Create a new string */
-    retval = PyUnicode_New(MD5_DIGESTSIZE * 2, 127);
-    if (!retval)
-            return NULL;
-    hex_digest = PyUnicode_1BYTE_DATA(retval);
-
-    /* Make hex version of the digest */
-    for(i=j=0; i<MD5_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, MD5_DIGESTSIZE);
 }
 
 /*[clinic input]

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


More information about the Python-checkins mailing list