-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors_test.go
More file actions
152 lines (141 loc) · 3.86 KB
/
errors_test.go
File metadata and controls
152 lines (141 loc) · 3.86 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package pyffi_test
import (
"errors"
"strings"
"testing"
"github.com/i2y/pyffi"
)
// TestErrorHandling consolidates all error-related tests into a single Runtime
// to minimize Py_Initialize/Py_Finalize cycles (known CPython instability).
func TestErrorHandling(t *testing.T) {
rt := newOrSkip(t)
t.Run("ExecErrorHasTypeAndMessage", func(t *testing.T) {
err := rt.Exec(`raise ValueError("test")`)
if err == nil {
t.Fatal("expected error from Exec")
}
var pyErr *pyffi.PythonError
if !errors.As(err, &pyErr) {
t.Fatalf("expected *PythonError, got %T: %v", err, err)
}
if pyErr.Type != "ValueError" {
t.Errorf("Type = %q, want %q", pyErr.Type, "ValueError")
}
if pyErr.Message != "test" {
t.Errorf("Message = %q, want %q", pyErr.Message, "test")
}
})
t.Run("ErrorTraceback", func(t *testing.T) {
err := rt.Exec(`
def foo():
raise ValueError("test error")
foo()
`)
if err == nil {
t.Fatal("expected error from Exec")
}
var pyErr *pyffi.PythonError
if !errors.As(err, &pyErr) {
t.Fatalf("expected *PythonError, got %T: %v", err, err)
}
if pyErr.Type != "ValueError" {
t.Errorf("Type = %q, want %q", pyErr.Type, "ValueError")
}
if pyErr.Message != "test error" {
t.Errorf("Message = %q, want %q", pyErr.Message, "test error")
}
if pyErr.Traceback == "" {
t.Fatal("expected non-empty Traceback")
}
if !strings.Contains(pyErr.Traceback, "foo") {
t.Errorf("Traceback should contain function name 'foo':\n%s", pyErr.Traceback)
}
if !strings.Contains(pyErr.Traceback, "Traceback") {
t.Errorf("Traceback should contain 'Traceback':\n%s", pyErr.Traceback)
}
})
t.Run("NestedErrorTraceback", func(t *testing.T) {
err := rt.Exec(`
def a():
return b()
def b():
return c()
def c():
raise RuntimeError("deep error")
a()
`)
if err == nil {
t.Fatal("expected error from Exec")
}
var pyErr *pyffi.PythonError
if !errors.As(err, &pyErr) {
t.Fatalf("expected *PythonError, got %T: %v", err, err)
}
if pyErr.Type != "RuntimeError" {
t.Errorf("Type = %q, want %q", pyErr.Type, "RuntimeError")
}
if pyErr.Message != "deep error" {
t.Errorf("Message = %q, want %q", pyErr.Message, "deep error")
}
if pyErr.Traceback == "" {
t.Fatal("expected non-empty Traceback")
}
for _, name := range []string{"a", "b", "c"} {
if !strings.Contains(pyErr.Traceback, name) {
t.Errorf("Traceback should contain function name %q:\n%s", name, pyErr.Traceback)
}
}
})
t.Run("SyntaxError", func(t *testing.T) {
err := rt.Exec(`def`)
if err == nil {
t.Fatal("expected error from Exec")
}
var pyErr *pyffi.PythonError
if !errors.As(err, &pyErr) {
t.Fatalf("expected *PythonError, got %T: %v", err, err)
}
if pyErr.Type != "SyntaxError" {
t.Errorf("Type = %q, want %q", pyErr.Type, "SyntaxError")
}
})
t.Run("CallErrorTraceback", func(t *testing.T) {
// Define a function that raises.
if err := rt.Exec(`
def failing_func():
raise TypeError("bad type")
`); err != nil {
t.Fatalf("setup Exec failed: %v", err)
}
mod, err := rt.Import("__main__")
if err != nil {
t.Fatalf("Import __main__ failed: %v", err)
}
defer mod.Close()
fn := mod.Attr("failing_func")
if fn == nil {
t.Fatal("expected failing_func attribute")
}
defer fn.Close()
_, err = fn.Call()
if err == nil {
t.Fatal("expected error from Call")
}
var pyErr *pyffi.PythonError
if !errors.As(err, &pyErr) {
t.Fatalf("expected *PythonError, got %T: %v", err, err)
}
if pyErr.Type != "TypeError" {
t.Errorf("Type = %q, want %q", pyErr.Type, "TypeError")
}
if pyErr.Message != "bad type" {
t.Errorf("Message = %q, want %q", pyErr.Message, "bad type")
}
if pyErr.Traceback == "" {
t.Fatal("expected non-empty Traceback")
}
if !strings.Contains(pyErr.Traceback, "failing_func") {
t.Errorf("Traceback should contain 'failing_func':\n%s", pyErr.Traceback)
}
})
}