From 62158d7861f08534ae13774c9ecbaae15a9223ca Mon Sep 17 00:00:00 2001 From: John Garland Date: Mon, 26 Apr 2010 02:54:13 +1000 Subject: [PATCH] Renamed raiseError to raisesErrorsAs --- deluge/plugins/blocklist/blocklist/common.py | 32 +++++++++++++++---- deluge/plugins/blocklist/blocklist/readers.py | 4 +-- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/deluge/plugins/blocklist/blocklist/common.py b/deluge/plugins/blocklist/blocklist/common.py index d1181ddf5..c2076acae 100644 --- a/deluge/plugins/blocklist/blocklist/common.py +++ b/deluge/plugins/blocklist/blocklist/common.py @@ -36,19 +36,39 @@ import pkg_resources import os.path +from functools import wraps +from sys import exc_info def get_resource(filename): return pkg_resources.resource_filename("blocklist", os.path.join("data", filename)) -def raiseError(error): - def safer(func): - def new(self, *args, **kwargs): +def raisesErrorsAs(error): + """ + Factory class that returns a decorator which wraps + the decorated function to raise all exceptions as + the specified error type + """ + def decorator(func): + """ + Returns a function which wraps the given func + to raise all exceptions as error + """ + @wraps(func) + def wrapper(self, *args, **kwargs): + """ + Wraps the function in a try..except block + and calls it with the specified args + + Raises any exceptions as error preserving the + message and traceback + """ try: return func(self, *args, **kwargs) except: - raise error - return new - return safer + (value, tb) = exc_info()[1:] + raise error, value, tb + return wrapper + return decorator def remove_zeros(ip): """ diff --git a/deluge/plugins/blocklist/blocklist/readers.py b/deluge/plugins/blocklist/blocklist/readers.py index 011b2f2c4..518c6b813 100644 --- a/deluge/plugins/blocklist/blocklist/readers.py +++ b/deluge/plugins/blocklist/blocklist/readers.py @@ -33,7 +33,7 @@ # # -from common import raiseError, remove_zeros +from common import raisesErrorsAs, remove_zeros import re class ReaderParseError(Exception): @@ -82,7 +82,7 @@ class BaseReader(object): blocklist.close() return valid - @raiseError(ReaderParseError) + @raisesErrorsAs(ReaderParseError) def readranges(self): """Yields each ip range from the file""" blocklist = self.open()