From bbd9e1b52351119a87ca8e174cd091210c620704 Mon Sep 17 00:00:00 2001 From: Martin Zimmermann Date: Thu, 26 Jun 2014 15:33:28 +0200 Subject: [PATCH] add type checking utils --- isso/tests/test_utils_types.py | 27 +++++++++++++++++++++++++++ isso/utils/hash.py | 26 +++++++++----------------- isso/utils/types.py | 15 +++++++++++++++ 3 files changed, 51 insertions(+), 17 deletions(-) create mode 100644 isso/tests/test_utils_types.py create mode 100644 isso/utils/types.py diff --git a/isso/tests/test_utils_types.py b/isso/tests/test_utils_types.py new file mode 100644 index 0000000..3619f2b --- /dev/null +++ b/isso/tests/test_utils_types.py @@ -0,0 +1,27 @@ +# -*- encoding: utf-8 -*- + +from __future__ import unicode_literals + +try: + import unittest2 as unittest +except ImportError: + import unittest + +from isso.compat import string_types +from isso.utils import types + + +class TestTypes(unittest.TestCase): + + def test_require(self): + + try: + types.require("foo", string_types) + except TypeError: + self.assertTrue(False) + + def test_require_raises(self): + + self.assertRaises(TypeError, types.require, 1, bool) + self.assertRaises(TypeError, types.require, 1, str) + diff --git a/isso/utils/hash.py b/isso/utils/hash.py index c35f1a2..e98ed0a 100644 --- a/isso/utils/hash.py +++ b/isso/utils/hash.py @@ -5,6 +5,7 @@ from __future__ import unicode_literals import codecs import hashlib +from isso.utils import types from isso.compat import string_types, text_type as str try: @@ -20,11 +21,6 @@ except ImportError: "to `werkzeug` 0.9 or install `passlib`.") -def _TypeError(name, expected, val): - return TypeError("'{0}' must be {1}, not {2}".format( - name, expected, val.__class__.__name__)) - - class Hash(object): func = None @@ -37,29 +33,25 @@ class Hash(object): self.func = func if salt is not None: - if not isinstance(salt, bytes): - raise _TypeError("salt", "bytes", salt) + types.require(salt, bytes) self.salt = salt def hash(self, val): - """Calculate hash from value (must be bytes).""" + """Calculate hash from value (must be bytes). + """ - if not isinstance(val, bytes): - raise _TypeError("val", "bytes", val) + types.require(val, bytes) rv = self.compute(val) - if not isinstance(val, bytes): - raise _TypeError("val", "bytes", rv) + types.require(rv, bytes) return rv def uhash(self, val): - """Calculate hash from unicode value and return hex value as unicode""" - - if not isinstance(val, string_types): - raise _TypeError("val", "str", val) - + """Calculate hash from unicode value and return hex value as unicode + """ + types.require(val, string_types) return codecs.encode(self.hash(val.encode("utf-8")), "hex_codec").decode("utf-8") def compute(self, val): diff --git a/isso/utils/types.py b/isso/utils/types.py new file mode 100644 index 0000000..bee9b33 --- /dev/null +++ b/isso/utils/types.py @@ -0,0 +1,15 @@ +# -*- encoding: utf-8 -*- + + +def _TypeError(expected, val): + if isinstance(expected, (list, tuple)): + expected = ", ".join(expected.__name__) + else: + expected = expected.__name__ + return TypeError("Expected {0}, not {1}".format( + expected, val.__class__.__name__)) + + +def require(val, expected): + if not isinstance(val, expected): + raise _TypeError(expected, val)