@@ -13,47 +13,47 @@ Noteworthy: None is the 'nil' object; Ellipsis represents '...' in slices.`
1313// Initialise the module
1414func init () {
1515 methods := []* py.Method {
16- // py.NewMethodWithKeywords ("__build_class__", builtin___build_class__, py.METH_VARARGS|py.METH_KEYWORDS , build_class_doc),
17- // py.NewMethodWithKeywords ("__import__", builtin___import__, py.METH_VARARGS|py.METH_KEYWORDS , import_doc),
18- // py.NewMethod("abs", builtin_abs, py.METH_O , abs_doc),
19- // py.NewMethod("all", builtin_all, py.METH_O , all_doc),
20- // py.NewMethod("any", builtin_any, py.METH_O , any_doc),
21- // py.NewMethod("ascii", builtin_ascii, py.METH_O , ascii_doc),
22- // py.NewMethod("bin", builtin_bin, py.METH_O , bin_doc),
23- // py.NewMethod("callable", builtin_callable, py.METH_O , callable_doc),
24- // py.NewMethod("chr", builtin_chr, py.METH_VARARGS , chr_doc),
25- // py.NewMethodWithKeywords ("compile", builtin_compile, py.METH_VARARGS|py.METH_KEYWORDS , compile_doc),
26- // py.NewMethod("delattr", builtin_delattr, py.METH_VARARGS , delattr_doc),
27- // py.NewMethod("dir", builtin_dir, py.METH_VARARGS , dir_doc),
28- // py.NewMethod("divmod", builtin_divmod, py.METH_VARARGS , divmod_doc),
29- // py.NewMethod("eval", builtin_eval, py.METH_VARARGS , eval_doc),
30- // py.NewMethod("exec", builtin_exec, py.METH_VARARGS , exec_doc),
31- // py.NewMethod("format", builtin_format, py.METH_VARARGS , format_doc),
32- // py.NewMethod("getattr", builtin_getattr, py.METH_VARARGS , getattr_doc),
16+ // py.NewMethod ("__build_class__", builtin___build_class__, 0 , build_class_doc),
17+ // py.NewMethod ("__import__", builtin___import__, 0 , import_doc),
18+ py .NewMethod ("abs" , builtin_abs , 0 , abs_doc ),
19+ // py.NewMethod("all", builtin_all, 0 , all_doc),
20+ // py.NewMethod("any", builtin_any, 0 , any_doc),
21+ // py.NewMethod("ascii", builtin_ascii, 0 , ascii_doc),
22+ // py.NewMethod("bin", builtin_bin, 0 , bin_doc),
23+ // py.NewMethod("callable", builtin_callable, 0 , callable_doc),
24+ // py.NewMethod("chr", builtin_chr, 0 , chr_doc),
25+ // py.NewMethod ("compile", builtin_compile, 0 , compile_doc),
26+ // py.NewMethod("delattr", builtin_delattr, 0 , delattr_doc),
27+ // py.NewMethod("dir", builtin_dir, 0 , dir_doc),
28+ // py.NewMethod("divmod", builtin_divmod, 0 , divmod_doc),
29+ // py.NewMethod("eval", builtin_eval, 0 , eval_doc),
30+ // py.NewMethod("exec", builtin_exec, 0 , exec_doc),
31+ // py.NewMethod("format", builtin_format, 0 , format_doc),
32+ // py.NewMethod("getattr", builtin_getattr, 0 , getattr_doc),
3333 // py.NewMethod("globals", builtin_globals, py.METH_NOARGS, globals_doc),
34- // py.NewMethod("hasattr", builtin_hasattr, py.METH_VARARGS , hasattr_doc),
35- // py.NewMethod("hash", builtin_hash, py.METH_O , hash_doc),
36- // py.NewMethod("hex", builtin_hex, py.METH_O , hex_doc),
37- // py.NewMethod("id", builtin_id, py.METH_O , id_doc),
38- // py.NewMethod("input", builtin_input, py.METH_VARARGS , input_doc),
39- // py.NewMethod("isinstance", builtin_isinstance, py.METH_VARARGS , isinstance_doc),
40- // py.NewMethod("issubclass", builtin_issubclass, py.METH_VARARGS , issubclass_doc),
41- // py.NewMethod("iter", builtin_iter, py.METH_VARARGS , iter_doc),
42- // py.NewMethod("len", builtin_len, py.METH_O , len_doc),
34+ // py.NewMethod("hasattr", builtin_hasattr, 0 , hasattr_doc),
35+ // py.NewMethod("hash", builtin_hash, 0 , hash_doc),
36+ // py.NewMethod("hex", builtin_hex, 0 , hex_doc),
37+ // py.NewMethod("id", builtin_id, 0 , id_doc),
38+ // py.NewMethod("input", builtin_input, 0 , input_doc),
39+ // py.NewMethod("isinstance", builtin_isinstance, 0 , isinstance_doc),
40+ // py.NewMethod("issubclass", builtin_issubclass, 0 , issubclass_doc),
41+ // py.NewMethod("iter", builtin_iter, 0 , iter_doc),
42+ // py.NewMethod("len", builtin_len, 0 , len_doc),
4343 // py.NewMethod("locals", builtin_locals, py.METH_NOARGS, locals_doc),
44- // py.NewMethodWithKeywords ("max", builtin_max, py.METH_VARARGS|py.METH_KEYWORDS , max_doc),
45- // py.NewMethodWithKeywords ("min", builtin_min, py.METH_VARARGS|py.METH_KEYWORDS , min_doc),
46- // py.NewMethod("next", builtin_next, py.METH_VARARGS , next_doc),
47- // py.NewMethod("oct", builtin_oct, py.METH_O , oct_doc),
48- // py.NewMethod("ord", builtin_ord, py.METH_O , ord_doc),
49- // py.NewMethod("pow", builtin_pow, py.METH_VARARGS , pow_doc),
50- py .NewMethodWithKeywords ("print" , builtin_print , py . METH_VARARGS | py . METH_KEYWORDS , print_doc ),
51- // py.NewMethod("repr", builtin_repr, py.METH_O , repr_doc),
52- // py.NewMethodWithKeywords ("round", builtin_round, py.METH_VARARGS|py.METH_KEYWORDS , round_doc),
53- // py.NewMethod("setattr", builtin_setattr, py.METH_VARARGS , setattr_doc),
54- // py.NewMethodWithKeywords ("sorted", builtin_sorted, py.METH_VARARGS|py.METH_KEYWORDS , sorted_doc),
55- // py.NewMethod("sum", builtin_sum, py.METH_VARARGS , sum_doc),
56- // py.NewMethod("vars", builtin_vars, py.METH_VARARGS , vars_doc),
44+ // py.NewMethod ("max", builtin_max, 0 , max_doc),
45+ // py.NewMethod ("min", builtin_min, 0 , min_doc),
46+ // py.NewMethod("next", builtin_next, 0 , next_doc),
47+ // py.NewMethod("oct", builtin_oct, 0 , oct_doc),
48+ // py.NewMethod("ord", builtin_ord, 0 , ord_doc),
49+ py .NewMethod ("pow" , builtin_pow , 0 , pow_doc ),
50+ py .NewMethod ("print" , builtin_print , 0 , print_doc ),
51+ // py.NewMethod("repr", builtin_repr, 0 , repr_doc),
52+ py .NewMethod ("round" , builtin_round , 0 , round_doc ),
53+ // py.NewMethod("setattr", builtin_setattr, 0 , setattr_doc),
54+ // py.NewMethod ("sorted", builtin_sorted, 0 , sorted_doc),
55+ // py.NewMethod("sum", builtin_sum, 0 , sum_doc),
56+ // py.NewMethod("vars", builtin_vars, 0 , vars_doc),
5757 }
5858 py .NewModule ("builtins" , builtin_doc , methods )
5959}
@@ -71,3 +71,45 @@ func builtin_print(self py.Object, args py.Tuple, kwargs py.StringDict) py.Objec
7171 fmt .Printf ("print %v, %v, %v\n " , self , args , kwargs )
7272 return py .None
7373}
74+
75+ const pow_doc = `pow(x, y[, z]) -> number
76+
77+ With two arguments, equivalent to x**y. With three arguments,
78+ equivalent to (x**y) % z, but may be more efficient (e.g. for ints).`
79+
80+ func builtin_pow (self py.Object , args py.Tuple ) py.Object {
81+ var v , w , z py.Object
82+ z = py .None
83+ py .UnpackTuple (args , "pow" , 2 , 3 , & v , & w , & z )
84+ return py .Pow (v , w , z )
85+ }
86+
87+ const abs_doc = `"abs(number) -> number
88+
89+ Return the absolute value of the argument.`
90+
91+ func builtin_abs (self , v py.Object ) py.Object {
92+ return py .Abs (v )
93+ }
94+
95+ const round_doc = `round(number[, ndigits]) -> number
96+
97+ Round a number to a given precision in decimal digits (default 0 digits).
98+ This returns an int when called with one argument, otherwise the
99+ same type as the number. ndigits may be negative.`
100+
101+ func builtin_round (self py.Object , args py.Tuple , kwargs py.StringDict ) py.Object {
102+ var number , ndigits py.Object
103+ ndigits = py .Int (0 )
104+ // var kwlist = []string{"number", "ndigits"}
105+ // FIXME py.ParseTupleAndKeywords(args, kwargs, "O|O:round", kwlist, &number, &ndigits)
106+ py .UnpackTuple (args , "round" , 1 , 2 , & number , & ndigits )
107+
108+ numberRounder , ok := number .(py.I__round__ )
109+ if ! ok {
110+ // FIXME TypeError
111+ panic (fmt .Sprintf ("TypeError: type %s doesn't define __round__ method" , number .Type ().Name ))
112+ }
113+
114+ return numberRounder .M__round__ (ndigits )
115+ }
0 commit comments