Skip to content

Commit

Permalink
Simplify most uses of reraise via a helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
jamadden committed Feb 14, 2017
1 parent eb210fc commit 1dbc242
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 22 deletions.
7 changes: 6 additions & 1 deletion ZConfig/_compat.py
Expand Up @@ -89,6 +89,11 @@ def exec_(code, globs=None, locs=None): #pragma NO COVER
raise tp, value, tb
""")

import abc

def raise_with_same_tb(exception):
"Raise an exception having the current traceback (if there is one)"
reraise(type(exception), exception, sys.exc_info()[2])

import abc
# workaround the metaclass diff in Py2/Py3
AbstractBaseClass = abc.ABCMeta('AbstractBaseClass', (object,), {})
8 changes: 5 additions & 3 deletions ZConfig/cfgparser.py
Expand Up @@ -18,7 +18,7 @@
import ZConfig.url

from ZConfig.substitution import isname, substitute
from ZConfig._compat import reraise
from ZConfig._compat import raise_with_same_tb

class ZConfigParser(object):

Expand Down Expand Up @@ -170,8 +170,10 @@ def replace(self, text):
raise

def error(self, message):
v = ZConfig.ConfigurationSyntaxError(message, self.url, self.lineno)
reraise(type(v), v, sys.exc_info()[2])
raise_with_same_tb(
ZConfig.ConfigurationSyntaxError(
message, self.url, self.lineno))


def _normalize_case(self, string):
# This method is factored out solely to allow subclasses to modify
Expand Down
11 changes: 5 additions & 6 deletions ZConfig/cmdline.py
Expand Up @@ -30,7 +30,7 @@ class from the :mod:`ZConfig.loader` module. This provides support for
import ZConfig.loader
import ZConfig.matcher

from ZConfig._compat import reraise
from ZConfig._compat import raise_with_same_tb

class ExtendedConfigLoader(ZConfig.loader.ConfigLoader):
"""A :class:`~.ConfigLoader` subclass that adds support for
Expand Down Expand Up @@ -118,9 +118,8 @@ def basic_key(self, s, pos):
try:
return self._basic_key(s)
except ValueError as e:
e = ZConfig.ConfigurationSyntaxError(
"could not convert basic-key value: " + str(e), *pos)
reraise(type(e), e, sys.exc_info()[2])
raise_with_same_tb(ZConfig.ConfigurationSyntaxError(
"could not convert basic-key value: " + str(e), *pos))

def add_value(self, name, val, pos):
if name in self.keypairs:
Expand Down Expand Up @@ -185,8 +184,8 @@ def addValue(self, key, value, position):
try:
realkey = self.type.keytype(key)
except ValueError as e:
dce = ZConfig.DataConversionError(e, key, position)
reraise(type(dce), dce, sys.exc_info()[2])
raise_with_same_tb(ZConfig.DataConversionError(e, key, position))

if realkey in self.optionbag:
return
ZConfig.matcher.BaseMatcher.addValue(self, key, value, position)
Expand Down
4 changes: 2 additions & 2 deletions ZConfig/loader.py
Expand Up @@ -29,6 +29,7 @@
import ZConfig.url

from ZConfig._compat import reraise
from ZConfig._compat import raise_with_same_tb
from ZConfig._compat import urllib2
from ZConfig._compat import AbstractBaseClass
from ZConfig._compat import pathname2url
Expand Down Expand Up @@ -242,8 +243,7 @@ def _raise_open_error(self, url, message):
error = ZConfig.ConfigurationError(
"error opening %s %s: %s" % (what, ident, message),
url)

reraise(type(error), error, sys.exc_info()[2])
raise_with_same_tb(error)

def normalizeURL(self, url):
"""Return a URL for *url*
Expand Down
14 changes: 7 additions & 7 deletions ZConfig/matcher.py
Expand Up @@ -18,7 +18,7 @@
import ZConfig

from ZConfig.info import ValueInfo
from ZConfig._compat import reraise
from ZConfig._compat import raise_with_same_tb


class BaseMatcher(object):
Expand Down Expand Up @@ -191,9 +191,9 @@ def constuct(self):
try:
s = st.datatype(s)
except ValueError as e:
dce = ZConfig.DataConversionError(
e, s, (-1, -1, None))
reraise(type(dce), dce, sys.exc_info()[2])
raise_with_same_tb(ZConfig.DataConversionError(
e, s, (-1, -1, None)))

v.append(s)
elif ci.name == '+':
v = values[attr]
Expand All @@ -207,9 +207,9 @@ def constuct(self):
try:
v = st.datatype(values[attr])
except ValueError as e:
dce = ZConfig.DataConversionError(
e, values[attr], (-1, -1, None))
reraise(type(dce), dce, sys.exc_info()[2])
raise_with_same_tb(ZConfig.DataConversionError(
e, values[attr], (-1, -1, None)))

else:
v = None
elif name == '+':
Expand Down
5 changes: 2 additions & 3 deletions ZConfig/schema.py
Expand Up @@ -22,7 +22,7 @@
from ZConfig import info
from ZConfig import url

from ZConfig._compat import reraise
from ZConfig._compat import raise_with_same_tb

BLANK = u''

Expand Down Expand Up @@ -469,8 +469,7 @@ def initerror(self, e):
return e

def error(self, message):
e = self.initerror(ZConfig.SchemaError(message))
reraise(type(e), e, sys.exc_info()[2])
raise_with_same_tb(self.initerror(ZConfig.SchemaError(message)))


class SchemaParser(BaseParser):
Expand Down

0 comments on commit 1dbc242

Please sign in to comment.