1 /* Boolean type, a subtype of int */
4 #include "longintrepr.h"
6 /* We define bool_repr to return "False" or "True" */
8 static PyObject
*false_str
= NULL
;
9 static PyObject
*true_str
= NULL
;
12 bool_repr(PyObject
*self
)
17 s
= true_str
? true_str
:
18 (true_str
= PyUnicode_InternFromString("True"));
20 s
= false_str
? false_str
:
21 (false_str
= PyUnicode_InternFromString("False"));
26 /* Function to return a bool from a C long */
28 PyObject
*PyBool_FromLong(long ok
)
40 /* We define bool_new to always return either Py_True or Py_False */
43 bool_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
45 static char *kwlist
[] = {"x", 0};
46 PyObject
*x
= Py_False
;
49 if (!PyArg_ParseTupleAndKeywords(args
, kwds
, "|O:bool", kwlist
, &x
))
51 ok
= PyObject_IsTrue(x
);
54 return PyBool_FromLong(ok
);
57 /* Arithmetic operations redefined to return bool if both args are bool. */
60 bool_and(PyObject
*a
, PyObject
*b
)
62 if (!PyBool_Check(a
) || !PyBool_Check(b
))
63 return PyLong_Type
.tp_as_number
->nb_and(a
, b
);
64 return PyBool_FromLong((a
== Py_True
) & (b
== Py_True
));
68 bool_or(PyObject
*a
, PyObject
*b
)
70 if (!PyBool_Check(a
) || !PyBool_Check(b
))
71 return PyLong_Type
.tp_as_number
->nb_or(a
, b
);
72 return PyBool_FromLong((a
== Py_True
) | (b
== Py_True
));
76 bool_xor(PyObject
*a
, PyObject
*b
)
78 if (!PyBool_Check(a
) || !PyBool_Check(b
))
79 return PyLong_Type
.tp_as_number
->nb_xor(a
, b
);
80 return PyBool_FromLong((a
== Py_True
) ^ (b
== Py_True
));
85 PyDoc_STRVAR(bool_doc
,
88 Returns True when the argument x is true, False otherwise.\n\
89 The builtins True and False are the only two instances of the class bool.\n\
90 The class bool is a subclass of the class int, and cannot be subclassed.");
92 /* Arithmetic methods -- only so we can override &, |, ^. */
94 static PyNumberMethods bool_as_number
= {
108 bool_and
, /* nb_and */
109 bool_xor
, /* nb_xor */
114 0, /* nb_inplace_add */
115 0, /* nb_inplace_subtract */
116 0, /* nb_inplace_multiply */
117 0, /* nb_inplace_remainder */
118 0, /* nb_inplace_power */
119 0, /* nb_inplace_lshift */
120 0, /* nb_inplace_rshift */
121 0, /* nb_inplace_and */
122 0, /* nb_inplace_xor */
123 0, /* nb_inplace_or */
124 0, /* nb_floor_divide */
125 0, /* nb_true_divide */
126 0, /* nb_inplace_floor_divide */
127 0, /* nb_inplace_true_divide */
131 /* The type object for bool. Note that this cannot be subclassed! */
133 PyTypeObject PyBool_Type
= {
134 PyVarObject_HEAD_INIT(&PyType_Type
, 0)
136 sizeof(struct _longobject
),
143 bool_repr
, /* tp_repr */
144 &bool_as_number
, /* tp_as_number */
145 0, /* tp_as_sequence */
146 0, /* tp_as_mapping */
149 bool_repr
, /* tp_str */
152 0, /* tp_as_buffer */
153 Py_TPFLAGS_DEFAULT
, /* tp_flags */
154 bool_doc
, /* tp_doc */
157 0, /* tp_richcompare */
158 0, /* tp_weaklistoffset */
164 &PyLong_Type
, /* tp_base */
166 0, /* tp_descr_get */
167 0, /* tp_descr_set */
168 0, /* tp_dictoffset */
171 bool_new
, /* tp_new */
174 /* The objects representing bool values False and True */
176 struct _longobject _Py_FalseStruct
= {
177 PyVarObject_HEAD_INIT(&PyBool_Type
, 0)
181 struct _longobject _Py_TrueStruct
= {
182 PyVarObject_HEAD_INIT(&PyBool_Type
, 1)