Skip to content

Commit fa772b2

Browse files
committed
Make einfo compatible with inspect (fixes #176)
1 parent cbe5a3f commit fa772b2

2 files changed

Lines changed: 54 additions & 1 deletion

File tree

billiard/einfo.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import sys
22
import traceback
3+
import types
34

45
__all__ = ['ExceptionInfo', 'Traceback']
56

@@ -25,6 +26,10 @@ def __init__(self, code):
2526
if sys.version_info >= (3, 11):
2627
self._co_positions = list(code.co_positions())
2728

29+
@property
30+
def __class__(self):
31+
return types.CodeType
32+
2833
if sys.version_info >= (3, 11):
2934
@property
3035
def co_positions(self):
@@ -57,6 +62,10 @@ def __init__(self, frame):
5762
# don't want to hit https://bugs.python.org/issue21967
5863
self.f_restricted = False
5964

65+
@property
66+
def __class__(self):
67+
return types.FrameType
68+
6069

6170
class _Object:
6271

@@ -79,6 +88,10 @@ def __init__(self):
7988
self.tb_next = None
8089
self.tb_lasti = 0
8190

91+
@property
92+
def __class__(self):
93+
return types.TracebackType
94+
8295

8396
class Traceback:
8497
Frame = _Frame
@@ -94,6 +107,10 @@ def __init__(self, tb, max_frames=DEFAULT_MAX_FRAMES, depth=0):
94107
else:
95108
self.tb_next = _Truncated()
96109

110+
@property
111+
def __class__(self):
112+
return types.TracebackType
113+
97114

98115
class RemoteTraceback(Exception):
99116
def __init__(self, tb):

t/unit/test_einfo.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
import sys
12
import pickle
23
import logging
3-
from billiard.einfo import ExceptionInfo
4+
import inspect
5+
import types
6+
from billiard.einfo import ExceptionInfo, Traceback, _Code, _Frame
47

58
logger = logging.getLogger(__name__)
69

@@ -26,3 +29,36 @@ def test_exception_info_log_after_pickle(caplog):
2629
logger.exception("failed", exc_info=exception)
2730
assert ' raise RuntimeError("some message")' in caplog.text
2831
assert "RuntimeError: some message" in caplog.text
32+
33+
34+
def make_python_tb():
35+
tb = None
36+
depth = 0
37+
while True:
38+
try:
39+
frame = sys._getframe(depth)
40+
except ValueError:
41+
break
42+
else:
43+
depth += 1
44+
45+
tb = types.TracebackType(tb, frame, frame.f_lasti, frame.f_lineno)
46+
47+
return tb
48+
49+
50+
class test_inspect:
51+
def test_istraceback(self):
52+
assert inspect.istraceback(Traceback(tb=make_python_tb()))
53+
54+
def test_isframe(self):
55+
assert inspect.isframe(_Frame(make_python_tb().tb_frame))
56+
57+
def test_iscode(self):
58+
assert inspect.iscode(_Code(make_python_tb().tb_frame.f_code))
59+
60+
def test_getframeinfo(self):
61+
assert inspect.getframeinfo(Traceback(tb=make_python_tb()))
62+
63+
def test_getinnerframes(self):
64+
assert inspect.getinnerframes(Traceback(tb=make_python_tb()))

0 commit comments

Comments
 (0)