Skip to content

added examples and py utils #159

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

Merged
merged 14 commits into from
Feb 12, 2022
Merged
Show file tree
Hide file tree
Changes from 8 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
4 changes: 1 addition & 3 deletions examples/embedding/lib/mylib.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import mylib_go as _go

PY_VERSION = _go.PY_VERSION
GO_VERSION = _go.GO_VERSION


print('''
==========================================================
%s
%s
==========================================================
''' % (PY_VERSION, GO_VERSION))
''' % (PY_VERSION))


def Stop(location, num_nights = 2):
Expand Down
39 changes: 25 additions & 14 deletions examples/embedding/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,55 @@ package main

import (
"bytes"
"flag"
"os"
"os/exec"
"path/filepath"
"testing"
)

const embeddingTestOutput = "embedding-test-output.txt"
const embeddingTestOutput = "testdata/embedding_out_golden.txt"

var regen = flag.Bool("regen", false, "regenerate golden files")

func TestEmbeddedExample(t *testing.T) {

cmd := exec.Command("go", "build", ".")
err := cmd.Run()
tmp, err := os.MkdirTemp("", "go-python-embedding-")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmp)

cmd := exec.Command("go", "build", "-o", filepath.Join(tmp, "exe"), ".")
err = cmd.Run()
if err != nil {
t.Fatalf("failed to compile embedding example: %v", err)
}

out := new(bytes.Buffer)
cmd = exec.Command("./embedding", "mylib-demo.py")
cmd = exec.Command(filepath.Join(tmp, "exe"), "mylib-demo.py")
cmd.Stdout = out

err = cmd.Run()
if err != nil {
t.Fatalf("failed to run embedding binary: %v", err)
}

resetTest := false // true
testOutput := out.Bytes()
if resetTest {

flag.Parse()
if *regen {
err = os.WriteFile(embeddingTestOutput, testOutput, 0644)
if err != nil {
t.Fatalf("failed to write test output: %v", err)
}
} else {
mustMatch, err := os.ReadFile(embeddingTestOutput)
if err != nil {
t.Fatalf("failed read %q", embeddingTestOutput)
}
if !bytes.Equal(testOutput, mustMatch) {
t.Fatalf("embedded test output did not match accepted output from %q", embeddingTestOutput)
}
}

mustMatch, err := os.ReadFile(embeddingTestOutput)
if err != nil {
t.Fatalf("failed read %q", embeddingTestOutput)
}
if !bytes.Equal(testOutput, mustMatch) {
t.Fatalf("embedded test output did not match accepted output from %q", embeddingTestOutput)
}
}
2 changes: 1 addition & 1 deletion examples/embedding/mylib.module.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func init() {
Globals: py.StringDict{
"PY_VERSION": py.String("Python 3.4 (github.com/go-python/gpython)"),
"GO_VERSION": py.String(fmt.Sprintf("%s on %s %s", runtime.Version(), runtime.GOOS, runtime.GOARCH)),
"MYLIB_VERS": py.String("Vacaton 1.0 by Fletch F. Fletcher"),
"MYLIB_VERS": py.String("Vacation 1.0 by Fletch F. Fletcher"),
},
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ Welcome to a gpython embedded example,

==========================================================
Python 3.4 (github.com/go-python/gpython)
go1.17.6 on darwin amd64
==========================================================

Spring Break itinerary:
Stop 1: Miami, Florida | 7 nights
Stop 2: Mallorca, Spain | 3 nights
Stop 3: Ibiza, Spain | 14 nights
Stop 4: Monaco | 12 nights
### Made with Vacaton 1.0 by Fletch F. Fletcher
### Made with Vacation 1.0 by Fletch F. Fletcher

I bet Monaco will be the best!