Skip to content

Commit 2bc7a53

Browse files
committed
builtin: implement divmod
1 parent db8950c commit 2bc7a53

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

builtin/builtin.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func init() {
2929
py.MustNewMethod("compile", builtin_compile, 0, compile_doc),
3030
// py.MustNewMethod("delattr", builtin_delattr, 0, delattr_doc),
3131
// py.MustNewMethod("dir", builtin_dir, 0, dir_doc),
32-
// py.MustNewMethod("divmod", builtin_divmod, 0, divmod_doc),
32+
py.MustNewMethod("divmod", builtin_divmod, 0, divmod_doc),
3333
// py.MustNewMethod("eval", builtin_eval, 0, eval_doc),
3434
// py.MustNewMethod("exec", builtin_exec, 0, exec_doc),
3535
// py.MustNewMethod("format", builtin_format, 0, format_doc),
@@ -553,6 +553,23 @@ func builtin_compile(self py.Object, args py.Tuple, kwargs py.StringDict) (py.Ob
553553
return result, nil
554554
}
555555

556+
const divmod_doc = `divmod(x, y) -> (quotient, remainder)
557+
558+
Return the tuple ((x-x%y)/y, x%y). Invariant: div*y + mod == x.`
559+
560+
func builtin_divmod(self py.Object, args py.Tuple) (py.Object, error) {
561+
var x, y py.Object
562+
err := py.UnpackTuple(args, nil, "divmod", 2, 2, &x, &y)
563+
if err != nil {
564+
return nil, err
565+
}
566+
q, r, err := py.DivMod(x, y)
567+
if err != nil {
568+
return nil, err
569+
}
570+
return py.Tuple{q, r}, nil
571+
}
572+
556573
const len_doc = `len(object) -> integer
557574
558575
Return the number of items of a sequence or mapping.`

builtin/tests/builtin.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,12 @@
1010

1111
doc="compile"
1212
code = compile("pass", "<string>", "exec")
13+
assert code is not None
1314
# FIXME
1415

16+
doc="divmod"
17+
assert divmod(34,7) == (4, 6)
18+
1519
doc="getattr"
1620
class C:
1721
def __init__(self):
@@ -37,7 +41,6 @@ def __init__(self):
3741

3842
doc="locals"
3943
def fn(x):
40-
print(locals())
4144
assert locals()["x"] == 1
4245
fn(1)
4346

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy