core_shell_microgelscostrafo411magnetic_modelrelease_v0.94release_v0.95ticket-1257-vesicle-productticket_1156ticket_1265_superballticket_822_more_unit_tests
Last change
on this file since 5111921 was
823e620,
checked in by Paul Kienzle <pkienzle@…>, 8 years ago
|
delint
|
-
Property mode set to
100644
|
File size:
1.3 KB
|
Rev | Line | |
---|
[9e430d0] | 1 | """ |
---|
| 2 | Utility to add annotations to python exceptions. |
---|
| 3 | """ |
---|
| 4 | |
---|
[0763009] | 5 | # Platform cruft: WindowsError is only defined on Windows. |
---|
| 6 | try: |
---|
| 7 | WindowsError |
---|
| 8 | except NameError: |
---|
| 9 | class WindowsError(Exception): |
---|
[823e620] | 10 | """ |
---|
| 11 | Fake WindowsException when not on Windows. |
---|
| 12 | """ |
---|
[0763009] | 13 | pass |
---|
| 14 | |
---|
[9e430d0] | 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. |
---|
[d138d43] | 20 | |
---|
[9e430d0] | 21 | Example:: |
---|
[d138d43] | 22 | |
---|
[9e430d0] | 23 | >>> D = {} |
---|
| 24 | >>> try: |
---|
[9404dd3] | 25 | ... print(D['hello']) |
---|
| 26 | ... except Exception as exc: |
---|
[9e430d0] | 27 | ... annotate_exception(exc, "while accessing 'D'") |
---|
| 28 | ... raise |
---|
| 29 | Traceback (most recent call last): |
---|
| 30 | ... |
---|
| 31 | KeyError: "hello while accessing 'D'" |
---|
| 32 | """ |
---|
[0763009] | 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): |
---|
[823e620] | 36 | raise OSError(str(exc) + " " + msg) |
---|
[0763009] | 37 | |
---|
[9e430d0] | 38 | args = exc.args |
---|
| 39 | if not args: |
---|
| 40 | exc.args = (msg,) |
---|
| 41 | else: |
---|
| 42 | try: |
---|
[823e620] | 43 | arg0 = " ".join((args[0], msg)) |
---|
[9e430d0] | 44 | exc.args = tuple([arg0] + list(args[1:])) |
---|
| 45 | except: |
---|
[823e620] | 46 | exc.args = (" ".join((str(exc), msg)),) |
---|
Note: See
TracBrowser
for help on using the repository browser.