Skip to content

Commit e5ef464

Browse files
committed
CompileString returns error
1 parent 796854c commit e5ef464

File tree

4 files changed

+30
-10
lines changed

4 files changed

+30
-10
lines changed

demo/autoderef/autoderef.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@ for i in range(10):
2929

3030
mod := gp.ImportModule("__main__")
3131
gbl := mod.Dict()
32-
code := gp.CompileString(pythonCode, "<string>", gp.FileInput)
32+
code, err := gp.CompileString(pythonCode, "<string>", gp.FileInput)
33+
if err != nil {
34+
fmt.Printf("Failed to compile Python code: %v\n", err)
35+
return
36+
}
3337
_ = gp.EvalCode(code, gbl, gp.Nil().AsDict())
3438
for i := 0; i < 10; i++ {
3539
result := gp.EvalCode(code, gbl, gp.Nil().AsDict())

object_test.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,10 @@ class TestClass:
8282
globals := MakeDict(nil)
8383
globals.Set(MakeStr("__builtins__"), builtins.Object)
8484

85-
code := CompileString(pyCode, "<string>", FileInput)
85+
code, err := CompileString(pyCode, "<string>", FileInput)
86+
if err != nil {
87+
t.Errorf("CompileString() error = %v", err)
88+
}
8689

8790
EvalCode(code, globals, locals).AsModule()
8891
testClass := locals.Get(MakeStr("TestClass")).AsFunc()
@@ -237,7 +240,10 @@ class TestClass:
237240
builtins := ImportModule("builtins")
238241
globals.Set(MakeStr("__builtins__"), builtins.Object)
239242

240-
code := CompileString(pyCode, "<string>", FileInput)
243+
code, err := CompileString(pyCode, "<string>", FileInput)
244+
if err != nil {
245+
t.Errorf("CompileString() error = %v", err)
246+
}
241247
EvalCode(code, globals, locals)
242248

243249
testClass := locals.Get(MakeStr("TestClass")).AsFunc()
@@ -278,7 +284,10 @@ def make_tuple():
278284
builtins := ImportModule("builtins")
279285
globals.Set(MakeStr("__builtins__"), builtins.Object)
280286

281-
code := CompileString(pyCode, "<string>", FileInput)
287+
code, err := CompileString(pyCode, "<string>", FileInput)
288+
if err != nil {
289+
t.Errorf("CompileString() error = %v", err)
290+
}
282291
EvalCode(code, globals, locals)
283292

284293
makeTuple := locals.Get(MakeStr("make_tuple")).AsFunc()

python.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,21 @@ const (
3636
EvalInput InputType = C.Py_eval_input
3737
)
3838

39-
func CompileString(code, filename string, start InputType) Object {
39+
func CompileString(code, filename string, start InputType) (Object, error) {
4040
ccode := AllocCStr(code)
4141
cfilename := AllocCStr(filename)
4242
o := C.Py_CompileString(ccode, cfilename, C.int(start))
4343
// TODO: check why double free
4444
C.free(unsafe.Pointer(ccode))
4545
C.free(unsafe.Pointer(cfilename))
46-
return newObject(o)
46+
if o == nil {
47+
err := FetchError()
48+
if err != nil {
49+
return Object{}, err
50+
}
51+
return Object{}, fmt.Errorf("failed to compile code")
52+
}
53+
return newObject(o), nil
4754
}
4855

4956
func EvalCode(code Object, globals, locals Dict) Object {
@@ -91,9 +98,9 @@ func RunString(code string) error {
9198
dict := main.Dict()
9299

93100
// Run the code string
94-
codeObj := CompileString(code, "<string>", FileInput)
95-
if codeObj.Nil() {
96-
return fmt.Errorf("failed to compile code")
101+
codeObj, err := CompileString(code, "<string>", FileInput)
102+
if err != nil {
103+
return err
97104
}
98105

99106
ret := EvalCode(codeObj, dict, dict)

python_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func TestCompileString(t *testing.T) {
7171
}
7272

7373
for _, tt := range tests {
74-
obj := CompileString(tt.code, tt.filename, tt.start)
74+
obj, _ := CompileString(tt.code, tt.filename, tt.start)
7575
if obj.Nil() != tt.wantNil {
7676
t.Errorf("CompileString() returned nil = %v, want %v", obj.Nil(), tt.wantNil)
7777
}

0 commit comments

Comments
 (0)