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

Commit a92f9c4

Browse files
committed
extra_args is now either a string or a list
Using ducktyping to decide on the type of extra_args and extending the argument list only if needed. Also changing default value to None rather than an empty string
1 parent 1cc66da commit a92f9c4

File tree

1 file changed

+28
-15
lines changed

1 file changed

+28
-15
lines changed

ethereum/_solidity.py

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import re
44
import subprocess
55
import warnings
6+
import shlex
67

78
import yaml
89

@@ -40,17 +41,22 @@ def get_solidity():
4041
return solc_wrapper
4142

4243

43-
def solc_arguments(libraries=None, combined='bin,abi', optimize=True, extra_args=''):
44+
def solc_arguments(libraries=None, combined='bin,abi', optimize=True, extra_args=None):
4445
""" Build the arguments to call the solc binary. """
4546
args = [
4647
'--combined-json', combined,
47-
'--add-std',
48-
extra_args
48+
'--add-std'
4949
]
5050

5151
if optimize:
5252
args.append('--optimize')
5353

54+
if extra_args:
55+
try:
56+
args.extend(shlex.split(extra_args))
57+
except: # if not a parseable string then treat it as a list
58+
args.extend(extra_args)
59+
5460
if libraries is not None and len(libraries):
5561
addresses = [
5662
'{name}:{address}'.format(name=name, address=address.decode('utf8'))
@@ -66,6 +72,10 @@ def solc_arguments(libraries=None, combined='bin,abi', optimize=True, extra_args
6672

6773
def solc_parse_output(compiler_output):
6874
""" Parses the compiler output. """
75+
# At the moment some solc output like --hashes or -- gas will not output
76+
# json at all so if used with those arguments the logic here will break.
77+
# Perhaps solidity will slowly switch to a json only output and this comment
78+
# can eventually go away and we will not need to add more logic here at all.
6979
result = yaml.safe_load(compiler_output)['contracts']
7080

7181
if 'bin' in tuple(result.values())[0]:
@@ -222,7 +232,7 @@ def solidity_unresolved_symbols(hex_code):
222232
return set(re.findall(r"_.{39}", hex_code))
223233

224234

225-
def compile_file(filepath, libraries=None, combined='bin,abi', optimize=True, extra_args=''):
235+
def compile_file(filepath, libraries=None, combined='bin,abi', optimize=True, extra_args=None):
226236
""" Return the compile contract code.
227237
228238
Args:
@@ -246,7 +256,7 @@ def compile_file(filepath, libraries=None, combined='bin,abi', optimize=True, ex
246256
return solc_parse_output(output)
247257

248258

249-
def compile_contract(filepath, contract_name, libraries=None, combined='bin,abi', optimize=True, extra_args=''):
259+
def compile_contract(filepath, contract_name, libraries=None, combined='bin,abi', optimize=True, extra_args=None):
250260
all_contracts = compile_file(
251261
filepath,
252262
libraries=libraries,
@@ -258,7 +268,7 @@ def compile_contract(filepath, contract_name, libraries=None, combined='bin,abi'
258268
return all_contracts[contract_name]
259269

260270

261-
def compile_last_contract(filepath, libraries=None, combined='bin,abi', optimize=True, extra_args=''):
271+
def compile_last_contract(filepath, libraries=None, combined='bin,abi', optimize=True, extra_args=None):
262272
with open(filepath) as handler:
263273
all_names = solidity_names(handler.read())
264274

@@ -279,7 +289,7 @@ def compile_last_contract(filepath, libraries=None, combined='bin,abi', optimize
279289
)
280290

281291

282-
def compile_code(sourcecode, libraries=None, combined='bin,abi', optimize=True, extra_args=''):
292+
def compile_code(sourcecode, libraries=None, combined='bin,abi', optimize=True, extra_args=None):
283293
args = solc_arguments(libraries=libraries, combined=combined, optimize=optimize, extra_args=extra_args)
284294
args.insert(0, get_compiler_path())
285295

@@ -323,37 +333,40 @@ def _code_or_path(sourcecode, path, contract_name, libraries, combined, extra_ar
323333
return result[last_contract]
324334

325335
@classmethod
326-
def compile(cls, code, path=None, libraries=None, contract_name='', extra_args=''):
336+
def compile(cls, code, path=None, libraries=None, contract_name='', extra_args=None):
327337
""" Return the binary of last contract in code. """
328338
result = cls._code_or_path(code, path, contract_name, libraries, 'bin', extra_args)
329339
return result['bin']
330340

331341
@classmethod
332-
def mk_full_signature(cls, code, path=None, libraries=None, contract_name='', extra_args=''):
342+
def mk_full_signature(cls, code, path=None, libraries=None, contract_name='', extra_args=None):
333343
"returns signature of last contract in code"
334344

335345
result = cls._code_or_path(code, path, contract_name, libraries, 'abi', extra_args)
336346
return result['abi']
337347

338348
@classmethod
339-
def combined(cls, code, path=None):
349+
def combined(cls, code, path=None, extra_args=None):
340350
""" Compile combined-json with abi,bin,devdoc,userdoc.
341351
342352
@param code: literal solidity code as a string.
343-
@param path: absolute path to solidity-file. Note: code & path are exclusive!
353+
@param path: absolute path to solidity-file. Note: code & path are
354+
mutually exclusive!
355+
@param extra_args: Either a space separated string or a list of extra
356+
arguments to be passed to the solidity compiler.
344357
"""
345358

346359
if code and path:
347360
raise ValueError('sourcecode and path are mutually exclusive.')
348361

349362
if path:
350-
contracts = compile_file(path)
363+
contracts = compile_file(path, extra_args=extra_args)
351364

352365
with open(path) as handler:
353366
code = handler.read()
354367

355368
elif code:
356-
contracts = compile_code(code)
369+
contracts = compile_code(code, extra_args=extra_args)
357370

358371
else:
359372
raise ValueError('either code or path needs to be supplied.')
@@ -364,7 +377,7 @@ def combined(cls, code, path=None):
364377
return sorted_contracts
365378

366379
@classmethod
367-
def compile_rich(cls, code, path=None):
380+
def compile_rich(cls, code, path=None, extra_args=None):
368381
"""full format as returned by jsonrpc"""
369382

370383
return {
@@ -381,7 +394,7 @@ def compile_rich(cls, code, path=None):
381394
},
382395
}
383396
for contract_name, contract
384-
in cls.combined(code, path=path)
397+
in cls.combined(code, path=path, extra_args=extra_args)
385398
}
386399

387400

0 commit comments

Comments
 (0)