"""Makes True into False, and False into True. Works only on CPython
Cosmetically. The object that you get from True still evaluates to true for the
most part, and similarly, the object you get from False still evaluates to false
for the most part. If we would want a complete replacement, it would require a
very annoying job of scanning through all the code of CPython, checking for
pointers to those objects and replacing them, since those are static. Way too
much work for a simple toy.
But, this will still be very confusing, as repr and str of bools will be
switched up, and int(True) will return 0.
No idea why int(False) still returns 0, but don't have time to work that out.
"""
import ctypes
# This is basically a python implementation of a bool, just with some faking
class BadBool(int):
"""bool(x) -> bool
Returns True when the argument x is true, False otherwise.
The builtins True and False are the only two instances of the class bool.
The class bool is a subclass of the class int, and cannot be subclassed."""
__slots__ = []
def __new__(cls, val=0):
# This constructor always returns an existing instance
if val:
return BadTrue
else:
return BadFalse
def __repr__(self):
if self:
return "False"
else:
return "True"
__str__ = __repr__
def __and__(self, other):
if isinstance(other, bool):
return bool(int(self) & int(other))
else:
return int.__and__(self, other)
__rand__ = __and__
def __or__(self, other):
if isinstance(other, bool):
return bool(int(self) | int(other))
else:
return int.__or__(self, other)
__ror__ = __or__
def __xor__(self, other):
if isinstance(other, bool):
return bool(int(self) ^ int(other))
else:
return int.__xor__(self, other)
__rxor__ = __xor__
# Hiding expert right here
@property
def __class__(self):
return bool
# Make stuff look more real - probably don't need those, but doens't hurt.
BadBool.__name__ = "bool"
BadBool.__qualname__ = "bool"
del BadBool.__slots__
# Bootstrap the initial instances through sheer willpower
BadTrue = int.__new__(BadBool, 0)
BadFalse = int.__new__(BadBool, 1)
# This is actually an int struct, that's what bools actually are in Python.
class BoolStruct(ctypes.Structure):
_fields_ = [
("refcnt", ctypes.c_long),
("ob_type", ctypes.c_long),
("int", ctypes.c_int),
]
# Hack to avoid getting garbage collected. Removes messages when exiting.
my_true = ctypes.cast(id(BadTrue), ctypes.POINTER(BoolStruct))[0]
my_true.refcnt = 1000000
my_false = ctypes.cast(id(BadFalse), ctypes.POINTER(BoolStruct))[0]
my_false.refcnt = 1000000
# Get the address of the original object, and replace it with our fake one.
true = ctypes.cast(id(True), ctypes.POINTER(BoolStruct))
true[0] = ctypes.cast(id(BadTrue), ctypes.POINTER(BoolStruct))[0]
false = ctypes.cast(id(False), ctypes.POINTER(BoolStruct))
false[0] = ctypes.cast(id(BadFalse), ctypes.POINTER(BoolStruct))[0]