# HG changeset patch # User paulb@localhost.localdomain # Date 1164414978 -3600 # Node ID 20e8405a3ee5c4a036c397368234e562ec01ba7f # Parent 793337218643d146342a6353413ad2e6827f60e4 Changed boolean to bool and __true__ to __bool__. diff -r 793337218643 -r 20e8405a3ee5 lib/builtins.py --- a/lib/builtins.py Sat Nov 25 00:40:21 2006 +0100 +++ b/lib/builtins.py Sat Nov 25 01:36:18 2006 +0100 @@ -22,11 +22,13 @@ """ class object: - def __true__(self): + def __bool__(self): + # NOTE: Should really test for __len__ and then resort to False. + # NOTE: See the reference: 3.3.1 Basic customization return False -class boolean: - def __true__(self): +class bool: + def __bool__(self): return self def __str__(self): @@ -104,37 +106,37 @@ def __lt__(self, other): if isinstance(other, int): - return boolean() + return bool() else: raise TypeError def __gt__(self, other): if isinstance(other, int): - return boolean() + return bool() else: raise TypeError def __le__(self, other): if isinstance(other, int): - return boolean() + return bool() else: raise TypeError def __ge__(self, other): if isinstance(other, int): - return boolean() + return bool() else: raise TypeError def __eq__(self, other): if isinstance(other, int): - return boolean() + return bool() else: raise TypeError def __ne__(self, other): if isinstance(other, int): - return boolean() + return bool() else: raise TypeError @@ -147,7 +149,7 @@ def __str__(self): return string() - def __true__(self): + def __bool__(self): return self != 0 class long: @@ -201,49 +203,49 @@ def __lt__(self, other): if isinstance(other, int): - return boolean() + return bool() elif isinstance(other, long): - return boolean() + return bool() else: raise TypeError def __gt__(self, other): if isinstance(other, int): - return boolean() + return bool() elif isinstance(other, long): - return boolean() + return bool() else: raise TypeError def __le__(self, other): if isinstance(other, int): - return boolean() + return bool() elif isinstance(other, long): - return boolean() + return bool() else: raise TypeError def __ge__(self, other): if isinstance(other, int): - return boolean() + return bool() elif isinstance(other, long): - return boolean() + return bool() else: raise TypeError def __eq__(self, other): if isinstance(other, int): - return boolean() + return bool() elif isinstance(other, long): - return boolean() + return bool() else: raise TypeError def __ne__(self, other): if isinstance(other, int): - return boolean() + return bool() elif isinstance(other, long): - return boolean() + return bool() else: raise TypeError @@ -256,7 +258,7 @@ def __str__(self): return string() - def __true__(self): + def __bool__(self): return self != 0 class float: @@ -382,61 +384,61 @@ def __lt__(self, other): if isinstance(other, int): - return boolean() + return bool() elif isinstance(other, long): - return boolean() + return bool() elif isinstance(other, float): - return boolean() + return bool() else: raise TypeError def __gt__(self, other): if isinstance(other, int): - return boolean() + return bool() elif isinstance(other, long): - return boolean() + return bool() elif isinstance(other, float): - return boolean() + return bool() else: raise TypeError def __le__(self, other): if isinstance(other, int): - return boolean() + return bool() elif isinstance(other, long): - return boolean() + return bool() elif isinstance(other, float): - return boolean() + return bool() else: raise TypeError def __ge__(self, other): if isinstance(other, int): - return boolean() + return bool() elif isinstance(other, long): - return boolean() + return bool() elif isinstance(other, float): - return boolean() + return bool() else: raise TypeError def __eq__(self, other): if isinstance(other, int): - return boolean() + return bool() elif isinstance(other, long): - return boolean() + return bool() elif isinstance(other, float): - return boolean() + return bool() else: raise TypeError def __ne__(self, other): if isinstance(other, int): - return boolean() + return bool() elif isinstance(other, long): - return boolean() + return bool() elif isinstance(other, float): - return boolean() + return bool() else: raise TypeError @@ -449,7 +451,7 @@ def __str__(self): return string() - def __true__(self): + def __bool__(self): return self != 0 class str: @@ -474,7 +476,7 @@ def __str__(self): return self - def __true__(self): + def __bool__(self): return self.__len__() != 0 def join(self, l): @@ -611,7 +613,7 @@ def __iter__(self): return listiterator(self) - def __true__(self): + def __bool__(self): return self.__len__() != 0 class listiterator: @@ -627,8 +629,8 @@ else: raise StopIteration - def __true__(self): - return boolean() + def __bool__(self): + return bool() class tuple: def __init__(self, *args): @@ -703,7 +705,7 @@ def __iter__(self): return tupleiterator(self) - def __true__(self): + def __bool__(self): return self.__len__() != 0 class tupleiterator: @@ -719,8 +721,8 @@ else: raise StopIteration - def __true__(self): - return boolean() + def __bool__(self): + return bool() class dict: pass @@ -741,7 +743,7 @@ pass class none: - def __true__(self): + def __bool__(self): return False def __str__(self): @@ -770,10 +772,10 @@ # General functions. def isinstance(obj, cls): - return boolean() + return bool() def issubclass(cls1, cls2): - return boolean() + return bool() def len(x): return x.__len__() @@ -807,20 +809,20 @@ # Special values. -True = boolean() -False = boolean() +True = bool() +False = bool() None = none() Undefined = undefined() # Special functions. These all operate on references at run-time. def __is__(a, b): - return boolean() + return bool() def __is_not__(a, b): - return boolean() + return bool() def __not__(a): - return boolean() + return bool() # vim: tabstop=4 expandtab shiftwidth=4 diff -r 793337218643 -r 20e8405a3ee5 simplify.py --- a/simplify.py Sat Nov 25 00:40:21 2006 +0100 +++ b/simplify.py Sat Nov 25 01:36:18 2006 +0100 @@ -277,7 +277,7 @@ results = nodes = [] # Produce something like... - # expr.__true__() ? body + # expr.__bool__() ? body first = 1 for compare, stmt in if_.tests: @@ -288,7 +288,7 @@ test=InvokeFunction( expr=LoadAttr( expr=self.dispatch(compare), - name="__true__" + name="__bool__" ), args=[], star=None, @@ -534,7 +534,7 @@ if node is not last: nodes.append(StoreTemp(expr=expr)) - #invocation = InvokeFunction(expr=LoadAttr(expr=LoadTemp(), name="__true__"), args=[], star=None, dstar=None) + #invocation = InvokeFunction(expr=LoadAttr(expr=LoadTemp(), name="__bool__"), args=[], star=None, dstar=None) test = Conditional(test=self._visitNot(LoadTemp()), body=[Return(expr=LoadTemp())]) nodes.append(test) @@ -594,7 +594,7 @@ if node is not last: nodes.append(StoreTemp(expr=expr)) - invocation = InvokeFunction(expr=LoadAttr(expr=LoadTemp(), name="__true__"), args=[], star=None, dstar=None) + invocation = InvokeFunction(expr=LoadAttr(expr=LoadTemp(), name="__bool__"), args=[], star=None, dstar=None) test = Conditional(test=invocation, body=[Return(expr=LoadTemp())]) nodes.append(test) @@ -626,7 +626,7 @@ invocation = InvokeFunction( expr=LoadAttr( expr=expr, - name="__true__" + name="__bool__" ), args=[], star=None, @@ -1195,7 +1195,7 @@ # Include a conditional statement in the subprogram. test = Conditional(else_=[]) - test.test = InvokeFunction(expr=LoadAttr(expr=self.dispatch(while_.test), name="__true__"), args=[], star=None, dstar=None) + test.test = InvokeFunction(expr=LoadAttr(expr=self.dispatch(while_.test), name="__bool__"), args=[], star=None, dstar=None) # Inside the conditional, add a recursive invocation to the subprogram # if the test condition was satisfied.