Skip to content
This repository was archived by the owner on May 23, 2023. It is now read-only.

Commit 085ce48

Browse files
authored
Merge pull request #403 from hackaugusto/issue_395
close #395
2 parents e7f995a + d671afc commit 085ce48

File tree

1 file changed

+25
-6
lines changed

1 file changed

+25
-6
lines changed

ethereum/blocks.py

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,36 @@ def __init__(self, data):
4040
self.data = data
4141

4242
def encode_entry(self, entry):
43-
assert isinstance(entry, Iterable)
43+
def safe_encode(value):
44+
# handle binary data (assuming all str are binary)
45+
if isinstance(value, (str, unicode)):
46+
return encode_hex(value)
47+
48+
# handle non binary data, eg.: floats, ints ...
49+
return repr(value)
50+
51+
if not isinstance(entry, Iterable):
52+
raise ValueError('entry must be iterable')
53+
54+
entry = list(entry) # consume the iterable
55+
4456
if len(entry) == 3:
4557
name, addr, v = entry
4658
if name == 'code':
47-
return [name, encode_hex(addr), encode_hex(v)]
59+
return [name, safe_encode(addr), safe_encode(v)]
4860
else:
49-
return [name, encode_hex(addr), v]
50-
elif len(entry) == 4:
61+
return [name, safe_encode(addr), v]
62+
63+
if len(entry) == 4:
5164
name, addr, k, v = entry
52-
return [name, encode_hex(addr), encode_hex(k), encode_hex(v)]
53-
assert False, ("unexpected entry format", entry)
65+
return [
66+
name,
67+
safe_encode(addr),
68+
safe_encode(k),
69+
safe_encode(v),
70+
]
71+
72+
raise ValueError('Unexpected entry format {}'.format(entry))
5473

5574
def __repr__(self):
5675
return repr([self.encode_entry(entry) for entry in self.data])

0 commit comments

Comments
 (0)