~mcfletch/basicproperty/trunk

« back to all changes in this revision

Viewing changes to basictypes/booleanfix.py

  • Committer: Mike C. Fletcher
  • Date: 2011-11-30 01:55:05 UTC
  • Revision ID: mcfletch@vrplumber.com-20111130015505-1po5tzzfydky7n5y
Eliminate support for Python 2.2, and with it the booleanfix module, which blew up on Python 3.x

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
"""Hack to provide Python 2.3-style boolean operation in 2.2
2
 
"""
3
 
try:
4
 
        if str(True) != 'True':
5
 
                # Python 2.2.3 has True, but it's just 1 and 0 refs...
6
 
                raise NameError
7
 
        True = True
8
 
        False = False
9
 
        bool = bool
10
 
except NameError:
11
 
        class bool(int):
12
 
                def __new__(cls, val=0):
13
 
                        # This constructor always returns an existing instance
14
 
                        if val:
15
 
                                return True
16
 
                        else:
17
 
                                return False
18
 
 
19
 
                def __repr__(self):
20
 
                        if self:
21
 
                                return "True"
22
 
                        else:
23
 
                                return "False"
24
 
 
25
 
                __str__ = __repr__
26
 
 
27
 
                def __and__(self, other):
28
 
                        if isinstance(other, bool):
29
 
                                return bool(int(self) & int(other))
30
 
                        else:
31
 
                                return int.__and__(self, other)
32
 
 
33
 
                __rand__ = __and__
34
 
 
35
 
                def __or__(self, other):
36
 
                        if isinstance(other, bool):
37
 
                                return bool(int(self) | int(other))
38
 
                        else:
39
 
                                return int.__or__(self, other)
40
 
 
41
 
                __ror__ = __or__
42
 
 
43
 
                def __xor__(self, other):
44
 
                        if isinstance(other, bool):
45
 
                                return bool(int(self) ^ int(other))
46
 
                        else:
47
 
                                return int.__xor__(self, other)
48
 
 
49
 
                __rxor__ = __xor__
50
 
 
51
 
        # Bootstrap truth values through sheer willpower
52
 
        False = int.__new__(bool, 0==1)
53
 
        True = int.__new__(bool, 1==1)
54
 
 
55
 
        def install():
56
 
                """Install the enhanced bool, True and False in __builtin__"""
57
 
                import __builtin__
58
 
                __builtin__.True = True
59
 
                __builtin__.False = False
60
 
                __builtin__.bool = bool
61
 
        #install()
62
 
 
63
 
if __name__ == "__main__":
64
 
        assert True == 1
65
 
        assert False == 0
66