Commit a693bba6 authored by Julian Andres Klode's avatar Julian Andres Klode

Drop deprecated uses of Hashes, add hashes attribute

APT uses GetHashStringList() here, but we'll use hashes
for consistency with PackageRecords.
parent 0d70b710
......@@ -1834,25 +1834,8 @@ of the ones provides in Python's :mod:`hashlib` module.
The module provides the two classes :class:`Hashes` and :class:`HashString` for
generic hash support:
.. class:: Hashes(object)
Calculate all supported hashes of the object. *object* may either be a
string, in which cases the hashes of the string are calculated; or a
:class:`file()` object or file descriptor, in which case the hashes of
its contents is calculated. The calculated hashes are then available via
attributes:
.. attribute:: md5
The MD5 hash of the data, as string.
.. attribute:: sha1
The SHA1 hash of the data, as string.
.. attribute:: sha256
The SHA256 hash of the data, as string.
.. autoclass:: Hashes
:members:
.. class:: HashString(type: str[, hash: str])
......
......@@ -62,25 +62,57 @@ static int hashes_init(PyObject *self, PyObject *args, PyObject *kwds)
return 0;
}
static PyObject *hashes_get_hashes(PyObject *self, void*)
{
auto py = CppPyObject_NEW<HashStringList>(nullptr, &PyHashStringList_Type);
py->Object = GetCpp<Hashes>(self).GetHashStringList();
return py;
}
static PyObject *hashes_get_md5(PyObject *self, void*)
{
APT_IGNORE_DEPRECATED_PUSH
if (PyErr_WarnEx(PyExc_DeprecationWarning,
"md5 is deprecated, use hashes instead", 1) == -1)
return NULL;
return CppPyString(GetCpp<Hashes>(self).MD5.Result().Value());
APT_IGNORE_DEPRECATED_POP
}
static PyObject *hashes_get_sha1(PyObject *self, void*)
{
APT_IGNORE_DEPRECATED_PUSH
if (PyErr_WarnEx(PyExc_DeprecationWarning,
"sha1 is deprecated, use hashes instead", 1) == -1)
return NULL;
return CppPyString(GetCpp<Hashes>(self).SHA1.Result().Value());
APT_IGNORE_DEPRECATED_POP
}
static PyObject *hashes_get_sha256(PyObject *self, void*)
{
APT_IGNORE_DEPRECATED_PUSH
if (PyErr_WarnEx(PyExc_DeprecationWarning,
"sha256 is deprecated, use hashes instead", 1) == -1)
return NULL;
return CppPyString(GetCpp<Hashes>(self).SHA256.Result().Value());
APT_IGNORE_DEPRECATED_POP
}
static PyGetSetDef hashes_getset[] = {
{"md5",hashes_get_md5,0,"The MD5Sum of the file as a string."},
{"sha1",hashes_get_sha1,0,"The SHA1Sum of the file as a string."},
{"sha256",hashes_get_sha256,0,"The SHA256Sum of the file as a string."},
{"hashes",hashes_get_hashes,0,
"A :class:`HashStringList` of all hashes.\n\n"
".. versionadded:: 1.1"},
{"md5",hashes_get_md5,0,
"The MD5Sum of the file as a string.\n\n"
".. deprecated:: 1.1"},
{"sha1",hashes_get_sha1,0,
"The SHA1Sum of the file as a string.\n\n"
".. deprecated:: 1.1"},
{"sha256",hashes_get_sha256,0,
"The SHA256Sum of the file as a string.\n\n"
".. deprecated:: 1.1"},
{}
};
......@@ -88,7 +120,7 @@ static char *hashes_doc =
"Hashes([object: (bytes, file)])\n\n"
"Calculate hashes for the given object. It can be used to create all\n"
"supported hashes for a file.\n\n"
"The parameter 'object' can be a bytestring, an object providing the\n"
"The parameter *object* can be a bytestring, an object providing the\n"
"fileno() method, or an integer describing a file descriptor.";
PyTypeObject PyHashes_Type = {
......
......@@ -12,6 +12,7 @@ functions like md5sum."""
import unittest
import hashlib
import sys
import warnings
import apt_pkg
......@@ -53,15 +54,39 @@ class TestHashes(unittest.TestCase):
def test_bytes(self):
"""hashes: Test apt_pkg.Hashes(bytes)"""
self.assertEqual(self.hashes.md5, self.md5)
self.assertEqual(self.hashes.sha1, self.sha1)
self.assertEqual(self.hashes.sha256, self.sha256)
with warnings.catch_warnings(record=True) as caught_warnings:
self.assertEqual(self.hashes.md5, self.md5)
self.assertEqual(self.hashes.sha1, self.sha1)
self.assertEqual(self.hashes.sha256, self.sha256)
self.assertEqual(len(caught_warnings), 3)
self.assertTrue(issubclass(caught_warnings[0].category,
DeprecationWarning))
self.assertTrue(issubclass(caught_warnings[1].category,
DeprecationWarning))
self.assertTrue(issubclass(caught_warnings[2].category,
DeprecationWarning))
def test_file(self):
"""hashes: Test apt_pkg.Hashes(file)."""
self.assertEqual(self.hashes.md5, self.fhashes.md5)
self.assertEqual(self.hashes.sha1, self.fhashes.sha1)
self.assertEqual(self.hashes.sha256, self.fhashes.sha256)
with warnings.catch_warnings(record=True) as caught_warnings:
self.assertEqual(self.hashes.md5, self.fhashes.md5)
self.assertEqual(self.hashes.sha1, self.fhashes.sha1)
self.assertEqual(self.hashes.sha256, self.fhashes.sha256)
self.assertEqual(len(caught_warnings), 6)
self.assertTrue(issubclass(caught_warnings[0].category,
DeprecationWarning))
self.assertTrue(issubclass(caught_warnings[1].category,
DeprecationWarning))
self.assertTrue(issubclass(caught_warnings[2].category,
DeprecationWarning))
self.assertTrue(issubclass(caught_warnings[3].category,
DeprecationWarning))
self.assertTrue(issubclass(caught_warnings[4].category,
DeprecationWarning))
self.assertTrue(issubclass(caught_warnings[5].category,
DeprecationWarning))
def test_unicode(self):
"""hashes: Test apt_pkg.Hashes(unicode)."""
......@@ -84,9 +109,18 @@ class TestHashString(unittest.TestCase):
"""Prepare the test by reading the file."""
self.file = open(apt_pkg.__file__)
self.hashes = apt_pkg.Hashes(self.file)
self.md5 = apt_pkg.HashString("MD5Sum", self.hashes.md5)
self.sha1 = apt_pkg.HashString("SHA1", self.hashes.sha1)
self.sha256 = apt_pkg.HashString("SHA256", self.hashes.sha256)
with warnings.catch_warnings(record=True) as caught_warnings:
self.md5 = apt_pkg.HashString("MD5Sum", self.hashes.md5)
self.sha1 = apt_pkg.HashString("SHA1", self.hashes.sha1)
self.sha256 = apt_pkg.HashString("SHA256", self.hashes.sha256)
self.assertEqual(len(caught_warnings), 3)
self.assertTrue(issubclass(caught_warnings[0].category,
DeprecationWarning))
self.assertTrue(issubclass(caught_warnings[1].category,
DeprecationWarning))
self.assertTrue(issubclass(caught_warnings[2].category,
DeprecationWarning))
def tearDown(self):
"""Cleanup, Close the file object used for the tests."""
......@@ -94,17 +128,21 @@ class TestHashString(unittest.TestCase):
def test_md5(self):
"""hashes: Test apt_pkg.HashString().md5"""
self.assertEqual("MD5Sum:%s" % self.hashes.md5, str(self.md5))
with warnings.catch_warnings(record=True):
self.assertEqual("MD5Sum:%s" % self.hashes.md5, str(self.md5))
self.assertTrue(self.md5.verify_file(apt_pkg.__file__))
def test_sha1(self):
"""hashes: Test apt_pkg.HashString().sha1"""
self.assertEqual("SHA1:%s" % self.hashes.sha1, str(self.sha1))
with warnings.catch_warnings(record=True):
self.assertEqual("SHA1:%s" % self.hashes.sha1, str(self.sha1))
self.assertTrue(self.sha1.verify_file(apt_pkg.__file__))
def test_sha256(self):
"""hashes: Test apt_pkg.HashString().sha256"""
self.assertEqual("SHA256:%s" % self.hashes.sha256, str(self.sha256))
with warnings.catch_warnings(record=True):
self.assertEqual("SHA256:%s" % self.hashes.sha256,
str(self.sha256))
self.assertTrue(self.sha256.verify_file(apt_pkg.__file__))
def test_wrong(self):
......@@ -167,8 +205,9 @@ class TestHashStringList(unittest.TestCase):
def test_verify_file(self):
with open(apt_pkg.__file__) as fobj:
hashes = apt_pkg.Hashes(fobj)
sha1 = apt_pkg.HashString("SHA1", hashes.sha1)
sha256 = apt_pkg.HashString("SHA256", hashes.sha256)
with warnings.catch_warnings(record=True):
sha1 = apt_pkg.HashString("SHA1", hashes.sha1)
sha256 = apt_pkg.HashString("SHA256", hashes.sha256)
hsl = apt_pkg.HashStringList()
hsl.append(sha1)
......@@ -182,5 +221,10 @@ class TestHashStringList(unittest.TestCase):
self.assertFalse(hsl.verify_file(apt_pkg.__file__))
hsl2 = hashes.hashes
self.assertIsInstance(hsl2, apt_pkg.HashStringList)
self.assertGreater(len(hsl2), 0)
self.assertTrue(hsl2.verify_file(apt_pkg.__file__))
if __name__ == "__main__":
unittest.main()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment