Skip to content
This repository has been archived by the owner on Mar 28, 2023. It is now read-only.

Fix abi generation using Python 3.8 #137

Open
wants to merge 4 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 18 additions & 9 deletions boa/code/method.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,31 +182,40 @@ def setup(self):
self._expressions = []

def evaluate_annotations(self, index):
import sys
ixje marked this conversation as resolved.
Show resolved Hide resolved
# decorators were included in the same block as the function until python 3.7
if sys.version_info < (3, 8):
self.iterate_block(self.block, index)
else:
# in python 3.8, they're in different blocks
for block in self._extra:
self.iterate_block(block, len(block))

def iterate_block(self, block, index):
block_index = 0
args_types = []
while block_index < index:
if self.block[block_index].opcode == pyop.LOAD_NAME and 'abi' in self.block[block_index].arg:
block_index = self.include_abi_info(block_index)
if block[block_index].opcode == pyop.LOAD_NAME and 'abi' in block[block_index].arg:
block_index = self.include_abi_info(block, block_index)
else:
block_index = block_index + 1
block_index += 1

def include_abi_info(self, start_index):
def include_abi_info(self, block, start_index):
index = start_index
load_method_instr = self.block[index]
load_method_instr = block[index]

while load_method_instr.opcode != pyop.LOAD_METHOD and load_method_instr.opcode != pyop.LOAD_NAME:
index = index + 1
load_method_instr = self.block[index]
load_method_instr = block[index]

args_types = []
if load_method_instr.arg == 'abi_method' or load_method_instr.arg == 'abi_entry_point':
index = index + 1
arg_instr = self.block[index]
arg_instr = block[index]
while arg_instr.opcode == pyop.LOAD_NAME or arg_instr.opcode == pyop.LOAD_ATTR:
if abi.is_abi_type(arg_instr.arg):
args_types.append(arg_instr.arg)
index = index + 1
arg_instr = self.block[index]
arg_instr = block[index]

# return type not specified
if len(args_types) == len(self.args):
Expand Down
50 changes: 35 additions & 15 deletions boa/code/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,12 +201,31 @@ def build(self):
elif type == BlockType.APPCALL_REG:
self.app_call_registrations.append(BoaAppcall(blk))

last_m = None
for m in new_method_blks:

new_method = BoaMethod(self, m, self.module_name, self._extra_instr)
new_method = BoaMethod(self, m, self.module_name, self._method_extra_instr(m, last_m))

if not self.has_method(new_method.full_name):
self.methods.append(new_method)
last_m = m

def _method_extra_instr(self, method_block, last_method_block):
# decorators were included in the same block as the method until python 3.7
# in python 3.8, they're in different blocks, that are in the `_extra_instr`
if sys.version_info < (3, 8):
return self._extra_instr
else:
block = method_block
if last_method_block:
block = last_method_block.next_block
elif len(self._extra_instr) > 0:
block = self._extra_instr[0]

extra_instr = []
while block is not None and block is not method_block:
extra_instr.append(block)
block = block.next_block
return extra_instr

def write(self):
"""
Expand Down Expand Up @@ -375,19 +394,20 @@ def to_s(self):
return "\n".join(output)

def include_abi_method(self, method, types):
num_methods = len(method.args)
num_types = len(types)

args_types = {}
# params and return types
if num_types == num_methods + 1:
for index, arg in enumerate(method.args):
args_types[arg] = types[index]
args_types['return'] = types[num_types - 1]
else:
raise Exception("Number of arguments for the abi is incompatible with the function '%s'" % method.full_name)

self.abi_methods[method.full_name] = args_types
if method.full_name not in self.abi_methods:
num_methods = len(method.args)
num_types = len(types)

args_types = {}
# params and return types
if num_types == num_methods + 1:
for index, arg in enumerate(method.args):
args_types[arg] = types[index]
args_types['return'] = types[num_types - 1]
else:
raise Exception("Number of arguments for the abi is incompatible with the function '%s'" % method.full_name)

self.abi_methods[method.full_name] = args_types

def set_abi_entry_point(self, method, types):
if self.abi_entry_point is None:
Expand Down