Skip to main content

Objects/boolobject.c

Map

LinesSymbolRolegopy
1–22(file header)bool inherits from long; immortal singletonsobjects.Bool embeds Int
24PyBool_Type forwardType declared before useobjects.BoolType
33–50PyBool_FromLongReturns Py_True or Py_False without allocationobjects.NewBool
52–66bool_reprReturns the string "True" or "False"objects.boolRepr
68–90bool_andBitwise AND short-circuit for booleansnot yet ported as separate slot
91–113bool_orBitwise OR short-circuit for booleansnot yet ported as separate slot
114–136bool_xorBitwise XOR short-circuit for booleansnot yet ported as separate slot
138–160bool_newRaises TypeError when called on a subclassnot yet ported
222PyBool_TypeFull type singleton; tp_base = &PyLong_Typeobjects.BoolType

Reading

Singleton subtype

sets tp_base = &PyLong_Type, making bool a subtype of int. There are exactly two instances: Py_True (internal value 1) and Py_False (internal value 0). Both are immortal: CPython 3.12+ marks them with _Py_IMMORTAL_REFCNT so Py_INCREF/Py_DECREF become no-ops for them.

gopy mirrors this with objects.Bool embedding objects.Int and exposing objects.True() / objects.False() as fixed package-level pointers. Go's garbage collector handles lifetime, so there is no immortality mechanism; the singletons are simply never unreachable.

Construction

is the only legitimate constructor. A non-zero argument returns Py_True; zero returns Py_False. No allocation happens. objects.NewBool is a direct translation: one if branch returning trueSingleton or falseSingleton.

Repr

compares the object pointer against Py_True and emits the string literal "True" or "False". gopy's boolRepr does the same pointer comparison against trueSingleton.

Arithmetic bypass

bool_and, bool_or, and bool_xor avoid the full long arithmetic path for the common bool-op-bool case: they extract the underlying 0/1 integer directly and call PyBool_FromLong on the result. For mixed bool op int calls they fall through to the regular long slots. gopy inherits the Int arithmetic slots and does not yet implement the bypass; the result is always correct but slightly slower for pure-boolean bitwise operations.

Subclassing guard

raises TypeError: type 'bool' is not an acceptable base type when type is not bool itself. This makes bool effectively final. gopy does not yet enforce this guard.

Hash

does not define a separate tp_hash; PyBool_Type inherits long_hash. gopy's BoolType registers an inline function that returns 1 for trueSingleton and 0 for falseSingleton, which matches long_hash for those two values.

gopy mirror

The entire port is in /objects/bool.go. Bool, BoolType, True, False, NewBool, and boolRepr are all present. The arithmetic bypass slots (bool_and, bool_or, bool_xor) and the bool_new subclassing guard are pending.