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-
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
}
50+
return false, nil
51+
default:
52+
return false, nil
4853
}
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-
}
54+
if err != nil {
55+
return false, err
56+
}
57+
58+
switch t := res.(type) {
59+
case Bool:
60+
return t == True, nil
61+
case Int:
62+
return t > 0, nil
63+
default:
64+
return false, nil
5965
}
60-
return false, nil
6166
}
6267

6368
// Return whether the object is True or not
6469
func ObjectIsSequence(o Object) bool {
65-
if _, ok := o.(I__getitem__); ok {
70+
switch t := o.(type) {
71+
case I__getitem__:
6672
return true
67-
} else if t, ok := o.(*Type); ok {
73+
case *Type:
6874
if t.GetAttrOrNil("__getitem__") != nil {
6975
return true
7076
}

py/tests/filter.py

Lines changed: 21 additions & 0 deletions
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+
def __repr__(self):
9+
return 'T0'
10+
class T1:
11+
def __len__(self):
12+
return 1
13+
def __repr__(self):
14+
return 'T1'
15+
class T2:
16+
pass
17+
t0, t1, t2 = T0(), T1(), T2()
18+
assert list(filter(None, [1, 2, t0, t1, 3, t2, 4])) == [1, 2, t0, t1, 3, 4]
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy