Skip to content

Commit 427419c

Browse files
committed
Add module objects, method objects and a start at builtins
1 parent 06dc53f commit 427419c

File tree

3 files changed

+219
-0
lines changed

3 files changed

+219
-0
lines changed

builtin/builtin.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Built-in functions
2+
package builtin
3+
4+
import (
5+
"fmt"
6+
"github.com/ncw/gpython/py"
7+
)
8+
9+
const builtin_doc = `Built-in functions, exceptions, and other objects.
10+
11+
Noteworthy: None is the 'nil' object; Ellipsis represents '...' in slices.`
12+
13+
// Initialise the module
14+
func init() {
15+
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),
33+
// 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),
43+
// 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),
57+
}
58+
py.NewModule("builtins", builtin_doc, methods)
59+
}
60+
61+
const print_doc = `print(value, ..., sep=' ', end='\\n', file=sys.stdout, flush=False)
62+
63+
Prints the values to a stream, or to sys.stdout by default.
64+
Optional keyword arguments:
65+
file: a file-like object (stream); defaults to the current sys.stdout.
66+
sep: string inserted between values, default a space.
67+
end: string appended after the last value, default a newline.
68+
flush: whether to forcibly flush the stream.`
69+
70+
func builtin_print(self py.Object, args py.Tuple, kwargs py.Dict) py.Object {
71+
fmt.Printf("print %v, %v, %v\n", self, args, kwargs)
72+
return py.None
73+
}

py/method.go

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
// Method objects
2+
//
3+
// This is about the type 'builtin_function_or_method', not Python
4+
// methods in user-defined classes. See class.go for the latter.
5+
6+
package py
7+
8+
// Types for methods
9+
type PyCFunction func(Object, Tuple) Object
10+
type PyCFunctionWithKeywords func(Object, Tuple, Dict) Object
11+
12+
const (
13+
14+
// This is the typical calling convention, where the methods have the
15+
// type PyCFunction. The function expects two PyObject* values. The
16+
// first one is the self object for methods; for module functions, it
17+
// is the module object. The second parameter (often called args) is a
18+
// tuple object representing all arguments. This parameter is
19+
// typically processed using PyArg_ParseTuple() or
20+
// PyArg_UnpackTuple().
21+
METH_VARARGS = 0x0001
22+
23+
// Methods with these flags must be of type
24+
// PyCFunctionWithKeywords. The function expects three parameters:
25+
// self, args, and a dictionary of all the keyword arguments. The flag
26+
// is typically combined with METH_VARARGS, and the parameters are
27+
// typically processed using PyArg_ParseTupleAndKeywords().
28+
METH_KEYWORDS = 0x0002
29+
30+
// Methods without parameters don’t need to check whether arguments
31+
// are given if they are listed with the METH_NOARGS flag. They need
32+
// to be of type PyCFunction. The first parameter is typically named
33+
// self and will hold a reference to the module or object instance. In
34+
// all cases the second parameter will be NULL.
35+
METH_NOARGS = 0x0004
36+
37+
// Methods with a single object argument can be listed with the METH_O
38+
// flag, instead of invoking PyArg_ParseTuple() with a "O"
39+
// argument. They have the type PyCFunction, with the self parameter,
40+
// and a PyObject* parameter representing the single argument.
41+
METH_O = 0x0008
42+
43+
// These two constants are not used to indicate the calling convention
44+
// but the binding when use with methods of classes. These may not be
45+
// used for functions defined for modules. At most one of these flags
46+
// may be set for any given method.
47+
48+
// The method will be passed the type object as the first parameter
49+
// rather than an instance of the type. This is used to create class
50+
// methods, similar to what is created when using the classmethod()
51+
// built-in function.
52+
METH_CLASS = 0x0010
53+
54+
// The method will be passed NULL as the first parameter rather than
55+
// an instance of the type. This is used to create static methods,
56+
// similar to what is created when using the staticmethod() built-in
57+
// function.
58+
METH_STATIC = 0x0020
59+
60+
// One other constant controls whether a method is loaded in
61+
// place of another definition with the same method name.
62+
63+
// The method will be loaded in place of existing definitions. Without
64+
// METH_COEXIST, the default is to skip repeated definitions. Since
65+
// slot wrappers are loaded before the method table, the existence of
66+
// a sq_contains slot, for example, would generate a wrapped method
67+
// named __contains__() and preclude the loading of a corresponding
68+
// PyCFunction with the same name. With the flag defined, the
69+
// PyCFunction will be loaded in place of the wrapper object and will
70+
// co-exist with the slot. This is helpful because calls to
71+
// PyCFunctions are optimized more than wrapper object calls.
72+
METH_COEXIST = 0x0040
73+
)
74+
75+
// A python Method object
76+
type Method struct {
77+
// Name of this function
78+
name string
79+
// Doc string
80+
doc string
81+
// Flags - see METH_* flags
82+
flags int
83+
// C function implementation (two definitions, only one is used)
84+
method PyCFunction
85+
methodWithKeywords PyCFunctionWithKeywords
86+
}
87+
88+
var MethodType = NewType("method")
89+
90+
// Type of this object
91+
func (o *Method) Type() *Type {
92+
return MethodType
93+
}
94+
95+
// Define a new method
96+
func NewMethod(name string, method PyCFunction, flags int, doc string) *Method {
97+
if flags&METH_KEYWORDS != 0 {
98+
panic("Can't set METH_KEYWORDS")
99+
}
100+
return &Method{
101+
name: name,
102+
doc: doc,
103+
flags: flags,
104+
method: method,
105+
}
106+
}
107+
108+
// Define a new method with keyword arguments
109+
func NewMethodWithKeywords(name string, method PyCFunctionWithKeywords, flags int, doc string) *Method {
110+
if flags&METH_KEYWORDS == 0 {
111+
panic("Must set METH_KEYWORDS")
112+
}
113+
return &Method{
114+
name: name,
115+
doc: doc,
116+
flags: flags,
117+
methodWithKeywords: method,
118+
}
119+
}

py/module.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Module objects
2+
3+
package py
4+
5+
// A python Module object
6+
type Module struct {
7+
name string
8+
doc string
9+
methods []*Method
10+
// dict Dict
11+
}
12+
13+
var ModuleType = NewType("module")
14+
15+
// Type of this object
16+
func (o *Module) Type() *Type {
17+
return ModuleType
18+
}
19+
20+
// Define a new module
21+
func NewModule(name, doc string, methods []*Method) *Module {
22+
return &Module{
23+
name: name,
24+
doc: doc,
25+
methods: methods,
26+
}
27+
}

0 commit comments

Comments
 (0)