Skip to content

Commit

Permalink
Merge pull request #187 from zopefoundation/tb_format_signatures
Browse files Browse the repository at this point in the history
make signatures in `tb_format` compatible with Python 3.12+
  • Loading branch information
d-maurer authored Dec 10, 2024
2 parents a7b36ab + 3dc51e1 commit 73692d3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 18 deletions.
3 changes: 2 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
6.7 (unreleased)
================

- Nothing changed yet.
- Make signatures in ``tb_format`` Python 3.12+ compatible
(`#186 <https://github.com/zopefoundation/zope.testrunner/issues/186>`_)


6.6 (2024-10-16)
Expand Down
36 changes: 19 additions & 17 deletions src/zope/testrunner/tb_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,20 @@
import zope.testrunner.feature


try:
from traceback import _parse_value_tb
from traceback import _sentinel
except ImportError:
# before 3.10
# for Python before 3.10, the first 3 parameters of
# ``print_exception`` and ``format_exception`` are all mandatory
# and the first one (``etype`` alias ``t``) is ignored
_sentinel = object()

def _parse_value_tb(ignored, value, tb):
return value, tb


def _iter_chain(exc, custom_tb=None, seen=None):
if seen is None:
seen = set()
Expand All @@ -43,21 +57,8 @@ def _iter_chain(exc, custom_tb=None, seen=None):
yield from it


def _parse_value_tb(exc, value, tb):
# Taken straight from the traceback module code on Python 3.10, which
# introduced the ability to call print_exception with an exception
# instance as first parameter
if (value is None) != (tb is None):
raise ValueError("Both or neither of value and tb must be given")
if value is tb is None:
if exc is not None:
return exc, exc.__traceback__
else:
return None, None
return value, tb


def format_exception(t, v, tb, limit=None, chain=None):
def format_exception(t, value=_sentinel, tb=_sentinel, limit=None, chain=None):
v, tb = _parse_value_tb(t, value, tb)
if chain:
values = _iter_chain(v, tb)
else:
Expand All @@ -70,8 +71,9 @@ def format_exception(t, v, tb, limit=None, chain=None):
return fmt.formatException(t, v, tb)


def print_exception(t, v=None, tb=None, limit=None, file=None, chain=None):
v, tb = _parse_value_tb(t, v, tb)
def print_exception(t, value=_sentinel, tb=_sentinel,
limit=None, file=None, chain=None):
v, tb = _parse_value_tb(t, value, tb)
if chain:
values = _iter_chain(v, tb)
else:
Expand Down

0 comments on commit 73692d3

Please sign in to comment.