blob: fd73d2809535db429c1452f3fb37d33d7b087368 [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{
Brett Cannon01531592007-09-17 03:28:34 +000010 Py_BEGIN_ALLOW_THREADS
Guido van Rossum645a22e2002-04-04 01:00:42 +000011 fputs(self->ob_ival == 0 ? "False" : "True", fp);
Brett Cannon01531592007-09-17 03:28:34 +000012 Py_END_ALLOW_THREADS
Guido van Rossum5f820362002-04-03 23:01:45 +000013 return 0;
14}
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{
24 PyObject *s;
25
26 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;
34}
35
36/* Function to return a bool from a C long */
37
38PyObject *PyBool_FromLong(long ok)
39{
40 PyObject *result;
41
42 if (ok)
43 result = Py_True;
44 else
45 result = Py_False;
46 Py_INCREF(result);
47 return result;
48}
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{
Martin v. Löwis15e62742006-02-27 16:46:16 +000055 static char *kwlist[] = {"x", 0};
Guido van Rossumaa86e352003-04-19 18:15:10 +000056 PyObject *x = Py_False;
Guido van Rossum5f820362002-04-03 23:01:45 +000057 long ok;
58
Guido van Rossumaa86e352003-04-19 18:15:10 +000059 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:bool", kwlist, &x))
Guido van Rossum5f820362002-04-03 23:01:45 +000060 return NULL;
61 ok = PyObject_IsTrue(x);
62 if (ok < 0)
63 return NULL;
64 return PyBool_FromLong(ok);
65}
66
67/* Arithmetic operations redefined to return bool if both args are bool. */
68
69static PyObject *
70bool_and(PyObject *a, PyObject *b)
71{
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);
76}
77
78static PyObject *
79bool_or(PyObject *a, PyObject *b)
80{
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);
85}
86
87static PyObject *
88bool_xor(PyObject *a, PyObject *b)
89{
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);
94}
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 = {
Georg Brandl347b3002006-03-30 11:57:00 +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 = {
Martin v. Löwis68192102007-07-21 06:55:02 +0000151 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Guido van Rossum5f820362002-04-03 23:01:45 +0000152 "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 */
189};
190
191/* The objects representing bool values False and True */
192
193/* Named Zero for link-level compatibility */
194PyIntObject _Py_ZeroStruct = {
195 PyObject_HEAD_INIT(&PyBool_Type)
196 0
197};
198
199PyIntObject _Py_TrueStruct = {
200 PyObject_HEAD_INIT(&PyBool_Type)
201 1
202};