[Python-checkins] r77252 - in python/trunk: Lib/test/test_hashlib.py Modules/_hashopenssl.c Modules/md5module.c Modules/sha256module.c Modules/sha512module.c Modules/shamodule.c

gregory.p.smith python-checkins at python.org
Sat Jan 2 23:28:49 CET 2010


Author: gregory.p.smith
Date: Sat Jan  2 23:28:48 2010
New Revision: 77252

Log:
Issue #3745: Undo the requirement for new buffer API only objects to be passed
to hashlib functions in python 2.x.  The module now uses the 's*' for argument
parsing which auto encodes unicode objects to the system default encoding for
us.


Modified:
   python/trunk/Lib/test/test_hashlib.py
   python/trunk/Modules/_hashopenssl.c
   python/trunk/Modules/md5module.c
   python/trunk/Modules/sha256module.c
   python/trunk/Modules/sha512module.c
   python/trunk/Modules/shamodule.c

Modified: python/trunk/Lib/test/test_hashlib.py
==============================================================================
--- python/trunk/Lib/test/test_hashlib.py	(original)
+++ python/trunk/Lib/test/test_hashlib.py	Sat Jan  2 23:28:48 2010
@@ -71,18 +71,23 @@
         computed = hashlib.new(name, data).hexdigest()
         self.assertEqual(computed, digest)
 
-    def check_no_unicode(self, algorithm_name):
+    def check_unicode(self, algorithm_name):
         # Unicode objects are not allowed as input.
-        self.assertRaises(TypeError, getattr(hashlib, algorithm_name), u'spam')
-        self.assertRaises(TypeError, hashlib.new, algorithm_name, u'spam')
-
-    def test_no_unicode(self):
-        self.check_no_unicode('md5')
-        self.check_no_unicode('sha1')
-        self.check_no_unicode('sha224')
-        self.check_no_unicode('sha256')
-        self.check_no_unicode('sha384')
-        self.check_no_unicode('sha512')
+        expected = hashlib.new(algorithm_name, str(u'spam')).hexdigest()
+        self.assertEqual(getattr(hashlib, algorithm_name)(u'spam').hexdigest(),
+                        expected)
+        self.assertEqual(hashlib.new(algorithm_name, u'spam').hexdigest(),
+                         expected)
+
+    def test_unicode(self):
+        # In python 2.x unicode is auto-encoded to the system default encoding
+        # when passed to hashlib functions.
+        self.check_unicode('md5')
+        self.check_unicode('sha1')
+        self.check_unicode('sha224')
+        self.check_unicode('sha256')
+        self.check_unicode('sha384')
+        self.check_unicode('sha512')
 
     def test_case_md5_0(self):
         self.check('md5', '', 'd41d8cd98f00b204e9800998ecf8427e')

Modified: python/trunk/Modules/_hashopenssl.c
==============================================================================
--- python/trunk/Modules/_hashopenssl.c	(original)
+++ python/trunk/Modules/_hashopenssl.c	Sat Jan  2 23:28:48 2010
@@ -15,7 +15,6 @@
 
 #include "Python.h"
 #include "structmember.h"
-#include "hashlib.h"
 
 #ifdef WITH_THREAD
 #include "pythread.h"
@@ -218,14 +217,11 @@
 static PyObject *
 EVP_update(EVPobject *self, PyObject *args)
 {
-    PyObject *obj;
     Py_buffer view;
 
-    if (!PyArg_ParseTuple(args, "O:update", &obj))
+    if (!PyArg_ParseTuple(args, "s*:update", &view))
         return NULL;
 
-    GET_BUFFER_VIEW_OR_ERROUT(obj, &view, NULL);
-
 #ifdef WITH_THREAD
     if (self->lock == NULL && view.len >= HASHLIB_GIL_MINSIZE) {
         self->lock = PyThread_allocate_lock();
@@ -238,17 +234,16 @@
         EVP_hash(self, view.buf, view.len);
         PyThread_release_lock(self->lock);
         Py_END_ALLOW_THREADS
-    } else {
-        EVP_hash(self, view.buf, view.len);
     }
-#else
-    EVP_hash(self, view.buf, view.len);
+    else
 #endif
+    {
+        EVP_hash(self, view.buf, view.len);
+    }
 
     PyBuffer_Release(&view);
 
-    Py_INCREF(Py_None);
-    return Py_None;
+    Py_RETURN_NONE;
 }
 
 static PyMethodDef EVP_methods[] = {
@@ -314,31 +309,25 @@
 {
     static char *kwlist[] = {"name", "string", NULL};
     PyObject *name_obj = NULL;
-    PyObject *data_obj = NULL;
-    Py_buffer view;
+    Py_buffer view = { 0 };
     char *nameStr;
     const EVP_MD *digest;
 
-    if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:HASH", kwlist,
-                                     &name_obj, &data_obj)) {
+    if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|s*:HASH", kwlist,
+                                     &name_obj, &view)) {
         return -1;
     }
 
