Commit eb35dd7c authored by Dom Sekotill's avatar Dom Sekotill
Browse files

Remove the reraise with additional message feature

Appending additional string data to a reraised exception is too prone
to breaking if the 'args' attribute does not faithfully record the
__init__ arguments of an exception, which is apparently all too common.
parent ca63cc21
Loading
Loading
Loading
Loading
Loading
+5 −22
Original line number Diff line number Diff line
@@ -74,25 +74,9 @@ class suppress(object):

	__nonzero__ = __bool__ # python2.7 support

	@staticmethod
	def _append_args(args, extra):
		for arg in args:
			if isinstance(arg, six.string_types) and extra:
				yield '{0}: {1}'.format(arg, extra)
				extra = None
			else:
				yield arg

	def _exc_info(self, extra_msg):
		if self.exc_info[0] is None or extra_msg is None:
			return self.exc_info
		tp, val, tb = self.exc_info
		args = self._append_args(val.args, extra_msg)
		return tp, tp(*args), tb

	def reraise(self, extra_msg=None):
	def reraise(self):
		"""
		Raise the suppressed exception with an optional addendum appended.
		Raise the suppressed exception
		
		In Python 3.x, if called inside an `except` clause, the suppressed
		exception is chained with the current exception.
@@ -100,13 +84,12 @@ class suppress(object):
		if self.exc_info[0] is None:
			return
		sys_exc_info = sys.exc_info()
		exc_info = self._exc_info(extra_msg)
		if sys_exc_info:
			six.raise_from(exc_info[1], sys_exc_info[1])
			six.raise_from(self.exc_info[1], sys_exc_info[1])
		six.reraise(*exc_info)

	@contextmanager
	def reraise_on_error(self, extra_msg=None):
	def reraise_on_error(self):
		"""
		Raise the suppressed exception if another is raised out of the context.

@@ -117,4 +100,4 @@ class suppress(object):
		except (Exception):
			if self.exc_info[0] is None:
				raise
			self.reraise(extra_msg)
			self.reraise()
+1 −2
Original line number Diff line number Diff line
@@ -65,7 +65,6 @@ def import_from(context, items):
		add = (value and value[0] == '+')
		if add:
			value = value[1:]
		supp_msg = "\n{0} = {1}".format(name, value.replace('\n', '\n    '))

		with suppress(ValueError) as suppressed:
			value = json.loads(value)
@@ -84,7 +83,7 @@ def import_from(context, items):
			else:
				LOGGER.debug("setting %s to %s", name, value)

		with suppressed.reraise_on_error(supp_msg):
		with suppressed.reraise_on_error():
			if hasattr(cur, 'extend'):
				if not isinstance(value, list):
					raise ValueError(
+0 −15
Original line number Diff line number Diff line
@@ -77,21 +77,6 @@ class TestSuppress(tests.TestCase):
		suppressed = context_managers.suppress(ValueError, TypeError)
		self.assertIsNone(suppressed.reraise())

	@tests.test_generator([
		[ValueError("foo")],
		[TypeError("foo")],
		[OSError(1, "foo")],
	])
	def reraise_with_additional(self, exc):
		"""Test re-raising appends the optional addendum when given"""
		with context_managers.suppress(Exception) as suppressed:
			raise exc

		try:
			suppressed.reraise("barbaz")
		except (type(exc)) as raised:
			self.assertTrue("barbaz" in str(raised))

	def test_reraise_on_error(self):
		"""Test the reraise_on_error helper context manager"""
		with context_managers.suppress(ValueError, TypeError) as suppressed: