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‎

Lines changed: 1 addition & 1 deletion
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‎

Lines changed: 1 addition & 1 deletion
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‎

Lines changed: 30 additions & 28 deletions
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-
funcObjectIsTrue(oObject) (bool, error){
27-
ifo==True{
26+
funcObjectIsTrue(oObject) (cmpbool, errerror){
27+
switcho{
28+
caseTrue:
2829
returntrue, nil
29-
}
30-
ifo==False{
30+
caseFalse:
3131
returnfalse, nil
32-
}
33-
34-
ifo==None{
32+
caseNone:
3533
returnfalse, nil
3634
}
3735

38-
ifI, ok:=o.(I__bool__); ok{
39-
cmp, err:=I.M__bool__()
40-
iferr!=nil{
41-
returnfalse, err
36+
varresObject
37+
switcht:=o.(type){
38+
caseI__bool__:
39+
res, err=t.M__bool__()
40+
caseI__len__:
41+
res, err=t.M__len__()
42+
case*Type:
43+
varokbool
44+
ifres, ok, err=TypeCall0(o, "__bool__"); ok{
45+
break
4246
}
43-
ifcmp==True{
44-
returntrue, nil
45-
} elseifcmp==False{
46-
returnfalse, nil
47+
ifres, ok, err=TypeCall0(o, "__len__"); ok{
48+
break
4749
}
4850
}
49-
ifI, ok:=o.(I__len__); ok{
50-
l, err:=I.M__len__()
51-
iferr!=nil{
52-
returnfalse, err
53-
}
54-
ifl.(Int) >0{
55-
returntrue, nil
56-
} else{
57-
returnfalse, nil
58-
}
51+
iferr!=nil{
52+
returnfalse, err
53+
}
54+
55+
switcht:=res.(type){
56+
caseBool:
57+
returnt==True, nil
58+
caseInt:
59+
returnt>0, nil
5960
}
60-
returnfalse, nil
61+
returntrue, nil
6162
}
6263

6364
// Return whether the object is True or not
6465
funcObjectIsSequence(oObject) bool{
65-
if_, ok:=o.(I__getitem__); ok{
66+
switcht:=o.(type){
67+
caseI__getitem__:
6668
returntrue
67-
} elseift, ok:=o.(*Type); ok{
69+
case*Type:
6870
ift.GetAttrOrNil("__getitem__") !=nil{
6971
returntrue
7072
}

‎py/tests/filter.py‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,27 @@
22
fromlibtestimportassertRaises
33

44
doc="filter"
5+
classT0:
6+
def__bool__(self):
7+
returnTrue
8+
classT1:
9+
def__len__(self):
10+
return1
11+
classT2:
12+
def__bool__(self):
13+
returnFalse
14+
classT3:
15+
pass
16+
t0, t1, t2, t3=T0(), T1(), T2(), T3()
17+
assertlist(filter(None, [t0, t1, t2, t3])) == [t0, t1, t3]
18+
assertlist(filter(None, [1, [], 2, ''])) == [1, 2]
19+
20+
classT3:
21+
def__len__(self):
22+
raiseValueError
23+
t3=T3()
24+
assertRaises(ValueError, list, filter(None, [t3]))
25+
526
classSquares:
627
def__init__(self, max):
728
self.max=max

0 commit comments

Comments
(0)