blob: b786966533e1d6a320a4ec95f06f6f16c42ce941 [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 PyObject *x = Py_False;
46 long ok;
Guido van Rossum5f820362002-04-03 23:01:45 +000047
Serhiy Storchaka6cca5c82017-06-08 14:41:19 +030048 if (!_PyArg_NoKeywords("bool", kwds))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000049 return NULL;
Serhiy Storchaka2e564242017-03-06 17:01:06 +020050 if (!PyArg_UnpackTuple(args, "bool", 0, 1, &x))
51 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000052 ok = PyObject_IsTrue(x);
53 if (ok < 0)
54 return NULL;
55 return PyBool_FromLong(ok);
Guido van Rossum5f820362002-04-03 23:01:45 +000056}
57
Dong-hee Naa195bce2020-09-29 01:16:21 +090058static PyObject *
59bool_vectorcall(PyObject *type, PyObject * const*args,
60 size_t nargsf, PyObject *kwnames)
61{
62 long ok = 0;
63 if (!_PyArg_NoKwnames("bool", kwnames)) {
64 return NULL;
65 }
66
67 Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
68 if (!_PyArg_CheckPositional("bool", nargs, 0, 1)) {
69 return NULL;
70 }
71
72 assert(PyType_Check(type));
73 if (nargs) {
74 ok = PyObject_IsTrue(args[0]);
Dong-hee Nafa7ce082020-10-01 13:50:40 +090075 if (ok < 0) {
76 return NULL;
77 }
Dong-hee Naa195bce2020-09-29 01:16:21 +090078 }
79 return PyBool_FromLong(ok);
80}
81
Guido van Rossum5f820362002-04-03 23:01:45 +000082/* Arithmetic operations redefined to return bool if both args are bool. */
83
84static PyObject *
85bool_and(PyObject *a, PyObject *b)
86{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000087 if (!PyBool_Check(a) || !PyBool_Check(b))
88 return PyLong_Type.tp_as_number->nb_and(a, b);
89 return PyBool_FromLong((a == Py_True) & (b == Py_True));
Guido van Rossum5f820362002-04-03 23:01:45 +000090}
91
92static PyObject *
93bool_or(PyObject *a, PyObject *b)
94{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000095 if (!PyBool_Check(a) || !PyBool_Check(b))
96 return PyLong_Type.tp_as_number->nb_or(a, b);
97 return PyBool_FromLong((a == Py_True) | (b == Py_True));
Guido van Rossum5f820362002-04-03 23:01:45 +000098}
99
100static PyObject *
101bool_xor(PyObject *a, PyObject *b)
102{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000103 if (!PyBool_Check(a) || !PyBool_Check(b))
104 return PyLong_Type.tp_as_number->nb_xor(a, b);
105 return PyBool_FromLong((a == Py_True) ^ (b == Py_True));
Guido van Rossum5f820362002-04-03 23:01:45 +0000106}
107
108/* Doc string */
109
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000110PyDoc_STRVAR(bool_doc,
Guido van Rossum5f820362002-04-03 23:01:45 +0000111"bool(x) -> bool\n\
112\n\
113Returns True when the argument x is true, False otherwise.\n\
114The builtins True and False are the only two instances of the class bool.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000115The class bool is a subclass of the class int, and cannot be subclassed.");
Guido van Rossum5f820362002-04-03 23:01:45 +0000116
117/* Arithmetic methods -- only so we can override &, |, ^. */
118
119static PyNumberMethods bool_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000120 0, /* nb_add */
121 0, /* nb_subtract */
122 0, /* nb_multiply */
123 0, /* nb_remainder */
124 0, /* nb_divmod */
125 0, /* nb_power */
126 0, /* nb_negative */
127 0, /* nb_positive */
128 0, /* nb_absolute */
129 0, /* nb_bool */
130 0, /* nb_invert */
131 0, /* nb_lshift */
132 0, /* nb_rshift */
133 bool_and, /* nb_and */
134 bool_xor, /* nb_xor */
135 bool_or, /* nb_or */
136 0, /* nb_int */
137 0, /* nb_reserved */
138 0, /* nb_float */
139 0, /* nb_inplace_add */
140 0, /* nb_inplace_subtract */
141 0, /* nb_inplace_multiply */
142 0, /* nb_inplace_remainder */
143 0, /* nb_inplace_power */
144 0, /* nb_inplace_lshift */
145 0, /* nb_inplace_rshift */
146 0, /* nb_inplace_and */
147 0, /* nb_inplace_xor */
148 0, /* nb_inplace_or */
149 0, /* nb_floor_divide */
150 0, /* nb_true_divide */
151 0, /* nb_inplace_floor_divide */
152 0, /* nb_inplace_true_divide */
153 0, /* nb_index */
Guido van Rossum5f820362002-04-03 23:01:45 +0000154};
155
156/* The type object for bool. Note that this cannot be subclassed! */
157
158PyTypeObject PyBool_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000159 PyVarObject_HEAD_INIT(&PyType_Type, 0)
160 "bool",
161 sizeof(struct _longobject),
162 0,
163 0, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200164 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000165 0, /* tp_getattr */
166 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200167 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000168 bool_repr, /* tp_repr */
169 &bool_as_number, /* tp_as_number */
170 0, /* tp_as_sequence */
171 0, /* tp_as_mapping */
172 0, /* tp_hash */
173 0, /* tp_call */
Serhiy Storchaka96aeaec2019-05-06 22:29:40 +0300174 0, /* tp_str */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000175 0, /* tp_getattro */
176 0, /* tp_setattro */
177 0, /* tp_as_buffer */
178 Py_TPFLAGS_DEFAULT, /* tp_flags */
179 bool_doc, /* tp_doc */
180 0, /* tp_traverse */
181 0, /* tp_clear */
182 0, /* tp_richcompare */
183 0, /* tp_weaklistoffset */
184 0, /* tp_iter */
185 0, /* tp_iternext */
186 0, /* tp_methods */
187 0, /* tp_members */
188 0, /* tp_getset */
189 &PyLong_Type, /* tp_base */
190 0, /* tp_dict */
191 0, /* tp_descr_get */
192 0, /* tp_descr_set */
193 0, /* tp_dictoffset */
194 0, /* tp_init */
195 0, /* tp_alloc */
196 bool_new, /* tp_new */
Dong-hee Naa195bce2020-09-29 01:16:21 +0900197 .tp_vectorcall = bool_vectorcall,
Guido van Rossum5f820362002-04-03 23:01:45 +0000198};
199
200/* The objects representing bool values False and True */
201
Guido van Rossumddefaf32007-01-14 03:31:43 +0000202struct _longobject _Py_FalseStruct = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000203 PyVarObject_HEAD_INIT(&PyBool_Type, 0)
204 { 0 }
Guido van Rossum5f820362002-04-03 23:01:45 +0000205};
206
Guido van Rossumddefaf32007-01-14 03:31:43 +0000207struct _longobject _Py_TrueStruct = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000208 PyVarObject_HEAD_INIT(&PyBool_Type, 1)
209 { 1 }
Guido van Rossum5f820362002-04-03 23:01:45 +0000210};