Skip to content

builtin: Implement delattr #55

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

Merged
merged 1 commit into from
Feb 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion builtin/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func init() {
// py.MustNewMethod("callable", builtin_callable, 0, callable_doc),
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("delattr", builtin_delattr, 0, delattr_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),
Expand Down Expand Up @@ -633,6 +633,27 @@ func source_as_string(cmd py.Object, funcname, what string /*, PyCompilerFlags *
// return nil;
}

const delattr_doc = `Deletes the named attribute from the given object.

delattr(x, 'y') is equivalent to "del x.y"
`

func builtin_delattr(self py.Object, args py.Tuple) (py.Object, error) {
var v py.Object
var name py.Object

err := py.UnpackTuple(args, nil, "delattr", 2, 2, &v, &name)
if err != nil {
return nil, err
}

err = py.DeleteAttr(v, name)
if err != nil {
return nil, err
}
return py.None, nil
}

const compile_doc = `compile(source, filename, mode[, flags[, dont_inherit]]) -> code object

Compile the source string (a Python module, statement or expression)
Expand Down
9 changes: 9 additions & 0 deletions builtin/tests/builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,15 @@ class C: pass
setattr(c, "potato", "spud")
assert getattr(c, "potato") == "spud"
assert c.potato == "spud"
delattr(c, "potato")
assert not hasattr(c, "potato")
ok = False
try:
delattr(c, "potato")
except AttributeError as e:
ok = True
finally:
assert ok

doc="sum"
assert sum([1,2,3]) == 6
Expand Down