Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
81 commits
Select commit Hold shift + click to select a range
fc66b7f
py.CompileMode now a string enum
Dec 8, 2021
d10c35f
addressed benign Go warnings
Dec 8, 2021
2161b71
make Float implement M__index__
Dec 8, 2021
247ec42
added string helper functions
Dec 8, 2021
9a75271
updated to 1.15
Dec 8, 2021
90a8988
new Ctx-aware module model
Dec 9, 2021
c51644e
py.Ctx model allows concurrent execution
Dec 9, 2021
7f7d213
repl.New() now accepts an existing py.Ctx
Dec 9, 2021
708bb7e
fixed typo in callInternal()
Dec 9, 2021
a244256
unbroke test compatibilty
Dec 9, 2021
75d8743
cleaned up py.Ctx initialization wrt sys and DefaultCtxOpts()
Dec 10, 2021
d5fb8e8
added multi Ctx benchmark
Dec 10, 2021
269bdc0
WIP
Dec 10, 2021
4db9c65
fixed py.NewCtx not being set
Dec 10, 2021
53d05db
fixed List.sort() detection of instance invocation
Dec 10, 2021
7fd1c29
separated path resolution from path evaluation
Dec 11, 2021
8f3a104
removed cruft
Dec 18, 2021
5ebaf66
place ctx at top
Dec 18, 2021
d423190
err msg tweak
drew-512 Dec 18, 2021
b33c266
typo fix
drew-512 Dec 18, 2021
efca761
for loops we can all agree on
drew-512 Dec 18, 2021
1569475
removed outdated comments
Dec 18, 2021
a9c5f1f
Merge branch 'master' of https://github.com/drew-512/gpython
Dec 18, 2021
6372aee
added info for Frame.Ctx
Dec 19, 2021
4af4f90
removed unnecessary assignment
Dec 19, 2021
aec59f8
copyright and formatting
Dec 19, 2021
74f1a19
Ctx and NewCtx docs
Dec 19, 2021
8fa6622
switch cleanup
Dec 19, 2021
26fb9aa
leaner ModuleImpl schema
Dec 20, 2021
8614aeb
added GetDict() impl
Dec 20, 2021
86531c6
Compile() now returns py.Code (vs py.Object)
Dec 20, 2021
f71b961
tighter py.Ctx invocation patterns
Dec 20, 2021
77a7954
WIP
Dec 20, 2021
6bba7da
reverted to 644
Dec 20, 2021
46d1052
chmod 644
Dec 20, 2021
66fad23
enhanced RunFile() to enclose RunInNewModule()
Dec 27, 2021
95afd7b
Update examples/multi-ctx/main.go
drew-512 Jan 10, 2022
319edb7
Update modules/runtime.go
drew-512 Jan 10, 2022
06a2eae
Update modules/runtime.go
drew-512 Jan 10, 2022
8d4d586
Update modules/runtime.go
drew-512 Jan 10, 2022
d475e67
Update vm/eval.go
drew-512 Jan 10, 2022
6afac99
Update py/run.go
drew-512 Jan 10, 2022
931c346
Update modules/runtime.go
drew-512 Jan 10, 2022
a7ce4bb
Update modules/runtime.go
drew-512 Jan 10, 2022
c1f8809
Update modules/runtime.go
drew-512 Jan 10, 2022
9f02cf1
Update modules/runtime.go
drew-512 Jan 10, 2022
85e3a4a
Update modules/runtime.go
drew-512 Jan 10, 2022
fbec34e
Update modules/runtime.go
drew-512 Jan 10, 2022
dbd54d8
code cleanuo
Jan 10, 2022
71a5604
Module doc str
Jan 10, 2022
634823c
consistent arg order of NewFunction
Jan 10, 2022
6a0c11e
license and formatting
Jan 10, 2022
6242836
comment and cruft cleanup
Jan 10, 2022
61c7830
renamed Store to ModuleStore + docs
Jan 10, 2022
bb98aa8
added import docs
Jan 10, 2022
36b1018
shadowing cleanup
Jan 10, 2022
f2602bc
docs tweaks
Jan 10, 2022
e25e3bb
code cleanup
Jan 10, 2022
9421b6b
synced with gpython
Jan 10, 2022
92aed8a
Merge branch 'go-python:master' into master
drew-512 Jan 10, 2022
96515ea
Update modules/runtime.go
drew-512 Jan 10, 2022
f62f2b7
Update modules/runtime.go
drew-512 Jan 10, 2022
3bdba62
reverted from %w to %v (linux CI compile failure)
Jan 10, 2022
8033615
renamed py.Ctx to py.Context
Jan 10, 2022
681abf2
added Context.Close() and ModuleImpl.OnContextClosed()
Jan 18, 2022
7508556
docs and LoadIntsFromList
Jan 19, 2022
99bcb40
rename edits
Jan 21, 2022
0cc7650
push/popBusy now private
Jan 21, 2022
9afb2d3
doc edits
Jan 21, 2022
7d2a8d9
fixed kwarg issues in ParseTupleAndKeywords() and builtin_print test
Jan 25, 2022
ba7db80
added Flush kwarg to print test
Jan 25, 2022
61e42c8
added embedding example
Jan 26, 2022
168c337
main README makeover
Jan 26, 2022
2b7e1ab
fixed type conversion
Jan 26, 2022
3821b4b
Update README.md
drew-512 Jan 28, 2022
170f0d2
Update README.md
drew-512 Jan 28, 2022
b68b9f6
Update py/util.go
drew-512 Jan 28, 2022
759023b
Update py/util.go
drew-512 Jan 28, 2022
0c77716
Update py/util.go
drew-512 Jan 28, 2022
505755d
LoadIntsFromList cleanup
Jan 28, 2022
02d6b3e
comment edits
Jan 28, 2022
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
tighter py.Ctx invocation patterns
  • Loading branch information
