Skip to content

Commit 26e7ac7

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 26e7ac7

File tree

2 files changed

+77
-1
lines changed

2 files changed

+77
-1
lines changed

tests/test_compat.py

Lines changed: 40 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,44 @@ def test_unicode_examples_multiline(self):
216218
self.assertEqual(ast.literal_eval(actual), u)
217219

218220

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

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)