-    if (data_obj)
-        GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view, -1);
-
     if (!PyArg_Parse(name_obj, "s", &nameStr)) {
         PyErr_SetString(PyExc_TypeError, "name must be a string");
-        if (data_obj)
-            PyBuffer_Release(&view);
+	PyBuffer_Release(&view);
         return -1;
     }
 
     digest = EVP_get_digestbyname(nameStr);
     if (!digest) {
         PyErr_SetString(PyExc_ValueError, "unknown hash function");
-        if (data_obj)
-            PyBuffer_Release(&view);
+	PyBuffer_Release(&view);
         return -1;
     }
     EVP_DigestInit(&self->ctx, digest);
@@ -346,7 +335,7 @@
     self->name = name_obj;
     Py_INCREF(self->name);
 
-    if (data_obj) {
+    if (view.obj) {
         if (view.len >= HASHLIB_GIL_MINSIZE) {
             Py_BEGIN_ALLOW_THREADS
             EVP_hash(self, view.buf, view.len);
@@ -471,14 +460,13 @@
 {
     static char *kwlist[] = {"name", "string", NULL};
     PyObject *name_obj = NULL;
-    PyObject *data_obj = NULL;
     Py_buffer view = { 0 };
     PyObject *ret_obj;
     char *name;
     const EVP_MD *digest;
 
-    if (!PyArg_ParseTupleAndKeywords(args, kwdict, "O|O:new", kwlist,
-                                     &name_obj, &data_obj)) {
+    if (!PyArg_ParseTupleAndKeywords(args, kwdict, "O|s*:new", kwlist,
+                                     &name_obj, &view)) {
         return NULL;
     }
 
@@ -487,16 +475,12 @@
         return NULL;
     }
 
-    if (data_obj)
-        GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view, NULL);
-
     digest = EVP_get_digestbyname(name);
 
     ret_obj = EVPnew(name_obj, digest, NULL, (unsigned char*)view.buf,
-                        Py_SAFE_DOWNCAST(view.len, Py_ssize_t, unsigned int));
+		     view.len);
+    PyBuffer_Release(&view);
 
-    if (data_obj)
-        PyBuffer_Release(&view);
     return ret_obj;
 }
 
@@ -511,26 +495,19 @@
     static PyObject * \
     EVP_new_ ## NAME (PyObject *self, PyObject *args) \
     { \
-        PyObject *data_obj = NULL; \
         Py_buffer view = { 0 }; \
         PyObject *ret_obj; \
      \
-        if (!PyArg_ParseTuple(args, "|O:" #NAME , &data_obj)) { \
+        if (!PyArg_ParseTuple(args, "|s*:" #NAME , &view)) { \
             return NULL; \
         } \
      \
-        if (data_obj) \
-            GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view, NULL); \
-     \
-        ret_obj = EVPnew( \
-                    CONST_ ## NAME ## _name_obj, \
-                    NULL, \
-                    CONST_new_ ## NAME ## _ctx_p, \
-                    (unsigned char*)view.buf, \
-                    Py_SAFE_DOWNCAST(view.len, Py_ssize_t, unsigned int)); \
-     \
-        if (data_obj) \
-            PyBuffer_Release(&view); \
+	ret_obj = EVPnew( \
+		    CONST_ ## NAME ## _name_obj, \
+		    NULL, \
+		    CONST_new_ ## NAME ## _ctx_p, \
+		    (unsigned char*)view.buf, view.len); \
+	PyBuffer_Release(&view); \
         return ret_obj; \
     }
 

Modified: python/trunk/Modules/md5module.c
==============================================================================
--- python/trunk/Modules/md5module.c	(original)
+++ python/trunk/Modules/md5module.c	Sat Jan  2 23:28:48 2010
@@ -12,7 +12,6 @@
 #include "Python.h"
 #include "structmember.h"
 #include "md5.h"
-#include "hashlib.h"
 
 typedef struct {
 	PyObject_HEAD
@@ -51,20 +50,16 @@
 static PyObject *
 md5_update(md5object *self, PyObject *args)
 {
-	PyObject *data_obj;
 	Py_buffer view;
 
-	if (!PyArg_ParseTuple(args, "O:update", &data_obj))
+	if (!PyArg_ParseTuple(args, "s*:update", &view))
 		return NULL;
 
-	GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view, NULL);
-
 	md5_append(&self->md5, (unsigned char*)view.buf,
 		   Py_SAFE_DOWNCAST(view.len, Py_ssize_t, unsigned int));
 
 	PyBuffer_Release(&view);
-	Py_INCREF(Py_None);
-	return Py_None;
+	Py_RETURN_NONE;
 }
 
 PyDoc_STRVAR(update_doc,
@@ -266,26 +261,21 @@
 MD5_new(PyObject *self, PyObject *args)
 {
 	md5object *md5p;
-	PyObject *data_obj = NULL;
-	Py_buffer view;
+	Py_buffer view = { 0 };
 
-	if (!PyArg_ParseTuple(args, "|O:new", &data_obj))
+	if (!PyArg_ParseTuple(args, "|s*:new", &view))
 		return NULL;
 
-	if (data_obj)
-		GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view, NULL);
-
 	if ((md5p = newmd5object()) == NULL) {
-		if (data_obj)
-			PyBuffer_Release(&view);
+		PyBuffer_Release(&view);
 		return NULL;
 	}
 
-	if (data_obj) {
+	if (view.len > 0) {
 		md5_append(&md5p->md5, (unsigned char*)view.buf,
 		       Py_SAFE_DOWNCAST(view.len, Py_ssize_t, unsigned int));
-		PyBuffer_Release(&view);
 	}
+	PyBuffer_Release(&view);
 	
 	return (PyObject *)md5p;
 }

Modified: python/trunk/Modules/sha256module.c
==============================================================================
--- python/trunk/Modules/sha256module.c	(original)
+++ python/trunk/Modules/sha256module.c	Sat Jan  2 23:28:48 2010
@@ -18,7 +18,6 @@
 
 #include "Python.h"
 #include "structmember.h"
-#include "hashlib.h"
 
 
 /* Endianness testing and definitions */
@@ -481,19 +480,15 @@
 static PyObject *
 SHA256_update(SHAobject *self, PyObject *args)
 {
-    PyObject *obj;
     Py_buffer buf;
 
-    if (!PyArg_ParseTuple(args, "O:update", &obj))
+    if (!PyArg_ParseTuple(args, "s*:update", &buf))
         return NULL;
 
-    GET_BUFFER_VIEW_OR_ERROUT(obj, &buf, NULL);
-
     sha_update(self, buf.buf, buf.len);
 
     PyBuffer_Release(&buf);
-    Py_INCREF(Py_None);
-    return Py_None;
+    Py_RETURN_NONE;
 }
 
 static PyMethodDef SHA_methods[] = {
@@ -618,20 +613,15 @@
 {
     static char *kwlist[] = {"string", NULL};
     SHAobject *new;
-    PyObject *data_obj = NULL;
-    Py_buffer buf;
+    Py_buffer buf = { 0 };
 
-    if (!PyArg_ParseTupleAndKeywords(args, kwdict, "|O:new", kwlist,
-                                     &data_obj)) {
+    if (!PyArg_ParseTupleAndKeywords(args, kwdict, "|s*:new", kwlist,
+                                     &buf)) {
         return NULL;
     }
 
-    if (data_obj)
-        GET_BUFFER_VIEW_OR_ERROUT(data_obj, &buf, NULL);
-
     if ((new = newSHA256object()) == NULL) {
-        if (data_obj)
-            PyBuffer_Release(&buf);
+	PyBuffer_Release(&buf);
         return NULL;
     }
 
@@ -639,14 +629,13 @@
 
     if (PyErr_Occurred()) {
         Py_DECREF(new);
-        if (data_obj)
-            PyBuffer_Release(&buf);
+	PyBuffer_Release(&buf);
         return NULL;
     }
-    if (data_obj) {
+    if (buf.len > 0) {
         sha_update(new, buf.buf, buf.len);
-        PyBuffer_Release(&buf);
     }
+    PyBuffer_Release(&buf);
 
     return (PyObject *)new;
 }
@@ -659,20 +648,15 @@
 {
     static char *kwlist[] = {"string", NULL};
     SHAobject *new;
-    PyObject *data_obj = NULL;
-    Py_buffer buf;
+    Py_buffer buf = { 0 };
 
-    if (!PyArg_ParseTupleAndKeywords(args, kwdict, "|O:new", kwlist,
-                                     &data_obj)) {
+    if (!PyArg_ParseTupleAndKeywords(args, kwdict, "|s*:new", kwlist,
+                                     &buf)) {
         return NULL;
     }
 
-    if (data_obj)
-        GET_BUFFER_VIEW_OR_ERROUT(data_obj, &buf, NULL);
-
     if ((new = newSHA224object()) == NULL) {
-        if (data_obj)
-            PyBuffer_Release(&buf);
+	PyBuffer_Release(&buf);
         return NULL;
     }
 
@@ -680,14 +664,13 @@
 
     if (PyErr_Occurred()) {
         Py_DECREF(new);
-        if (data_obj)
-            PyBuffer_Release(&buf);
+	PyBuffer_Release(&buf);
         return NULL;
     }
-    if (data_obj) {
+    if (buf.len > 0) {
         sha_update(new, buf.buf, buf.len);
-        PyBuffer_Release(&buf);
     }
+    PyBuffer_Release(&buf);
 
     return (PyObject *)new;
 }

Modified: python/trunk/Modules/sha512module.c
==============================================================================
--- python/trunk/Modules/sha512module.c	(original)
+++ python/trunk/Modules/sha512module.c	Sat Jan  2 23:28:48 2010
@@ -18,7 +18,6 @@
 
 #include "Python.h"
 #include "structmember.h"
-#include "hashlib.h"
 
 #ifdef PY_LONG_LONG /* If no PY_LONG_LONG, don't compile anything! */
 
@@ -547,19 +546,15 @@
 static PyObject *
 SHA512_update(SHAobject *self, PyObject *args)
 {
-    PyObject *obj;
     Py_buffer buf;
 
-    if (!PyArg_ParseTuple(args, "O:update", &obj))
+    if (!PyArg_ParseTuple(args, "s*:update", &buf))
         return NULL;
 
-    GET_BUFFER_VIEW_OR_ERROUT(obj, &buf, NULL);
-
     sha512_update(self, buf.buf, buf.len);
 
     PyBuffer_Release(&buf);
-    Py_INCREF(Py_None);
-    return Py_None;
+    Py_RETURN_NONE;
 }
 
 static PyMethodDef SHA_methods[] = {
@@ -684,20 +679,15 @@
 {
     static char *kwlist[] = {"string", NULL};
     SHAobject *new;
-    PyObject *data_obj = NULL;
-    Py_buffer buf;
+    Py_buffer buf = { 0 };
 
-    if (!PyArg_ParseTupleAndKeywords(args, kwdict, "|O:new", kwlist,
-                                     &data_obj)) {
+    if (!PyArg_ParseTupleAndKeywords(args, kwdict, "|s*:new", kwlist,
+                                     &buf)) {
         return NULL;
     }
 
-    if (data_obj)
-        GET_BUFFER_VIEW_OR_ERROUT(data_obj, &buf, NULL);
-
     if ((new = newSHA512object()) == NULL) {
-        if (data_obj)
-            PyBuffer_Release(&buf);
+	PyBuffer_Release(&buf);
         return NULL;
     }
 
@@ -705,14 +695,13 @@
 
     if (PyErr_Occurred()) {
         Py_DECREF(new);
-        if (data_obj)
-            PyBuffer_Release(&buf);
+	PyBuffer_Release(&buf);
         return NULL;
     }
-    if (data_obj) {
+    if (buf.len > 0) {
         sha512_update(new, buf.buf, buf.len);
-        PyBuffer_Release(&buf);
     }
+    PyBuffer_Release(&buf);
 
     return (PyObject *)new;
 }
@@ -725,20 +714,15 @@
 {
     static char *kwlist[] = {"string", NULL};
     SHAobject *new;
-    PyObject *data_obj = NULL;
-    Py_buffer buf;
+    Py_buffer buf = { 0 };
 
-    if (!PyArg_ParseTupleAndKeywords(args, kwdict, "|O:new", kwlist,
-                                     &data_obj)) {
+    if (!PyArg_ParseTupleAndKeywords(args, kwdict, "|s*:new", kwlist,
+                                     &buf)) {
         return NULL;
     }
 
-    if (data_obj)
-        GET_BUFFER_VIEW_OR_ERROUT(data_obj, &buf, NULL);
-
     if ((new = newSHA384object()) == NULL) {
-        if (data_obj)
-            PyBuffer_Release(&buf);
+	PyBuffer_Release(&buf);
         return NULL;
     }
 
@@ -746,14 +730,13 @@
 
     if (PyErr_Occurred()) {
         Py_DECREF(new);
-        if (data_obj)
-            PyBuffer_Release(&buf);
+	PyBuffer_Release(&buf);
         return NULL;
     }
-    if (data_obj) {
+    if (buf.len > 0) {
         sha512_update(new, buf.buf, buf.len);
-        PyBuffer_Release(&buf);
     }
+    PyBuffer_Release(&buf);
 
     return (PyObject *)new;
 }

Modified: python/trunk/Modules/shamodule.c
==============================================================================
--- python/trunk/Modules/shamodule.c	(original)
+++ python/trunk/Modules/shamodule.c	Sat Jan  2 23:28:48 2010
@@ -17,7 +17,6 @@
 
 #include "Python.h"
 #include "structmember.h"
-#include "hashlib.h"
 
 
 /* Endianness testing and definitions */
@@ -429,20 +428,16 @@
 static PyObject *
 SHA_update(SHAobject *self, PyObject *args)
 {
-    PyObject *data_obj;
     Py_buffer view;
 
-    if (!PyArg_ParseTuple(args, "O:update", &data_obj))
+    if (!PyArg_ParseTuple(args, "s*:update", &view))
         return NULL;
 
-    GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view, NULL);
-
     sha_update(self, (unsigned char*)view.buf,
                Py_SAFE_DOWNCAST(view.len, Py_ssize_t, unsigned int));
 
     PyBuffer_Release(&view);
-    Py_INCREF(Py_None);
-    return Py_None;
+    Py_RETURN_NONE;
 }
 
 static PyMethodDef SHA_methods[] = {
@@ -540,20 +535,15 @@
 {
     static char *kwlist[] = {"string", NULL};
     SHAobject *new;
-    PyObject *data_obj = NULL;
-    Py_buffer view;
+    Py_buffer view = { 0 };
 
-    if (!PyArg_ParseTupleAndKeywords(args, kwdict, "|O:new", kwlist,
-                                     &data_obj)) {
+    if (!PyArg_ParseTupleAndKeywords(args, kwdict, "|s*:new", kwlist,
+                                     &view)) {
         return NULL;
     }
 
-    if (data_obj)
-        GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view, NULL);
-
     if ((new = newSHAobject()) == NULL) {
-        if (data_obj)
-            PyBuffer_Release(&view);
+	PyBuffer_Release(&view);
         return NULL;
     }
 
@@ -561,15 +551,14 @@
 
     if (PyErr_Occurred()) {
         Py_DECREF(new);
-        if (data_obj)
-            PyBuffer_Release(&view);
+	PyBuffer_Release(&view);
         return NULL;
     }
-    if (data_obj) {
+    if (view.len > 0) {
         sha_update(new, (unsigned char*)view.buf,
                    Py_SAFE_DOWNCAST(view.len, Py_ssize_t, unsigned int));
-        PyBuffer_Release(&view);
     }
+    PyBuffer_Release(&view);
 
     return (PyObject *)new;
 }


More information about the Python-checkins mailing list