|
| 1 | +import inspect |
1 | 2 | import logging |
2 | 3 | import pickle |
3 | 4 | import sys |
| 5 | +import types |
4 | 6 |
|
5 | 7 | from billiard.einfo import _Code # noqa |
6 | 8 | from billiard.einfo import _Frame # noqa |
@@ -86,7 +88,7 @@ def test_code(): |
86 | 88 | assert isinstance(code.co_argcount, int) |
87 | 89 | if sys.version_info >= (3, 11): |
88 | 90 | assert callable(code.co_positions) |
89 | | - assert next(code.co_positions()) == (77, 77, 0, 0) |
| 91 | + assert next(code.co_positions()) == (79, 79, 0, 0) |
90 | 92 |
|
91 | 93 |
|
92 | 94 | def test_object_init(): |
@@ -116,3 +118,70 @@ def test_truncated_co_positions(): |
116 | 118 | assert list(iter(truncated.co_positions())) == list( |
117 | 119 | iter(truncated.tb_frame.co_positions()) |
118 | 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 | +class test_inspect: |
| 160 | + def test_istraceback(self): |
| 161 | + tb = Traceback(tb=make_python_tb()) |
| 162 | + assert inspect.istraceback(tb) |
| 163 | + assert inspect.istraceback(repickle(tb)) |
| 164 | + |
| 165 | + def test_isframe(self): |
| 166 | + frame = _Frame(make_python_tb().tb_frame) |
| 167 | + assert inspect.isframe(frame) |
| 168 | + assert inspect.isframe(repickle(frame)) |
| 169 | + |
| 170 | + def test_iscode(self): |
| 171 | + code = _Code(make_python_tb().tb_frame.f_code) |
| 172 | + assert inspect.iscode(code) |
| 173 | + assert inspect.iscode(repickle(code)) |
| 174 | + |
| 175 | + def test_getframeinfo(self): |
| 176 | + tb = Traceback(make_python_tb()) |
| 177 | + assert inspect.getframeinfo(tb) |
| 178 | + assert inspect.getframeinfo(repickle(tb)) |
| 179 | + |
| 180 | + frame = tb.tb_frame |
| 181 | + assert inspect.getframeinfo(frame) |
| 182 | + assert inspect.getframeinfo(repickle(frame)) |
| 183 | + |
| 184 | + def test_getinnerframes(self): |
| 185 | + tb = Traceback(tb=make_python_tb()) |
| 186 | + assert inspect.getinnerframes(tb) |
| 187 | + assert inspect.getinnerframes(repickle(tb)) |
0 commit comments