Repository: web2py/web2py

Issue:
Bug in gluon/validators.py/is_empty() in Python 3
I was digging around trying to save an edit/sqlform with an upload field and it was failing with "Invalid image" error message. My upload field is using "requires=IS_EMPTY_OR(IS_IMAGE())" validator.
Since the field already have an image and I just want to update others fields it does not make sense to fire this error. Debugging a bit I found that upload field value (from input) is sent empty (b'') and the is_empty() function (called from IS_EMPTY_OR()) is returning "NOT EMPTY" cause only value '' and [] are treated as empty (not b''). It worked fine when I changed the bold line in is_empty() function to:

<pre>
def is_empty(value, empty_regex=None):
    _value = value
    """test empty field"""
    if isinstance(value, (str, unicodeT)):
        value = value.strip()
        if empty_regex is not None and empty_regex.match(value):
            value = ''
    <b>if value is None or value == '' or value == b'' or value == []:</b>
        return (_value, True)
    return (_value, False)
</pre>

Thanks in advance.

Bug in gluon/validators.py/is_empty() in Python 3
I was digging around trying to save an edit/sqlform with an upload field and it was failing with "Invalid image" error message. My upload field is using "requires=IS_EMPTY_OR(IS_IMAGE())" validator.
Since the field already have an image and I just want to update others fields it does not make sense to fire this error. Debugging a bit I found that upload field value (from input) is sent empty (b'') and the is_empty() function (called from IS_EMPTY_OR()) is returning "NOT EMPTY" cause only value '' and [] are treated as empty (not b''). It worked fine when I changed the bold line in is_empty() function to:

<pre>
def is_empty(value, empty_regex=None):
    _value = value
    """test empty field"""
    if isinstance(value, (str, unicodeT)):
        value = value.strip()
        if empty_regex is not None and empty_regex.match(value):
            value = ''
    <b>if value is None or value == '' or value == b'' or value == []:</b>
        return (_value, True)
    return (_value, False)
</pre>

Thanks in advance.



Discussion:
The problem occurs only with Python 3. I tested it with Python 2.7 and it runs fine both versions (web2py 2.15.4 and my updated version).

I'm using web2py 2.15.4 and Python 3.5.

@tiagobar your fix adding "**or value == b**" seems perfectly fine to me and will work with python 3 and 2.7, can you submit a pull request?
The problem occurs only with Python 3. I tested it with Python 2.7 and it runs fine both versions (web2py 2.15.4 and my updated version).

I'm using web2py 2.15.4 and Python 3.5.

@tiagobar your fix adding "**or value == b**" seems perfectly fine to me and will work with python 3 and 2.7, can you submit a pull request?

Resolving patch:
diff --git a/gluon/validators.py b/gluon/validators.py
--- a/gluon/validators.py
+++ b/gluon/validators.py
@@ -996,7 +996,7 @@ def is_empty(value, empty_regex=None):
         value = value.strip()
         if empty_regex is not None and empty_regex.match(value):
             value = ''
-    if value is None or value == '' or value == []:
+    if value is None or value == '' or value == b'' or value == []:
         return (_value, True)
     return (_value, False)
 


Test patch:
diff --git a/gluon/tests/test_validators.py b/gluon/tests/test_validators.py
--- a/gluon/tests/test_validators.py
+++ b/gluon/tests/test_validators.py
@@ -444,6 +444,8 @@ def test_IS_NOT_EMPTY(self):
         self.assertEqual(rtn, (None, 'Enter a value'))
         rtn = IS_NOT_EMPTY()('')
         self.assertEqual(rtn, ('', 'Enter a value'))
+        rtn = IS_NOT_EMPTY()(b'')
+        self.assertEqual(rtn, (b'', 'Enter a value'))
         rtn = IS_NOT_EMPTY()('  ')
         self.assertEqual(rtn, ('  ', 'Enter a value'))
         rtn = IS_NOT_EMPTY()(' \n\t')