Skip to content

Commit

Permalink
builtin: implement divmod
Browse files Browse the repository at this point in the history
  • Loading branch information
ncw committed Jun 3, 2015
1 parent db8950c commit 2bc7a53
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
19 changes: 18 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("compile", builtin_compile, 0, compile_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("divmod", builtin_divmod, 0, divmod_doc),
// py.MustNewMethod("eval", builtin_eval, 0, eval_doc),
// py.MustNewMethod("exec", builtin_exec, 0, exec_doc),
// py.MustNewMethod("format", builtin_format, 0, format_doc),
Expand Down Expand Up @@ -553,6 +553,23 @@ func builtin_compile(self py.Object, args py.Tuple, kwargs py.StringDict) (py.Ob
return result, nil
}

const divmod_doc = `divmod(x, y) -> (quotient, remainder)
Return the tuple ((x-x%y)/y, x%y). Invariant: div*y + mod == x.`

func builtin_divmod(self py.Object, args py.Tuple) (py.Object, error) {
var x, y py.Object
err := py.UnpackTuple(args, nil, "divmod", 2, 2, &x, &y)
if err != nil {
return nil, err
}
q, r, err := py.DivMod(x, y)
if err != nil {
return nil, err
}
return py.Tuple{q, r}, nil
}

const len_doc = `len(object) -> integer
Return the number of items of a sequence or mapping.`
Expand Down
5 changes: 4 additions & 1 deletion builtin/tests/builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@

doc="compile"
code = compile("pass", "<string>", "exec")
assert code is not None
# FIXME

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

doc="getattr"
class C:
def __init__(self):
Expand All @@ -37,7 +41,6 @@ def __init__(self):

doc="locals"
def fn(x):
print(locals())
assert locals()["x"] == 1
fn(1)

Expand Down

0 comments on commit 2bc7a53

Please sign in to comment.