Skip to content

Commit 5911bd8

Browse files
committed
doc(jetsam) doctest example, suggest ok-flag; pep8
1 parent a6263d1 commit 5911bd8

File tree

1 file changed

+30
-11
lines changed

1 file changed

+30
-11
lines changed

graphtik/jetsam.py

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2016, Yahoo Inc.
1+
# Copyright 2019-2020, Kostis Anagnostopoulos;
22
# Licensed under the terms of the Apache License, Version 2.0. See the LICENSE file associated with the project for terms.
33
""":term:`jetsam` utility for annotating exceptions from ``locals()``.
44
@@ -10,8 +10,9 @@
1010
>>> from graphtik.jetsam import *
1111
>>> __name__ = "graphtik.jetsam"
1212
"""
13-
1413
import logging
14+
import sys
15+
from contextlib import contextmanager
1516
from pathlib import Path
1617

1718
log = logging.getLogger(__name__)
@@ -124,20 +125,38 @@ def save_jetsam(ex, locs, *salvage_vars: str, annotation="jetsam", **salvage_map
124125
in case of errors::
125126
126127
127-
try:
128-
a = 1
129-
b = 2
130-
raise Exception()
131-
exception Exception as ex:
132-
save_jetsam(ex, locals(), "a", b="salvaged_b", c_var="c")
133-
raise
128+
>>> try:
129+
... a = 1
130+
... b = 2
131+
... raise Exception("trouble!")
132+
... except Exception as ex:
133+
... save_jetsam(ex, locals(), "a", b="salvaged_b", c_var="c")
134+
... raise
135+
Traceback (most recent call last):
136+
Exception: trouble!
134137
135138
And then from a REPL::
136139
137-
import sys
138-
sys.last_value.jetsam
140+
>>> import sys
141+
>>> sys.exc_info()[1].jetsam # doctest: +SKIP
139142
{'a': 1, 'salvaged_b': 2, "c_var": None}
140143
144+
.. Note::
145+
146+
In order not to obfuscate the landing position of post-mortem debuggers
147+
in the case of errors, use the ``try-finally`` with ``ok`` flag pattern:
148+
149+
>>> ok = False
150+
>>> try:
151+
...
152+
... pass # do risky stuff
153+
...
154+
... ok = True # last statement in the try-body.
155+
... except Exception as ex:
156+
... if not ok:
157+
... ex = sys.exc_info()[1]
158+
... save_jetsam(...)
159+
141160
** Reason:**
142161
143162
Graphs may become arbitrary deep. Debugging such graphs is notoriously hard.

0 commit comments

Comments
 (0)