Skip to content

builtin: add dir builtin - #20

Open
sbinet wants to merge 1 commit into
mainfrom
issue-12
Open

builtin: add dir builtin#20
sbinet wants to merge 1 commit into
mainfrom
issue-12

Conversation

@sbinet

Copy link
Copy Markdown
Member

Fixes#12.

@sbinet

Copy link
Copy Markdown
MemberAuthor

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

ncw commented Sep 3, 2018

Copy link
Copy Markdown
Collaborator

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

funccallInternal(fn py.Object, args py.Tuple, kwargs py.StringDict, f*py.Frame) (py.Object, error) {
ifmethod, ok:=fn.(*py.Method); ok {
switchx:=method.Internal(); x {
casepy.InternalMethodNone:
casepy.InternalMethodGlobals:
returnf.Globals, nil
casepy.InternalMethodLocals:
f.FastToLocals()
returnf.Locals, nil
casepy.InternalMethodImport:
returnpy.BuiltinImport(nil, args, kwargs, f.Globals)
casepy.InternalMethodEval:
f.FastToLocals()
returnbuiltinEval(nil, args, kwargs, f.Locals, f.Globals, f.Builtins)
casepy.InternalMethodExec:
f.FastToLocals()
returnbuiltinExec(nil, args, kwargs, f.Locals, f.Globals, f.Builtins)
default:
returnnil, py.ExceptionNewf(py.SystemError, "Internal method %v not found", x)
}
}
returnpy.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.

@drew-512

Copy link
Copy Markdown
Contributor

@sbinet after the py.Context merge, I could look at this out if we think dir() is worth it.

@drew-512

Copy link
Copy Markdown
Contributor

@sbinet lmk if this is worth adding/improving so we can work towards a cleared PR section. Also, any suggestions on the next version/tag? I think we're worthy of a 0.1 or 1.0 -- open to discussion.

@sbinet

Copy link
Copy Markdown
MemberAuthor

in order to get the nice discoverability look and feel of CPython into gpython, having a working dir() builtin is definitely a plus.

but as we survived w/o it for almost 4 years, I don't think cleaning up this PR should be a blocker for the next tagged version of gpython.

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

builtin: implement dir

3 participants

@sbinet@ncw@drew-512