Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docs #12

Merged
merged 3 commits into from
Nov 4, 2024
Merged

Docs #12

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@

## Examples

See the [examples](_demo).
See the [examples](demo).

### Hello World: Plot a line

```go
// _demo/plot/plot.go
// demo/plot/plot.go

package main

Expand All @@ -58,7 +58,7 @@ func main() {
### Typed Python Objects

```go
// _demo/plot2/plot2.go
// demo/plot2/plot2.go

package main

Expand Down Expand Up @@ -93,7 +93,7 @@ func main() {
### Define Python Objects with Go

```go
// _demo/module/foo/foo.go
// demo/module/foo/foo.go

package foo

Expand Down Expand Up @@ -145,15 +145,15 @@ func InitFooModule() gp.Module {
Call foo module from Python and Go.

```go
// _demo/module/module.go
// demo/module/module.go

package main

import (
"fmt"

gp "github.com/cpunion/go-python"
"github.com/cpunion/go-python/_demo/module/foo"
"github.com/cpunion/go-python/demo/module/foo"
)

func main() {
Expand Down Expand Up @@ -214,7 +214,7 @@ point.print()
### Call gradio

```go
// _demo/gradio/gradio.go
// demo/gradio/gradio.go

package main

Expand Down Expand Up @@ -265,6 +265,7 @@ func main() {
}

gp.Initialize()
defer gp.Finalize()
gr = gp.ImportModule("gradio")
fn := gp.CreateFunc(UpdateExamples,
"(country, /)\n--\n\nUpdate examples based on country")
Expand Down
13 changes: 7 additions & 6 deletions _demo/autoderef/autoderef.go → demo/autoderef/autoderef.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@

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 All @@ -49,12 +53,9 @@
runtime.GC()
}

for i := 1; i <= 100000; i++ {
println(i)
for i := 1; i <= 1000000; i++ {

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

View check run for this annotation

Codecov / codecov/patch

demo/autoderef/autoderef.go#L56

Added line #L56 was not covered by tests
f := gp.MakeFloat(float64(i))
r := pymath.Sqrt(f)
b := r.IsInteger()
var _ bool = b.Bool()
_ = pymath.Sqrt(f)

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

View check run for this annotation

Codecov / codecov/patch

demo/autoderef/autoderef.go#L58

Added line #L58 was not covered by tests
if i%10000 == 0 {
fmt.Printf("Iteration %d in go\n", i)
}
Expand Down
1 change: 1 addition & 0 deletions _demo/gradio/gradio.go → demo/gradio/gradio.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@
}

gp.Initialize()
defer gp.Finalize()
gr = gp.ImportModule("gradio")

Check warning on line 51 in demo/gradio/gradio.go

View check run for this annotation

Codecov / codecov/patch

demo/gradio/gradio.go#L50-L51

Added lines #L50 - L51 were not covered by tests
fn := gp.CreateFunc(UpdateExamples,
"(country, /)\n--\n\nUpdate examples based on country")
// Would be (in the future):
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion _demo/module/module.go → demo/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"

gp "github.com/cpunion/go-python"
"github.com/cpunion/go-python/_demo/module/foo"
"github.com/cpunion/go-python/demo/module/foo"
)

func main() {
Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 0 additions & 2 deletions math/math_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package math

import (
"runtime"
"testing"

gp "github.com/cpunion/go-python"
)

func TestSqrt(t *testing.T) {
runtime.LockOSThread()
// Initialize Python
gp.Initialize()
defer gp.Finalize()
Expand Down
1 change: 1 addition & 0 deletions object.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func (obj Object) object() Object {

func newObject(obj *PyObject) Object {
if obj == nil {
C.PyErr_Print()
return Object{}
}
o := &pyObject{obj: obj}
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
21 changes: 15 additions & 6 deletions python.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ import "C"
import (
"fmt"
"reflect"
"runtime"
"unsafe"
)

type PyObject = C.PyObject
type PyCFunction = C.PyCFunction

func Initialize() {
runtime.LockOSThread()
C.Py_Initialize()
}

Expand All @@ -34,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 All @@ -68,7 +77,7 @@ func With[T Objecter](obj T, fn func(v T)) T {
// ----------------------------------------------------------------------------

func MainModule() Module {
return GetModule("__main__")
return ImportModule("__main__")
}

func None() Object {
Expand All @@ -89,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
3 changes: 1 addition & 2 deletions python_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
)

func setupTest(t *testing.T) {
runtime.LockOSThread()
Initialize()
t.Cleanup(func() {
runtime.GC()
Expand Down Expand Up @@ -72,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
Loading