def _validate_multitask(node): validations.raise_if_try_in_node_body(node.body) if validations.check_if_any_in_block(ast.Return, node.body): raise errors.TranslateError( _errormessages.return_unsupported_unless_last) # this validation is the opposite of the usual funcdef check. # we must make sure ALL statements are ast.FunctionDef if any(not isinstance(stmt, ast.FunctionDef) for stmt in node.body): raise errors.TranslateError( _errormessages.return_unsupported_unless_last) if 'items' in dir(node): if len(node.items) > 1 or len(node.items) <= 0: raise errors.TranslateError(_errormessages.invalid_with_block) expr = node.items[0].context_expr opt_var = node.items[0].optional_vars else: expr = node.context_expr opt_var = node.optional_vars if not isinstance(expr, ast.Call): raise errors.TranslateError(_errormessages.invalid_with_block) # make sure the expression is "multitask()" func_name = _get_name_without_namespace_from_node(expr.func) if func_name is not _tasks.multitask.__name__ \ or len(expr.args) > 0 \ or opt_var is None: raise errors.TranslateError(_errormessages.invalid_with_block) return opt_var
def _validate_restrictions(node): if node.handlers or node.orelse: raise TranslateError(_errormessages.invalid_try_except_orelse) if validations.check_if_any_in_block(ast.Return, node.body) or\ validations.check_if_any_in_block(ast.Return, node.finalbody): raise TranslateError( _errormessages.return_not_supported_in_try_finally) validations.raise_if_try_in_node_body(node.body)
def _validate_restrictions(node, resources): if node.orelse: raise errors.TranslateError(_errormessages.unsupported_orelse_while) if validations.check_if_any_in_block(ast.Return, node.body): raise errors.TranslateError( _errormessages.return_unsupported_unless_last) if validations.check_if_any_in_block(ast.FunctionDef, node.body): raise errors.TranslateError(_errormessages.invalid_nested_funcdef) validations.raise_if_try_in_node_body(node.body)
def _validate_restrictions(node): if node.orelse: raise TranslateError(_errormessages.for_else_not_supported) if not isinstance(node.iter, (ast.Name, ast.Attribute, ast.Call)): raise TranslateError(_errormessages.invalid_iterable_collection) if not isinstance(node.target, ast.Name): raise TranslateError(_errormessages.invalid_for_loop_iterator) if validations.check_if_any_in_block(ast.Return, node.body): raise TranslateError(_errormessages.return_unsupported_unless_last) if validations.check_if_any_in_block(ast.FunctionDef, node.body): raise TranslateError(_errormessages.invalid_nested_funcdef) validations.raise_if_try_in_node_body(node.body)
def _validate_task(node, mt_name): body = node.body validations.raise_if_try_in_node_body(body) if validations.check_if_any_in_block(ast.FunctionDef, body): raise errors.TranslateError(_errormessages.invalid_function_definition) if validations.check_if_any_in_block(ast.Return, body): raise errors.TranslateError( _errormessages.return_unsupported_unless_last) if len(node.args.args) > 0: raise errors.TranslateError(_errormessages.invalid_with_block) decs = node.decorator_list if len(decs) is not 1 \ or not(isinstance(decs[0], ast.Call)) \ or _get_name_without_namespace_from_node(decs[0].func) != _decorators.task.__name__ \ or len(decs[0].args) is not 1 \ or decs[0].args[0].id is not mt_name.id: raise errors.TranslateError(_errormessages.invalid_with_block) return node