From 69c888d0714df6b475844dac184c8dd640652d61 Mon Sep 17 00:00:00 2001 From: mdipierro Date: Mon, 27 May 2013 10:33:37 -0500 Subject: [PATCH] Issue 1506: length argument wrong with unicode --- VERSION | 2 +- gluon/validators.py | 18 ++++++++++++------ 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/VERSION b/VERSION index 37bd1e9d..5bf09298 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -Version 2.4.7-stable+timestamp.2013.05.25.18.35.29 +Version 2.4.7-stable+timestamp.2013.05.27.10.32.53 diff --git a/gluon/validators.py b/gluon/validators.py index 8fa964c3..a7284285 100644 --- a/gluon/validators.py +++ b/gluon/validators.py @@ -315,15 +315,21 @@ class IS_LENGTH(Validator): length = 0 if self.minsize <= length <= self.maxsize: return (value, None) - elif isinstance(value, (str, unicode, list)): + elif isinstance(value, str): + try: + lvalue = len(value.decode('utf8')) + except: + lvalue = len(value) + if self.minsize <= lvalue <= self.maxsize: + return (value, None) + elif isinstance(value, unicode): + if self.minsize <= len(value) <= self.maxsize: + return (value.encode('utf8'), None) + elif isinstance(value, (tuple, list)): if self.minsize <= len(value) <= self.maxsize: return (value, None) elif self.minsize <= len(str(value)) <= self.maxsize: - try: - value.decode('utf8') - return (value, None) - except: - pass + return (str(value), None) return (value, translate(self.error_message) % dict(min=self.minsize, max=self.maxsize))