--- /dev/null
+/* Boolean type, a subtype of int */\r
+\r
+#include "Python.h"\r
+\r
+/* We need to define bool_print to override int_print */\r
+\r
+static int\r
+bool_print(PyBoolObject *self, FILE *fp, int flags)\r
+{\r
+ Py_BEGIN_ALLOW_THREADS\r
+ fputs(self->ob_ival == 0 ? "False" : "True", fp);\r
+ Py_END_ALLOW_THREADS\r
+ return 0;\r
+}\r
+\r
+/* We define bool_repr to return "False" or "True" */\r
+\r
+static PyObject *false_str = NULL;\r
+static PyObject *true_str = NULL;\r
+\r
+static PyObject *\r
+bool_repr(PyBoolObject *self)\r
+{\r
+ PyObject *s;\r
+\r
+ if (self->ob_ival)\r
+ s = true_str ? true_str :\r
+ (true_str = PyString_InternFromString("True"));\r
+ else\r
+ s = false_str ? false_str :\r
+ (false_str = PyString_InternFromString("False"));\r
+ Py_XINCREF(s);\r
+ return s;\r
+}\r
+\r
+/* Function to return a bool from a C long */\r
+\r
+PyObject *PyBool_FromLong(long ok)\r
+{\r
+ PyObject *result;\r
+\r
+ if (ok)\r
+ result = Py_True;\r
+ else\r
+ result = Py_False;\r
+ Py_INCREF(result);\r
+ return result;\r
+}\r
+\r
+/* We define bool_new to always return either Py_True or Py_False */\r
+\r
+static PyObject *\r
+bool_new(PyTypeObject *type, PyObject *args, PyObject *kwds)\r
+{\r
+ static char *kwlist[] = {"x", 0};\r
+ PyObject *x = Py_False;\r
+ long ok;\r
+\r
+ if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:bool", kwlist, &x))\r
+ return NULL;\r
+ ok = PyObject_IsTrue(x);\r
+ if (ok < 0)\r
+ return NULL;\r
+ return PyBool_FromLong(ok);\r
+}\r
+\r
+/* Arithmetic operations redefined to return bool if both args are bool. */\r
+\r
+static PyObject *\r
+bool_and(PyObject *a, PyObject *b)\r
+{\r
+ if (!PyBool_Check(a) || !PyBool_Check(b))\r
+ return PyInt_Type.tp_as_number->nb_and(a, b);\r
+ return PyBool_FromLong(\r
+ ((PyBoolObject *)a)->ob_ival & ((PyBoolObject *)b)->ob_ival);\r
+}\r
+\r
+static PyObject *\r
+bool_or(PyObject *a, PyObject *b)\r
+{\r
+ if (!PyBool_Check(a) || !PyBool_Check(b))\r
+ return PyInt_Type.tp_as_number->nb_or(a, b);\r
+ return PyBool_FromLong(\r
+ ((PyBoolObject *)a)->ob_ival | ((PyBoolObject *)b)->ob_ival);\r
+}\r
+\r
+static PyObject *\r
+bool_xor(PyObject *a, PyObject *b)\r
+{\r
+ if (!PyBool_Check(a) || !PyBool_Check(b))\r
+ return PyInt_Type.tp_as_number->nb_xor(a, b);\r
+ return PyBool_FromLong(\r
+ ((PyBoolObject *)a)->ob_ival ^ ((PyBoolObject *)b)->ob_ival);\r
+}\r
+\r
+/* Doc string */\r
+\r
+PyDoc_STRVAR(bool_doc,\r
+"bool(x) -> bool\n\\r
+\n\\r
+Returns True when the argument x is true, False otherwise.\n\\r
+The builtins True and False are the only two instances of the class bool.\n\\r
+The class bool is a subclass of the class int, and cannot be subclassed.");\r
+\r
+/* Arithmetic methods -- only so we can override &, |, ^. */\r
+\r
+static PyNumberMethods bool_as_number = {\r
+ 0, /* nb_add */\r
+ 0, /* nb_subtract */\r
+ 0, /* nb_multiply */\r
+ 0, /* nb_divide */\r
+ 0, /* nb_remainder */\r
+ 0, /* nb_divmod */\r
+ 0, /* nb_power */\r
+ 0, /* nb_negative */\r
+ 0, /* nb_positive */\r
+ 0, /* nb_absolute */\r
+ 0, /* nb_nonzero */\r
+ 0, /* nb_invert */\r
+ 0, /* nb_lshift */\r
+ 0, /* nb_rshift */\r
+ bool_and, /* nb_and */\r
+ bool_xor, /* nb_xor */\r
+ bool_or, /* nb_or */\r
+ 0, /* nb_coerce */\r
+ 0, /* nb_int */\r
+ 0, /* nb_long */\r
+ 0, /* nb_float */\r
+ 0, /* nb_oct */\r
+ 0, /* nb_hex */\r
+ 0, /* nb_inplace_add */\r
+ 0, /* nb_inplace_subtract */\r
+ 0, /* nb_inplace_multiply */\r
+ 0, /* nb_inplace_divide */\r
+ 0, /* nb_inplace_remainder */\r
+ 0, /* nb_inplace_power */\r
+ 0, /* nb_inplace_lshift */\r
+ 0, /* nb_inplace_rshift */\r
+ 0, /* nb_inplace_and */\r
+ 0, /* nb_inplace_xor */\r
+ 0, /* nb_inplace_or */\r
+ 0, /* nb_floor_divide */\r
+ 0, /* nb_true_divide */\r
+ 0, /* nb_inplace_floor_divide */\r
+ 0, /* nb_inplace_true_divide */\r
+};\r
+\r
+/* The type object for bool. Note that this cannot be subclassed! */\r
+\r
+PyTypeObject PyBool_Type = {\r
+ PyVarObject_HEAD_INIT(&PyType_Type, 0)\r
+ "bool",\r
+ sizeof(PyIntObject),\r
+ 0,\r
+ 0, /* tp_dealloc */\r
+ (printfunc)bool_print, /* tp_print */\r
+ 0, /* tp_getattr */\r
+ 0, /* tp_setattr */\r
+ 0, /* tp_compare */\r
+ (reprfunc)bool_repr, /* tp_repr */\r
+ &bool_as_number, /* tp_as_number */\r
+ 0, /* tp_as_sequence */\r
+ 0, /* tp_as_mapping */\r
+ 0, /* tp_hash */\r
+ 0, /* tp_call */\r
+ (reprfunc)bool_repr, /* tp_str */\r
+ 0, /* tp_getattro */\r
+ 0, /* tp_setattro */\r
+ 0, /* tp_as_buffer */\r
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES, /* tp_flags */\r
+ bool_doc, /* tp_doc */\r
+ 0, /* tp_traverse */\r
+ 0, /* tp_clear */\r
+ 0, /* tp_richcompare */\r
+ 0, /* tp_weaklistoffset */\r
+ 0, /* tp_iter */\r
+ 0, /* tp_iternext */\r
+ 0, /* tp_methods */\r
+ 0, /* tp_members */\r
+ 0, /* tp_getset */\r
+ &PyInt_Type, /* tp_base */\r
+ 0, /* tp_dict */\r
+ 0, /* tp_descr_get */\r
+ 0, /* tp_descr_set */\r
+ 0, /* tp_dictoffset */\r
+ 0, /* tp_init */\r
+ 0, /* tp_alloc */\r
+ bool_new, /* tp_new */\r
+};\r
+\r
+/* The objects representing bool values False and True */\r
+\r
+/* Named Zero for link-level compatibility */\r
+PyIntObject _Py_ZeroStruct = {\r
+ PyObject_HEAD_INIT(&PyBool_Type)\r
+ 0\r
+};\r
+\r
+PyIntObject _Py_TrueStruct = {\r
+ PyObject_HEAD_INIT(&PyBool_Type)\r
+ 1\r
+};\r