| 1 |
import inspect |
| 2 |
import logging |
| 3 |
import pickle |
| 4 |
import sys |
| 5 |
import types |
| 6 |
|
| 7 |
from billiard.einfo import _Code # noqa |
| 8 |
from billiard.einfo import _Frame # noqa |
| 9 |
from billiard.einfo import _Object # noqa |
| 10 |
from billiard.einfo import _Truncated # noqa |
| 11 |
from billiard.einfo import ExceptionInfo, Traceback |
| 12 |
|
| 13 |
logger = logging.getLogger(__name__) |
| 14 |
|
| 15 |
|
| 16 |
def test_exception_info_log_before_pickle(caplog): |
| 17 |
try: |
| 18 |
raise RuntimeError("some message") |
| 19 |
except Exception: |
| 20 |
exception = ExceptionInfo().exception |
| 21 |
|
| 22 |
logger.exception("failed", exc_info=exception) |
| 23 |
assert ' raise RuntimeError("some message")' in caplog.text |
| 24 |
assert "RuntimeError: some message" in caplog.text |
| 25 |
|
| 26 |
|
| 27 |
def test_exception_info_log_after_pickle(caplog): |
| 28 |
try: |
| 29 |
raise RuntimeError("some message") |
| 30 |
except Exception: |
| 31 |
exception = ExceptionInfo().exception |
| 32 |
exception = pickle.loads(pickle.dumps(exception)) |
| 33 |
|
| 34 |
logger.exception("failed", exc_info=exception) |
| 35 |
assert ' raise RuntimeError("some message")' in caplog.text |
| 36 |
assert "RuntimeError: some message" in caplog.text |
| 37 |
|
| 38 |
|
| 39 |
def test_exception_info(): |
| 40 |
try: |
| 41 |
raise ValueError("Test Exception") |
| 42 |
except ValueError: |
| 43 |
exc_info = sys.exc_info() |
| 44 |
e_info = ExceptionInfo(exc_info) |
| 45 |
assert isinstance(e_info.type, type) |
| 46 |
assert isinstance(e_info.exception, Exception) |
| 47 |
assert isinstance(e_info.tb, Traceback) |
| 48 |
assert isinstance(e_info.traceback, str) |
| 49 |
|
| 50 |
|
| 51 |
def test_traceback(): |
| 52 |
try: |
| 53 |
raise ValueError("Test Exception") |
| 54 |
except ValueError: |
| 55 |
tb = sys.exc_info()[2] |
| 56 |
trace = Traceback(tb) |
| 57 |
assert isinstance(trace.tb_frame, _Frame) |
| 58 |
assert isinstance(trace.tb_lineno, int) |
| 59 |
assert isinstance(trace.tb_lasti, int) |
| 60 |
assert trace.tb_next is None or isinstance(trace.tb_next, Traceback) |
| 61 |
|
| 62 |
|
| 63 |
def test_frame(): |
| 64 |
try: |
| 65 |
raise ValueError("Test Exception") |
| 66 |
except ValueError: |
| 67 |
tb = sys.exc_info()[2] |
| 68 |
frame = _Frame(tb.tb_frame) |
| 69 |
assert isinstance(frame.f_code, _Code) |
| 70 |
assert isinstance(frame.f_lineno, int) |
| 71 |
assert isinstance(frame.f_lasti, int) |
| 72 |
assert frame.f_globals == { |
| 73 |
"__file__": frame.f_globals.get("__file__", "__main__"), |
| 74 |
"__name__": frame.f_globals.get("__name__"), |
| 75 |
"__loader__": None, |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
def test_code(): |
| 80 |
try: |
| 81 |
raise ValueError("Test Exception") |
| 82 |
except ValueError: |
| 83 |
tb = sys.exc_info()[2] |
| 84 |
frame = tb.tb_frame |
| 85 |
code = _Code(frame.f_code) |
| 86 |
assert isinstance(code.co_filename, str) |
| 87 |
assert isinstance(code.co_name, str) |
| 88 |
assert isinstance(code.co_argcount, int) |
| 89 |
if sys.version_info >= (3, 11): |
| 90 |
assert callable(code.co_positions) |
| 91 |
assert next(code.co_positions()) == (79, 79, 0, 0) |
| 92 |
|
| 93 |
|
| 94 |
def test_object_init(): |
| 95 |
obj = _Object(a=1, b=2, c=3) |
| 96 |
assert obj.a == 1 |
| 97 |
assert obj.b == 2 |
| 98 |
assert obj.c == 3 |
| 99 |
|
| 100 |
|
| 101 |
if sys.version_info >= (3, 11): |
| 102 |
|
| 103 |
def test_object_co_positions(): |
| 104 |
obj = _Object() |
| 105 |
|
| 106 |
default = ((None, None, None, None),) |
| 107 |
# Test that it returns the default co_positions |
| 108 |
assert list(iter(obj.co_positions())) == list(default) |
| 109 |
|
| 110 |
# Test setting co_positions |
| 111 |
new_value = ((1, 2, 3, 4),) |
| 112 |
obj.co_positions = new_value |
| 113 |
assert list(iter(obj.co_positions())) == list(new_value) |
| 114 |
|
| 115 |
def test_truncated_co_positions(): |
| 116 |
truncated = _Truncated() |
| 117 |
|
| 118 |
assert list(iter(truncated.co_positions())) == list( |
| 119 |
iter(truncated.tb_frame.co_positions()) |
| 120 |
) |
| 121 |
|
| 122 |
|
| 123 |
def make_python_tb(): |
| 124 |
tb = None |
| 125 |
depth = 0 |
| 126 |
while True: |
| 127 |
try: |
| 128 |
frame = sys._getframe(depth) |
| 129 |
except ValueError: |
| 130 |
break |
| 131 |
else: |
| 132 |
depth += 1 |
| 133 |
|
| 134 |
tb = types.TracebackType(tb, frame, frame.f_lasti, frame.f_lineno) |
| 135 |
|
| 136 |
assert tb is not None, "Failed to create a traceback object" |
| 137 |
|
| 138 |
return tb |
| 139 |
|
| 140 |
|
| 141 |
def test_isinstance(): |
| 142 |
tb = Traceback(tb=make_python_tb()) |
| 143 |
frame = tb.tb_frame |
| 144 |
code = frame.f_code |
| 145 |
|
| 146 |
assert isinstance(tb, types.TracebackType) |
| 147 |
assert isinstance(tb, Traceback) |
| 148 |
assert isinstance(frame, types.FrameType) |
| 149 |
assert isinstance(frame, _Frame) |
| 150 |
assert isinstance(code, types.CodeType) |
| 151 |
assert isinstance(code, _Code) |
| 152 |
|
| 153 |
|
| 154 |
def repickle(obj): |
| 155 |
"""Round-trip an object through pickle.""" |
| 156 |
return pickle.loads(pickle.dumps(obj)) |
| 157 |
|
| 158 |
|
| 159 |
def test_pickle(): |
| 160 |
""" |
| 161 |
While `__class__` is overridden to return the built-in types, |
| 162 |
this would break unpickling in Python versions prior to 3.10 |
| 163 |
""" |
| 164 |
tb = Traceback(tb=make_python_tb()) |
| 165 |
tb2 = repickle(tb) |
| 166 |
|
| 167 |
assert type(tb2) == type(tb) |
| 168 |
assert tb2.tb_lineno == tb.tb_lineno |
| 169 |
|
| 170 |
frame = tb.tb_frame |
| 171 |
frame2 = repickle(frame) |
| 172 |
|
| 173 |
assert type(frame2) == type(frame) |
| 174 |
assert frame2.f_lineno == frame.f_lineno |
| 175 |
|
| 176 |
code = frame.f_code |
| 177 |
code2 = repickle(code) |
| 178 |
|
| 179 |
assert type(code2) == type(code) |
| 180 |
assert code2.co_name == code.co_name |
| 181 |
|
| 182 |
|
| 183 |
class TestInspect: |
| 184 |
def test_istraceback(self): |
| 185 |
tb = Traceback(tb=make_python_tb()) |
| 186 |
assert inspect.istraceback(tb) |
| 187 |
assert inspect.istraceback(repickle(tb)) |
| 188 |
|
| 189 |
def test_isframe(self): |
| 190 |
frame = _Frame(make_python_tb().tb_frame) |
| 191 |
assert inspect.isframe(frame) |
| 192 |
assert inspect.isframe(repickle(frame)) |
| 193 |
|
| 194 |
def test_iscode(self): |
| 195 |
code = _Code(make_python_tb().tb_frame.f_code) |
| 196 |
assert inspect.iscode(code) |
| 197 |
assert inspect.iscode(repickle(code)) |
| 198 |
|
| 199 |
def test_getframeinfo(self): |
| 200 |
tb = Traceback(make_python_tb()) |
| 201 |
assert inspect.getframeinfo(tb) |
| 202 |
assert inspect.getframeinfo(repickle(tb)) |
| 203 |
|
| 204 |
frame = tb.tb_frame |
| 205 |
assert inspect.getframeinfo(frame) |
| 206 |
assert inspect.getframeinfo(repickle(frame)) |
| 207 |
|
| 208 |
def test_getinnerframes(self): |
| 209 |
tb = Traceback(tb=make_python_tb()) |
| 210 |
assert inspect.getinnerframes(tb) |
| 211 |
assert inspect.getinnerframes(repickle(tb)) |