-
Notifications
You must be signed in to change notification settings - Fork 0
/
maketraceback3.py
38 lines (35 loc) · 909 Bytes
/
maketraceback3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from types import CodeType
def make_raiser(name, lineno, filename, type_to_raise):
def raises():
raise type_to_raise()
co = raises.__code__
raises.__name__ = name
raises.__code__ = CodeType(
co.co_argcount,
co.co_kwonlyargcount,
co.co_nlocals,
co.co_stacksize,
co.co_flags,
co.co_code,
co.co_consts,
co.co_names,
co.co_varnames,
filename,
name,
lineno - 1,
co.co_lnotab,
co.co_freevars,
co.co_cellvars,
)
return raises
foo = make_raiser('foo', 3, 'foo.rs', lambda: IndexError(0))
bar = make_raiser('bar', 4, 'bar.rs', foo)
bar()
# Output:
# $ python3 maketraceback3.py
# Traceback (most recent call last):
# File "maketraceback3.py", line 29, in <module>
# bar()
# File "bar.rs", line 4, in bar
# File "foo.rs", line 3, in foo
# IndexError: 0