1 /* Boolean type, a subtype of int */
5 /* We need to define bool_print to override int_print */
8 bool_print(PyBoolObject
*self
, FILE *fp
, int flags
)
10 fputs(self
->ob_ival
== 0 ? "False" : "True", fp
);
14 /* We define bool_repr to return "False" or "True" */
16 static PyObject
*false_str
= NULL
;
17 static PyObject
*true_str
= NULL
;
20 bool_repr(PyBoolObject
*self
)
25 s
= true_str
? true_str
:
26 (true_str
= PyString_InternFromString("True"));
28 s
= false_str
? false_str
:
29 (false_str
= PyString_InternFromString("False"));
34 /* Function to return a bool from a C long */
36 PyObject
*PyBool_FromLong(long ok
)
48 /* We define bool_new to always return either Py_True or Py_False */
51 bool_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
53 static char *kwlist
[] = {"x", 0};
57 if (!PyArg_ParseTupleAndKeywords(args
, kwds
, "O:bool", kwlist
, &x
))
59 ok
= PyObject_IsTrue(x
);
62 return PyBool_FromLong(ok
);
65 /* Arithmetic operations redefined to return bool if both args are bool. */
68 bool_and(PyObject
*a
, PyObject
*b
)
70 if (!PyBool_Check(a
) || !PyBool_Check(b
))
71 return PyInt_Type
.tp_as_number
->nb_and(a
, b
);
72 return PyBool_FromLong(
73 ((PyBoolObject
*)a
)->ob_ival
& ((PyBoolObject
*)b
)->ob_ival
);
77 bool_or(PyObject
*a
, PyObject
*b
)
79 if (!PyBool_Check(a
) || !PyBool_Check(b
))
80 return PyInt_Type
.tp_as_number
->nb_or(a
, b
);
81 return PyBool_FromLong(
82 ((PyBoolObject
*)a
)->ob_ival
| ((PyBoolObject
*)b
)->ob_ival
);
86 bool_xor(PyObject
*a
, PyObject
*b
)
88 if (!PyBool_Check(a
) || !PyBool_Check(b
))
89 return PyInt_Type
.tp_as_number
->nb_xor(a
, b
);
90 return PyBool_FromLong(
91 ((PyBoolObject
*)a
)->ob_ival
^ ((PyBoolObject
*)b
)->ob_ival
);
96 PyDoc_STRVAR(bool_doc
,
99 Returns True when the argument x is true, False otherwise.\n\
100 The builtins True and False are the only two instances of the class bool.\n\
101 The class bool is a subclass of the class int, and cannot be subclassed.");
103 /* Arithmetic methods -- only so we can override &, |, ^. */
105 static PyNumberMethods bool_as_number
= {
110 0, /* nb_remainder */
120 (binaryfunc
)bool_and
, /* nb_and */
121 (binaryfunc
)bool_xor
, /* nb_xor */
122 (binaryfunc
)bool_or
, /* nb_or */
129 0, /* nb_inplace_add */
130 0, /* nb_inplace_subtract */
131 0, /* nb_inplace_multiply */
132 0, /* nb_inplace_divide */
133 0, /* nb_inplace_remainder */
134 0, /* nb_inplace_power */
135 0, /* nb_inplace_lshift */
136 0, /* nb_inplace_rshift */
137 0, /* nb_inplace_and */
138 0, /* nb_inplace_xor */
139 0, /* nb_inplace_or */
140 0, /* nb_floor_divide */
141 0, /* nb_true_divide */
142 0, /* nb_inplace_floor_divide */
143 0, /* nb_inplace_true_divide */
146 /* The type object for bool. Note that this cannot be subclassed! */
148 PyTypeObject PyBool_Type
= {
149 PyObject_HEAD_INIT(&PyType_Type
)
155 (printfunc
)bool_print
, /* tp_print */
159 (reprfunc
)bool_repr
, /* tp_repr */
160 &bool_as_number
, /* tp_as_number */
161 0, /* tp_as_sequence */
162 0, /* tp_as_mapping */
165 (reprfunc
)bool_repr
, /* tp_str */
168 0, /* tp_as_buffer */
169 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_CHECKTYPES
, /* tp_flags */
170 bool_doc
, /* tp_doc */
173 0, /* tp_richcompare */
174 0, /* tp_weaklistoffset */
180 &PyInt_Type
, /* tp_base */
182 0, /* tp_descr_get */
183 0, /* tp_descr_set */
184 0, /* tp_dictoffset */
187 bool_new
, /* tp_new */
190 /* The objects representing bool values False and True */
192 /* Named Zero for link-level compatibility */
193 PyIntObject _Py_ZeroStruct
= {
194 PyObject_HEAD_INIT(&PyBool_Type
)
198 PyIntObject _Py_TrueStruct
= {
199 PyObject_HEAD_INIT(&PyBool_Type
)