Skip to content

Commit 81db8ab

Browse files
committed
Re-add removed compat helpers
These were dropped in 7d62575 but that should have resulted in a major release since it was an API break. Re-add them temporarily until we release a v3.0.0. Signed-off-by: Stephen Finucane <stephen@that.guru>
1 parent 0ba2f4d commit 81db8ab

File tree

2 files changed

+76
-1
lines changed

2 files changed

+76
-1
lines changed

tests/test_compat.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
import ast
66
import io
77
import sys
8+
import traceback
89

910
import testtools
1011
from testtools.compat import (
12+
reraise,
1113
text_repr,
1214
unicode_output_stream,
1315
)
@@ -216,6 +218,43 @@ def test_unicode_examples_multiline(self):
216218
self.assertEqual(ast.literal_eval(actual), u)
217219

218220

221+
class TestReraise(testtools.TestCase):
222+
"""Tests for trivial reraise wrapper needed for Python 2/3 changes"""
223+
224+
def test_exc_info(self):
225+
"""After reraise exc_info matches plus some extra traceback"""
226+
try:
227+
raise ValueError("Bad value")
228+
except ValueError:
229+
_exc_info = sys.exc_info()
230+
try:
231+
reraise(*_exc_info)
232+
except ValueError:
233+
_new_exc_info = sys.exc_info()
234+
self.assertIs(_exc_info[0], _new_exc_info[0])
235+
self.assertIs(_exc_info[1], _new_exc_info[1])
236+
expected_tb = traceback.extract_tb(_exc_info[2])
237+
self.assertEqual(
238+
expected_tb, traceback.extract_tb(_new_exc_info[2])[-len(expected_tb) :]
239+
)
240+
241+
def test_custom_exception_no_args(self):
242+
"""Reraising does not require args attribute to contain params"""
243+
244+
class CustomException(Exception):
245+
"""Exception that expects and sets attrs but not args"""
246+
247+
def __init__(self, value):
248+
Exception.__init__(self)
249+
self.value = value
250+
251+
try:
252+
raise CustomException("Some value")
253+
except CustomException:
254+
_exc_info = sys.exc_info()
255+
self.assertRaises(CustomException, reraise, *_exc_info)
256+
257+
219258
def test_suite():
220259
from unittest import TestLoader
221260

testtools/compat.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,45 @@
1212
import codecs
1313
import io
1414
import sys
15+
import types
1516
import unicodedata
17+
import warnings
1618
from io import BytesIO, StringIO # for backwards-compat
17-
from typing import IO
19+
from typing import IO, Any, NoReturn
20+
21+
22+
def reraise(
23+
exc_class: type[BaseException],
24+
exc_obj: BaseException,
25+
exc_tb: types.TracebackType,
26+
_marker: Any = object(),
27+
) -> NoReturn:
28+
"""Re-raise an exception received from sys.exc_info() or similar."""
29+
warnings.warn(
30+
"This is not necessary in Python 3.",
31+
DeprecationWarning,
32+
stacklevel=2,
33+
)
34+
raise exc_obj.with_traceback(exc_tb)
35+
36+
37+
def _u(s: str) -> str:
38+
warnings.warn(
39+
"This is not necessary in Python 3.",
40+
DeprecationWarning,
41+
stacklevel=2,
42+
)
43+
return s
44+
45+
46+
def _b(s: str) -> bytes:
47+
"""A byte literal."""
48+
warnings.warn(
49+
"This is not necessary in Python 3.",
50+
DeprecationWarning,
51+
stacklevel=2,
52+
)
53+
return s.encode("latin-1")
1854

1955

2056
def _slow_escape(text: str) -> str:

0 commit comments

Comments
 (0)