Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
high level edits for py.Context additions
  • Loading branch information
Drew O'Meara committed Feb 3, 2022
commit 57bf0c8396e1506c8863cc22767e3da417c9450c
15 changes: 5 additions & 10 deletions compile/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func EqCode(t *testing.T, name string, a, b *py.Code) {
func TestCompile(t *testing.T) {
for _, test := range compileTestData {
// log.Printf(">>> %s", test.in)
codeObj, err := Compile(test.in, "<string>", test.mode, 0, true)
code, err := Compile(test.in, "<string>", test.mode, 0, true)
if err != nil {
if test.exceptionType == nil {
t.Errorf("%s: Got exception %v when not expecting one", test.in, err)
Expand Down Expand Up @@ -196,17 +196,12 @@ func TestCompile(t *testing.T) {
}
} else {
if test.out == nil {
if codeObj != nil {
t.Errorf("%s: Expecting nil *py.Code but got %T", test.in, codeObj)
if code != nil {
t.Errorf("%s: Expecting nil *py.Code but got %T", test.in, code)
}
} else {
code, ok := codeObj.(*py.Code)
if !ok {
t.Errorf("%s: Expecting *py.Code but got %T", test.in, codeObj)
} else {
//t.Logf("Testing %q", test.in)
EqCode(t, test.in, test.out, code)
}
//t.Logf("Testing %q", test.in)
EqCode(t, test.in, test.out, code)
}
}
}
Expand Down
84 changes: 19 additions & 65 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,13 @@ import (
"runtime"
"runtime/pprof"

_ "github.com/go-python/gpython/builtin"
"github.com/go-python/gpython/repl"
"github.com/go-python/gpython/repl/cli"

//_ "github.com/go-python/gpython/importlib"
"io/ioutil"
"log"
"os"
"strings"

"github.com/go-python/gpython/compile"
"github.com/go-python/gpython/marshal"
_ "github.com/go-python/gpython/math"
"github.com/go-python/gpython/py"
pysys "github.com/go-python/gpython/sys"
_ "github.com/go-python/gpython/time"
"github.com/go-python/gpython/vm"
)

// Globals
Expand All @@ -48,33 +39,14 @@ Full options:
flag.PrintDefaults()
}

// Exit with the message
func fatal(message string, args ...interface{}) {
if !strings.HasSuffix(message, "\n") {
message += "\n"
}
syntaxError()
fmt.Fprintf(os.Stderr, message, args...)
os.Exit(1)
}

func main() {
flag.Usage = syntaxError
flag.Parse()
args := flag.Args()
py.MustGetModule("sys").Globals["argv"] = pysys.MakeArgv(args)
if len(args) == 0 {

fmt.Printf("Python 3.4.0 (%s, %s)\n", commit, date)
fmt.Printf("[Gpython %s]\n", version)
fmt.Printf("- os/arch: %s/%s\n", runtime.GOOS, runtime.GOARCH)
fmt.Printf("- go version: %s\n", runtime.Version())

cli.RunREPL()
return
}
prog := args[0]
// fmt.Printf("Running %q\n", prog)
opts := py.DefaultContextOpts()
opts.SysArgs = flag.Args()
ctx := py.NewContext(opts)

if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
Expand All @@ -88,41 +60,23 @@ func main() {
defer pprof.StopCPUProfile()
}

// FIXME should be using ImportModuleLevelObject() here
f, err := os.Open(prog)
if err != nil {
log.Fatalf("Failed to open %q: %v", prog, err)
}
var obj py.Object
if strings.HasSuffix(prog, ".pyc") {
obj, err = marshal.ReadPyc(f)
if err != nil {
log.Fatalf("Failed to marshal %q: %v", prog, err)
}
} else if strings.HasSuffix(prog, ".py") {
str, err := ioutil.ReadAll(f)
if err != nil {
log.Fatalf("Failed to read %q: %v", prog, err)
}
obj, err = compile.Compile(string(str), prog, "exec", 0, true)
// IF no args, enter REPL mode
if len(args) == 0 {

fmt.Printf("Python 3.4.0 (%s, %s)\n", commit, date)
fmt.Printf("[Gpython %s]\n", version)
fmt.Printf("- os/arch: %s/%s\n", runtime.GOOS, runtime.GOARCH)
fmt.Printf("- go version: %s\n", runtime.Version())

replCtx := repl.New(ctx)
cli.RunREPL(replCtx)

} else {
_, err := py.RunFile(ctx, args[0], py.CompileOpts{}, nil)
if err != nil {
log.Fatalf("Can't compile %q: %v", prog, err)
py.TracebackDump(err)
log.Fatal(err)
}
} else {
log.Fatalf("Can't execute %q", prog)
}
if err = f.Close(); err != nil {
log.Fatalf("Failed to close %q: %v", prog, err)
}
code := obj.(*py.Code)
module := py.NewModule("__main__", "", nil, nil)
module.Globals["__file__"] = py.String(prog)
res, err := vm.Run(module.Globals, module.Globals, code, nil)
if err != nil {
py.TracebackDump(err)
log.Fatal(err)
}
// fmt.Printf("Return = %v\n", res)
_ = res

}
10 changes: 6 additions & 4 deletions repl/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,12 @@ func (rl *readline) Print(out string) {
}

// RunREPL starts the REPL loop
func RunREPL() {
repl := repl.New()
rl := newReadline(repl)
repl.SetUI(rl)
func RunREPL(replCtx *repl.REPL) {
if replCtx == nil {
replCtx = repl.New(nil)
}
rl := newReadline(replCtx)
replCtx.SetUI(rl)
defer rl.Close()
err := rl.ReadHistory()
if err != nil {
Expand Down
9 changes: 3 additions & 6 deletions repl/repl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ import (
"testing"

// import required modules
_ "github.com/go-python/gpython/builtin"
_ "github.com/go-python/gpython/math"
_ "github.com/go-python/gpython/sys"
_ "github.com/go-python/gpython/time"
_ "github.com/go-python/gpython/modules"
)

type replTest struct {
Expand Down Expand Up @@ -38,7 +35,7 @@ func (rt *replTest) assert(t *testing.T, what, wantPrompt, wantOut string) {
}

func TestREPL(t *testing.T) {
r := New()
r := New(nil)
rt := &replTest{}
r.SetUI(rt)

Expand Down Expand Up @@ -78,7 +75,7 @@ func TestREPL(t *testing.T) {
}

func TestCompleter(t *testing.T) {
r := New()
r := New(nil)
rt := &replTest{}
r.SetUI(rt)

Expand Down
9 changes: 4 additions & 5 deletions repl/web/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build js
// +build js

package main
Expand All @@ -15,11 +16,9 @@ import (
"github.com/gopherjs/gopherwasm/js" // gopherjs to wasm converter shim

// import required modules
_ "github.com/go-python/gpython/builtin"
_ "github.com/go-python/gpython/math"
_ "github.com/go-python/gpython/modules"

"github.com/go-python/gpython/repl"
_ "github.com/go-python/gpython/sys"
_ "github.com/go-python/gpython/time"
)

// Implement the replUI interface
Expand Down Expand Up @@ -77,7 +76,7 @@ func main() {
node.Get("classList").Call("add", "active")

// Make a repl referring to an empty term for the moment
REPL := repl.New()
REPL := repl.New(nil)
cb := js.NewCallback(func(args []js.Value) {
REPL.Run(args[0].String())
})
Expand Down