FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

builtin: add dir builtin by sbinet · Pull Request #20 · go-python/gpython · GitHub

Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .go  (1) .py  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
35 changes: 34 additions & 1 deletion builtin/builtin.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func init() {
py.MustNewMethod("chr", builtin_chr, 0, chr_doc),
py.MustNewMethod("compile", builtin_compile, 0, compile_doc),
// py.MustNewMethod("delattr", builtin_delattr, 0, delattr_doc),
// py.MustNewMethod("dir", builtin_dir, 0, dir_doc),
py.MustNewMethod("dir", builtin_dir, 0, dir_doc),
py.MustNewMethod("divmod", builtin_divmod, 0, divmod_doc),
py.MustNewMethod("eval", py.InternalMethodEval, 0, eval_doc),
py.MustNewMethod("exec", py.InternalMethodExec, 0, exec_doc),
Expand Down Expand Up @@ -642,6 +642,39 @@ func builtin_compile(self py.Object, args py.Tuple, kwargs py.StringDict) (py.Ob
return result, nil
}

const dir_doc = `dir([object]) -> list of strings

If called without an argument, return the names in the current scope.
Else, return an alphabetized list of names comprising (some of) the attributes
of the given object, and of attributes reachable from it.
If the object supplies a method named __dir__, it will be used; otherwise
the default dir() logic is used and returns:
for a module object: the module's attributes.
for a class object: its attributes, and recursively the attributes
of its bases.
for any other object: its attributes, its class's attributes, and
recursively the attributes of its class's base classes.
`

func builtin_dir(self py.Object, args py.Tuple) (py.Object, error) {
n, err := args.M__len__()
if err != nil {
return nil, err
}

nn := n.(py.Int)
if nn == 0 {
// list current scope.
panic("dir() not implemented")
}

if nn > 1 {
return nil, py.ExceptionNewf(py.TypeError, "dir expected at most 1 arguments, got %d", nn)
}

panic("dir(n) not implemented")
}

const divmod_doc = `divmod(x, y) -> (quotient, remainder)

Return the tuple ((x-x%y)/y, x%y). Invariant: div*y + mod == x.`
Expand Down
16 changes: 16 additions & 0 deletions builtin/tests/builtin.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,22 @@
assert code is not None
# FIXME

doc="dir"
def testDir():
a = 1
assert dir() == ["a"]
b = 2
assert dir() == ["a", "b"]
class A:
def method(self): pass
assert "method" in dir(A())
testDir()
try:
dir(1,2,3)
ok=False
except TypeError: ok=True
assert ok, "no exception raised"

doc="divmod"
assert divmod(34,7) == (4, 6)

Expand Down

Back | FazBrowse Home | New Git URL