-
Notifications
You must be signed in to change notification settings - Fork 95
/
Copy pathstdlib.go
298 lines (251 loc) · 7.06 KB
/
stdlib.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
// Copyright 2022 The go-python Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package stdlib provides the bootstrap code to wire in all the stdlib
// (python) modules into a gpython context and VM.
package stdlib
import (
"bytes"
"os"
"path"
"path/filepath"
"strings"
"sync"
"github.com/go-python/gpython/py"
"github.com/go-python/gpython/stdlib/marshal"
"github.com/go-python/gpython/vm"
_ "github.com/go-python/gpython/stdlib/array"
_ "github.com/go-python/gpython/stdlib/binascii"
_ "github.com/go-python/gpython/stdlib/builtin"
_ "github.com/go-python/gpython/stdlib/glob"
_ "github.com/go-python/gpython/stdlib/math"
_ "github.com/go-python/gpython/stdlib/os"
_ "github.com/go-python/gpython/stdlib/string"
_ "github.com/go-python/gpython/stdlib/sys"
_ "github.com/go-python/gpython/stdlib/tempfile"
_ "github.com/go-python/gpython/stdlib/time"
)
func init() {
// Assign the base-level py.Context creation function while also preventing an import cycle.
py.NewContext = NewContext
}
// context implements interface py.Context
type context struct {
store *py.ModuleStore
opts py.ContextOpts
closeOnce sync.Once
closing bool
closed bool
running sync.WaitGroup
done chan struct{}
}
// NewContext creates a new gpython interpreter instance context.
//
// See interface py.Context defined in py/run.go
func NewContext(opts py.ContextOpts) py.Context {
ctx := &context{
opts: opts,
done: make(chan struct{}),
closing: false,
closed: false,
}
ctx.store = py.NewModuleStore()
py.Import(ctx, "builtins", "sys")
sys_mod := ctx.Store().MustGetModule("sys")
sys_mod.Globals["argv"] = py.NewListFromStrings(opts.SysArgs)
sys_mod.Globals["path"] = py.NewListFromStrings(opts.SysPaths)
return ctx
}
// ModuleInit digests a ModuleImpl, compiling and marshalling as needed, creating a new Module instance in this Context.
func (ctx *context) ModuleInit(impl *py.ModuleImpl) (*py.Module, error) {
err := ctx.pushBusy()
defer ctx.popBusy()
if err != nil {
return nil, err
}
if impl.Code == nil && len(impl.CodeSrc) > 0 {
impl.Code, err = py.Compile(string(impl.CodeSrc), impl.Info.FileDesc, py.ExecMode, 0, true)
if err != nil {
return nil, err
}
}
if impl.Code == nil && len(impl.CodeBuf) > 0 {
codeBuf := bytes.NewBuffer(impl.CodeBuf)
obj, err := marshal.ReadObject(codeBuf)
if err != nil {
return nil, err
}
impl.Code, _ = obj.(*py.Code)
if impl.Code == nil {
return nil, py.ExceptionNewf(py.AssertionError, "Embedded code did not produce a py.Code object")
}
}
module, err := ctx.Store().NewModule(ctx, impl)
if err != nil {
return nil, err
}
if impl.Code != nil {
_, err = ctx.RunCode(impl.Code, module.Globals, module.Globals, nil)
if err != nil {
return nil, err
}
}
return module, nil
}
// See interface py.Context defined in py/run.go
func (ctx *context) ResolveAndCompile(pathname string, opts py.CompileOpts) (py.CompileOut, error) {
err := ctx.pushBusy()
defer ctx.popBusy()
if err != nil {
return py.CompileOut{}, err
}
tryPaths := defaultPaths
if opts.UseSysPaths {
tryPaths = ctx.Store().MustGetModule("sys").Globals["path"].(*py.List).Items
}
out := py.CompileOut{}
err = resolveRunPath(pathname, opts, tryPaths, func(fpath string) (bool, error) {
stat, err := os.Stat(fpath)
if err == nil && stat.IsDir() {
// FIXME this is a massive simplification!
fpath = path.Join(fpath, "__init__.py")
_, err = os.Stat(fpath)
}
ext := strings.ToLower(filepath.Ext(fpath))
if ext == "" && os.IsNotExist(err) {
fpath += ".py"
ext = ".py"
_, err = os.Stat(fpath)
}
// Keep searching while we get FNFs, stop on an error
if err != nil {
if os.IsNotExist(err) {
return true, nil
}
err = py.ExceptionNewf(py.OSError, "Error accessing %q: %v", fpath, err)
return false, err
}
switch ext {
case ".py":
var pySrc []byte
pySrc, err = os.ReadFile(fpath)
if err != nil {
return false, py.ExceptionNewf(py.OSError, "Error reading %q: %v", fpath, err)
}
out.Code, err = py.Compile(string(pySrc), fpath, py.ExecMode, 0, true)
if err != nil {
return false, err
}
out.SrcPathname = fpath
case ".pyc":
file, err := os.Open(fpath)
if err != nil {
return false, py.ExceptionNewf(py.OSError, "Error opening %q: %v", fpath, err)
}
defer file.Close()
codeObj, err := marshal.ReadPyc(file)
if err != nil {
return false, py.ExceptionNewf(py.ImportError, "Failed to marshal %q: %v", fpath, err)
}
out.Code, _ = codeObj.(*py.Code)
out.PycPathname = fpath
}
out.FileDesc = fpath
return false, nil
})
if out.Code == nil && err == nil {
err = py.ExceptionNewf(py.AssertionError, "Missing code object")
}
if err != nil {
return py.CompileOut{}, err
}
return out, nil
}
func (ctx *context) pushBusy() error {
if ctx.closed {
return py.ExceptionNewf(py.RuntimeError, "Context closed")
}
ctx.running.Add(1)
return nil
}
func (ctx *context) popBusy() {
ctx.running.Done()
}
// See interface py.Context defined in py/run.go
func (ctx *context) Close() error {
ctx.closeOnce.Do(func() {
ctx.closing = true
ctx.running.Wait()
ctx.closed = true
// Give each module a chance to release resources
ctx.store.OnContextClosed()
close(ctx.done)
})
return nil
}
// See interface py.Context defined in py/run.go
func (ctx *context) Done() <-chan struct{} {
return ctx.done
}
var defaultPaths = []py.Object{
py.String("."),
}
func resolveRunPath(runPath string, opts py.CompileOpts, pathObjs []py.Object, tryPath func(pyPath string) (bool, error)) error {
runPath = strings.TrimSuffix(runPath, "/")
var (
err error
cwd string
cont = true
)
for _, pathObj := range pathObjs {
pathStr, ok := pathObj.(py.String)
if !ok {
continue
}
// If an absolute path, just try that.
// Otherwise, check from the passed current dir then check from the current working dir.
fpath := path.Join(string(pathStr), runPath)
if filepath.IsAbs(fpath) {
cont, err = tryPath(fpath)
} else {
if len(opts.CurDir) > 0 {
subPath := path.Join(opts.CurDir, fpath)
cont, err = tryPath(subPath)
}
if cont && err == nil {
if cwd == "" {
cwd, _ = os.Getwd()
}
subPath := path.Join(cwd, fpath)
cont, err = tryPath(subPath)
}
}
if !cont {
break
}
}
if err != nil {
return err
}
if cont {
return py.ExceptionNewf(py.FileNotFoundError, "Failed to resolve %q", runPath)
}
return err
}
// See interface py.Context defined in py/run.go
func (ctx *context) RunCode(code *py.Code, globals, locals py.StringDict, closure py.Tuple) (py.Object, error) {
err := ctx.pushBusy()
defer ctx.popBusy()
if err != nil {
return nil, err
}
return vm.EvalCode(ctx, code, globals, locals, nil, nil, nil, nil, closure)
}
// See interface py.Context defined in py/run.go
func (ctx *context) GetModule(moduleName string) (*py.Module, error) {
return ctx.store.GetModule(moduleName)
}
// See interface py.Context defined in py/run.go
func (ctx *context) Store() *py.ModuleStore {
return ctx.store
}