blob: 00d85156a6cdbce1d51ce0452a63f8ef49cc2c4f [file] [log] [blame]
Guido van Rossum5f820362002-04-03 23:01:45 +00001/* Boolean type, a subtype of int */
2
3#include "Python.h"
Guido van Rossumddefaf32007-01-14 03:31:43 +00004#include "longintrepr.h"
Guido van Rossum5f820362002-04-03 23:01:45 +00005
6/* We need to define bool_print to override int_print */
7
8static int
Guido van Rossumddefaf32007-01-14 03:31:43 +00009bool_print(PyObject *self, FILE *fp, int flags)
Guido van Rossum5f820362002-04-03 23:01:45 +000010{
Guido van Rossumddefaf32007-01-14 03:31:43 +000011 fputs(self == Py_False ? "False" : "True", fp);
Guido van Rossum5f820362002-04-03 23:01:45 +000012 return 0;
13}
14
15/* We define bool_repr to return "False" or "True" */
16
17static PyObject *false_str = NULL;
18static PyObject *true_str = NULL;
19
Neal Norwitz657d2222002-08-06 22:12:52 +000020static PyObject *
Guido van Rossumddefaf32007-01-14 03:31:43 +000021bool_repr(PyObject *self)
Guido van Rossum5f820362002-04-03 23:01:45 +000022{
23 PyObject *s;
24
Guido van Rossumddefaf32007-01-14 03:31:43 +000025 if (self == Py_True)
Guido van Rossum5f820362002-04-03 23:01:45 +000026 s = true_str ? true_str :
Walter Dörwald16807132007-05-25 13:52:07 +000027 (true_str = PyUnicode_InternFromString("True"));
Guido van Rossum5f820362002-04-03 23:01:45 +000028 else
29 s = false_str ? false_str :
Walter Dörwald16807132007-05-25 13:52:07 +000030 (false_str = PyUnicode_InternFromString("False"));
Guido van Rossum5f820362002-04-03 23:01:45 +000031 Py_XINCREF(s);
32 return s;
33}
34
35/* Function to return a bool from a C long */
36
37PyObject *PyBool_FromLong(long ok)
38{
39 PyObject *result;
40
41 if (ok)
42 result = Py_True;
43 else
44 result = Py_False;
45 Py_INCREF(result);
46 return result;
47}
48
49/* We define bool_new to always return either Py_True or Py_False */
50
Neal Norwitz657d2222002-08-06 22:12:52 +000051static PyObject *
Guido van Rossum5f820362002-04-03 23:01:45 +000052bool_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
53{
Martin v. Löwis15e62742006-02-27 16:46:16 +000054 static char *kwlist[] = {"x", 0};
Guido van Rossumaa86e352003-04-19 18:15:10 +000055 PyObject *x = Py_False;
Guido van Rossum5f820362002-04-03 23:01:45 +000056 long ok;
57
Guido van Rossumaa86e352003-04-19 18:15:10 +000058 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:bool", kwlist, &x))
Guido van Rossum5f820362002-04-03 23:01:45 +000059 return NULL;
60 ok = PyObject_IsTrue(x);
61 if (ok < 0)
62 return NULL;
63 return PyBool_FromLong(ok);
64}
65
66/* Arithmetic operations redefined to return bool if both args are bool. */
67
68static PyObject *
69bool_and(PyObject *a, PyObject *b)
70{
71 if (!PyBool_Check(a) || !PyBool_Check(b))
Guido van Rossumddefaf32007-01-14 03:31:43 +000072 return PyLong_Type.tp_as_number->nb_and(a, b);
73 return PyBool_FromLong((a == Py_True) & (b == Py_True));
Guido van Rossum5f820362002-04-03 23:01:45 +000074}
75
76static PyObject *
77bool_or(PyObject *a, PyObject *b)
78{
79 if (!PyBool_Check(a) || !PyBool_Check(b))
Guido van Rossumddefaf32007-01-14 03:31:43 +000080 return PyLong_Type.tp_as_number->nb_or(a, b);
81 return PyBool_FromLong((a == Py_True) | (b == Py_True));
Guido van Rossum5f820362002-04-03 23:01:45 +000082}
83
84static PyObject *
85bool_xor(PyObject *a, PyObject *b)
86{
87 if (!PyBool_Check(a) || !PyBool_Check(b))
Guido van Rossumddefaf32007-01-14 03:31:43 +000088 return PyLong_Type.tp_as_number->nb_xor(a, b);
89 return PyBool_FromLong((a == Py_True) ^ (b == Py_True));
Guido van Rossum5f820362002-04-03 23:01:45 +000090}
91
92/* Doc string */
93
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000094PyDoc_STRVAR(bool_doc,
Guido van Rossum5f820362002-04-03 23:01:45 +000095"bool(x) -> bool\n\
96\n\
97Returns True when the argument x is true, False otherwise.\n\
98The builtins True and False are the only two instances of the class bool.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000099The class bool is a subclass of the class int, and cannot be subclassed.");
Guido van Rossum5f820362002-04-03 23:01:45 +0000100
101/* Arithmetic methods -- only so we can override &, |, ^. */
102
103static PyNumberMethods bool_as_number = {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000104 0, /* nb_add */
105 0, /* nb_subtract */
106 0, /* nb_multiply */
107 0, /* nb_remainder */
108 0, /* nb_divmod */
109 0, /* nb_power */
110 0, /* nb_negative */
111 0, /* nb_positive */
112 0, /* nb_absolute */
Jack Diederich4dafcc42006-11-28 19:15:13 +0000113 0, /* nb_bool */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000114 0, /* nb_invert */
115 0, /* nb_lshift */
116 0, /* nb_rshift */
117 bool_and, /* nb_and */
118 bool_xor, /* nb_xor */
119 bool_or, /* nb_or */
120 0, /* nb_coerce */
121 0, /* nb_int */
122 0, /* nb_long */
123 0, /* nb_float */
124 0, /* nb_oct */
125 0, /* nb_hex */
126 0, /* nb_inplace_add */
127 0, /* nb_inplace_subtract */
128 0, /* nb_inplace_multiply */
129 0, /* nb_inplace_remainder */
130 0, /* nb_inplace_power */
131 0, /* nb_inplace_lshift */
132 0, /* nb_inplace_rshift */
133 0, /* nb_inplace_and */
134 0, /* nb_inplace_xor */
135 0, /* nb_inplace_or */
136 0, /* nb_floor_divide */
137 0, /* nb_true_divide */
138 0, /* nb_inplace_floor_divide */
139 0, /* nb_inplace_true_divide */
Guido van Rossumddefaf32007-01-14 03:31:43 +0000140 0, /* nb_index */
Guido van Rossum5f820362002-04-03 23:01:45 +0000141};
142
143/* The type object for bool. Note that this cannot be subclassed! */
144
145PyTypeObject PyBool_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000146 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Guido van Rossum5f820362002-04-03 23:01:45 +0000147 "bool",
Guido van Rossumddefaf32007-01-14 03:31:43 +0000148 sizeof(struct _longobject),
Guido van Rossum5f820362002-04-03 23:01:45 +0000149 0,
150 0, /* tp_dealloc */
Guido van Rossumddefaf32007-01-14 03:31:43 +0000151 bool_print, /* tp_print */
Guido van Rossum5f820362002-04-03 23:01:45 +0000152 0, /* tp_getattr */
153 0, /* tp_setattr */
154 0, /* tp_compare */
Guido van Rossumddefaf32007-01-14 03:31:43 +0000155 bool_repr, /* tp_repr */
Guido van Rossum5f820362002-04-03 23:01:45 +0000156 &bool_as_number, /* tp_as_number */
157 0, /* tp_as_sequence */
158 0, /* tp_as_mapping */
159 0, /* tp_hash */
160 0, /* tp_call */
Guido van Rossumddefaf32007-01-14 03:31:43 +0000161 bool_repr, /* tp_str */
Guido van Rossum5f820362002-04-03 23:01:45 +0000162 0, /* tp_getattro */
163 0, /* tp_setattro */
164 0, /* tp_as_buffer */
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000165 Py_TPFLAGS_DEFAULT, /* tp_flags */
Guido van Rossum5f820362002-04-03 23:01:45 +0000166 bool_doc, /* tp_doc */
167 0, /* tp_traverse */
168 0, /* tp_clear */
169 0, /* tp_richcompare */
170 0, /* tp_weaklistoffset */
171 0, /* tp_iter */
172 0, /* tp_iternext */
173 0, /* tp_methods */
174 0, /* tp_members */
175 0, /* tp_getset */
Guido van Rossumddefaf32007-01-14 03:31:43 +0000176 &PyLong_Type, /* tp_base */
Guido van Rossum5f820362002-04-03 23:01:45 +0000177 0, /* tp_dict */
178 0, /* tp_descr_get */
179 0, /* tp_descr_set */
180 0, /* tp_dictoffset */
181 0, /* tp_init */
182 0, /* tp_alloc */
183 bool_new, /* tp_new */
184};
185
186/* The objects representing bool values False and True */
187
188/* Named Zero for link-level compatibility */
Guido van Rossumddefaf32007-01-14 03:31:43 +0000189struct _longobject _Py_FalseStruct = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000190 PyVarObject_HEAD_INIT(&PyBool_Type, 0)
191 { 0 }
Guido van Rossum5f820362002-04-03 23:01:45 +0000192};
193
Guido van Rossumddefaf32007-01-14 03:31:43 +0000194struct _longobject _Py_TrueStruct = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000195 PyVarObject_HEAD_INIT(&PyBool_Type, 1)
196 { 1 }
Guido van Rossum5f820362002-04-03 23:01:45 +0000197};