Skip to content

Commit

Permalink
CompileString returns error
Browse files Browse the repository at this point in the history
  • Loading branch information
cpunion committed Nov 4, 2024
1 parent 796854c commit e5ef464
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 10 deletions.
6 changes: 5 additions & 1 deletion demo/autoderef/autoderef.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ for i in range(10):

mod := gp.ImportModule("__main__")
gbl := mod.Dict()
code := gp.CompileString(pythonCode, "<string>", gp.FileInput)
code, err := gp.CompileString(pythonCode, "<string>", gp.FileInput)
if err != nil {
fmt.Printf("Failed to compile Python code: %v\n", err)
return
}

Check warning on line 36 in demo/autoderef/autoderef.go

View check run for this annotation

Codecov / codecov/patch

demo/autoderef/autoderef.go#L32-L36

Added lines #L32 - L36 were not covered by tests
_ = gp.EvalCode(code, gbl, gp.Nil().AsDict())
for i := 0; i < 10; i++ {
result := gp.EvalCode(code, gbl, gp.Nil().AsDict())
Expand Down
15 changes: 12 additions & 3 deletions object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,10 @@ class TestClass:
globals := MakeDict(nil)
globals.Set(MakeStr("__builtins__"), builtins.Object)

code := CompileString(pyCode, "<string>", FileInput)
code, err := CompileString(pyCode, "<string>", FileInput)
if err != nil {
t.Errorf("CompileString() error = %v", err)
}

EvalCode(code, globals, locals).AsModule()
testClass := locals.Get(MakeStr("TestClass")).AsFunc()
Expand Down Expand Up @@ -237,7 +240,10 @@ class TestClass:
builtins := ImportModule("builtins")
globals.Set(MakeStr("__builtins__"), builtins.Object)

code := CompileString(pyCode, "<string>", FileInput)
code, err := CompileString(pyCode, "<string>", FileInput)
if err != nil {
t.Errorf("CompileString() error = %v", err)
}
EvalCode(code, globals, locals)

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

code := CompileString(pyCode, "<string>", FileInput)
code, err := CompileString(pyCode, "<string>", FileInput)
if err != nil {
t.Errorf("CompileString() error = %v", err)
}
EvalCode(code, globals, locals)

makeTuple := locals.Get(MakeStr("make_tuple")).AsFunc()
Expand Down
17 changes: 12 additions & 5 deletions python.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,21 @@ const (
EvalInput InputType = C.Py_eval_input
)

func CompileString(code, filename string, start InputType) Object {
func CompileString(code, filename string, start InputType) (Object, error) {
ccode := AllocCStr(code)
cfilename := AllocCStr(filename)
o := C.Py_CompileString(ccode, cfilename, C.int(start))
// TODO: check why double free
C.free(unsafe.Pointer(ccode))
C.free(unsafe.Pointer(cfilename))
return newObject(o)
if o == nil {
err := FetchError()
if err != nil {
return Object{}, err
}
return Object{}, fmt.Errorf("failed to compile code")
}
return newObject(o), nil
}

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

// Run the code string
codeObj := CompileString(code, "<string>", FileInput)
if codeObj.Nil() {
return fmt.Errorf("failed to compile code")
codeObj, err := CompileString(code, "<string>", FileInput)
if err != nil {
return err
}

ret := EvalCode(codeObj, dict, dict)
Expand Down
2 changes: 1 addition & 1 deletion python_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func TestCompileString(t *testing.T) {
}

for _, tt := range tests {
obj := CompileString(tt.code, tt.filename, tt.start)
obj, _ := CompileString(tt.code, tt.filename, tt.start)
if obj.Nil() != tt.wantNil {
t.Errorf("CompileString() returned nil = %v, want %v", obj.Nil(), tt.wantNil)
}
Expand Down

0 comments on commit e5ef464

Please sign in to comment.