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 Py_BEGIN_ALLOW_THREADS
11 fputs(self
->ob_ival
== 0 ? "False" : "True", fp
);
16 /* We define bool_repr to return "False" or "True" */
18 static PyObject
*false_str
= NULL
;
19 static PyObject
*true_str
= NULL
;
22 bool_repr(PyBoolObject
*self
)
27 s
= true_str
? true_str
:
28 (true_str
= PyString_InternFromString("True"));
30 s
= false_str
? false_str
:
31 (false_str
= PyString_InternFromString("False"));
36 /* Function to return a bool from a C long */
38 PyObject
*PyBool_FromLong(long ok
)
50 /* We define bool_new to always return either Py_True or Py_False */
53 bool_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
55 static char *kwlist
[] = {"x", 0};
56 PyObject
*x
= Py_False
;
59 if (!PyArg_ParseTupleAndKeywords(args
, kwds
, "|O:bool", kwlist
, &x
))
61 ok
= PyObject_IsTrue(x
);
64 return PyBool_FromLong(ok
);
67 /* Arithmetic operations redefined to return bool if both args are bool. */
70 bool_and(PyObject
*a
, PyObject
*b
)
72 if (!PyBool_Check(a
) || !PyBool_Check(b
))
73 return PyInt_Type
.tp_as_number
->nb_and(a
, b
);
74 return PyBool_FromLong(
75 ((PyBoolObject
*)a
)->ob_ival
& ((PyBoolObject
*)b
)->ob_ival
);
79 bool_or(PyObject
*a
, PyObject
*b
)
81 if (!PyBool_Check(a
) || !PyBool_Check(b
))
82 return PyInt_Type
.tp_as_number
->nb_or(a
, b
);
83 return PyBool_FromLong(
84 ((PyBoolObject
*)a
)->ob_ival
| ((PyBoolObject
*)b
)->ob_ival
);
88 bool_xor(PyObject
*a
, PyObject
*b
)
90 if (!PyBool_Check(a
) || !PyBool_Check(b
))
91 return PyInt_Type
.tp_as_number
->nb_xor(a
, b
);
92 return PyBool_FromLong(
93 ((PyBoolObject
*)a
)->ob_ival
^ ((PyBoolObject
*)b
)->ob_ival
);
98 PyDoc_STRVAR(bool_doc
,
101 Returns True when the argument x is true, False otherwise.\n\
102 The builtins True and False are the only two instances of the class bool.\n\
103 The class bool is a subclass of the class int, and cannot be subclassed.");
105 /* Arithmetic methods -- only so we can override &, |, ^. */
107 static PyNumberMethods bool_as_number
= {
112 0, /* nb_remainder */
122 bool_and
, /* nb_and */
123 bool_xor
, /* nb_xor */
131 0, /* nb_inplace_add */
132 0, /* nb_inplace_subtract */
133 0, /* nb_inplace_multiply */
134 0, /* nb_inplace_divide */
135 0, /* nb_inplace_remainder */
136 0, /* nb_inplace_power */
137 0, /* nb_inplace_lshift */
138 0, /* nb_inplace_rshift */
139 0, /* nb_inplace_and */
140 0, /* nb_inplace_xor */
141 0, /* nb_inplace_or */
142 0, /* nb_floor_divide */
143 0, /* nb_true_divide */
144 0, /* nb_inplace_floor_divide */
145 0, /* nb_inplace_true_divide */
148 /* The type object for bool. Note that this cannot be subclassed! */
150 PyTypeObject PyBool_Type
= {
151 PyVarObject_HEAD_INIT(&PyType_Type
, 0)
156 (printfunc
)bool_print
, /* tp_print */
160 (reprfunc
)bool_repr
, /* tp_repr */
161 &bool_as_number
, /* tp_as_number */
162 0, /* tp_as_sequence */
163 0, /* tp_as_mapping */
166 (reprfunc
)bool_repr
, /* tp_str */
169 0, /* tp_as_buffer */
170 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_CHECKTYPES
, /* tp_flags */
171 bool_doc
, /* tp_doc */
174 0, /* tp_richcompare */
175 0, /* tp_weaklistoffset */
181 &PyInt_Type
, /* tp_base */
183 0, /* tp_descr_get */
184 0, /* tp_descr_set */
185 0, /* tp_dictoffset */
188 bool_new
, /* tp_new */
191 /* The objects representing bool values False and True */
193 /* Named Zero for link-level compatibility */
194 PyIntObject _Py_ZeroStruct
= {
195 PyObject_HEAD_INIT(&PyBool_Type
)
199 PyIntObject _Py_TrueStruct
= {
200 PyObject_HEAD_INIT(&PyBool_Type
)