From 676ce91131ca56de318a6459d2ec697e9d6b1f74 Mon Sep 17 00:00:00 2001 From: Marion Date: Wed, 28 Aug 2013 08:41:07 +0000 Subject: better handling for TATypeErrors coming from the type system --- (limited to 'TurtleArt/tatype.py') diff --git a/TurtleArt/tatype.py b/TurtleArt/tatype.py index d4fec2b..6b0acd6 100644 --- a/TurtleArt/tatype.py +++ b/TurtleArt/tatype.py @@ -195,22 +195,34 @@ def get_call_ast(func_name, args=None, keywords=None): class TATypeError(BaseException): """ TypeError with the types from the hierarchy, not with Python types """ - def __init__(self, message, type1=None, type2=None): - """ message -- the error message (type1 and type2 are inserted - automatically iff message contains '%s' for them) """ + def __init__(self, bad_value, bad_type=None, req_type=None, message=''): + """ bad_value -- the mis-typed value that caused the error + bad_type -- the type of the bad_value + req_type -- the type that the value was expected to have + message -- short statement about the cause of the error. It is + not shown to the user, but may appear in debugging output. """ + self.bad_value = bad_value + self.bad_type = bad_type + self.req_type = req_type self.message = message - self.type1 = type1 - self.type2 = type2 def __str__(self): - num = self.message.count('%s') - if num == 2: - msg = self.message % (self.type1, self.type2) - elif num == 1: - msg = self.message % (self.type1) - else: - msg = self.message - return "TA TypeError: " + msg + msg = [] + if self.message: + msg.append(self.message) + msg.append(" (") + msg.append("bad value: ") + msg.append(repr(self.bad_value)) + if self.bad_type is not None: + msg.append(", bad type: ") + msg.append(repr(self.bad_type)) + if self.req_type is not None: + msg.append(", req type: ") + msg.append(repr(self.req_type)) + if self.message: + msg.append(")") + return "".join(msg) + __repr__ = __str__ def get_converter(old_type, new_type): @@ -285,8 +297,9 @@ def convert(x, new_type, old_type=None, converter=None): converter = get_converter(old_type, new_type) if converter is None: # no converter available - raise TATypeError("type %s cannot be converted to type %s" - % (repr(old_type), repr(new_type))) + raise TATypeError(bad_value=x, bad_type=old_type, + req_type=new_type, message=("found no converter" + " for this type combination")) def _apply_converter(converter, y): if is_an_ast: -- cgit v0.9.1