The Wayback Machine - https://web.archive.org/web/20200910210557/https://github.com/go-python/gpython/pull/20
Skip to content
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

builtin: add dir builtin #20

Open
wants to merge 1 commit into
base: master
from
Open

builtin: add dir builtin #20

wants to merge 1 commit into from

Conversation

@sbinet
Copy link
Member

sbinet commented Aug 30, 2018

Fixes #12.

Fixes #12.
@sbinet
Copy link
Member Author

sbinet commented Aug 30, 2018

this is a WIP.

@ncw, for dir(), I need to be able to get at the content of the local scope.
how would I do that from within builtin_dir ?

@ncw
Copy link
Collaborator

ncw commented Sep 3, 2018

The way I've done that in the past, eg for implementing locals() is to implement them as InternalMethods.

Eg here is where the magic is...

gpython/vm/eval.go

Lines 1578 to 1600 in eaa7d28

func callInternal(fn py.Object, args py.Tuple, kwargs py.StringDict, f *py.Frame) (py.Object, error) {
if method, ok := fn.(*py.Method); ok {
switch x := method.Internal(); x {
case py.InternalMethodNone:
case py.InternalMethodGlobals:
return f.Globals, nil
case py.InternalMethodLocals:
f.FastToLocals()
return f.Locals, nil
case py.InternalMethodImport:
return py.BuiltinImport(nil, args, kwargs, f.Globals)
case py.InternalMethodEval:
f.FastToLocals()
return builtinEval(nil, args, kwargs, f.Locals, f.Globals, f.Builtins)
case py.InternalMethodExec:
f.FastToLocals()
return builtinExec(nil, args, kwargs, f.Locals, f.Globals, f.Builtins)
default:
return nil, py.ExceptionNewf(py.SystemError, "Internal method %v not found", x)
}
}
return py.Call(fn, args, kwargs)
}

This is where the other magic is!

$ git grep InternalMethodLocals
builtin/builtin.go:             py.MustNewMethod("locals", py.InternalMethodLocals, 0, locals_doc),
py/method.go:   InternalMethodLocals
vm/eval.go:             case py.InternalMethodLocals:

That is the only way I could find to avoid using global variables etc. It might be too limiting eventually.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

2 participants
You can’t perform that action at this time.
Morty Proxy This is a proxified and sanitized view of the page, visit original site.