Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/TurtleArt/taexportpython.py
diff options
context:
space:
mode:
authorMarion <marion.zepf@gmail.com>2013-08-01 16:35:58 (GMT)
committer Marion <marion.zepf@gmail.com>2013-08-01 16:35:58 (GMT)
commit555063f706111aaae91fe0e2714feab88c608463 (patch)
tree41464ff7f4286a98bda7a8092708e1bf50332c36 /TurtleArt/taexportpython.py
parente7712483930edb82a57e47e8afd104bfc305ee7f (diff)
show a nice error message if the program contains non-exportable blocks
Diffstat (limited to 'TurtleArt/taexportpython.py')
-rw-r--r--TurtleArt/taexportpython.py22
1 files changed, 10 insertions, 12 deletions
diff --git a/TurtleArt/taexportpython.py b/TurtleArt/taexportpython.py
index 52216d9..4688ea9 100644
--- a/TurtleArt/taexportpython.py
+++ b/TurtleArt/taexportpython.py
@@ -27,7 +27,7 @@ import util.codegen as codegen
#from ast_pprint import * # only used for debugging, safe to comment out
-from taprimitive import Primitive
+from taprimitive import (Primitive, PyExportError)
from tautils import (debug_output, find_top_block)
@@ -126,12 +126,17 @@ def _walk_action_stack(top_block, lc):
arg_asts = []
def _finish_off(block):
+ """ Convert block to an AST and add it to the ast_list. Raise a
+ PyExportError on failure. """
prim = lc.get_prim_callable(block.primitive)
- # TODO fail gracefully if primitive is not a Primitive object
+ # fail gracefully if primitive is not a Primitive object
if not isinstance(prim, Primitive):
- raise TypeError("cannot export block " + repr(block))
+ raise PyExportError("block is not exportable", block=block)
if prim.export_me:
- new_ast = prim.get_ast(*arg_asts)
+ try:
+ new_ast = prim.get_ast(*arg_asts)
+ except ValueError:
+ raise PyExportError("error while exporting block", block=block)
if isinstance(new_ast, (list, tuple)):
ast_list.extend(new_ast)
else:
@@ -182,7 +187,7 @@ def _value_to_ast(value):
elif isinstance(value, basestring):
return ast.Str(value)
else:
- raise TypeError("unknown type of raw value: " + repr(type(value)))
+ raise ValueError("unknown type of raw value: " + repr(type(value)))
def _make_identifier(name):
""" Turn name into a Python identifier name by replacing illegal
@@ -204,10 +209,3 @@ def _indent(code, num_levels=1):
return linesep.join(new_line_list)
-
-if __name__ == "__main__":
- f = open("/bigrepos/sugar-labs/ta-python-export-dev/exported.py", "w")
- f.write(_action_stack_to_python([]))
- f.close()
-
-