Drew O'Meara committed Dec 20, 2021
commit f71b961f6f9d285b6177deae1caa435b7c59a460
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func main() {
cli.RunREPL(replCtx)

} else {
_, err := ctx.RunFile(args[0], py.RunOpts{})
err := py.RunFile(ctx, args[0], py.CompileOpts{})
if err != nil {
py.TracebackDump(err)
log.Fatal(err)
Expand Down
77 changes: 58 additions & 19 deletions py/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package py
type CompileMode string

const (
ExecMode CompileMode = "exec"
EvalMode CompileMode = "eval"
SingleMode CompileMode = "single"
ExecMode CompileMode = "exec" // Compile a module
EvalMode CompileMode = "eval" // Compile an expression
SingleMode CompileMode = "single" // Compile a single (interactive) statement
)

type RunFlags int32
Expand All @@ -21,14 +21,20 @@ const (
// In general, one creates a py.Ctx (via py.NewCtx) for each concurrent goroutine to be running an interpreter.
// In other words, ensure that a py.Ctx is never concurrently accessed across goroutines.
//
// See BenchmarkCtx() in vm/vm_test.go
// RunFile() and RunCode() block until code execution is complete.
// In the future, they will abort early if the parent associated py.Ctx is signaled.
//
// See examples/multi-ctx
type Ctx interface {

// These each initiate blocking execution.
RunCode(code *Code, globals, locals StringDict, closure Tuple) (res Object, err error)
// Resolves then compiles (if applicable) the given file system pathname into a py.Code ready to be executed.
ResolveAndCompile(pathname string, opts CompileOpts) (CompileOut, error)

// Creates a new py.Module instance and initializes ModuleImpl's code in the new module (if applicable).
ModuleInit(impl *ModuleImpl) (*Module, error)

// Runs a given file in the given host module.
RunFile(runPath string, opts RunOpts) (*Module, error)
// RunCode is a lower-level invocation to execute the given py.Code.
RunCode(code *Code, globals, locals StringDict, closure Tuple) (result Object, err error)

// Execution of any of the above will stop when the next opcode runs
// @@TODO
Expand All @@ -44,8 +50,6 @@ type Ctx interface {
const (
SrcFileExt = ".py"
CodeFileExt = ".pyc"

DefaultModuleName = "__main__"
)

type StdLib int32
Expand All @@ -57,11 +61,16 @@ const (
CoreLibs = Lib_sys | Lib_time
)

type RunOpts struct {
HostModule *Module // Host module to execute within (if nil, a new module is created)
ModuleName string // If HostModule == nil, this is the name of the newly created module. If nil, "__main__" is used.
CurDir string // If non-nil, this is the path of the current working directory. If nil, os.Getwd() is used
UseSysPaths bool
type CompileOpts struct {
UseSysPaths bool // If set, sys.path will be used to resolve relative pathnames
CurDir string // If non-nil, this is the path of the current working directory. If nil, os.Getwd() is used.
}

type CompileOut struct {
SrcPathname string // Resolved pathname the .py file that was compiled (if applicable)
PycPathname string // Pathname of the .pyc file read and/or written (if applicable)
FileDesc string // Pathname to be used for a a module's "__file__" attrib
Code *Code // Read/Output code object ready for execution
}

// Can be changed during runtime and will \play nice with others using DefaultCtxOpts()
Expand All @@ -83,7 +92,7 @@ type CtxOpts struct {
}

var (
// DefaultCtxOpts should be default opts created for py.NewCtx.
// DefaultCtxOpts should be the default opts created for py.NewCtx.
// Calling this ensure that you future proof you code for suggested/default settings.
DefaultCtxOpts = func() CtxOpts {
opts := CtxOpts{
Expand All @@ -94,10 +103,40 @@ var (
}

// NewCtx is a high-level call to create a new gpython interpreter context.
// It allows you specify default settings, sys search paths, and is the foundational object for concurrent interpreter execution.
// See type Ctx interface.
NewCtx func(opts CtxOpts) Ctx

// Compile is a high-level call to compile a python buffer into a py.Code object.
// Compiles a python buffer into a py.Code object.
// Returns a py.Code object or otherwise an error.
Compile func(str, filename string, mode CompileMode, flags int, dont_inherit bool) (Object, error)
Compile func(src, srcDesc string, mode CompileMode, flags int, dont_inherit bool) (*Code, error)
)

// Convenience function that resolves then executes the given file pathname in a new module.
// If moduleName is nil, "__main__" is used
func RunInNewModule(
ctx Ctx,
pathname string,
opts CompileOpts,
moduleName string,
) (*Module, error) {
out, err := ctx.ResolveAndCompile(pathname, opts)
if err != nil {
return nil, err
}

moduleImpl := ModuleImpl{
Info: ModuleInfo{
Name: moduleName,
FileDesc: out.FileDesc,
},
Code: out.Code,
}

return ctx.ModuleInit(&moduleImpl)
}

// Convenience function that resolves then executes the given file pathname in a new __main__ module
func RunFile(ctx Ctx, pathname string, opts CompileOpts) error {
_, err := RunInNewModule(ctx, pathname, opts, "")
return err
}
13 changes: 7 additions & 6 deletions repl/repl.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type UI interface {
Print(string)
}

// New create a new REPL and initialises the state machine
// New create a new REPL and initializes the state machine
func New(ctx py.Ctx) *REPL {
if ctx == nil {
ctx = py.NewCtx(py.DefaultCtxOpts())
Expand All @@ -51,9 +51,11 @@ func New(ctx py.Ctx) *REPL {
continuation: false,
previous: "",
}
r.Module = ctx.Store().NewModule(ctx, py.ModuleInfo{
FileDesc: r.prog,
}, nil, nil)
r.Module, _ = ctx.ModuleInit(&py.ModuleImpl{
Info: py.ModuleInfo{
FileDesc: r.prog,
},
})
return r
}

Expand Down Expand Up @@ -82,7 +84,7 @@ func (r *REPL) Run(line string) {
if toCompile == "" {
return
}
obj, err := py.Compile(toCompile+"\n", r.prog, py.SingleMode, 0, true)
code, err := py.Compile(toCompile+"\n", r.prog, py.SingleMode, 0, true)
if err != nil {
// Detect that we should start a continuation line
// FIXME detect EOF properly!
Expand All @@ -105,7 +107,6 @@ func (r *REPL) Run(line string) {
r.term.Print(fmt.Sprintf("Compile error: %v", err))
return
}
code := obj.(*py.Code)
_, err = r.Ctx.RunCode(code, r.Module.Globals, r.Module.Globals, nil)
if err != nil {
py.TracebackDump(err)
Expand Down