← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] ~cjwatson/launchpad:unsixify-hexdigest into launchpad:master

 

Colin Watson has proposed merging ~cjwatson/launchpad:unsixify-hexdigest into launchpad:master.

Commit message:
Stop explicitly converting hash.hexdigest() to text

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

For more details, see:
https://code.launchpad.net/~cjwatson/launchpad/+git/launchpad/+merge/420748

On Python 3, the `hexdigest` method of hash objects already returns a Unicode string, so there's no need to explicitly convert it.  I also dropped explicit encoding parameters to `str.encode()` where the default encoding of UTF-8 is sufficient.
-- 
Your team Launchpad code reviewers is requested to review the proposed merge of ~cjwatson/launchpad:unsixify-hexdigest into launchpad:master.
diff --git a/lib/lp/code/browser/tests/test_branchmergeproposal.py b/lib/lp/code/browser/tests/test_branchmergeproposal.py
index f774c48..0309499 100644
--- a/lib/lp/code/browser/tests/test_branchmergeproposal.py
+++ b/lib/lp/code/browser/tests/test_branchmergeproposal.py
@@ -20,7 +20,6 @@ from lazr.lifecycle.event import ObjectModifiedEvent
 from lazr.restful.interfaces import IJSONRequestCache
 import pytz
 import simplejson
