blob: 31510bb07d56f70299627ceb2999c27b9339b217 [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
Guido van Rossum5f820362002-04-03 23:01:45 +00006/* We define bool_repr to return "False" or "True" */
7
8static PyObject *false_str = NULL;
9static PyObject *true_str = NULL;
10
Neal Norwitz657d2222002-08-06 22:12:52 +000011static PyObject *
Guido van Rossumddefaf32007-01-14 03:31:43 +000012bool_repr(PyObject *self)
Guido van Rossum5f820362002-04-03 23:01:45 +000013{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000014 PyObject *s;
Guido van Rossum5f820362002-04-03 23:01:45 +000015
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000016 if (self == Py_True)
17 s = true_str ? true_str :
18 (true_str = PyUnicode_InternFromString("True"));
19 else
20 s = false_str ? false_str :
21 (false_str = PyUnicode_InternFromString("False"));
22 Py_XINCREF(s);
23 return s;
Guido van Rossum5f820362002-04-03 23:01:45 +000024}
25
26/* Function to return a bool from a C long */
27
28PyObject *PyBool_FromLong(long ok)
29{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000030 PyObject *result;
Guido van Rossum5f820362002-04-03 23:01:45 +000031
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000032 if (ok)
33 result = Py_True;
34 else
35 result = Py_False;
36 Py_INCREF(result);
37 return result;
Guido van Rossum5f820362002-04-03 23:01:45 +000038}
39
40/* We define bool_new to always return either Py_True or Py_False */
41
Neal Norwitz657d2222002-08-06 22:12:52 +000042static PyObject *
Guido van Rossum5f820362002-04-03 23:01:45 +000043bool_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
44{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000045 static char *kwlist[] = {"x", 0};
46 PyObject *x = Py_False;
47 long ok;
Guido van Rossum5f820362002-04-03 23:01:45 +000048
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000049 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:bool", kwlist, &x))
50 return NULL;
51 ok = PyObject_IsTrue(x);
52 if (ok < 0)
53 return NULL;
54 return PyBool_FromLong(ok);
Guido van Rossum5f820362002-04-03 23:01:45 +000055}
56
57/* Arithmetic operations redefined to return bool if both args are bool. */
58
59static PyObject *
60bool_and(PyObject *a, PyObject *b)
61{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000062 if (!PyBool_Check(a) || !PyBool_Check(b))
63 return PyLong_Type.tp_as_number->nb_and(a, b);
64 return PyBool_FromLong((a == Py_True) & (b == Py_True));
Guido van Rossum5f820362002-04-03 23:01:45 +000065}
66
67static PyObject *
68bool_or(PyObject *a, PyObject *b)
69{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000070 if (!PyBool_Check(a) || !PyBool_Check(b))
71 return PyLong_Type.tp_as_number->nb_or(a, b);
72 return PyBool_FromLong((a == Py_True) | (b == Py_True));
Guido van Rossum5f820362002-04-03 23:01:45 +000073}
74
75static PyObject *
76bool_xor(PyObject *a, PyObject *b)
77{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000078 if (!PyBool_Check(a) || !PyBool_Check(b))
79 return PyLong_Type.tp_as_number->nb_xor(a, b);
80 return PyBool_FromLong((a == Py_True) ^ (b == Py_True));
Guido van Rossum5f820362002-04-03 23:01:45 +000081}
82
83/* Doc string */
84
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000085PyDoc_STRVAR(bool_doc,
Guido van Rossum5f820362002-04-03 23:01:45 +000086"bool(x) -> bool\n\
87\n\
88Returns True when the argument x is true, False otherwise.\n\
89The builtins True and False are the only two instances of the class bool.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000090The class bool is a subclass of the class int, and cannot be subclassed.");
Guido van Rossum5f820362002-04-03 23:01:45 +000091
92/* Arithmetic methods -- only so we can override &, |, ^. */
93
94static PyNumberMethods bool_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000095 0, /* nb_add */
96 0, /* nb_subtract */
97 0, /* nb_multiply */
98 0, /* nb_remainder */
99 0, /* nb_divmod */
100 0, /* nb_power */
101 0, /* nb_negative */
102 0, /* nb_positive */
103 0, /* nb_absolute */
104 0, /* nb_bool */
105 0, /* nb_invert */
106 0, /* nb_lshift */
107 0, /* nb_rshift */
108 bool_and, /* nb_and */
109 bool_xor, /* nb_xor */
110 bool_or, /* nb_or */
111 0, /* nb_int */
112 0, /* nb_reserved */
113 0, /* nb_float */
114 0, /* nb_inplace_add */
115 0, /* nb_inplace_subtract */
116 0, /* nb_inplace_multiply */
117 0, /* nb_inplace_remainder */
118 0, /* nb_inplace_power */
119 0, /* nb_inplace_lshift */
120 0, /* nb_inplace_rshift */
121 0, /* nb_inplace_and */
122 0, /* nb_inplace_xor */
123 0, /* nb_inplace_or */
124 0, /* nb_floor_divide */
125 0, /* nb_true_divide */
126 0, /* nb_inplace_floor_divide */
127 0, /* nb_inplace_true_divide */
128 0, /* nb_index */
Guido van Rossum5f820362002-04-03 23:01:45 +0000129};
130
131/* The type object for bool. Note that this cannot be subclassed! */
132
133PyTypeObject PyBool_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000134 PyVarObject_HEAD_INIT(&PyType_Type, 0)
135 "bool",
136 sizeof(struct _longobject),
137 0,
138 0, /* tp_dealloc */
139 0, /* tp_print */
140 0, /* tp_getattr */
141 0, /* tp_setattr */
142 0, /* tp_reserved */
143 bool_repr, /* tp_repr */
144 &bool_as_number, /* tp_as_number */
145 0, /* tp_as_sequence */
146 0, /* tp_as_mapping */
147 0, /* tp_hash */
148 0, /* tp_call */
149 bool_repr, /* tp_str */
150 0, /* tp_getattro */
151 0, /* tp_setattro */
152 0, /* tp_as_buffer */
153 Py_TPFLAGS_DEFAULT, /* tp_flags */
154 bool_doc, /* tp_doc */
155 0, /* tp_traverse */
156 0, /* tp_clear */
157 0, /* tp_richcompare */
158 0, /* tp_weaklistoffset */
159 0, /* tp_iter */
160 0, /* tp_iternext */
161 0, /* tp_methods */
162 0, /* tp_members */
163 0, /* tp_getset */
164 &PyLong_Type, /* tp_base */
165 0, /* tp_dict */
166 0, /* tp_descr_get */
167 0, /* tp_descr_set */
168 0, /* tp_dictoffset */
169 0, /* tp_init */
170 0, /* tp_alloc */
171 bool_new, /* tp_new */
Guido van Rossum5f820362002-04-03 23:01:45 +0000172};
173
174/* The objects representing bool values False and True */
175
Guido van Rossumddefaf32007-01-14 03:31:43 +0000176struct _longobject _Py_FalseStruct = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000177 PyVarObject_HEAD_INIT(&PyBool_Type, 0)
178 { 0 }
Guido van Rossum5f820362002-04-03 23:01:45 +0000179};
180
Guido van Rossumddefaf32007-01-14 03:31:43 +0000181struct _longobject _Py_TrueStruct = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000182 PyVarObject_HEAD_INIT(&PyBool_Type, 1)
183 { 1 }
Guido van Rossum5f820362002-04-03 23:01:45 +0000184};