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

Commit 49b1ddf

Browse files
committed
py3 compat
1 parent a433a03 commit 49b1ddf

File tree

3 files changed

+87
-87
lines changed

3 files changed

+87
-87
lines changed

ethereum/abi.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ def encode_single(typ, arg): # pylint: disable=too-many-return-statements,too-m
309309
return zpad(int_to_big_endian(value), 32)
310310

311311
if base == 'string':
312-
if isinstance(arg, unicode):
312+
if isinstance(arg, utils.unicode):
313313
arg = arg.encode('utf8')
314314
else:
315315
try:

ethereum/tests/test_abi.py

Lines changed: 82 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import pytest
33

44
from ethereum.utils import (
5-
big_endian_to_int, int_to_big_endian, rzpad, sha3, zpad,
5+
big_endian_to_int, int_to_big_endian, decode_hex, encode_hex, rzpad, sha3, zpad,
66
)
77
from ethereum.abi import (
88
_canonical_type, decode_abi, decode_single, decint, encode_abi,
@@ -37,10 +37,10 @@ def test_canonical_types():
3737

3838
def test_function_selector():
3939
# https://github.com/ethereum/wiki/wiki/Ethereum-Contract-ABI#function-selector-and-argument-encoding
40-
baz_selector = big_endian_to_int('CDCD77C0'.decode('hex'))
41-
bar_selector = big_endian_to_int('AB55044D'.decode('hex'))
42-
sam_selector = big_endian_to_int('A5643BF2'.decode('hex'))
43-
f_selector = big_endian_to_int('8BE65246'.decode('hex'))
40+
baz_selector = big_endian_to_int(decode_hex('CDCD77C0'))
41+
bar_selector = big_endian_to_int(decode_hex('AB55044D'))
42+
sam_selector = big_endian_to_int(decode_hex('A5643BF2'))
43+
f_selector = big_endian_to_int(decode_hex('8BE65246'))
4444

4545
assert big_endian_to_int(sha3('baz(uint32,bool)')[:4]) == baz_selector
4646
assert big_endian_to_int(sha3('bar(fixed128x128[2])')[:4]) == bar_selector
@@ -79,7 +79,7 @@ def test_event():
7979

8080
result = contract_abi.decode_event(topics, data)
8181

82-
assert result['_event_type'] == 'Test'
82+
assert result['_event_type'] == b'Test'
8383
assert result['a'] == 1
8484
assert result['b'] == 2
8585

@@ -126,55 +126,55 @@ def test_encode_int():
126126
int256 = ('int', '256', [])
127127

128128
int256_maximum = (
129-
'\x7f\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff'
130-
'\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff'
129+
b'\x7f\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff'
130+
b'\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff'
131131
)
132132
int256_minimum = (
133-
'\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
134-
'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
133+
b'\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
134+
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
135135
)
136136
int256_128 = (
137-
'\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff'
138-
'\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\x80'
137+
b'\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff'
138+
b'\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\x80'
139139
)
140140
int256_2_to_31 = (
141-
'\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff'
142-
'\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\x80\x00\x00\x00'
141+
b'\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff'
142+
b'\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\x80\x00\x00\x00'
143143
)
144144
int256_negative_one = (
145-
'\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff'
146-
'\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff'
145+
b'\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff'
146+
b'\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff'
147147
)
148148

149149
assert encode_single(int256, int256_minimum) == int256_minimum
150150

151-
assert encode_single(int8, 0) == zpad('\x00', 32)
152-
assert encode_single(int8, 2 ** 7 - 1) == zpad('\x7f', 32)
153-
assert encode_single(int8, -1) == zpad('\xff', 32)
154-
assert encode_single(int8, -2 ** 7) == zpad('\x80', 32)
151+
assert encode_single(int8, 0) == zpad(b'\x00', 32)
152+
assert encode_single(int8, 2 ** 7 - 1) == zpad(b'\x7f', 32)
153+
assert encode_single(int8, -1) == zpad(b'\xff', 32)
154+
assert encode_single(int8, -2 ** 7) == zpad(b'\x80', 32)
155155

156156
with pytest.raises(ValueOutOfBounds):
157157
encode_single(int8, 128)
158158

159159
with pytest.raises(ValueOutOfBounds):
160160
encode_single(int8, -129)
161161

162-
assert encode_single(int32, 0) == zpad('\x00', 32)
163-
assert encode_single(int32, 2 ** 7 - 1) == zpad('\x7f', 32)
164-
assert encode_single(int32, 2 ** 31 - 1) == zpad('\x7f\xff\xff\xff', 32)
165-
assert encode_single(int32, -1) == zpad('\xff\xff\xff\xff', 32)
166-
assert encode_single(int32, -2 ** 7) == zpad('\xff\xff\xff\x80', 32)
167-
assert encode_single(int32, -2 ** 31) == zpad('\x80\x00\x00\x00', 32)
162+
assert encode_single(int32, 0) == zpad(b'\x00', 32)
163+
assert encode_single(int32, 2 ** 7 - 1) == zpad(b'\x7f', 32)
164+
assert encode_single(int32, 2 ** 31 - 1) == zpad(b'\x7f\xff\xff\xff', 32)
165+
assert encode_single(int32, -1) == zpad(b'\xff\xff\xff\xff', 32)
166+
assert encode_single(int32, -2 ** 7) == zpad(b'\xff\xff\xff\x80', 32)
167+
assert encode_single(int32, -2 ** 31) == zpad(b'\x80\x00\x00\x00', 32)
168168

169169
with pytest.raises(ValueOutOfBounds):
170170
encode_single(int32, 2 ** 32)
171171

172172
with pytest.raises(ValueOutOfBounds):
173173
encode_single(int32, -(2 ** 32))
174174

175-
assert encode_single(int256, 0) == zpad('\x00', 32)
176-
assert encode_single(int256, 2 ** 7 - 1) == zpad('\x7f', 32)
177-
assert encode_single(int256, 2 ** 31 - 1) == zpad('\x7f\xff\xff\xff', 32)
175+
assert encode_single(int256, 0) == zpad(b'\x00', 32)
176+
assert encode_single(int256, 2 ** 7 - 1) == zpad(b'\x7f', 32)
177+
assert encode_single(int256, 2 ** 31 - 1) == zpad(b'\x7f\xff\xff\xff', 32)
178178
assert encode_single(int256, 2 ** 255 - 1) == int256_maximum
179179
assert encode_single(int256, -1) == int256_negative_one
180180
assert encode_single(int256, -2 ** 7) == int256_128
@@ -202,24 +202,24 @@ def test_encode_uint():
202202
with pytest.raises(ValueOutOfBounds):
203203
encode_single(uint256, -1)
204204

205-
assert encode_single(uint8, 0) == zpad('\x00', 32)
206-
assert encode_single(uint32, 0) == zpad('\x00', 32)
207-
assert encode_single(uint256, 0) == zpad('\x00', 32)
205+
assert encode_single(uint8, 0) == zpad(b'\x00', 32)
206+
assert encode_single(uint32, 0) == zpad(b'\x00', 32)
207+
assert encode_single(uint256, 0) == zpad(b'\x00', 32)
208208

209-
assert encode_single(uint8, 1) == zpad('\x01', 32)
210-
assert encode_single(uint32, 1) == zpad('\x01', 32)
211-
assert encode_single(uint256, 1) == zpad('\x01', 32)
209+
assert encode_single(uint8, 1) == zpad(b'\x01', 32)
210+
assert encode_single(uint32, 1) == zpad(b'\x01', 32)
211+
assert encode_single(uint256, 1) == zpad(b'\x01', 32)
212212

213-
assert encode_single(uint8, 2 ** 8 - 1) == zpad('\xff', 32)
214-
assert encode_single(uint32, 2 ** 8 - 1) == zpad('\xff', 32)
215-
assert encode_single(uint256, 2 ** 8 - 1) == zpad('\xff', 32)
213+
assert encode_single(uint8, 2 ** 8 - 1) == zpad(b'\xff', 32)
214+
assert encode_single(uint32, 2 ** 8 - 1) == zpad(b'\xff', 32)
215+
assert encode_single(uint256, 2 ** 8 - 1) == zpad(b'\xff', 32)
216216

217-
assert encode_single(uint32, 2 ** 32 - 1) == zpad('\xff\xff\xff\xff', 32)
218-
assert encode_single(uint256, 2 ** 32 - 1) == zpad('\xff\xff\xff\xff', 32)
217+
assert encode_single(uint32, 2 ** 32 - 1) == zpad(b'\xff\xff\xff\xff', 32)
218+
assert encode_single(uint256, 2 ** 32 - 1) == zpad(b'\xff\xff\xff\xff', 32)
219219

220220
uint256_maximum = (
221-
'\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff'
222-
'\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff'
221+
b'\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff'
222+
b'\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff'
223223
)
224224
assert encode_single(uint256, 2 ** 256 - 1) == uint256_maximum
225225

@@ -228,8 +228,8 @@ def test_encode_bool():
228228
bool_ = ('bool', '', [])
229229
uint8 = ('uint', '8', [])
230230

231-
assert encode_single(bool_, True) == zpad('\x01', 32)
232-
assert encode_single(bool_, False) == zpad('\x00', 32)
231+
assert encode_single(bool_, True) == zpad(b'\x01', 32)
232+
assert encode_single(bool_, False) == zpad(b'\x00', 32)
233233

234234
assert encode_single(bool_, True) == encode_single(uint8, 1)
235235
assert encode_single(bool_, False) == encode_single(uint8, 0)
@@ -238,21 +238,21 @@ def test_encode_bool():
238238
def test_encode_fixed():
239239
fixed128x128 = ('fixed', '128x128', [])
240240

241-
_2_125 = (
242-
'00000000000000000000000000000002'
243-
'20000000000000000000000000000000'
244-
).decode('hex')
241+
_2_125 = decode_hex(
242+
b'00000000000000000000000000000002'
243+
b'20000000000000000000000000000000'
244+
)
245245

246-
_8_5 = (
247-
'00000000000000000000000000000008'
248-
'80000000000000000000000000000000'
249-
).decode('hex')
246+
_8_5 = decode_hex(
247+
b'00000000000000000000000000000008'
248+
b'80000000000000000000000000000000'
249+
)
250250

251251
assert encode_single(fixed128x128, 2.125) == _2_125
252252
assert encode_single(fixed128x128, 8.5) == _8_5
253253

254-
assert encode_single(fixed128x128, 1.125) == (b'\x00'*15 + b'\x01\x20' + b'\x00'*15)
255-
assert encode_single(fixed128x128, -1.125) == (b'\xff'*15 + b'\xfe' + b'\xe0' + b'\x00'*15)
254+
assert encode_single(fixed128x128, 1.125) == (b'\x00' * 15 + b'\x01\x20' + b'\x00' * 15)
255+
assert encode_single(fixed128x128, -1.125) == (b'\xff' * 15 + b'\xfe' + b'\xe0' + b'\x00' * 15)
256256

257257
with pytest.raises(ValueOutOfBounds):
258258
encode_single(fixed128x128, 2 ** 127)
@@ -264,22 +264,22 @@ def test_encode_fixed():
264264
def test_encoded_ufixed():
265265
ufixed128x128 = ('ufixed', '128x128', [])
266266

267-
_2_125 = (
268-
'00000000000000000000000000000002'
269-
'20000000000000000000000000000000'
270-
).decode('hex')
267+
_2_125 = decode_hex(
268+
b'00000000000000000000000000000002'
269+
b'20000000000000000000000000000000'
270+
)
271271

272-
_8_5 = (
273-
'00000000000000000000000000000008'
274-
'80000000000000000000000000000000'
275-
).decode('hex')
272+
_8_5 = decode_hex(
273+
b'00000000000000000000000000000008'
274+
b'80000000000000000000000000000000'
275+
)
276276

277277
assert encode_single(ufixed128x128, 2.125) == _2_125
278278
assert encode_single(ufixed128x128, 8.5) == _8_5
279279

280-
assert encode_single(ufixed128x128, 0) == (b'\x00'*32)
281-
assert encode_single(ufixed128x128, 1.125) == (b'\x00'*15 + b'\x01\x20' + '\x00'*15)
282-
assert encode_single(ufixed128x128, 2**127-1) == (b'\x7f' + b'\xff'*15 + b'\x00'*16)
280+
assert encode_single(ufixed128x128, 0) == (b'\x00' * 32)
281+
assert encode_single(ufixed128x128, 1.125) == (b'\x00' * 15 + b'\x01\x20' + b'\x00' * 15)
282+
assert encode_single(ufixed128x128, 2**127 - 1) == (b'\x7f' + b'\xff' * 15 + b'\x00' * 16)
283283

284284
with pytest.raises(ValueOutOfBounds):
285285
encode_single(ufixed128x128, 2 ** 128 + 1)
@@ -293,18 +293,18 @@ def test_encode_dynamic_bytes():
293293
uint256 = ('uint', '256', [])
294294

295295
# only the size of the bytes
296-
assert encode_single(dynamic_bytes, '') == zpad('\x00', 32)
296+
assert encode_single(dynamic_bytes, b'') == zpad(b'\x00', 32)
297297

298-
a = encode_single(uint256, 1) + rzpad('\x61', 32)
299-
assert encode_single(dynamic_bytes, '\x61') == a
298+
a = encode_single(uint256, 1) + rzpad(b'\x61', 32)
299+
assert encode_single(dynamic_bytes, b'\x61') == a
300300

301-
dave_bin = (
302-
'0000000000000000000000000000000000000000000000000000000000000004'
303-
'6461766500000000000000000000000000000000000000000000000000000000'
304-
).decode('hex')
305-
dave = encode_single(uint256, 4) + rzpad('\x64\x61\x76\x65', 32)
306-
assert encode_single(dynamic_bytes, '\x64\x61\x76\x65') == dave_bin
307-
assert encode_single(dynamic_bytes, '\x64\x61\x76\x65') == dave
301+
dave_bin = decode_hex(
302+
b'0000000000000000000000000000000000000000000000000000000000000004'
303+
b'6461766500000000000000000000000000000000000000000000000000000000'
304+
)
305+
dave = encode_single(uint256, 4) + rzpad(b'\x64\x61\x76\x65', 32)
306+
assert encode_single(dynamic_bytes, b'\x64\x61\x76\x65') == dave_bin
307+
assert encode_single(dynamic_bytes, b'\x64\x61\x76\x65') == dave
308308

309309

310310
def test_encode_dynamic_string():
@@ -325,7 +325,7 @@ def test_encode_hash():
325325
hash8 = ('hash', '8', [])
326326

327327
assert encode_single(hash8, b'\x00' * 8) == b'\x00' * 32
328-
assert encode_single(hash8, '00'*8) == b'\x00'*32
328+
assert encode_single(hash8, b'00' * 8) == b'\x00' * 32
329329

330330

331331
def test_encode_address():
@@ -378,8 +378,8 @@ def test_encode_decode_fixed():
378378
fixed_data = encode_single(fixed128x128, 1)
379379
assert decode_single(fixed128x128, fixed_data) == 1
380380

381-
fixed_data = encode_single(fixed128x128, 2**127-1)
382-
assert decode_single(fixed128x128, fixed_data) == (2**127-1)*1.0
381+
fixed_data = encode_single(fixed128x128, 2**127 - 1)
382+
assert decode_single(fixed128x128, fixed_data) == (2**127 - 1) * 1.0
383383

384384
fixed_data = encode_single(fixed128x128, -1)
385385
assert decode_single(fixed128x128, fixed_data) == -1
@@ -414,12 +414,12 @@ def test_encode_decode_address():
414414
address3,
415415
]
416416
all_addresses_encoded = [
417-
address1.encode('hex'),
418-
address2.encode('hex'),
419-
address3.encode('hex'),
417+
encode_hex(address1),
418+
encode_hex(address2),
419+
encode_hex(address3),
420420
]
421421

