From 64e4af2d05612a5db0f71d4dfe2bfe65c7c5aacf Mon Sep 17 00:00:00 2001 From: Itamar Turner-Trauring Date: Wed, 22 Jul 2020 13:17:49 -0400 Subject: [PATCH] Port to Python 3. --- misc/python3/ratchet-passing | 8 ++++++++ newsfragments/3354.minor | 1 + src/allmydata/hashtree.py | 31 +++++++++++++++++++---------- src/allmydata/test/test_hashtree.py | 6 +++++- src/allmydata/util/_python3.py | 2 ++ 5 files changed, 36 insertions(+), 12 deletions(-) create mode 100644 newsfragments/3354.minor diff --git a/misc/python3/ratchet-passing b/misc/python3/ratchet-passing index 7208bd56e..293733b2b 100644 --- a/misc/python3/ratchet-passing +++ b/misc/python3/ratchet-passing @@ -18,6 +18,14 @@ allmydata.test.test_deferredutil.DeferredUtilTests.test_failure allmydata.test.test_deferredutil.DeferredUtilTests.test_gather_results allmydata.test.test_deferredutil.DeferredUtilTests.test_success allmydata.test.test_deferredutil.DeferredUtilTests.test_wait_for_delayed_calls +allmydata.test.test_hashtree.Complete.test_create +allmydata.test.test_hashtree.Complete.test_dump +allmydata.test.test_hashtree.Complete.test_needed_hashes +allmydata.test.test_hashtree.Incomplete.test_check +allmydata.test.test_hashtree.Incomplete.test_create +allmydata.test.test_hashtree.Incomplete.test_depth_of +allmydata.test.test_hashtree.Incomplete.test_large +allmydata.test.test_hashtree.Incomplete.test_needed_hashes allmydata.test.test_hashutil.HashUtilTests.test_chk allmydata.test.test_hashutil.HashUtilTests.test_hashers allmydata.test.test_hashutil.HashUtilTests.test_known_answers diff --git a/newsfragments/3354.minor b/newsfragments/3354.minor new file mode 100644 index 000000000..8b1378917 --- /dev/null +++ b/newsfragments/3354.minor @@ -0,0 +1 @@ + diff --git a/src/allmydata/hashtree.py b/src/allmydata/hashtree.py index 576d72a0e..77798b3c2 100644 --- a/src/allmydata/hashtree.py +++ b/src/allmydata/hashtree.py @@ -1,7 +1,4 @@ # -*- test-case-name: allmydata.test.test_hashtree -*- - -from allmydata.util import mathutil # from the pyutil library - """ Read and write chunks from files. @@ -50,6 +47,17 @@ or implied. It probably won't make your computer catch on fire, or eat your children, but it might. Use at your own risk. """ +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function +from __future__ import unicode_literals + +from future.utils import PY2 +if PY2: + from builtins import filter, map, zip, ascii, chr, hex, input, next, oct, open, pow, round, super, bytes, dict, int, list, object, range, str, max, min # noqa: F401 + +from allmydata.util import mathutil # from the pyutil library + from allmydata.util import base32 from allmydata.util.hashutil import tagged_hash, tagged_pair_hash @@ -170,9 +178,10 @@ def depth_of(i): return mathutil.log_floor(i+1, 2) def empty_leaf_hash(i): - return tagged_hash('Merkle tree empty leaf', "%d" % i) + return tagged_hash(b'Merkle tree empty leaf', b"%d" % i) + def pair_hash(a, b): - return tagged_pair_hash('Merkle tree internal node', a, b) + return tagged_pair_hash(b'Merkle tree internal node', a, b) class HashTree(CompleteBinaryTreeMixin, list): """ @@ -215,7 +224,7 @@ class HashTree(CompleteBinaryTreeMixin, list): while len(rows[-1]) != 1: last = rows[-1] rows += [[pair_hash(last[2*i], last[2*i+1]) - for i in xrange(len(last)//2)]] + for i in range(len(last)//2)]] # Flatten the list of rows into a single list. rows.reverse() self[:] = sum(rows, []) @@ -289,7 +298,7 @@ class IncompleteHashTree(CompleteBinaryTreeMixin, list): rows = [L] while len(rows[-1]) != 1: last = rows[-1] - rows += [[None for i in xrange(len(last)//2)]] + rows += [[None for i in range(len(last)//2)]] # Flatten the list of rows into a single list. rows.reverse() self[:] = sum(rows, []) @@ -372,12 +381,12 @@ class IncompleteHashTree(CompleteBinaryTreeMixin, list): assert isinstance(hashes, dict) for h in hashes.values(): - assert isinstance(h, str) + assert isinstance(h, bytes) assert isinstance(leaves, dict) for h in leaves.values(): - assert isinstance(h, str) + assert isinstance(h, bytes) new_hashes = hashes.copy() - for leafnum,leafhash in leaves.iteritems(): + for leafnum,leafhash in leaves.items(): hashnum = self.first_leaf_num + leafnum if hashnum in new_hashes: if new_hashes[hashnum] != leafhash: @@ -416,7 +425,7 @@ class IncompleteHashTree(CompleteBinaryTreeMixin, list): # first we provisionally add all hashes to the tree, comparing # any duplicates - for i,h in new_hashes.iteritems(): + for i,h in new_hashes.items(): if self[i]: if self[i] != h: raise BadHashError("new hash %s does not match " diff --git a/src/allmydata/test/test_hashtree.py b/src/allmydata/test/test_hashtree.py index 47aa97b96..b96f4abfb 100644 --- a/src/allmydata/test/test_hashtree.py +++ b/src/allmydata/test/test_hashtree.py @@ -1,4 +1,8 @@ -# -*- test-case-name: allmydata.test.test_hashtree -*- +""" +Tests for allmydata.hashtree. + +Ported to Python 3. +""" from __future__ import absolute_import from __future__ import division diff --git a/src/allmydata/util/_python3.py b/src/allmydata/util/_python3.py index 4342b8c04..a79f20cd0 100644 --- a/src/allmydata/util/_python3.py +++ b/src/allmydata/util/_python3.py @@ -15,6 +15,7 @@ if PY2: # Keep these sorted alphabetically, to reduce merge conflicts: PORTED_MODULES = [ + "allmydata.hashtree", "allmydata.util.assertutil", "allmydata.util.base32", "allmydata.util.base62", @@ -32,6 +33,7 @@ PORTED_TEST_MODULES = [ "allmydata.test.test_base32", "allmydata.test.test_base62", "allmydata.test.test_deferredutil", + "allmydata.test.test_hashtree", "allmydata.test.test_hashutil", "allmydata.test.test_humanreadable", "allmydata.test.test_netstring",