Skip to content

Commit 1b2b0a2

Browse files
committed
all: resolve PR comments and add tests
1 parent aa3e6b1 commit 1b2b0a2

File tree

4 files changed

+53
-30
lines changed

4 files changed

+53
-30
lines changed

py/filter.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2018 The go-python Authors. All rights reserved.
1+
// Copyright 2023 The go-python Authors. All rights reserved.
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

py/map.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2018 The go-python Authors. All rights reserved.
1+
// Copyright 2023 The go-python Authors. All rights reserved.
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

py/object.go

+30-28
Original file line numberDiff line numberDiff line change
@@ -23,48 +23,50 @@ func ObjectRepr(o Object) Object {
2323
}
2424

2525
// Return whether the object is True or not
26-
func ObjectIsTrue(o Object) (bool, error) {
27-
if o == True {
26+
func ObjectIsTrue(o Object) (cmp bool, err error) {
27+
switch o {
28+
case True:
2829
return true, nil
29-
}
30-
if o == False {
30+
case False:
3131
return false, nil
32-
}
33-
34-
if o == None {
32+
case None:
3533
return false, nil
3634
}
3735

38-
if I, ok := o.(I__bool__); ok {
39-
cmp, err := I.M__bool__()
40-
if err != nil {
41-
return false, err
36+
var res Object
37+
switch t := o.(type) {
38+
case I__bool__:
39+
res, err = t.M__bool__()
40+
case I__len__:
41+
res, err = t.M__len__()
42+
case *Type:
43+
var ok bool
44+
if res, ok, err = TypeCall0(o, "__bool__"); ok {
45+
break
4246
}
43-
if cmp == True {
44-
return true, nil
45-
} else if cmp == False {
46-
return false, nil
47+
if res, ok, err = TypeCall0(o, "__len__"); ok {
48+
break
4749
}
4850
}
49-
if I, ok := o.(I__len__); ok {
50-
l, err := I.M__len__()
51-
if err != nil {
52-
return false, err
53-
}
54-
if l.(Int) > 0 {
55-
return true, nil
56-
} else {
57-
return false, nil
58-
}
51+
if err != nil {
52+
return false, err
53+
}
54+
55+
switch t := res.(type) {
56+
case Bool:
57+
return t == True, nil
58+
case Int:
59+
return t > 0, nil
5960
}
60-
return false, nil
61+
return true, nil
6162
}
6263

6364
// Return whether the object is True or not
6465
func ObjectIsSequence(o Object) bool {
65-
if _, ok := o.(I__getitem__); ok {
66+
switch t := o.(type) {
67+
case I__getitem__:
6668
return true
67-
} else if t, ok := o.(*Type); ok {
69+
case *Type:
6870
if t.GetAttrOrNil("__getitem__") != nil {
6971
return true
7072
}

py/tests/filter.py

+21
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,27 @@
22
from libtest import assertRaises
33

44
doc="filter"
5+
class T0:
6+
def __bool__(self):
7+
return True
8+
class T1:
9+
def __len__(self):
10+
return 1
11+
class T2:
12+
def __bool__(self):
13+
return False
14+
class T3:
15+
pass
16+
t0, t1, t2, t3 = T0(), T1(), T2(), T3()
17+
assert list(filter(None, [t0, t1, t2, t3])) == [t0, t1, t3]
18+
assert list(filter(None, [1, [], 2, ''])) == [1, 2]
19+
20+
class T3:
21+
def __len__(self):
22+
raise ValueError
23+
t3 = T3()
24+
assertRaises(ValueError, list, filter(None, [t3]))
25+
526
class Squares:
627
def __init__(self, max):
728
self.max = max

0 commit comments

Comments
 (0)