From 1581fb530536edb19848817476c2c193416c2286 Mon Sep 17 00:00:00 2001 From: Mark Huang Date: Fri, 8 Sep 2006 19:44:31 +0000 Subject: [PATCH] - add min and max validation to type_check routine --- PLC/Method.py | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/PLC/Method.py b/PLC/Method.py index f830776..54e5774 100644 --- a/PLC/Method.py +++ b/PLC/Method.py @@ -4,7 +4,7 @@ # Mark Huang # Copyright (C) 2006 The Trustees of Princeton University # -# $Id$ +# $Id: Method.py,v 1.1 2006/09/06 15:36:07 mlhuang Exp $ # import xmlrpclib @@ -198,7 +198,7 @@ class Method: return (min_args, max_args, defaults) - def type_check(self, name, value, expected): + def type_check(self, name, value, expected, min = None, max = None): """ Checks the type of the named value against the expected type, which may be a Python type, a typed value, a Parameter, a @@ -232,6 +232,8 @@ class Method: # Get actual expected type from within the Parameter structure elif isinstance(expected, Parameter): + min = expected.min + max = expected.max expected = expected.type expected_type = python_type(expected) @@ -252,6 +254,20 @@ class Method: xmlrpc_type(type(value))), name) + # If a minimum or maximum (length, value) has been specified + if expected_type in StringTypes: + if min is not None and \ + len(value.encode(self.api.encoding)) < min: + raise PLCInvalidArgument, "%s must be at least %d bytes long" % (name, min) + if max is not None and \ + len(value.encode(self.api.encoding)) > max: + raise PLCInvalidArgument, "%s must be at most %d bytes long" % (name, max) + else: + if min is not None and value < min: + raise PLCInvalidArgument, "%s must be > %s" % (name, str(min)) + if max is not None and value > max: + raise PLCInvalidArgument, "%s must be < %s" % (name, str(max)) + # If a list with particular types of items is expected if isinstance(expected, (list, tuple)): for i in range(len(value)): -- 2.43.0