blob: f98af9f2465d683939b55d6c2a5c3b9c2709debc [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 :
27 (true_str = PyString_InternFromString("True"));
28 else
29 s = false_str ? false_str :
30 (false_str = PyString_InternFromString("False"));
31 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 = {
146 PyObject_HEAD_INIT(&PyType_Type)
147 0,
148 "bool",
Guido van Rossumddefaf32007-01-14 03:31:43 +0000149 sizeof(struct _longobject),
Guido van Rossum5f820362002-04-03 23:01:45 +0000150 0,
151 0, /* tp_dealloc */
Guido van Rossumddefaf32007-01-14 03:31:43 +0000152 bool_print, /* tp_print */
Guido van Rossum5f820362002-04-03 23:01:45 +0000153 0, /* tp_getattr */
154 0, /* tp_setattr */
155 0, /* tp_compare */
Guido van Rossumddefaf32007-01-14 03:31:43 +0000156 bool_repr, /* tp_repr */
Guido van Rossum5f820362002-04-03 23:01:45 +0000157 &bool_as_number, /* tp_as_number */
158 0, /* tp_as_sequence */
159 0, /* tp_as_mapping */
160 0, /* tp_hash */
161 0, /* tp_call */
Guido van Rossumddefaf32007-01-14 03:31:43 +0000162 bool_repr, /* tp_str */
Guido van Rossum5f820362002-04-03 23:01:45 +0000163 0, /* tp_getattro */
164 0, /* tp_setattro */
165 0, /* tp_as_buffer */
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000166 Py_TPFLAGS_DEFAULT, /* tp_flags */
Guido van Rossum5f820362002-04-03 23:01:45 +0000167 bool_doc, /* tp_doc */
168 0, /* tp_traverse */
169 0, /* tp_clear */
170 0, /* tp_richcompare */
171 0, /* tp_weaklistoffset */
172 0, /* tp_iter */
173 0, /* tp_iternext */
174 0, /* tp_methods */
175 0, /* tp_members */
176 0, /* tp_getset */
Guido van Rossumddefaf32007-01-14 03:31:43 +0000177 &PyLong_Type, /* tp_base */
Guido van Rossum5f820362002-04-03 23:01:45 +0000178 0, /* tp_dict */
179 0, /* tp_descr_get */
180 0, /* tp_descr_set */
181 0, /* tp_dictoffset */
182 0, /* tp_init */
183 0, /* tp_alloc */
184 bool_new, /* tp_new */
185};
186
187/* The objects representing bool values False and True */
188
189/* Named Zero for link-level compatibility */
Guido van Rossumddefaf32007-01-14 03:31:43 +0000190struct _longobject _Py_FalseStruct = {
Guido van Rossum5f820362002-04-03 23:01:45 +0000191 PyObject_HEAD_INIT(&PyBool_Type)
Guido van Rossumddefaf32007-01-14 03:31:43 +0000192 0, { 0 }
Guido van Rossum5f820362002-04-03 23:01:45 +0000193};
194
Guido van Rossumddefaf32007-01-14 03:31:43 +0000195struct _longobject _Py_TrueStruct = {
Guido van Rossum5f820362002-04-03 23:01:45 +0000196 PyObject_HEAD_INIT(&PyBool_Type)
Guido van Rossumddefaf32007-01-14 03:31:43 +0000197 1, { 1 }
Guido van Rossum5f820362002-04-03 23:01:45 +0000198};