Skip to content

Commit

Permalink
Simplify API of compile_codes
Browse files Browse the repository at this point in the history
  • Loading branch information
davesque committed Jun 27, 2019
1 parent 9989699 commit c531068
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 25 deletions.
6 changes: 2 additions & 4 deletions bin/vyper
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,6 @@ if __name__ == '__main__':
out = vyper.compile_codes(
codes,
['bytecode', 'bytecode_runtime', 'abi', 'source_map', 'method_identifiers'],
output_type='dict',
exc_handler=exc_handler,
interface_codes=get_interface_codes(codes)
)
Expand All @@ -177,13 +176,12 @@ if __name__ == '__main__':
for f in orig_args:
formats.append(translate_map.get(f, f))

out_list = vyper.compile_codes(
out_list = list(vyper.compile_codes(
codes,
formats,
output_type='list',
exc_handler=exc_handler,
interface_codes=get_interface_codes(codes)
)
).values())

for out in out_list:
for f in orig_args:
Expand Down
1 change: 0 additions & 1 deletion bin/vyper-serve
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ class VyperRequestHandler(BaseHTTPRequestHandler):
out_dict = vyper.compile_codes(
{'': code},
vyper.compiler.output_formats_map.keys(),
output_type='dict',
)['']
out_dict['ir'] = str(out_dict['ir'])
except ParserException as e:
Expand Down
2 changes: 1 addition & 1 deletion tests/compiler/test_bytecode_runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def a() -> bool:
return True
"""

out = vyper.compile_codes({'': code}, ['bytecode_runtime', 'bytecode'])[0]
out = vyper.compile_code(code, ['bytecode_runtime', 'bytecode'])

assert len(out['bytecode']) > len(out['bytecode_runtime'])
assert out['bytecode_runtime'][2:] in out['bytecode'][2:]
2 changes: 1 addition & 1 deletion tests/compiler/test_opcodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def a() -> bool:
return True
"""

out = vyper.compile_codes({'': code}, ['opcodes_runtime', 'opcodes'])[0]
out = vyper.compile_code(code, ['opcodes_runtime', 'opcodes'])

assert len(out['opcodes']) > len(out['opcodes_runtime'])
assert out['opcodes_runtime'] in out['opcodes']
9 changes: 4 additions & 5 deletions tests/parser/functions/test_abi.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from vyper.compiler import (
compile_codes,
compile_code,
mk_full_signature,
)

Expand Down Expand Up @@ -56,11 +56,10 @@ def foo(x: uint256) -> bytes[100]:
return b"hello"
"""

out = compile_codes(
codes={'t.vy': code},
out = compile_code(
code,
output_formats=['method_identifiers'],
output_type='list'
)[0]
)

assert out['method_identifiers'] == {
'foo(uint256)': '0x2fbebd38',
Expand Down
8 changes: 4 additions & 4 deletions tests/parser/functions/test_interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def allowance(_owner: address, _spender: address) -> (uint256, uint256): constan
def test(_owner: address): modifying
"""

out = compile_codes({'one.vy': code}, ['external_interface'])[0]
out = compile_codes({'one.vy': code}, ['external_interface'])['one.vy']
out = out['external_interface']

assert interface.strip() == out.strip()
Expand All @@ -79,7 +79,7 @@ def test() -> bool:
"""

assert_compile_failed(
lambda: compile_codes({'one.vy': code}),
lambda: compile_code(code),
StructureException
)

Expand Down Expand Up @@ -121,7 +121,7 @@ def bar() -> uint256:
return 2
"""

assert compile_codes({'one.vy': code}, interface_codes=interface_codes)[0]
assert compile_code(code, interface_codes=interface_codes)

not_implemented_code = """
import a as FooBarInterface
Expand All @@ -135,7 +135,7 @@ def foo() -> uint256:
"""

assert_compile_failed(
lambda: compile_codes({'one.vy': not_implemented_code}, interface_codes=interface_codes)[0],
lambda: compile_code(not_implemented_code, interface_codes=interface_codes),
StructureException
)

Expand Down
20 changes: 11 additions & 9 deletions vyper/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,6 @@ def _mk_ast_dict(code, contract_name, interface_codes):

def compile_codes(codes,
output_formats=None,
output_type='list',
exc_handler=None,
interface_codes=None):
if output_formats is None:
Expand All @@ -240,14 +239,17 @@ def compile_codes(codes,
else:
raise exc

if output_type == 'list':
return list(out.values())
elif output_type == 'dict':
return out
else:
raise ValueError(f'Unsupported output type {repr(output_type)}')
return out


UNKNOWN_CONTRACT_NAME = '<unknown>'


def compile_code(code, output_formats=None, interface_codes=None):
codes = {'': code}
return compile_codes(codes, output_formats, 'list', interface_codes=interface_codes)[0]
codes = {UNKNOWN_CONTRACT_NAME: code}

return compile_codes(
codes,
output_formats,
interface_codes=interface_codes,
)[UNKNOWN_CONTRACT_NAME]

0 comments on commit c531068

Please sign in to comment.