blob: 595832a7a804b8828af8e467da5878da70e32018 [file] [log] [blame]
Guido van Rossum5f820362002-04-03 23:01:45 +00001/* Boolean type, a subtype of int */
2
3#include "Python.h"
4
5/* We need to define bool_print to override int_print */
6
7static int
8bool_print(PyBoolObject *self, FILE *fp, int flags)
9{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000010 Py_BEGIN_ALLOW_THREADS
11 fputs(self->ob_ival == 0 ? "False" : "True", fp);
12 Py_END_ALLOW_THREADS
13 return 0;
Guido van Rossum5f820362002-04-03 23:01:45 +000014}
15
16/* We define bool_repr to return "False" or "True" */
17
18static PyObject *false_str = NULL;
19static PyObject *true_str = NULL;
20
Neal Norwitz657d2222002-08-06 22:12:52 +000021static PyObject *
Guido van Rossum5f820362002-04-03 23:01:45 +000022bool_repr(PyBoolObject *self)
23{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000024 PyObject *s;
Guido van Rossum5f820362002-04-03 23:01:45 +000025
Antoine Pitrouc83ea132010-05-09 14:46:46 +000026 if (self->ob_ival)
27 s = true_str ? true_str :
28 (true_str = PyString_InternFromString("True"));
29 else
30 s = false_str ? false_str :
31 (false_str = PyString_InternFromString("False"));
32 Py_XINCREF(s);
33 return s;
Guido van Rossum5f820362002-04-03 23:01:45 +000034}
35
36/* Function to return a bool from a C long */
37
38PyObject *PyBool_FromLong(long ok)
39{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000040 PyObject *result;
Guido van Rossum5f820362002-04-03 23:01:45 +000041
Antoine Pitrouc83ea132010-05-09 14:46:46 +000042 if (ok)
43 result = Py_True;
44 else
45 result = Py_False;
46 Py_INCREF(result);
47 return result;
Guido van Rossum5f820362002-04-03 23:01:45 +000048}
49
50/* We define bool_new to always return either Py_True or Py_False */
51
Neal Norwitz657d2222002-08-06 22:12:52 +000052static PyObject *
Guido van Rossum5f820362002-04-03 23:01:45 +000053bool_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
54{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000055 static char *kwlist[] = {"x", 0};
56 PyObject *x = Py_False;
57 long ok;
Guido van Rossum5f820362002-04-03 23:01:45 +000058
Antoine Pitrouc83ea132010-05-09 14:46:46 +000059 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:bool", kwlist, &x))
60 return NULL;
61 ok = PyObject_IsTrue(x);
62 if (ok < 0)
63 return NULL;
64 return PyBool_FromLong(ok);
Guido van Rossum5f820362002-04-03 23:01:45 +000065}
66
67/* Arithmetic operations redefined to return bool if both args are bool. */
68
69static PyObject *
70bool_and(PyObject *a, PyObject *b)
71{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000072 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);
Guido van Rossum5f820362002-04-03 23:01:45 +000076}
77
78static PyObject *
79bool_or(PyObject *a, PyObject *b)
80{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000081 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);
Guido van Rossum5f820362002-04-03 23:01:45 +000085}
86
87static PyObject *
88bool_xor(PyObject *a, PyObject *b)
89{
Antoine Pitrouc83ea132010-05-09 14:46:46 +000090 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);
Guido van Rossum5f820362002-04-03 23:01:45 +000094}
95
96/* Doc string */
97
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000098PyDoc_STRVAR(bool_doc,
Guido van Rossum5f820362002-04-03 23:01:45 +000099"bool(x) -> bool\n\
100\n\
101Returns True when the argument x is true, False otherwise.\n\
102The builtins True and False are the only two instances of the class bool.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000103The class bool is a subclass of the class int, and cannot be subclassed.");
Guido van Rossum5f820362002-04-03 23:01:45 +0000104
105/* Arithmetic methods -- only so we can override &, |, ^. */
106
107static PyNumberMethods bool_as_number = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000108 0, /* nb_add */
109 0, /* nb_subtract */
110 0, /* nb_multiply */
111 0, /* nb_divide */
112 0, /* nb_remainder */
113 0, /* nb_divmod */
114 0, /* nb_power */
115 0, /* nb_negative */
116 0, /* nb_positive */
117 0, /* nb_absolute */
118 0, /* nb_nonzero */
119 0, /* nb_invert */
120 0, /* nb_lshift */
121 0, /* nb_rshift */
122 bool_and, /* nb_and */
123 bool_xor, /* nb_xor */
124 bool_or, /* nb_or */
125 0, /* nb_coerce */
126 0, /* nb_int */
127 0, /* nb_long */
128 0, /* nb_float */
129 0, /* nb_oct */
130 0, /* nb_hex */
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 */
Guido van Rossum5f820362002-04-03 23:01:45 +0000146};
147
148/* The type object for bool. Note that this cannot be subclassed! */
149
150PyTypeObject PyBool_Type = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000151 PyVarObject_HEAD_INIT(&PyType_Type, 0)
152 "bool",
153 sizeof(PyIntObject),
154 0,
155 0, /* tp_dealloc */
156 (printfunc)bool_print, /* tp_print */
157 0, /* tp_getattr */
158 0, /* tp_setattr */
159 0, /* tp_compare */
160 (reprfunc)bool_repr, /* tp_repr */
161 &bool_as_number, /* tp_as_number */
162 0, /* tp_as_sequence */
163 0, /* tp_as_mapping */
164 0, /* tp_hash */
165 0, /* tp_call */
166 (reprfunc)bool_repr, /* tp_str */
167 0, /* tp_getattro */
168 0, /* tp_setattro */
169 0, /* tp_as_buffer */
170 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES, /* tp_flags */
171 bool_doc, /* tp_doc */
172 0, /* tp_traverse */
173 0, /* tp_clear */
174 0, /* tp_richcompare */
175 0, /* tp_weaklistoffset */
176 0, /* tp_iter */
177 0, /* tp_iternext */
178 0, /* tp_methods */
179 0, /* tp_members */
180 0, /* tp_getset */
181 &PyInt_Type, /* tp_base */
182 0, /* tp_dict */
183 0, /* tp_descr_get */
184 0, /* tp_descr_set */
185 0, /* tp_dictoffset */
186 0, /* tp_init */
187 0, /* tp_alloc */
188 bool_new, /* tp_new */
Guido van Rossum5f820362002-04-03 23:01:45 +0000189};
190
191/* The objects representing bool values False and True */
192
193/* Named Zero for link-level compatibility */
194PyIntObject _Py_ZeroStruct = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000195 PyObject_HEAD_INIT(&PyBool_Type)
196 0
Guido van Rossum5f820362002-04-03 23:01:45 +0000197};
198
199PyIntObject _Py_TrueStruct = {
Antoine Pitrouc83ea132010-05-09 14:46:46 +0000200 PyObject_HEAD_INIT(&PyBool_Type)
201 1
Guido van Rossum5f820362002-04-03 23:01:45 +0000202};