Skip to content

Commit 2d94392

Browse files
authored
Add include_traceback option to SourceLine. (#133)
1 parent 70519d9 commit 2d94392

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

schema_salad/sourceline.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from ruamel.yaml.comments import CommentedBase, CommentedMap, CommentedSeq
44
import re
55
import os
6+
import traceback
67

78
from typing import (Any, AnyStr, Callable, cast, Dict, List, Iterable, Tuple,
89
TypeVar, Union, Text)
@@ -134,22 +135,26 @@ def cmap(d, lc=None, fn=None): # type: (Union[int, float, str, Text, Dict, List
134135
return d
135136

136137
class SourceLine(object):
137-
def __init__(self, item, key=None, raise_type=six.text_type): # type: (Any, Any, Callable) -> None
138+
def __init__(self, item, key=None, raise_type=six.text_type, include_traceback=False): # type: (Any, Any, Callable, bool) -> None
138139
self.item = item
139140
self.key = key
140141
self.raise_type = raise_type
142+
self.include_traceback = include_traceback
141143

142144
def __enter__(self): # type: () -> SourceLine
143145
return self
144146

145147
def __exit__(self,
146148
exc_type, # type: Any
147149
exc_value, # type: Any
148-
traceback # type: Any
149-
): # -> Any
150+
tb # type: Any
151+
): # -> Any
150152
if not exc_value:
151153
return
152-
raise self.makeError(six.text_type(exc_value))
154+
if self.include_traceback:
155+
raise self.makeError("\n".join(traceback.format_exception(exc_type, exc_value, tb)))
156+
else:
157+
raise self.makeError(six.text_type(exc_value))
153158

154159
def makeLead(self): # type: () -> Text
155160
if self.key is None or self.item.lc.data is None or self.key not in self.item.lc.data:

schema_salad/tests/test_examples.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import ruamel.yaml
1111
import json
1212
import os
13-
from schema_salad.sourceline import cmap
13+
from schema_salad.sourceline import cmap, SourceLine
1414

1515
try:
1616
from ruamel.yaml import CSafeLoader as SafeLoader
@@ -376,5 +376,30 @@ def test_file_uri(self):
376376
schema_salad.ref_resolver.uri_file_path("file:///foo/bar%20baz/quux#zing%20zong"))
377377

378378

379+
class SourceLineTest(unittest.TestCase):
380+
def test_sourceline(self):
381+
ldr = schema_salad.ref_resolver.Loader({"id": "@id"})
382+
b, _ = ldr.resolve_ref(get_data("tests/frag.yml"))
383+
384+
class TestExp(Exception):
385+
pass
386+
387+
try:
388+
with SourceLine(b, 1, TestExp, False):
389+
raise Exception("Whoops")
390+
except TestExp as e:
391+
self.assertTrue(str(e).endswith("frag.yml:3:3: Whoops"))
392+
except:
393+
self.assertFail()
394+
395+
try:
396+
with SourceLine(b, 1, TestExp, True):
397+
raise Exception("Whoops")
398+
except TestExp as e:
399+
self.assertTrue(str(e).splitlines()[0].endswith("frag.yml:3:3: Traceback (most recent call last):"))
400+
except:
401+
self.assertFail()
402+
403+
379404
if __name__ == '__main__':
380405
unittest.main()

0 commit comments

Comments
 (0)