core_shell_microgelscostrafo411magnetic_modelrelease_v0.94release_v0.95ticket-1257-vesicle-productticket_1156ticket_1265_superballticket_822_more_unit_tests
Last change
on this file since d19962c was
823e620,
checked in by Paul Kienzle <pkienzle@…>, 8 years ago
|
delint
|
-
Property mode set to
100644
|
File size:
1.3 KB
|
Line | |
---|
1 | """ |
---|
2 | Utility to add annotations to python exceptions. |
---|
3 | """ |
---|
4 | |
---|
5 | # Platform cruft: WindowsError is only defined on Windows. |
---|
6 | try: |
---|
7 | WindowsError |
---|
8 | except NameError: |
---|
9 | class WindowsError(Exception): |
---|
10 | """ |
---|
11 | Fake WindowsException when not on Windows. |
---|
12 | """ |
---|
13 | pass |
---|
14 | |
---|
15 | def annotate_exception(exc, msg): |
---|
16 | """ |
---|
17 | Add an annotation to the current exception, which can then be forwarded |
---|
18 | to the caller using a bare "raise" statement to reraise the annotated |
---|
19 | exception. |
---|
20 | |
---|
21 | Example:: |
---|
22 | |
---|
23 | >>> D = {} |
---|
24 | >>> try: |
---|
25 | ... print(D['hello']) |
---|
26 | ... except Exception as exc: |
---|
27 | ... annotate_exception(exc, "while accessing 'D'") |
---|
28 | ... raise |
---|
29 | Traceback (most recent call last): |
---|
30 | ... |
---|
31 | KeyError: "hello while accessing 'D'" |
---|
32 | """ |
---|
33 | # Can't extend WindowsError exceptions; instead raise a new exception. |
---|
34 | # TODO: try to incorporate current stack trace in the raised exception |
---|
35 | if isinstance(exc, WindowsError): |
---|
36 | raise OSError(str(exc) + " " + msg) |
---|
37 | |
---|
38 | args = exc.args |
---|
39 | if not args: |
---|
40 | exc.args = (msg,) |
---|
41 | else: |
---|
42 | try: |
---|
43 | arg0 = " ".join((args[0], msg)) |
---|
44 | exc.args = tuple([arg0] + list(args[1:])) |
---|
45 | except: |
---|
46 | exc.args = (" ".join((str(exc), msg)),) |
---|
Note: See
TracBrowser
for help on using the repository browser.