-import six
 from soupmatchers import (
     HTMLContains,
     Tag,
@@ -1589,7 +1588,7 @@ class TestBranchMergeProposalView(TestCaseWithFactory):
             date_created=review_date)
         self.useFixture(GitHostingFixture(log=[
             {
-                'sha1': six.ensure_text(hashlib.sha1(b'0').hexdigest()),
+                'sha1': hashlib.sha1(b'0').hexdigest(),
                 'message': '0',
                 'author': {
                     'name': author.display_name,
@@ -1660,7 +1659,7 @@ class TestBranchMergeProposalView(TestCaseWithFactory):
 
     def test_revisionStatusReports_display(self):
         bmp = self.factory.makeBranchMergeProposalForGit()
-        sha1 = six.ensure_text(hashlib.sha1(b'0').hexdigest())
+        sha1 = hashlib.sha1(b'0').hexdigest()
         commit_date = datetime(2015, 1, 1, tzinfo=pytz.UTC)
         report1 = self.factory.makeRevisionStatusReport(
             user=bmp.source_git_repository.owner,
@@ -1722,7 +1721,7 @@ class TestBranchMergeProposalView(TestCaseWithFactory):
         # Even if the source Git ref has been deleted, we still know its tip
         # SHA-1 and can ask the repository for its unmerged commits.
         bmp = self.factory.makeBranchMergeProposalForGit()
-        sha1 = six.ensure_text(hashlib.sha1(b'0').hexdigest())
+        sha1 = hashlib.sha1(b'0').hexdigest()
         commit_date = datetime(2015, 1, 1, tzinfo=pytz.UTC)
         self.useFixture(GitHostingFixture(log=[
             {
diff --git a/lib/lp/code/browser/tests/test_gitref.py b/lib/lp/code/browser/tests/test_gitref.py
index 85864ea..a7dcf14 100644
--- a/lib/lp/code/browser/tests/test_gitref.py
+++ b/lib/lp/code/browser/tests/test_gitref.py
@@ -10,7 +10,6 @@ from textwrap import dedent
 
 from fixtures import FakeLogger
 import pytz
-import six
 import soupmatchers
 from storm.store import Store
 from testtools.matchers import (
@@ -729,8 +728,7 @@ class TestGitRefView(BrowserTestCase):
             datetime(2015, 1, day + 1, tzinfo=pytz.UTC) for day in range(5)]
         return [
             {
-                "sha1": six.ensure_text(hashlib.sha1(
-                    str(i).encode("ASCII")).hexdigest()),
+                "sha1": hashlib.sha1(str(i).encode()).hexdigest(),
                 "message": "Commit %d" % i,
                 "author": {
                     "name": authors[i].display_name,
@@ -742,9 +740,8 @@ class TestGitRefView(BrowserTestCase):
                     "email": author_emails[i],
                     "time": int(seconds_since_epoch(dates[i])),
                     },
-                "parents": [six.ensure_text(
-                    hashlib.sha1(str(i - 1).encode("ASCII")).hexdigest())],
-                "tree": six.ensure_text(hashlib.sha1(b"").hexdigest()),
+                "parents": [hashlib.sha1(str(i - 1).encode()).hexdigest()],
+                "tree": hashlib.sha1(b"").hexdigest(),
                 }
             for i in range(5)]
 
@@ -819,8 +816,7 @@ class TestGitRefView(BrowserTestCase):
         self.scanRef(ref, log[-1], blobs={".launchpad.yaml": configuration})
         mp = self.factory.makeBranchMergeProposalForGit(target_ref=ref)
         merged_tip = dict(log[-1])
-        merged_tip["sha1"] = six.ensure_text(
-            hashlib.sha1(b"merged").hexdigest())
+        merged_tip["sha1"] = hashlib.sha1(b"merged").hexdigest()
         self.scanRef(mp.merge_source, merged_tip)
         mp.markAsMerged(merged_revision_id=log[0]["sha1"])
         view = create_initialized_view(ref, "+index")
@@ -862,8 +858,7 @@ class TestGitRefView(BrowserTestCase):
         self.scanRef(ref, log[-1], blobs={".launchpad.yaml": configuration})
         mp = self.factory.makeBranchMergeProposalForGit(target_ref=ref)
         merged_tip = dict(log[-1])
-        merged_tip["sha1"] = six.ensure_text(
-            hashlib.sha1(b"merged").hexdigest())
+        merged_tip["sha1"] = hashlib.sha1(b"merged").hexdigest()
         self.scanRef(mp.merge_source, merged_tip)
         mp.markAsMerged(merged_revision_id=log[0]["sha1"])
         mp.source_git_repository.removeRefs([mp.source_git_path])
diff --git a/lib/lp/code/model/tests/test_branchmergeproposal.py b/lib/lp/code/model/tests/test_branchmergeproposal.py
index fd51edf..78510f8 100644
--- a/lib/lp/code/model/tests/test_branchmergeproposal.py
+++ b/lib/lp/code/model/tests/test_branchmergeproposal.py
@@ -15,7 +15,6 @@ from fixtures import FakeLogger
 from lazr.lifecycle.event import ObjectCreatedEvent
 from lazr.restfulclient.errors import BadRequest
 from pytz import UTC
-import six
 from storm.locals import Store
 from testscenarios import (
     load_tests_apply_scenarios,
@@ -1588,16 +1587,16 @@ class TestBranchMergeProposalBugs(WithVCSScenarios, TestCaseWithFactory):
                 "message": "Commit 1\n\nLP: #%d" % bugs[0].id,
                 },
             {
-                "sha1": six.ensure_text(hashlib.sha1(b"1").hexdigest()),
+                "sha1": hashlib.sha1(b"1").hexdigest(),
                 # Will not be matched.
                 "message": "Commit 2; see LP #%d" % bugs[1].id,
                 },
             {
-                "sha1": six.ensure_text(hashlib.sha1(b"2").hexdigest()),
+                "sha1": hashlib.sha1(b"2").hexdigest(),
                 "message": "Commit 3; LP: #%d" % bugs[2].id,
                 },
             {
-                "sha1": six.ensure_text(hashlib.sha1(b"3").hexdigest()),
+                "sha1": hashlib.sha1(b"3").hexdigest(),
                 # Non-existent bug ID will not be returned.
                 "message": "Non-existent bug; LP: #%d" % (bugs[2].id + 100),
                 },
@@ -1617,8 +1616,7 @@ class TestBranchMergeProposalBugs(WithVCSScenarios, TestCaseWithFactory):
         """Set up a fake log response referring to the given bugs."""
         self.hosting_fixture.getLog.result = [
             {
-                "sha1": six.ensure_text(hashlib.sha1(
-                    str(i).encode("ASCII")).hexdigest()),
+                "sha1": hashlib.sha1(str(i).encode()).hexdigest(),
                 "message": "LP: #%d" % bug.id,
                 }
             for i, bug in enumerate(bugs)]
diff --git a/lib/lp/code/model/tests/test_branchmergeproposaljobs.py b/lib/lp/code/model/tests/test_branchmergeproposaljobs.py
index 44f04fc..69269a9 100644
--- a/lib/lp/code/model/tests/test_branchmergeproposaljobs.py
+++ b/lib/lp/code/model/tests/test_branchmergeproposaljobs.py
@@ -275,7 +275,7 @@ class TestUpdatePreviewDiffJob(DiffTestCase):
         committer = self.factory.makePerson()
         self.hosting_fixture.getLog.result = [
             {
-                "sha1": six.ensure_text(hashlib.sha1(b"tip").hexdigest()),
+                "sha1": hashlib.sha1(b"tip").hexdigest(),
                 "message": "Fix upside-down messages\n\nLP: #%d" % bug.id,
                 "committer": {
                     "name": committer.display_name,
diff --git a/lib/lp/code/model/tests/test_gitjob.py b/lib/lp/code/model/tests/test_gitjob.py
index 7392d4c..06c4f21 100644
--- a/lib/lp/code/model/tests/test_gitjob.py
+++ b/lib/lp/code/model/tests/test_gitjob.py
@@ -13,7 +13,6 @@ from unittest import mock
 from fixtures import FakeLogger
 from lazr.lifecycle.snapshot import Snapshot
 import pytz
-import six
 from testtools.matchers import (
     ContainsDict,
     Equals,
@@ -110,8 +109,7 @@ class TestGitRefScanJob(TestCaseWithFactory):
     def makeFakeCommits(author, author_date_gen, paths):
         dates = {path: next(author_date_gen) for path in paths}
         return [{
-            "sha1": six.ensure_text(hashlib.sha1(
-                path.encode("UTF-8")).hexdigest()),
+            "sha1": hashlib.sha1(path.encode()).hexdigest(),
             "message": "tip of %s" % path,
             "author": {
                 "name": author.displayname,
@@ -124,7 +122,7 @@ class TestGitRefScanJob(TestCaseWithFactory):
                 "time": int(seconds_since_epoch(dates[path])),
                 },
             "parents": [],
-            "tree": six.ensure_text(hashlib.sha1(b"").hexdigest()),
+            "tree": hashlib.sha1(b"").hexdigest(),
             } for path in paths]
 
     def assertRefsMatch(self, refs, repository, paths):
@@ -132,8 +130,7 @@ class TestGitRefScanJob(TestCaseWithFactory):
             MatchesStructure.byEquality(
                 repository=repository,
                 path=path,
-                commit_sha1=six.ensure_text(hashlib.sha1(
-                    path.encode("UTF-8")).hexdigest()),
+                commit_sha1=hashlib.sha1(path.encode()).hexdigest(),
                 object_type=GitObjectType.COMMIT)
             for path in paths]
         self.assertThat(refs, MatchesSetwise(*matchers))
diff --git a/lib/lp/code/model/tests/test_gitref.py b/lib/lp/code/model/tests/test_gitref.py
index f18287a..f48f21d 100644
--- a/lib/lp/code/model/tests/test_gitref.py
+++ b/lib/lp/code/model/tests/test_gitref.py
@@ -13,7 +13,6 @@ import json
 from breezy import urlutils
 import pytz
 import responses
-import six
 from storm.store import Store
 from testtools.matchers import (
     ContainsDict,
@@ -169,8 +168,8 @@ class TestGitRefGetCommits(TestCaseWithFactory):
             datetime(2015, 1, 1, 0, 0, 0, tzinfo=pytz.UTC),
             datetime(2015, 1, 2, 0, 0, 0, tzinfo=pytz.UTC),
             ]
-        self.sha1_tip = six.ensure_text(hashlib.sha1(b"tip").hexdigest())
-        self.sha1_root = six.ensure_text(hashlib.sha1(b"root").hexdigest())
+        self.sha1_tip = hashlib.sha1(b"tip").hexdigest()
+        self.sha1_root = hashlib.sha1(b"root").hexdigest()
         self.log = [
             {
                 "sha1": self.sha1_tip,
@@ -186,7 +185,7 @@ class TestGitRefGetCommits(TestCaseWithFactory):
                     "time": int(seconds_since_epoch(self.dates[1])),
                     },
                 "parents": [self.sha1_root],
-                "tree": six.ensure_text(hashlib.sha1(b"").hexdigest()),
+                "tree": hashlib.sha1(b"").hexdigest(),
                 },
             {
                 "sha1": self.sha1_root,
@@ -202,7 +201,7 @@ class TestGitRefGetCommits(TestCaseWithFactory):
                     "time": int(seconds_since_epoch(self.dates[0])),
                     },
                 "parents": [],
-                "tree": six.ensure_text(hashlib.sha1(b"").hexdigest()),
+                "tree": hashlib.sha1(b"").hexdigest(),
                 },
             ]
         self.hosting_fixture = self.useFixture(GitHostingFixture(log=self.log))
@@ -774,7 +773,7 @@ class TestGitRefWebservice(TestCaseWithFactory):
         self.assertThat(result["repository_link"], EndsWith(repository_url))
         self.assertEqual("refs/heads/master", result["path"])
         self.assertEqual(
-            six.ensure_text(hashlib.sha1(b"refs/heads/master").hexdigest()),
+            hashlib.sha1(b"refs/heads/master").hexdigest(),
             result["commit_sha1"])
 
     def test_landing_candidates(self):
diff --git a/lib/lp/code/model/tests/test_gitrepository.py b/lib/lp/code/model/tests/test_gitrepository.py
index 6e93fb2..cb0a8e5 100644
--- a/lib/lp/code/model/tests/test_gitrepository.py
+++ b/lib/lp/code/model/tests/test_gitrepository.py
@@ -1816,7 +1816,7 @@ class TestGitRepositoryRefs(TestCaseWithFactory):
 
     def test__convertRefInfo(self):
         # _convertRefInfo converts a valid info dictionary.
-        sha1 = six.ensure_text(hashlib.sha1(b"").hexdigest())
+        sha1 = hashlib.sha1(b"").hexdigest()
         info = {"object": {"sha1": sha1, "type": "commit"}}
         expected_info = {"sha1": sha1, "type": GitObjectType.COMMIT}
         self.assertEqual(expected_info, GitRepository._convertRefInfo(info))
@@ -1861,8 +1861,7 @@ class TestGitRepositoryRefs(TestCaseWithFactory):
             MatchesStructure.byEquality(
                 repository=repository,
                 path=path,
-                commit_sha1=six.ensure_text(hashlib.sha1(
-                    path.encode("UTF-8")).hexdigest()),
+                commit_sha1=hashlib.sha1(path.encode()).hexdigest(),
                 object_type=GitObjectType.COMMIT)
             for path in paths]
         self.assertThat(refs, MatchesSetwise(*matchers))
@@ -2013,13 +2012,11 @@ class TestGitRepositoryRefs(TestCaseWithFactory):
                 "type": GitObjectType.COMMIT,
                 },
             "refs/heads/foo": {
-                "sha1": six.ensure_text(
-                    hashlib.sha1(b"refs/heads/foo").hexdigest()),
+                "sha1": hashlib.sha1(b"refs/heads/foo").hexdigest(),
                 "type": GitObjectType.COMMIT,
                 },
             "refs/tags/1.0": {
-                "sha1": six.ensure_text(
-                    hashlib.sha1(b"refs/heads/master").hexdigest()),
+                "sha1": hashlib.sha1(b"refs/heads/master").hexdigest(),
                 "type": GitObjectType.COMMIT,
                 },
             }
@@ -2030,8 +2027,7 @@ class TestGitRepositoryRefs(TestCaseWithFactory):
         # planRefChanges does not attempt to update refs that point to
         # non-commits.
         repository = self.factory.makeGitRepository()
-        blob_sha1 = six.ensure_text(
-            hashlib.sha1(b"refs/heads/blob").hexdigest())
+        blob_sha1 = hashlib.sha1(b"refs/heads/blob").hexdigest()
         refs_info = {
             "refs/heads/blob": {
                 "sha1": blob_sha1,
@@ -2106,9 +2102,8 @@ class TestGitRepositoryRefs(TestCaseWithFactory):
         # fetchRefCommits fetches detailed tip commit metadata for the
         # requested refs.
         repository = self.factory.makeGitRepository()
-        master_sha1 = six.ensure_text(
-            hashlib.sha1(b"refs/heads/master").hexdigest())
-        foo_sha1 = six.ensure_text(hashlib.sha1(b"refs/heads/foo").hexdigest())
+        master_sha1 = hashlib.sha1(b"refs/heads/master").hexdigest()
+        foo_sha1 = hashlib.sha1(b"refs/heads/foo").hexdigest()
         author = self.factory.makePerson()
         with person_logged_in(author):
             author_email = author.preferredemail.email
@@ -2129,7 +2124,7 @@ class TestGitRepositoryRefs(TestCaseWithFactory):
                     "time": int(seconds_since_epoch(committer_date)),
                     },
                 "parents": [],
-                "tree": six.ensure_text(hashlib.sha1(b"").hexdigest()),
+                "tree": hashlib.sha1(b"").hexdigest(),
                 }]))
         refs = {
             "refs/heads/master": {
@@ -2187,9 +2182,8 @@ class TestGitRepositoryRefs(TestCaseWithFactory):
         # those containing the specified paths, and to return the contents
         # of those paths.
         repository = self.factory.makeGitRepository()
-        master_sha1 = six.ensure_text(
-            hashlib.sha1(b"refs/heads/master").hexdigest())
-        foo_sha1 = six.ensure_text(hashlib.sha1(b"refs/heads/foo").hexdigest())
+        master_sha1 = hashlib.sha1(b"refs/heads/master").hexdigest()
+        foo_sha1 = hashlib.sha1(b"refs/heads/foo").hexdigest()
         author = self.factory.makePerson()
         with person_logged_in(author):
             author_email = author.preferredemail.email
@@ -2204,7 +2198,7 @@ class TestGitRepositoryRefs(TestCaseWithFactory):
                     "time": int(seconds_since_epoch(author_date)),
                     },
                 "parents": [],
-                "tree": six.ensure_text(hashlib.sha1(b"").hexdigest()),
+                "tree": hashlib.sha1(b"").hexdigest(),
                 "blobs": {".launchpad.yaml": b"foo"},
                 }]))
         refs = {
@@ -2272,10 +2266,8 @@ class TestGitRepositoryRefs(TestCaseWithFactory):
         repository.synchroniseRefs(refs_to_upsert, refs_to_remove)
         expected_sha1s = [
             ("refs/heads/master", "1111111111111111111111111111111111111111"),
-            ("refs/heads/foo",
-             six.ensure_text(hashlib.sha1(b"refs/heads/foo").hexdigest())),
-            ("refs/tags/1.0",
-             six.ensure_text(hashlib.sha1(b"refs/heads/master").hexdigest())),
+            ("refs/heads/foo", hashlib.sha1(b"refs/heads/foo").hexdigest()),
+            ("refs/tags/1.0", hashlib.sha1(b"refs/heads/master").hexdigest()),
             ]
         matchers = [
             MatchesStructure.byEquality(
diff --git a/lib/lp/services/oauth/model.py b/lib/lp/services/oauth/model.py
index 2c07b19..d98575b 100644
--- a/lib/lp/services/oauth/model.py
+++ b/lib/lp/services/oauth/model.py
@@ -76,14 +76,8 @@ class OAuthBase:
 
 
 def sha256_digest(data):
-    """Return the SHA-256 hash of some data.
-
-    The returned string is always Unicode, to satisfy Storm.  In Python 3,
-    this is straightforward because hexdigest() returns that anyway, but in
-    Python 2 we must decode.
-    """
-    return six.ensure_text(
-        hashlib.sha256(six.ensure_binary(data)).hexdigest(), encoding='ASCII')
+    """Return the SHA-256 hash of some data."""
+    return hashlib.sha256(six.ensure_binary(data)).hexdigest()
 
 
 @implementer(IOAuthConsumer)
diff --git a/lib/lp/services/webapp/pgsession.py b/lib/lp/services/webapp/pgsession.py
index b23ffbd..b8fc07b 100644
--- a/lib/lp/services/webapp/pgsession.py
+++ b/lib/lp/services/webapp/pgsession.py
@@ -117,9 +117,8 @@ class PGSessionData(PGSessionBase):
     def __init__(self, session_data_container, client_id):
         self.session_data_container = session_data_container
         self.client_id = six.ensure_text(client_id, 'ascii')
-        self.hashed_client_id = six.ensure_text(
-            hashlib.sha256(self.client_id.encode('utf-8')).hexdigest(),
-            'ascii')
+        self.hashed_client_id = (
+            hashlib.sha256(self.client_id.encode()).hexdigest())
         self.lastAccessTime = time.time()
 
         # Update the last access time in the db if it is out of date
diff --git a/lib/lp/testing/factory.py b/lib/lp/testing/factory.py
index 6ae169e..1d104cb 100644
--- a/lib/lp/testing/factory.py
+++ b/lib/lp/testing/factory.py
@@ -1821,8 +1821,7 @@ class BareLaunchpadObjectFactory(ObjectFactory):
             paths = [self.getUniqueUnicode('refs/heads/path')]
         refs_info = {
             path: {
-                "sha1": six.ensure_text(
-                    hashlib.sha1(path.encode('utf-8')).hexdigest()),
+                "sha1": hashlib.sha1(path.encode()).hexdigest(),
                 "type": GitObjectType.COMMIT,
                 }
             for path in paths}