From 6835d03a9a307ef1624c8e97d826c42f9e2437d2 Mon Sep 17 00:00:00 2001 From: Marion Date: Thu, 15 Aug 2013 19:46:04 +0000 Subject: use utility function for extracting values out of ASTs - Fix a bug that was accidentally introduced in one of the previous commits. --- diff --git a/TurtleArt/taprimitive.py b/TurtleArt/taprimitive.py index e05460b..1fbeac9 100644 --- a/TurtleArt/taprimitive.py +++ b/TurtleArt/taprimitive.py @@ -588,25 +588,15 @@ class Primitive(object): Otherwise, convert both to a number. (Colors are converted to an integer before they are converted to a string.) """ convert_to_ast = False + (value1_ast, value2_ast) = (None, None) - def _get_value_and_ast(val): - if isinstance(val, ast.AST): - val_ast = val - val = None - if isinstance(val_ast, ast.Num): - val = val_ast.n - elif isinstance(val_ast, ast.Str): - val = val_ast.s - elif isinstance(val_ast, ast.List): - val = val_ast.elts - elif (isinstance(val_ast, ast.Name) and - val_ast.id.startswith("CONSTANTS[")): - val = eval(val_ast.id) - return (val, val_ast, True) - else: - return (val, None, False) - (value1, value1_ast, convert_to_ast) = _get_value_and_ast(value1) - (value2, value2_ast, convert_to_ast) = _get_value_and_ast(value2) + if isinstance(value1, ast.AST): + convert_to_ast = True + value1_ast = value1 + value1 = ast_to_value(value1_ast) + if isinstance(value2, ast.AST): + value2_ast = value2 + value2 = ast_to_value(value2_ast) def _to_string(val, val_ast): """ Return strings as they are, convert Colors to an integer and @@ -687,16 +677,9 @@ class Primitive(object): if isinstance(value, ast.AST): convert_to_ast = True value_ast = value - value = None - if isinstance(value_ast, ast.Str): - value = value_ast.s - elif isinstance(value_ast, ast.List): - value = value_ast.elts - elif (isinstance(value_ast, ast.Name) and - value_ast.id.startswith("CONSTANTS[")): - value = eval(value_ast.id) - if isinstance(decimal_point, ast.Str): - decimal_point = decimal_point.s + value = ast_to_value(value_ast) + if isinstance(decimal_point, ast.AST): + decimal_point = ast_to_value(decimal_point) # 2./3. string if isinstance(value, basestring): @@ -816,11 +799,9 @@ class Primitive(object): if isinstance(value, ast.AST): convert_to_ast = True value_ast = value - value = None - if isinstance(value_ast, ast.Str): - value = value_ast.s - if isinstance(decimal_point, ast.Str): - decimal_point = decimal_point.s + value = ast_to_value(value_ast) + if isinstance(decimal_point, ast.AST): + decimal_point = ast_to_value(decimal_point) if isinstance(value, basestring): # 1. string containing a number @@ -873,13 +854,6 @@ class Primitive(object): -class PrimitiveCall(Primitive): - """ Primitive that is called when it is a constant argument to another - Primitive. """ - pass - - - def is_instancemethod(method): # TODO how to access the type `instancemethod` directly? return type(method).__name__ == "instancemethod" @@ -923,7 +897,10 @@ def value_to_ast(value, *args_for_prim, **kwargs_for_prim): return ast.List(elts=ast_list, ctx=ast.Load) elif isinstance(value, Color): if str(value) in CONSTANTS: - return ast.Name(id='CONSTANTS[%s]' % repr(value), ctx=ast.Load) + # repr(str(value)) is necessary; it first converts the Color to a + # string and then adds appropriate quotes around that string + return ast.Name(id='CONSTANTS[%s]' % repr(str(value)), + ctx=ast.Load) else: # call to the Color constructor with this object's values, # e.g., Color('red', 0, 50, 100) @@ -932,7 +909,6 @@ def value_to_ast(value, *args_for_prim, **kwargs_for_prim): else: raise ValueError("unknown type of raw value: " + repr(type(value))) -# TODO replace every fiddling with a call to this function def ast_to_value(ast_object): """ Retrieve the value out of a value AST. Supported AST types: Num, Str, Name, List, Tuple, Set -- cgit v0.9.1