From 1cc929cd824a0629ce952ea26a2d0da79c2f725c Mon Sep 17 00:00:00 2001 From: Marion Date: Tue, 08 Oct 2013 15:00:14 +0000 Subject: always accept lambda functions as arguments (even though their type is unknown) - more debugging output on TATypeErrors in Primitive --- diff --git a/TurtleArt/taprimitive.py b/TurtleArt/taprimitive.py index e5266a1..112540a 100644 --- a/TurtleArt/taprimitive.py +++ b/TurtleArt/taprimitive.py @@ -22,6 +22,7 @@ import ast from gettext import gettext as _ from math import sqrt from random import uniform +import traceback #from ast_pprint import * # only used for debugging, safe to comment out @@ -184,6 +185,8 @@ class Primitive(object): convert_to_ast=convert_to_ast, call_my_args=call_my_args) except TATypeError as error: + if Primitive._DEBUG: + traceback.print_exc() break else: new_slot_list.append(const) @@ -875,30 +878,38 @@ class ArgSlot(object): for wrapper in wrapper_disjunction: # check if the argument can fill this slot (type-wise) - if wrapper is not None: - arg_types = get_type(wrapper)[0] - bad_value = wrapper - elif func is not None: - arg_types = get_type(func)[0] - bad_value = func - else: - arg_types = get_type(argument)[0] - bad_value = argument - converter = None - if not isinstance(arg_types, TypeDisjunction): - arg_types = TypeDisjunction((arg_types, )) - if isinstance(slot.type, TypeDisjunction): - slot_types = slot.type + # (lambda functions are always accepted) + if getattr(func, '__name__', None) == '': + converter = identity + old_type = TYPE_OBJECT + new_type = slot.type else: - slot_types = TypeDisjunction((slot.type, )) - for old_type in arg_types: - for new_type in slot_types: - converter = get_converter(old_type, new_type) + if wrapper is not None: + arg_types = get_type(wrapper)[0] + bad_value = wrapper + elif func is not None: + arg_types = get_type(func)[0] + bad_value = func + else: + arg_types = get_type(argument)[0] + bad_value = argument + converter = None + if not isinstance(arg_types, TypeDisjunction): + arg_types = TypeDisjunction((arg_types, )) + if isinstance(slot.type, TypeDisjunction): + slot_types = slot.type + else: + slot_types = TypeDisjunction((slot.type, )) + for old_type in arg_types: + for new_type in slot_types: + converter = get_converter(old_type, new_type) + if converter is not None: + break if converter is not None: break - # unable to convert, try next wrapper/ slot/ func - if converter is None: - continue + # unable to convert, try next wrapper/ slot/ func + if converter is None: + continue # 1. (cont'd) call the argument or pass it on as a callable called_argument = argument @@ -912,6 +923,8 @@ class ArgSlot(object): convert_to_ast=convert_to_ast, call_my_args=(slot.call_arg and call_my_args)) except TATypeError as error: + if Primitive._DEBUG: + traceback.print_exc() # on failure, try next wrapper/ slot/ func bad_value = error.bad_value continue @@ -958,6 +971,8 @@ class ArgSlot(object): new_type, old_type=old_type, converter=converter) except TATypeError as error: + if Primitive._DEBUG: + traceback.print_exc() # on failure, try next wrapper/ slot/ func bad_value = wrapped_argument continue -- cgit v0.9.1