From 71b02e3044d1a686a58f895d8a0431104b2c1d45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A1ssio=20Botaro?= Date: Sun, 3 May 2015 13:35:05 -0300 Subject: [PATCH 1/4] Maintain backward compatibility Little change to maintain backward compatibility, related to #590 --- gluon/storage.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/gluon/storage.py b/gluon/storage.py index 3b0780fd..117ccac1 100644 --- a/gluon/storage.py +++ b/gluon/storage.py @@ -275,7 +275,7 @@ class List(list): instead of `IndexOutOfBounds`. """ - def __call__(self, i, default=None, cast=None, otherwise=None): + def __call__(self, i, default=DEFAULT, cast=None, otherwise=None): """Allows to use a special syntax for fast-check of `request.args()` validity Args: @@ -291,7 +291,9 @@ class List(list): request.args(0,default=0,cast=int,otherwise='http://error_url') request.args(0,default=0,cast=int,otherwise=lambda:...) """ - value = self[i] or default + value = self[i] + if not value and default is not DEFAULT: + value, cast = default, False try: if cast: value = cast(value) From ccc4b96709edef936f29cf51c2b199fda1b93e6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A1ssio=20Botaro?= Date: Sun, 3 May 2015 13:36:24 -0300 Subject: [PATCH 2/4] Added one more test Added one more test to avoid mistake with backward compatibility --- gluon/tests/test_storage.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/gluon/tests/test_storage.py b/gluon/tests/test_storage.py index 804a3fb8..328f1e09 100644 --- a/gluon/tests/test_storage.py +++ b/gluon/tests/test_storage.py @@ -138,6 +138,8 @@ class TestList(unittest.TestCase): self.assertEqual(a(3, cast=int), 1234) a.append('x') self.assertRaises(HTTP, a, 4, cast=int) + b = List() + self.assertEqual(b(0, cast=int, default=None), None) if __name__ == '__main__': From f6db7c995f162c50b20b7cf857b543c37f5d3848 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A1ssio=20Botaro?= Date: Sun, 3 May 2015 14:02:11 -0300 Subject: [PATCH 3/4] Its necessary because of default=None trick --- gluon/storage.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gluon/storage.py b/gluon/storage.py index 117ccac1..4a1e46a7 100644 --- a/gluon/storage.py +++ b/gluon/storage.py @@ -293,7 +293,7 @@ class List(list): """ value = self[i] if not value and default is not DEFAULT: - value, cast = default, False + value, cast, otherwise = default, False, False try: if cast: value = cast(value) From c36c39178610867330ebf1927a2a3edeb7dcb35e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A1ssio=20Botaro?= Date: Sun, 3 May 2015 14:03:26 -0300 Subject: [PATCH 4/4] More test to prove backward compatibility --- gluon/tests/test_storage.py | 1 + 1 file changed, 1 insertion(+) diff --git a/gluon/tests/test_storage.py b/gluon/tests/test_storage.py index 328f1e09..e518e4d2 100644 --- a/gluon/tests/test_storage.py +++ b/gluon/tests/test_storage.py @@ -140,6 +140,7 @@ class TestList(unittest.TestCase): self.assertRaises(HTTP, a, 4, cast=int) b = List() self.assertEqual(b(0, cast=int, default=None), None) + self.assertEqual(b(0, cast=int, default=None, otherwise='something'), None) if __name__ == '__main__':