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.
This commit is contained in:
Gregory P. Smith 2010-01-02 22:28:48 +00:00
parent c2fa18ca20
commit 443ec6875f
6 changed files with 86 additions and 159 deletions

View File

@ -71,18 +71,23 @@ class HashLibTestCase(unittest.TestCase):
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')
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_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')
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')

View File

@ -15,7 +15,6 @@
#include "Python.h"
#include "structmember.h"
#include "hashlib.h"
#ifdef WITH_THREAD
#include "pythread.h"
@ -218,14 +217,11 @@ PyDoc_STRVAR(EVP_update__doc__,
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_update(EVPobject *self, PyObject *args)
EVP_hash(self, view.buf, view.len);
PyThread_release_lock(self->lock);
Py_END_ALLOW_THREADS
} else {
}
else
#endif
{
EVP_hash(self, view.buf, view.len);
}
#else
EVP_hash(self, view.buf, view.len);
#endif
PyBuffer_Release(&view);
Py_INCREF(Py_None);
return Py_None;
Py_RETURN_NONE;
}
static PyMethodDef EVP_methods[] = {
@ -314,31 +309,25 @@ EVP_tp_init(EVPobject *self, PyObject *args, PyObject *kwds)
{
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 @@ EVP_tp_init(EVPobject *self, PyObject *args, PyObject *kwds)
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 @@ EVP_new(PyObject *self, PyObject *args, PyObject *kwdict)
{
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 @@ EVP_new(PyObject *self, PyObject *args, PyObject *kwdict)
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 @@ EVP_new(PyObject *self, PyObject *args, PyObject *kwdict)
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; \
}

View File

@ -12,7 +12,6 @@
#include "Python.h"
#include "structmember.h"
#include "md5.h"
#include "hashlib.h"
typedef struct {
PyObject_HEAD
@ -51,20 +50,16 @@ md5_dealloc(md5object *md5p)
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 @@ static PyObject *
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;
}

View File

@ -18,7 +18,6 @@
#include "Python.h"
#include "structmember.h"
#include "hashlib.h"
/* Endianness testing and definitions */
@ -481,19 +480,15 @@ PyDoc_STRVAR(SHA256_update__doc__,
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 @@ SHA256_new(PyObject *self, PyObject *args, PyObject *kwdict)
{
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 @@ SHA256_new(PyObject *self, PyObject *args, PyObject *kwdict)
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 @@ SHA224_new(PyObject *self, PyObject *args, PyObject *kwdict)
{
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 @@ SHA224_new(PyObject *self, PyObject *args, PyObject *kwdict)
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;
}

View File

@ -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 @@ PyDoc_STRVAR(SHA512_update__doc__,
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 @@ SHA512_new(PyObject *self, PyObject *args, PyObject *kwdict)
{
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 @@ SHA512_new(PyObject *self, PyObject *args, PyObject *kwdict)
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 @@ SHA384_new(PyObject *self, PyObject *args, PyObject *kwdict)
{
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 @@ SHA384_new(PyObject *self, PyObject *args, PyObject *kwdict)
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;
}

View File

@ -17,7 +17,6 @@
#include "Python.h"
#include "structmember.h"
#include "hashlib.h"
/* Endianness testing and definitions */
@ -429,20 +428,16 @@ PyDoc_STRVAR(SHA_update__doc__,
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 @@ SHA_new(PyObject *self, PyObject *args, PyObject *kwdict)
{
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 @@ SHA_new(PyObject *self, PyObject *args, PyObject *kwdict)
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;
}