Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make einfo compatible with inspect (fixes #176) #392

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions billiard/einfo.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import sys
import traceback
import types

__all__ = ['ExceptionInfo', 'Traceback']

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

@property
def __class__(self):
return types.CodeType

if sys.version_info >= (3, 11):
@property
def co_positions(self):
Expand Down Expand Up @@ -57,6 +62,10 @@ def __init__(self, frame):
# don't want to hit https://bugs.python.org/issue21967
self.f_restricted = False

@property
def __class__(self):
return types.FrameType


class _Object:

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

@property
def __class__(self):
return types.TracebackType


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

@property
def __class__(self):
return types.TracebackType


class RemoteTraceback(Exception):
def __init__(self, tb):
Expand Down
38 changes: 37 additions & 1 deletion t/unit/test_einfo.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import sys
import pickle
import logging
from billiard.einfo import ExceptionInfo
import inspect
import types
from billiard.einfo import ExceptionInfo, Traceback, _Code, _Frame

logger = logging.getLogger(__name__)

Expand All @@ -26,3 +29,36 @@ def test_exception_info_log_after_pickle(caplog):
logger.exception("failed", exc_info=exception)
assert ' raise RuntimeError("some message")' in caplog.text
assert "RuntimeError: some message" in caplog.text


def make_python_tb():
tb = None
depth = 0
while True:
try:
frame = sys._getframe(depth)
except ValueError:
break
else:
depth += 1

tb = types.TracebackType(tb, frame, frame.f_lasti, frame.f_lineno)

return tb


class test_inspect:
def test_istraceback(self):
assert inspect.istraceback(Traceback(tb=make_python_tb()))

def test_isframe(self):
assert inspect.isframe(_Frame(make_python_tb().tb_frame))

def test_iscode(self):
assert inspect.iscode(_Code(make_python_tb().tb_frame.f_code))

def test_getframeinfo(self):
assert inspect.getframeinfo(Traceback(tb=make_python_tb()))

def test_getinnerframes(self):
assert inspect.getinnerframes(Traceback(tb=make_python_tb()))