Skip to content

Commit 045eb6e

Browse files
committed
fix(bool): type(bool)() is not None (followup 511e0f2); bool [] is True
1 parent 4866a19 commit 045eb6e

File tree

2 files changed

+32
-24
lines changed

2 files changed

+32
-24
lines changed

Objects/boolobject.nim

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@ import pyobject
33
declarePyType Bool(tpToken):
44
b: bool
55

6-
proc newPyBool(b: bool): PyBoolObject =
6+
proc newPyBoolInner(b: bool): PyBoolObject =
77
result = newPyBoolSimple()
88
result.b = b
99

1010

11-
let pyTrueObj* = newPyBool(true)
12-
let pyFalseObj* = newPyBool(false)
11+
let pyTrueObj* = newPyBoolInner(true) ## singleton
12+
let pyFalseObj* = newPyBoolInner(false) ## singleton
13+
14+
proc newPyBool*(b: bool): PyBoolObject =
15+
if b: pyTrueObj
16+
else: pyFalseObj

Objects/boolobjectImpl.nim

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import exceptions
77
import stringobject
88
import boolobject
99
import numobjects
10+
import ./noneobject
1011

1112
export boolobject
1213

@@ -16,11 +17,7 @@ method `$`*(obj: PyBoolObject): string =
1617
methodMacroTmpl(Bool)
1718

1819
implBoolMagic Not:
19-
if self == pyTrueObj:
20-
pyFalseObj
21-
else:
22-
pyTrueObj
23-
20+
newPyBool self != pyTrueObj
2421

2522
implBoolMagic bool:
2623
self
@@ -29,35 +26,23 @@ implBoolMagic bool:
2926
implBoolMagic And:
3027
let otherBoolObj = other.callMagic(bool)
3128
errorIfNotBool(otherBoolObj, "__bool__")
32-
if self.b and PyBoolObject(otherBoolObj).b:
33-
return pyTrueObj
34-
else:
35-
return pyFalseObj
29+
newPyBool self.b and PyBoolObject(otherBoolObj).b
3630

3731
implBoolMagic Xor:
3832
let otherBoolObj = other.callMagic(bool)
3933
errorIfNotBool(otherBoolObj, "__bool__")
40-
if self.b xor PyBoolObject(otherBoolObj).b:
41-
return pyTrueObj
42-
else:
43-
return pyFalseObj
34+
newPyBool self.b xor PyBoolObject(otherBoolObj).b
4435

4536
implBoolMagic Or:
4637
let otherBoolObj = other.callMagic(bool)
4738
errorIfNotBool(otherBoolObj, "__bool__")
48-
if self.b or PyBoolObject(otherBoolObj).b:
49-
return pyTrueObj
50-
else:
51-
return pyFalseObj
39+
newPyBool self.b or PyBoolObject(otherBoolObj).b
5240

5341
implBoolMagic eq:
5442
let otherBoolObj = other.callMagic(bool)
5543
errorIfNotBool(otherBoolObj, "__bool__")
5644
let otherBool = PyBoolObject(otherBoolObj).b
57-
if self.b == otherBool:
58-
return pyTrueObj
59-
else:
60-
return pyFalseObj
45+
newPyBool self.b == otherBool
6146

6247
implBoolMagic repr:
6348
if self.b:
@@ -67,3 +52,22 @@ implBoolMagic repr:
6752

6853
implBoolMagic hash:
6954
newPyInt(Hash(self.b))
55+
56+
57+
proc PyObject_IsTrue*(v: PyObject): bool =
58+
if v == pyTrueObj: return true
59+
if v == pyFalseObj: return false
60+
if v == pyNone: return false
61+
let boolMag = v.getMagic(bool)
62+
if not boolMag.isNil:
63+
return boolMag(v).PyBoolObject.b
64+
elif not v.getMagic(len).isNil:
65+
return v.getMagic(len)(v).PyIntObject.positive
66+
# We currently don't define:
67+
# as_sequence
68+
# as_mapping
69+
return true
70+
71+
implBoolMagic New(tp: PyObject, obj: PyObject):
72+
newPyBool PyObject_IsTrue obj
73+

0 commit comments

Comments
 (0)