422-
assert decode_abi(['address'], encode_abi(['address'], [address1]))[0] == address1.encode('hex')
422+
assert decode_abi(['address'], encode_abi(['address'], [address1]))[0] == encode_hex(address1)
423423

424424
addresses_encoded_together = encode_abi(['address[]'], [all_addresses])
425425
assert decode_abi(['address[]'], addresses_encoded_together)[0] == all_addresses_encoded

ethereum/tests/test_contracts.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1089,7 +1089,7 @@ def kall(a:arr, b, c:arr, d:str, e):
10891089
def test_multiarg_code():
10901090
s = tester.state()
10911091
c = s.abi_contract(multiarg_code)
1092-
o = c.kall([1, 2, 3], 4, [5, 6, 7], "doge", 8)
1092+
o = c.kall([1, 2, 3], 4, [5, 6, 7], b"doge", 8)
10931093
assert o == [862541, safe_ord('d') + safe_ord('o') + safe_ord('g'), 4]
10941094

10951095
peano_code = """
@@ -1385,7 +1385,7 @@ def mcopy_test(foo:str, a, b, c):
13851385
def test_mcopy():
13861386
s = tester.state()
13871387
c = s.abi_contract(mcopy_code)
1388-
assert c.mcopy_test("123", 5, 6, 259) == \
1388+
assert c.mcopy_test(b"123", 5, 6, 259) == \
13891389
b'\x00'*31+b'\x05'+b'\x00'*31+b'\x06'+b'\x00'*30+b'\x01\x03'+b'123'
13901390

13911391

@@ -1499,7 +1499,7 @@ def test_double_array():
14991499
s = tester.state()
15001500
c = s.abi_contract(double_array_code)
15011501
assert c.foo([1, 2, 3], [4, 5, 6, 7]) == [123, 4567]
1502-
assert c.bar([1, 2, 3], "moo", [4, 5, 6, 7]) == [123, 4567]
1502+
assert c.bar([1, 2, 3], b"moo", [4, 5, 6, 7]) == [123, 4567]
15031503

15041504

15051505
abi_logging_code = """
@@ -1533,7 +1533,7 @@ def test_abi_logging():
15331533
c.test_frog(5)
15341534
assert o == [{"_event_type": b"frog", "y": 5}]
15351535
o.pop()
1536-
c.test_moose(7, "nine", 11, [13, 15, 17])
1536+
c.test_moose(7, b"nine", 11, [13, 15, 17])
15371537
assert o == [{"_event_type": b"moose", "a": 7, "b": b"nine",
15381538
"c": 11, "d": [13, 15, 17]}]
15391539
o.pop()

0 commit comments

Comments
 (0)