diff options
Diffstat (limited to 'validate.py')
-rw-r--r-- | validate.py | 47 |
1 files changed, 27 insertions, 20 deletions
diff --git a/validate.py b/validate.py index 786d7c0..6d50921 100644 --- a/validate.py +++ b/validate.py @@ -141,28 +141,26 @@ def get_types(location, strfmt): class WrongNumberOfVars(FormatStringError): - def __init__(self, location, format_string, exp_types, actual_types): + def __init__(self, location, format_string, exp_types, num_args): FormatStringError.__init__(self, location, format_string) self.exp_types = exp_types - self.actual_types = actual_types + self.num_args = num_args class NotEnoughVars(WrongNumberOfVars): def _get_desc(self): - return 'Not enough arguments in "%s" : expected %i (%s), but got %i (%s)' % ( + return 'Not enough arguments in "%s" : expected %i (%s), but got %i' % ( self.format_string, len(self.exp_types), self.exp_types, - len(self.actual_types), - self.actual_types) + self.num_args) class TooManyVars(WrongNumberOfVars): def _get_desc(self): - return 'Too many arguments in "%s": expected %i (%s), but got %i (%s)' % ( + return 'Too many arguments in "%s": expected %i (%s), but got %i' % ( self.format_string, len(self.exp_types), self.exp_types, - len(self.actual_types), - self.actual_types) + self.num_args) class MismatchingType(FormatStringError): def __init__(self, location, format_string, arg_num, exp_type, actual_type): @@ -194,24 +192,33 @@ def type_equality(t1, t2): return False -def validate_types(location, format_string, actual_types): +def validate_type(location, format_string, index, actual_num_args, actual_type): if False: - print 'validate_types(%s, %s, %s)' % ( - repr(location), repr(format_string), repr(actual_types)) + print 'validate_types(%s, %s, %s, %s, %s)' % ( + repr(location), + repr(format_string), + repr(index), + repr(actual_num_args), + repr(actual_type)) + try: exp_types = get_types(location, format_string[1:-1]) # strip leading and trailing " chars - if len(actual_types) < len(exp_types): - raise NotEnoughVars(location, format_string, exp_types, actual_types) - if len(actual_types) > len(exp_types): - raise TooManyVars(location, format_string, exp_types, actual_types) - for i, (exp, actual) in enumerate(zip(exp_types, actual_types)): - if not type_equality(exp, actual): - raise MismatchingType(location, format_string, i+1, exp, actual) + if actual_num_args < len(exp_types): + raise NotEnoughVars(location, format_string, exp_types, actual_num_args) + if actual_num_args > len(exp_types): + raise TooManyVars(location, format_string, exp_types, actual_num_args) + exp_type = exp_types[index] + if not type_equality(exp_type, actual_type): + raise MismatchingType(location, format_string, index+1, exp_type, actual_type) except CExtensionError, err: print err if False: - print 'validate_types(%s, %s, %s)' % ( - repr(location), repr(format_string), repr(actual_types)) + print 'validate_types(%s, %s, %s, %s, %s)' % ( + repr(location), + repr(format_string), + repr(index), + repr(actual_num_args), + repr(actual_type)) return 1 return 0 |