From 68b9aaace4f14665c4e8ec98fd182a21cd5de76f Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Thu, 27 Nov 2014 20:44:48 +0100 Subject: [PATCH] Add utils.raises. Closes #274. --- qutebrowser/test/utils/test_utils.py | 41 ++++++++++++++++++++++++++++ qutebrowser/utils/urlutils.py | 17 ++---------- qutebrowser/utils/utils.py | 19 +++++++++++++ 3 files changed, 63 insertions(+), 14 deletions(-) diff --git a/qutebrowser/test/utils/test_utils.py b/qutebrowser/test/utils/test_utils.py index 296add4d7..5e9e2ff0b 100644 --- a/qutebrowser/test/utils/test_utils.py +++ b/qutebrowser/test/utils/test_utils.py @@ -340,5 +340,46 @@ class IsEnumTests(unittest.TestCase): self.assertFalse(utils.is_enum(23)) +class RaisesTests(unittest.TestCase): + + """Test raises.""" + + def do_raise(self): + raise Exception + + def do_nothing(self): + pass + + def test_raises_single_exc_true(self): + """Test raises with a single exception which gets raised.""" + self.assertTrue(utils.raises(ValueError, int, 'a')) + + def test_raises_single_exc_false(self): + """Test raises with a single exception which does not get raised.""" + self.assertFalse(utils.raises(ValueError, int, '1')) + + def test_raises_multiple_exc_true(self): + """Test raises with multiple exceptions which get raised.""" + self.assertTrue(utils.raises((ValueError, TypeError), int, 'a')) + self.assertTrue(utils.raises((ValueError, TypeError), int, None)) + + def test_raises_multiple_exc_false(self): + """Test raises with multiple exceptions which do not get raised.""" + self.assertFalse(utils.raises((ValueError, TypeError), int, '1')) + + def test_no_args(self): + """Test with no args and an exception which gets raised.""" + self.assertTrue(utils.raises(Exception, self.do_raise)) + + def test_no_args(self): + """Test with no args and an exception which does not get raised.""" + self.assertFalse(utils.raises(Exception, self.do_nothing)) + + def test_unrelated_exception(self): + """Test with an unrelated exception.""" + with self.assertRaises(Exception): + utils.raises(ValueError, self.do_raise) + + if __name__ == '__main__': unittest.main() diff --git a/qutebrowser/utils/urlutils.py b/qutebrowser/utils/urlutils.py index b48e8a413..2b0b1e4bd 100644 --- a/qutebrowser/utils/urlutils.py +++ b/qutebrowser/utils/urlutils.py @@ -28,7 +28,7 @@ from PyQt5.QtCore import QUrl from PyQt5.QtNetwork import QHostInfo from qutebrowser.config import config -from qutebrowser.utils import log, qtutils, message +from qutebrowser.utils import log, qtutils, message, utils from qutebrowser.commands import cmdexc @@ -68,18 +68,6 @@ def _get_search_url(txt): return url -def _is_numeric(s): - """Check if the given string is some valid number.""" - try: - int(s, 0) - except ValueError: - try: - float(s) - except ValueError: - return False - return True - - def _is_url_naive(urlstr): """Naive check if given URL is really a URL. @@ -101,7 +89,8 @@ def _is_url_naive(urlstr): # Qt treats things like "23.42" or "1337" or "0xDEAD" as valid URLs # which we don't want to. Note we already filtered *real* valid IPs # above. - if _is_numeric(urlstr): + if ((not utils.raises(ValueError, int, urlstr, 0)) or + (not utils.raises(ValueError, float, urlstr))): return False if not url.isValid(): diff --git a/qutebrowser/utils/utils.py b/qutebrowser/utils/utils.py index 3d3f6b90e..da9ccd40b 100644 --- a/qutebrowser/utils/utils.py +++ b/qutebrowser/utils/utils.py @@ -544,3 +544,22 @@ def qualname(obj): return "{}.{}".format(obj.__module__, name) else: return name + + +def raises(exc, func, *args): + """Check if a function raises a given exception. + + Args: + exc: A single exception or an iterable of exceptions. + func: A function to call. + *args: The arguments to pass to the function. + + Returns: + True if the exception was raised, False otherwise. + """ + try: + func(*args) + except exc: + return True + else: + return False