8000 Implement len() and py.Len · go-python/gpython@b30f5fb · GitHub
[go: up one dir, main page]

Skip to content

Commit b30f5fb

Browse files
committed
Implement len() and py.Len
1 parent bffb0f4 commit b30f5fb

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

builtin/builtin.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func init() {
4242
// py.NewMethod("isinstance", builtin_isinstance, 0, isinstance_doc),
4343
// py.NewMethod("issubclass", builtin_issubclass, 0, issubclass_doc),
4444
// py.NewMethod("iter", builtin_iter, 0, iter_doc),
45-
// py.NewMethod("len", builtin_len, 0, len_doc),
45+
py.NewMethod("len", builtin_len, 0, len_doc),
4646
// py.NewMethod("locals", builtin_locals, py.METH_NOARGS, locals_doc),
4747
// py.NewMethod("max", builtin_max, 0, max_doc),
4848
// py.NewMethod("min", builtin_min, 0, min_doc),
@@ -549,3 +549,11 @@ func builtin_compile(self py.Object, args py.Tuple, kwargs py.StringDict) py.Obj
549549

550550
return result
551551
}
552+
553+
const len_doc = `len(object) -> integer
554+
555+
Return the number of items of a sequence or mapping.`
556+
557+
func builtin_len(self, v py.Object) py.Object {
558+
return py.Len(v)
559+
}

notes.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,19 @@ When converting C code, run it through astyle first as a first pass
3131

3232
astyle --style=java --add-brackets < x.c > x.go
3333

34+
Roadmap
35+
=======
36+
* FIXME catching an exception is stopping the error return / traceback working
37+
* FIXME (int("a", base=16): gives int() got multiple values for argument 'base'
38+
* make pystone.py work
39+
* make enough of internals work so can run python tests
40+
* import python tests and write go test runner to run them
41+
* make lots of them pass
42+
43+
Missing parts
44+
=============
45+
* repr/str
46+
* subclassing built in types
3447

3548
Polymorphism
3649
============

py/internal.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,16 @@ func IndexIntCheck(a Object, max int) int {
7575
return i
7676
}
7777

78+
// Returns the number of items of a sequence or mapping
79+
func Len(self Object) Object {
80+
if I, ok := self.(I__len__); ok {
81+
return I.M__len__()
82+
} else if res, ok := TypeCall0(self, "__len__"); ok {
83+
return res
84+
}
85+
panic(ExceptionNewf(TypeError, "object of type '%s' has no len()", self.Type().Name))
86+
}
87+
7888
// Return the result of not a
7989
func Not(a Object) Object {
8090
switch MakeBool(a) {

0 commit comments

Comments
 (0)
0