Skip to content

Commit c1a7e15

Browse files
committed
Add raise_with_traceback(); describe this and raise_ in changelog
Also add an alias of reraise to raise_ for backward compatibility
1 parent 8192280 commit c1a7e15

2 files changed

Lines changed: 31 additions & 2 deletions

File tree

docs/changelog.rst

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,17 @@ or just those you need::
4949
>>> from future import open, str
5050

5151

52-
Deprecated isinstance removed
53-
-----------------------------
52+
Utility functions for raising exceptions with a traceback portably
53+
------------------------------------------------------------------
54+
55+
The functions ``raise_with_traceback()`` and ``raise_`` were added to
56+
``future.utils`` to offer either the Python 3.x or Python 2.x behaviour
57+
for raising exceptions. Thanks to Joel Tratner for the contribution of
58+
these.
59+
60+
61+
Deprecated ``isinstance`` replacement removed
62+
---------------------------------------------
5463

5564
``future`` v0.8.2 briefly introduced a replacement for the ``isinstance``
5665
builtin. This was then removed and its use was deprecated as of v0.9.0.

future/utils/__init__.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,13 +328,33 @@ def raise_(tp, value=None, tb=None):
328328
if exc.__traceback__ is not tb:
329329
raise exc.with_traceback(tb)
330330

331+
def raise_with_traceback(exc, traceback=Ellipsis):
332+
if traceback == Ellipsis:
333+
_, _, traceback = sys.exc_info()
334+
raise exc.with_traceback(traceback)
335+
331336
else:
332337
exec('''
333338
def raise_(tp, value=None, tb=None):
334339
raise tp, value, tb
340+
341+
def raise_with_traceback(exc, traceback=Ellipsis):
342+
if traceback == Ellipsis:
343+
_, _, traceback = sys.exc_info()
344+
raise exc, None, traceback
335345
'''.strip())
336346

337347

348+
raise_with_traceback.__doc__ = (
349+
"""Raise exception with existing traceback.
350+
If traceback is not passed, uses sys.exc_info() to get traceback."""
351+
)
352+
353+
354+
# Deprecated alias for backward compatibility with ``future`` versions < 0.11:
355+
reraise = raise_
356+
357+
338358
def implements_iterator(cls):
339359
'''
340360
From jinja2/_compat.py. License: BSD.

0 commit comments

Comments
 (0)