Port to Python 3.

This commit is contained in:
Itamar Turner-Trauring 2020-07-22 13:17:49 -04:00
parent 15254d08fc
commit 64e4af2d05
5 changed files with 36 additions and 12 deletions

View File

@ -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_gather_results
allmydata.test.test_deferredutil.DeferredUtilTests.test_success allmydata.test.test_deferredutil.DeferredUtilTests.test_success
allmydata.test.test_deferredutil.DeferredUtilTests.test_wait_for_delayed_calls 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_chk
allmydata.test.test_hashutil.HashUtilTests.test_hashers allmydata.test.test_hashutil.HashUtilTests.test_hashers
allmydata.test.test_hashutil.HashUtilTests.test_known_answers allmydata.test.test_hashutil.HashUtilTests.test_known_answers

1
newsfragments/3354.minor Normal file
View File

@ -0,0 +1 @@

View File

@ -1,7 +1,4 @@
# -*- test-case-name: allmydata.test.test_hashtree -*- # -*- test-case-name: allmydata.test.test_hashtree -*-
from allmydata.util import mathutil # from the pyutil library
""" """
Read and write chunks from files. 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. 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 import base32
from allmydata.util.hashutil import tagged_hash, tagged_pair_hash 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) return mathutil.log_floor(i+1, 2)
def empty_leaf_hash(i): 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): 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): class HashTree(CompleteBinaryTreeMixin, list):
""" """
@ -215,7 +224,7 @@ class HashTree(CompleteBinaryTreeMixin, list):
while len(rows[-1]) != 1: while len(rows[-1]) != 1:
last = rows[-1] last = rows[-1]
rows += [[pair_hash(last[2*i], last[2*i+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. # Flatten the list of rows into a single list.
rows.reverse() rows.reverse()
self[:] = sum(rows, []) self[:] = sum(rows, [])
@ -289,7 +298,7 @@ class IncompleteHashTree(CompleteBinaryTreeMixin, list):
rows = [L] rows = [L]
while len(rows[-1]) != 1: while len(rows[-1]) != 1:
last = rows[-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. # Flatten the list of rows into a single list.
rows.reverse() rows.reverse()
self[:] = sum(rows, []) self[:] = sum(rows, [])
@ -372,12 +381,12 @@ class IncompleteHashTree(CompleteBinaryTreeMixin, list):
assert isinstance(hashes, dict) assert isinstance(hashes, dict)
for h in hashes.values(): for h in hashes.values():
assert isinstance(h, str) assert isinstance(h, bytes)
assert isinstance(leaves, dict) assert isinstance(leaves, dict)
for h in leaves.values(): for h in leaves.values():
assert isinstance(h, str) assert isinstance(h, bytes)
new_hashes = hashes.copy() new_hashes = hashes.copy()
for leafnum,leafhash in leaves.iteritems(): for leafnum,leafhash in leaves.items():
hashnum = self.first_leaf_num + leafnum hashnum = self.first_leaf_num + leafnum
if hashnum in new_hashes: if hashnum in new_hashes:
if new_hashes[hashnum] != leafhash: if new_hashes[hashnum] != leafhash:
@ -416,7 +425,7 @@ class IncompleteHashTree(CompleteBinaryTreeMixin, list):
# first we provisionally add all hashes to the tree, comparing # first we provisionally add all hashes to the tree, comparing
# any duplicates # any duplicates
for i,h in new_hashes.iteritems(): for i,h in new_hashes.items():
if self[i]: if self[i]:
if self[i] != h: if self[i] != h:
raise BadHashError("new hash %s does not match " raise BadHashError("new hash %s does not match "

View File

@ -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 absolute_import
from __future__ import division from __future__ import division

View File

@ -15,6 +15,7 @@ if PY2:
# Keep these sorted alphabetically, to reduce merge conflicts: # Keep these sorted alphabetically, to reduce merge conflicts:
PORTED_MODULES = [ PORTED_MODULES = [
"allmydata.hashtree",
"allmydata.util.assertutil", "allmydata.util.assertutil",
"allmydata.util.base32", "allmydata.util.base32",
"allmydata.util.base62", "allmydata.util.base62",
@ -32,6 +33,7 @@ PORTED_TEST_MODULES = [
"allmydata.test.test_base32", "allmydata.test.test_base32",
"allmydata.test.test_base62", "allmydata.test.test_base62",
"allmydata.test.test_deferredutil", "allmydata.test.test_deferredutil",
"allmydata.test.test_hashtree",
"allmydata.test.test_hashutil", "allmydata.test.test_hashutil",
"allmydata.test.test_humanreadable", "allmydata.test.test_humanreadable",
"allmydata.test.test_netstring", "allmydata.test.test_netstring",