From: <pj...@us...> - 2009-09-09 04:16:57
|
Revision: 6771 http://jython.svn.sourceforge.net/jython/?rev=6771&view=rev Author: pjenvey Date: 2009-09-09 04:16:49 +0000 (Wed, 09 Sep 2009) Log Message: ----------- allow hashlib updating from arrays fixes #1443 Modified Paths: -------------- trunk/jython/Lib/test/test_hashlib_jy.py trunk/jython/NEWS trunk/jython/src/org/python/modules/_hashlib.java Modified: trunk/jython/Lib/test/test_hashlib_jy.py =================================================================== --- trunk/jython/Lib/test/test_hashlib_jy.py 2009-09-09 03:47:22 UTC (rev 6770) +++ trunk/jython/Lib/test/test_hashlib_jy.py 2009-09-09 04:16:49 UTC (rev 6771) @@ -5,6 +5,7 @@ """ import hashlib import unittest +from array import array from test import test_support class HashlibTestCase(unittest.TestCase): @@ -14,7 +15,14 @@ 'acbd18db4cc2f85cedef654fccc4a4d8') self.assertRaises(UnicodeEncodeError, hashlib.md5, u'Gráin amháiñ') + def test_array(self): + self.assertEqual(hashlib.sha1(array('c', 'hovercraft')).hexdigest(), + '496df4d8de2c71973d7e917c4fbe57e6ad46d738') + intarray = array('i', range(5)) + self.assertEqual(hashlib.sha1(intarray).hexdigest(), + hashlib.sha1(intarray.tostring()).hexdigest()) + def test_main(): test_support.run_unittest(HashlibTestCase) Modified: trunk/jython/NEWS =================================================================== --- trunk/jython/NEWS 2009-09-09 03:47:22 UTC (rev 6770) +++ trunk/jython/NEWS 2009-09-09 04:16:49 UTC (rev 6771) @@ -7,6 +7,7 @@ - [ 1425 ] distutils/util.py assumes too much posix - [ 1457 ] Cannot write an array in a file opened in r+b mode. - [ 1382 ] __cmp__ on certain types raises ArrayStoreException + - [ 1443 ] Can't update() hashlib.sha1() with array.array('c') Jython 2.5.1rc1 New Features Modified: trunk/jython/src/org/python/modules/_hashlib.java =================================================================== --- trunk/jython/src/org/python/modules/_hashlib.java 2009-09-09 03:47:22 UTC (rev 6770) +++ trunk/jython/src/org/python/modules/_hashlib.java 2009-09-09 04:16:49 UTC (rev 6771) @@ -8,6 +8,7 @@ import org.python.core.ClassDictInit; import org.python.core.Py; +import org.python.core.PyArray; import org.python.core.PyObject; import org.python.core.PyString; import org.python.core.PyType; @@ -168,15 +169,19 @@ @ExposedMethod final void HASH_update(PyObject obj) { - if (!(obj instanceof PyString)) { + String string; + if (obj instanceof PyUnicode) { + string = ((PyUnicode)obj).encode(); + } else if (obj instanceof PyString) { + string = obj.toString(); + } else if (obj instanceof PyArray) { + string = ((PyArray)obj).tostring(); + } + else { throw Py.TypeError("update() argument 1 must be string or read-only buffer, not " + obj.getType().fastGetName()); } - if (obj instanceof PyUnicode) { - obj = obj.__str__(); - } - byte[] bytes = ((PyString)obj).toBytes(); - digest.update(bytes); + digest.update(StringUtil.toBytes(string)); } public PyObject digest() { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |