代码拉取完成,页面将自动刷新
// Module objects
package py
import "fmt"
var (
// Registry of installed modules
modules = make(map[string]*Module)
// Builtin module
Builtins *Module
// this should be the frozen module importlib/_bootstrap.py generated
// by Modules/_freeze_importlib.c into Python/importlib.h
Importlib *Module
)
// A python Module object
type Module struct {
Name string
Doc string
Globals StringDict
// dict Dict
}
var ModuleType = NewType("module", "module object")
// Type of this object
func (o *Module) Type() *Type {
return ModuleType
}
func (m *Module) M__repr__() (Object, error) {
return String(fmt.Sprintf("<module %s>", m.Name)), nil
}
// Get the Dict
func (m *Module) GetDict() StringDict {
return m.Globals
}
// Define a new module
func NewModule(name, doc string, methods []*Method, globals StringDict) *Module {
m := &Module{
Name: name,
Doc: doc,
Globals: globals.Copy(),
}
// Insert the methods into the module dictionary
for _, method := range methods {
m.Globals[method.Name] = method
}
// Set some module globals
m.Globals["__name__"] = String(name)
m.Globals["__doc__"] = String(doc)
m.Globals["__package__"] = None
// Register the module
modules[name] = m
// Make a note of some modules
switch name {
case "builtins":
Builtins = m
case "importlib":
Importlib = m
}
// fmt.Printf("Registering module %q\n", name)
return m
}
// Gets a module
func GetModule(name string) (*Module, error) {
m, ok := modules[name]
if !ok {
return nil, ExceptionNewf(ImportError, "Module %q not found", name)
}
return m, nil
}
// Gets a module or panics
func MustGetModule(name string) *Module {
m, err := GetModule(name)
if err != nil {
panic(err)
}
return m
}
// Calls a named method of a module
func (m *Module) Call(name string, args Tuple, kwargs StringDict) (Object, error) {
attr, err := GetAttrString(m, name)
if err != nil {
return nil, err
}
return Call(attr, args, kwargs)
}
// Interfaces
var _ IGetDict = (*Module)(nil)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。