Commit 2fd391c6 authored by Pierre Augier's avatar Pierre Augier
Browse files

Refactor analysis blocks + replace str by objects for signatures in comments

parent d818b6c0b904
Pipeline #6749 passed with stage
in 9 minutes and 10 seconds
......@@ -34,6 +34,7 @@ from .util import (
from .capturex import CaptureX
from .blocks_if import get_block_definitions
from .parser import parse_transonic_def_commands
from .objects_from_str import replace_strings_by_objects
from . import extast
......@@ -330,16 +331,10 @@ def analyse_aot(code, pathfile, backend_name):
debug("get_block_definitions")
blocks = get_block_definitions(code, module, ancestors, duc, udc)
info_analysis = {
"ancestors": ancestors,
"duc": duc,
"udc": udc,
"module": module,
"def_nodes": def_nodes,
}
for block in blocks:
block.parse_comments(namespace, info_analysis)
replace_strings_by_objects(
block.signatures, module, ancestors, udc, duc, namespace
)
debug(pformat(blocks))
......@@ -418,6 +413,11 @@ def analyse_aot(code, pathfile, backend_name):
name_func
] = extract_returns_annotation(fdef.returns, namespace)
for signatures in annotations["__in_comments__"].values():
replace_strings_by_objects(
signatures, module, ancestors, udc, duc, namespace
)
debug("annotations:\n" + pformat(annotations))
return boosted_dicts, code_dependance, annotations, blocks, code_ext
......@@ -6,7 +6,6 @@
import gast as ast
from transonic.analyses.util import gather_rawcode_comments
from transonic.analyses.objects_from_str import replace_strings_by_objects
class BlockDefinition:
......@@ -15,15 +14,11 @@ class BlockDefinition:
def __init__(self, **kwargs):
self.kwargs = kwargs
self.__dict__.update(**kwargs)
self.signatures = get_signatures_from_comments(self.comments)
def __repr__(self):
return repr(self.kwargs)
def parse_comments(self, namespace=None, info_analysis=None):
self.signatures = get_signatures_from_comments(
self.comments, namespace, info_analysis
)
def get_block_definitions(code, module, ancestors, duc, udc):
"""Get all "if" block definitions"""
......@@ -119,12 +114,8 @@ def find_index_closing_parenthesis(string: str):
raise SyntaxError(f"Transonic syntax error for string {string}")
def get_signatures_from_comments(
comments, namespace_from_code=None, info_analysis=None
):
def get_signatures_from_comments(comments):
"""Get the blocks signatures for a block"""
if namespace_from_code is None:
namespace_from_code = {}
comments = comments.replace("#", "").replace("\n", "")
......@@ -139,39 +130,19 @@ def get_signatures_from_comments(
sig = sig[1 : find_index_closing_parenthesis(sig)]
signatures.append(sig)
types_NameError = set()
signatures_tmp = signatures
signatures = []
for sig_str in signatures_tmp:
sig_dict = {}
signature = {}
type_vars_strs = [tmp.strip() for tmp in sig_str.split(";")]
type_vars_strs = [tmp for tmp in type_vars_strs if tmp]
for type_vars_str in type_vars_strs:
type_, vars_str = type_vars_str.strip().split(" ", 1)
if type_ in namespace_from_code:
type_ = namespace_from_code[type_]
else:
try:
type_ = eval(type_)
except NameError:
types_NameError.add(type_)
except (SyntaxError, TypeError):
pass
type_as_str, vars_str = type_vars_str.strip().split(" ", 1)
for var_str in vars_str.split(","):
var_str = var_str.strip()
sig_dict[var_str] = type_
signatures.append(sig_dict)
if types_NameError:
replace_strings_by_objects(
signatures,
types_NameError,
info_analysis["def_nodes"],
info_analysis["module"],
info_analysis["ancestors"],
info_analysis["udc"],
info_analysis["duc"],
)
signature[var_str] = type_as_str
signatures.append(signature)
return signatures
......@@ -18,9 +18,7 @@ def find_last_def_node(variable, module):
raise RuntimeError
def find_def_code(
variables: set, def_nodes: list, module: dict, ancestors, udc, duc
):
def find_def_code(variables: set, module: dict, ancestors, udc, duc):
nodes_def_vars = [
find_last_def_node(variable, module) for variable in variables
......@@ -28,7 +26,7 @@ def find_def_code(
nodes_def_vars.sort(key=lambda x: x.lineno)
capturex = CaptureX(
list(nodes_def_vars) + def_nodes,
list(nodes_def_vars),
module,
ancestors,
defuse_chains=duc,
......@@ -47,23 +45,37 @@ def find_def_code(
return "\n".join(lines_ext)
def create_objects_from_names(names, def_nodes, module, ancestors, udc, duc):
def create_objects_from_names(names, module, ancestors, udc, duc):
"""create a namespace for the variables defined at the module level"""
code_def_var = find_def_code(names, def_nodes, module, ancestors, udc, duc,)
code_def_var = find_def_code(names, module, ancestors, udc, duc,)
namespace = {}
exec(code_def_var, namespace)
return namespace
def replace_strings_by_objects(
signatures: dict, names, def_nodes, module, ancestors, udc, duc
signatures: dict, module, ancestors, udc, duc, namespace_from_code
):
"""Replace strings in signatures by objects defined in the code"""
types_NameError = set()
for signature in signatures:
for var_str, type_as_str in signature.items():
if type_as_str in namespace_from_code:
signature[var_str] = namespace_from_code[type_as_str]
else:
try:
signature[var_str] = eval(type_as_str)
except NameError:
types_NameError.add(type_as_str)
except (SyntaxError, TypeError):
pass
namespace = create_objects_from_names(
names, def_nodes, module, ancestors, udc, duc
types_NameError, module, ancestors, udc, duc
)
for signature in signatures:
for var_str, type_as_str in signature.items():
if type_as_str in names:
if type_as_str in types_NameError:
signature[var_str] = namespace[type_as_str]
......@@ -21,9 +21,7 @@ def setup_module():
make_backend_files(transonic_src_paths)
@pytest.mark.skipif(
backend_default == "python", reason="Speedup Python backend tests"
)
@pytest.mark.skipif(backend_default != "pythran", reason="Speedup tests")
@pytest.mark.skipif(not path_data_tests.exists(), reason="no data tests")
@pytest.mark.skipif(nb_proc > 1, reason="No build_ext in MPI")
def test_buildext():
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment