Skip to content

Add multi context #158

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 6 commits into from
Feb 7, 2022
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
revised module model for py.Context
  • Loading branch information
Drew O'Meara committed Feb 3, 2022
commit 2b3aa676a0d437f2aa29b080e9087e80bddd292e
29 changes: 21 additions & 8 deletions builtin/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,16 @@ func init() {
"Warning": py.Warning,
"ZeroDivisionError": py.ZeroDivisionError,
}
py.NewModule("builtins", builtin_doc, methods, globals)

py.RegisterModule(&py.ModuleImpl{
Info: py.ModuleInfo{
Name: "builtins",
Doc: builtin_doc,
Flags: py.ShareModule,
},
Methods: methods,
Globals: globals,
})
}

const print_doc = `print(value, ..., sep=' ', end='\\n', file=sys.stdout, flush=False)
Expand All @@ -178,18 +187,22 @@ func builtin_print(self py.Object, args py.Tuple, kwargs py.StringDict) (py.Obje
var (
sepObj py.Object = py.String(" ")
endObj py.Object = py.String("\n")
file py.Object = py.MustGetModule("sys").Globals["stdout"]
flush py.Object
)
sysModule, err := self.(*py.Module).Context.GetModule("sys")
if err != nil {
return nil, err
}
stdout := sysModule.Globals["stdout"]
kwlist := []string{"sep", "end", "file", "flush"}
err := py.ParseTupleAndKeywords(nil, kwargs, "|ssOO:print", kwlist, &sepObj, &endObj, &file, &flush)
err = py.ParseTupleAndKeywords(nil, kwargs, "|ssOO:print", kwlist, &sepObj, &endObj, &stdout, &flush)
if err != nil {
return nil, err
}
sep := sepObj.(py.String)
end := endObj.(py.String)

write, err := py.GetAttrString(file, "write")
write, err := py.GetAttrString(stdout, "write")
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -219,7 +232,7 @@ func builtin_print(self py.Object, args py.Tuple, kwargs py.StringDict) (py.Obje
}

if shouldFlush, _ := py.MakeBool(flush); shouldFlush == py.True {
fflush, err := py.GetAttrString(file, "flush")
fflush, err := py.GetAttrString(stdout, "flush")
if err == nil {
return py.Call(fflush, nil, nil)
}
Expand Down Expand Up @@ -449,7 +462,7 @@ func builtin___build_class__(self py.Object, args py.Tuple, kwargs py.StringDict
}
// fmt.Printf("Calling %v with %v and %v\n", fn.Name, fn.Globals, ns)
// fmt.Printf("Code = %#v\n", fn.Code)
cell, err = py.VmRun(fn.Globals, ns, fn.Code, fn.Closure)
cell, err = fn.Context.RunCode(fn.Code, fn.Globals, ns, fn.Closure)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -750,7 +763,7 @@ func builtin_compile(self py.Object, args py.Tuple, kwargs py.StringDict) (py.Ob
}

// if dont_inherit.(py.Int) != 0 {
// PyEval_MergeCompilerFlags(&cf)
// PyEval_MergeCompilerFlags(&cf)
// }

// switch string(startstr.(py.String)) {
Expand Down Expand Up @@ -782,7 +795,7 @@ func builtin_compile(self py.Object, args py.Tuple, kwargs py.StringDict) (py.Ob
return nil, err
}
// result = py.CompileStringExFlags(str, filename, start[mode], &cf, optimize)
result, err = compile.Compile(str, string(filename.(py.String)), string(startstr.(py.String)), int(supplied_flags.(py.Int)), dont_inherit.(py.Int) != 0)
result, err = compile.Compile(str, string(filename.(py.String)), py.CompileMode(startstr.(py.String)), int(supplied_flags.(py.Int)), dont_inherit.(py.Int) != 0)
if err != nil {
return nil, err
}
Expand Down
13 changes: 8 additions & 5 deletions importlib/importlib.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,18 @@
package py

import (
"log"

"github.com/go-python/gpython/marshal"
"github.com/go-python/gpython/py"
)

// Load the frozen module
func init() {
_, err := marshal.LoadFrozenModule("importlib", data)
log.Fatalf("Failed to load importlib: %v", err)

py.RegisterModule(&py.ModuleImpl{
Info: py.ModuleInfo{
Name: "importlib",
},
CodeBuf: data,
})
}

// Auto-generated by Modules/_freeze_importlib.c
Expand Down
30 changes: 10 additions & 20 deletions marshal/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
package marshal

import (
"bytes"
"encoding/binary"
"errors"
"fmt"
Expand All @@ -15,7 +14,6 @@ import (
"strconv"

"github.com/go-python/gpython/py"
"github.com/go-python/gpython/vm"
)

const (
Expand Down Expand Up @@ -454,23 +452,6 @@ func ReadPyc(r io.Reader) (obj py.Object, err error) {
return ReadObject(r)
}

// Unmarshals a frozen module
func LoadFrozenModule(name string, data []byte) (*py.Module, error) {
r := bytes.NewBuffer(data)
obj, err := ReadObject(r)
if err != nil {
return nil, err
}
code := obj.(*py.Code)
module := py.NewModule(name, "", nil, nil)
_, err = vm.Run(module.Globals, module.Globals, code, nil)
if err != nil {
py.TracebackDump(err)
return nil, err
}
return module, nil
}

const dump_doc = `dump(value, file[, version])

Write the value on the open file. The value must be a supported type.
Expand Down Expand Up @@ -634,5 +615,14 @@ func init() {
globals := py.StringDict{
"version": py.Int(MARSHAL_VERSION),
}
py.NewModule("marshal", module_doc, methods, globals)

py.RegisterModule(&py.ModuleImpl{
Info: py.ModuleInfo{
Name: "marshal",
Doc: module_doc,
Flags: py.ShareModule,
},
Globals: globals,
Methods: methods,
})
}
18 changes: 13 additions & 5 deletions math/math.go
Original file line number Diff line number Diff line change
Expand Up @@ -1333,9 +1333,17 @@ func init() {
py.MustNewMethod("trunc", math_trunc, 0, math_trunc_doc),
py.MustNewMethod("to_ulps", math_to_ulps, 0, math_to_ulps_doc),
}
globals := py.StringDict{
"pi": py.Float(math.Pi),
"e": py.Float(math.E),
}
py.NewModule("math", math_doc, methods, globals)

py.RegisterModule(&py.ModuleImpl{
Info: py.ModuleInfo{
Name: "math",
Doc: math_doc,
Flags: py.ShareModule,
},
Methods: methods,
Globals: py.StringDict{
"pi": py.Float(math.Pi),
"e": py.Float(math.E),
},
})
}
Loading