@@ -7,6 +7,7 @@ import exceptions
77import stringobject
88import boolobject
99import numobjects
10+ import ./ noneobject
1011
1112export boolobject
1213
@@ -16,11 +17,7 @@ method `$`*(obj: PyBoolObject): string =
1617methodMacroTmpl (Bool )
1718
1819implBoolMagic Not :
19- if self == pyTrueObj:
20- pyFalseObj
21- else :
22- pyTrueObj
23-
20+ newPyBool self != pyTrueObj
2421
2522implBoolMagic bool :
2623 self
@@ -29,35 +26,23 @@ implBoolMagic bool:
2926implBoolMagic 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
3731implBoolMagic 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
4536implBoolMagic 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
5341implBoolMagic 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
6247implBoolMagic repr:
6348 if self.b:
@@ -67,3 +52,22 @@ implBoolMagic repr:
6752
6853implBoolMagic 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