Skip to content

Commit b184a43

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

File tree

4 files changed

+57
-30
lines changed

4 files changed

+57
-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: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -23,48 +23,54 @@ 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
}
50+
returnfalse, nil
51+
default:
52+
returnfalse, nil
4853
}
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-
}
54+
iferr!=nil{
55+
returnfalse, err
56+
}
57+
58+
switcht:=res.(type){
59+
caseBool:
60+
returnt==True, nil
61+
caseInt:
62+
returnt>0, nil
63+
default:
64+
returnfalse, nil
5965
}
60-
returnfalse, nil
6166
}
6267

6368
// Return whether the object is True or not
6469
funcObjectIsSequence(oObject) bool{
65-
if_, ok:=o.(I__getitem__); ok{
70+
switcht:=o.(type){
71+
caseI__getitem__:
6672
returntrue
67-
} elseift, ok:=o.(*Type); ok{
73+
case*Type:
6874
ift.GetAttrOrNil("__getitem__") !=nil{
6975
returntrue
7076
}

‎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+
def__repr__(self):
9+
return'T0'
10+
classT1:
11+
def__len__(self):
12+
return1
13+
def__repr__(self):
14+
return'T1'
15+
classT2:
16+
pass
17+
t0, t1, t2=T0(), T1(), T2()
18+
assertlist(filter(None, [1, 2, t0, t1, 3, t2, 4])) == [1, 2, t0, t1, 3, 4]
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)