-
Notifications
You must be signed in to change notification settings - Fork 95
/
Copy pathobject.go
76 lines (67 loc) · 1.38 KB
/
object.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Copyright 2018 The go-python Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Functions to operate on any object
package py
import (
"fmt"
)
// Gets the attribute attr from object or returns nil
func ObjectGetAttr(o Object, attr string) Object {
// FIXME
return nil
}
// Gets the repr for an object
func ObjectRepr(o Object) Object {
// FIXME
return String(fmt.Sprintf("<%s %v>", o.Type().Name, o))
}
// Return whether the object is True or not
func ObjectIsTrue(o Object) (cmp bool, err error) {
switch o {
case True:
return true, nil
case False:
return false, nil
case None:
return false, nil
}
var res Object
switch t := o.(type) {
case I__bool__:
res, err = t.M__bool__()
case I__len__:
res, err = t.M__len__()
case *Type:
var ok bool
if res, ok, err = TypeCall0(o, "__bool__"); ok {
break
}
if res, ok, err = TypeCall0(o, "__len__"); ok {
break
}
_ = ok // pass static-check
}
if err != nil {
return false, err
}
switch t := res.(type) {
case Bool:
return t == True, nil
case Int:
return t > 0, nil
}
return true, nil
}
// Return whether the object is a sequence
func ObjectIsSequence(o Object) bool {
switch t := o.(type) {
case I__getitem__:
return true
case *Type:
if t.GetAttrOrNil("__getitem__") != nil {
return true
}
}
return false
}