FetchErr retrieves the error indicator into three variables whose addresses are passed. If the error indicator is not set, set all three variables to NULL. If it is set, it will be cleared and you own a reference to each object retrieved. The value and traceback object may be NULL even when the type
()
| 37 | // set, set all three variables to NULL. If it is set, it will be cleared and you own a reference to each object retrieved. |
| 38 | // The value and traceback object may be NULL even when the type object is not. |
| 39 | func FetchErr() error { |
| 40 | if C.PyErr_Occurred() == nil { |
| 41 | // error indicator not set, nothing to do |
| 42 | return nil |
| 43 | } |
| 44 | |
| 45 | exc := &PyObject{} |
| 46 | val := &PyObject{} |
| 47 | traceback := &PyObject{} |
| 48 | defer exc.DecRef() |
| 49 | defer val.DecRef() |
| 50 | defer traceback.DecRef() |
| 51 | |
| 52 | C.PyErr_Fetch(&exc.rawptr, &val.rawptr, &traceback.rawptr) |
| 53 | // normalize exception values as per python C API |
| 54 | C.PyErr_NormalizeException(&exc.rawptr, &val.rawptr, &traceback.rawptr) |
| 55 | |
| 56 | if !traceback.IsNull() { |
| 57 | once.Do(func() { |
| 58 | tb, _ := NewModule("traceback") |
| 59 | if tb != nil { |
| 60 | formatException, _ = tb.GetAttrString("format_exception") |
| 61 | } |
| 62 | }) |
| 63 | if !formatException.IsNull() { |
| 64 | ob := formatException.Call(exc, val, traceback) |
| 65 | if !ob.IsNull() { |
| 66 | defer ob.DecRef() |
| 67 | return errors.New(ob.String()) |
| 68 | } |
| 69 | } |
| 70 | return errors.New("can't format traceback exception") |
| 71 | } |
| 72 | if !val.IsNull() { |
| 73 | return errors.New(val.String()) |
| 74 | } |
| 75 | if !exc.IsNull() { |
| 76 | return errors.New(exc.String()) |
| 77 | } |
| 78 | return nil |
| 79 | } |
| 80 | |
| 81 | // ClearError clears the error indicator. |
| 82 | func ClearError() { |