1
0
mirror of https://github.com/mikf/gallery-dl.git synced 2024-11-22 02:32:33 +01:00

adjust 'util.raises()'

This commit is contained in:
Mike Fährmann 2019-10-28 15:06:17 +01:00
parent d44f790e81
commit d5e3910270
No known key found for this signature in database
GPG Key ID: 5680CA389D365A88
2 changed files with 17 additions and 17 deletions

View File

@ -52,10 +52,10 @@ def advance(iterable, num):
return iterator
def raises(obj):
"""Returns a function that raises 'obj' as exception"""
def wrap():
raise obj
def raises(cls):
"""Returns a function that raises 'cls' as exception"""
def wrap(*args):
raise cls(*args)
return wrap
@ -287,21 +287,21 @@ class UniquePredicate():
class FilterPredicate():
"""Predicate; True if evaluating the given expression returns True"""
globalsdict = {
"parse_int": text.parse_int,
"urlsplit": urllib.parse.urlsplit,
"datetime": datetime.datetime,
"abort": raises(exception.StopExtraction()),
"re": re,
}
def __init__(self, filterexpr, target="image"):
name = "<{} filter>".format(target)
self.codeobj = compile(filterexpr, name, "eval")
self.globals = {
"parse_int": text.parse_int,
"urlsplit" : urllib.parse.urlsplit,
"datetime" : datetime.datetime,
"abort" : raises(exception.StopExtraction),
"re" : re,
}
def __call__(self, url, kwds):
try:
return eval(self.codeobj, self.globalsdict, kwds)
return eval(self.codeobj, self.globals, kwds)
except exception.GalleryDLException:
raise
except Exception as exc:

View File

@ -313,17 +313,17 @@ class TestOther(unittest.TestCase):
util.advance(util.advance(items, 1), 2), range(3, 5))
def test_raises(self):
func = util.raises(Exception())
func = util.raises(Exception)
with self.assertRaises(Exception):
func()
func = util.raises(ValueError(1))
func = util.raises(ValueError)
with self.assertRaises(ValueError):
func()
func(1)
with self.assertRaises(ValueError):
func()
func(2)
with self.assertRaises(ValueError):
func()
func(3)
def test_combine_dict(self):
self.assertEqual(