/src/cpython/Objects/boolobject.c
Line | Count | Source |
1 | | /* Boolean type, a subtype of int */ |
2 | | |
3 | | #include "Python.h" |
4 | | #include "pycore_long.h" // FALSE_TAG TRUE_TAG |
5 | | #include "pycore_object.h" // _Py_FatalRefcountError() |
6 | | #include "pycore_runtime.h" // _Py_ID() |
7 | | |
8 | | #include <stddef.h> |
9 | | |
10 | | /*[clinic input] |
11 | | class bool "PyObject *" "&PyBool_Type" |
12 | | [clinic start generated code]*/ |
13 | | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=ee492546880a882f]*/ |
14 | | |
15 | | #include "clinic/boolobject.c.h" |
16 | | |
17 | | /* We define bool_repr to return "False" or "True" */ |
18 | | |
19 | | static PyObject * |
20 | | bool_repr(PyObject *self) |
21 | 346 | { |
22 | 346 | return self == Py_True ? &_Py_ID(True) : &_Py_ID(False); |
23 | 346 | } |
24 | | |
25 | | /* Function to return a bool from a C long */ |
26 | | |
27 | | PyObject *PyBool_FromLong(long ok) |
28 | 215M | { |
29 | 215M | return ok ? Py_True : Py_False; |
30 | 215M | } |
31 | | |
32 | | /*[clinic input] |
33 | | @vectorcall |
34 | | @classmethod |
35 | | bool.__new__ as bool_new |
36 | | object as x: object = False |
37 | | / |
38 | | |
39 | | Returns True when the argument is true, False otherwise. |
40 | | |
41 | | The builtins True and False are the only two instances of the class |
42 | | bool. The class bool is a subclass of the class int, and cannot be |
43 | | subclassed. |
44 | | [clinic start generated code]*/ |
45 | | |
46 | | static PyObject * |
47 | | bool_new_impl(PyTypeObject *type, PyObject *x) |
48 | | /*[clinic end generated code: output=ec434408d43a3773 input=d96e25fce5a04f73]*/ |
49 | 15.1k | { |
50 | 15.1k | int ok = PyObject_IsTrue(x); |
51 | 15.1k | if (ok < 0) { |
52 | 0 | return NULL; |
53 | 0 | } |
54 | 15.1k | return PyBool_FromLong(ok); |
55 | 15.1k | } |
56 | | |
57 | | /* Arithmetic operations redefined to return bool if both args are bool. */ |
58 | | |
59 | | static PyObject * |
60 | | bool_invert(PyObject *v) |
61 | 0 | { |
62 | 0 | if (PyErr_WarnEx(PyExc_DeprecationWarning, |
63 | 0 | "Bitwise inversion '~' on bool is deprecated and will be removed in " |
64 | 0 | "Python 3.16. This returns the bitwise inversion of the underlying int " |
65 | 0 | "object and is usually not what you expect from negating " |
66 | 0 | "a bool. Use the 'not' operator for boolean negation or " |
67 | 0 | "~int(x) if you really want the bitwise inversion of the " |
68 | 0 | "underlying int.", |
69 | 0 | 1) < 0) { |
70 | 0 | return NULL; |
71 | 0 | } |
72 | 0 | return PyLong_Type.tp_as_number->nb_invert(v); |
73 | 0 | } |
74 | | |
75 | | static PyObject * |
76 | | bool_and(PyObject *a, PyObject *b) |
77 | 0 | { |
78 | 0 | if (!PyBool_Check(a) || !PyBool_Check(b)) |
79 | 0 | return PyLong_Type.tp_as_number->nb_and(a, b); |
80 | 0 | return PyBool_FromLong((a == Py_True) & (b == Py_True)); |
81 | 0 | } |
82 | | |
83 | | static PyObject * |
84 | | bool_or(PyObject *a, PyObject *b) |
85 | 1.02k | { |
86 | 1.02k | if (!PyBool_Check(a) || !PyBool_Check(b)) |
87 | 0 | return PyLong_Type.tp_as_number->nb_or(a, b); |
88 | 1.02k | return PyBool_FromLong((a == Py_True) | (b == Py_True)); |
89 | 1.02k | } |
90 | | |
91 | | static PyObject * |
92 | | bool_xor(PyObject *a, PyObject *b) |
93 | 1 | { |
94 | 1 | if (!PyBool_Check(a) || !PyBool_Check(b)) |
95 | 0 | return PyLong_Type.tp_as_number->nb_xor(a, b); |
96 | 1 | return PyBool_FromLong((a == Py_True) ^ (b == Py_True)); |
97 | 1 | } |
98 | | |
99 | | /* Arithmetic methods -- only so we can override &, |, ^. */ |
100 | | |
101 | | static PyNumberMethods bool_as_number = { |
102 | | 0, /* nb_add */ |
103 | | 0, /* nb_subtract */ |
104 | | 0, /* nb_multiply */ |
105 | | 0, /* nb_remainder */ |
106 | | 0, /* nb_divmod */ |
107 | | 0, /* nb_power */ |
108 | | 0, /* nb_negative */ |
109 | | 0, /* nb_positive */ |
110 | | 0, /* nb_absolute */ |
111 | | 0, /* nb_bool */ |
112 | | bool_invert, /* nb_invert */ |
113 | | 0, /* nb_lshift */ |
114 | | 0, /* nb_rshift */ |
115 | | bool_and, /* nb_and */ |
116 | | bool_xor, /* nb_xor */ |
117 | | bool_or, /* nb_or */ |
118 | | 0, /* nb_int */ |
119 | | 0, /* nb_reserved */ |
120 | | 0, /* nb_float */ |
121 | | 0, /* nb_inplace_add */ |
122 | | 0, /* nb_inplace_subtract */ |
123 | | 0, /* nb_inplace_multiply */ |
124 | | 0, /* nb_inplace_remainder */ |
125 | | 0, /* nb_inplace_power */ |
126 | | 0, /* nb_inplace_lshift */ |
127 | | 0, /* nb_inplace_rshift */ |
128 | | 0, /* nb_inplace_and */ |
129 | | 0, /* nb_inplace_xor */ |
130 | | 0, /* nb_inplace_or */ |
131 | | 0, /* nb_floor_divide */ |
132 | | 0, /* nb_true_divide */ |
133 | | 0, /* nb_inplace_floor_divide */ |
134 | | 0, /* nb_inplace_true_divide */ |
135 | | 0, /* nb_index */ |
136 | | }; |
137 | | |
138 | | static void |
139 | | bool_dealloc(PyObject *boolean) |
140 | 0 | { |
141 | | /* This should never get called, but we also don't want to SEGV if |
142 | | * we accidentally decref Booleans out of existence. Instead, |
143 | | * since bools are immortal, re-set the reference count. |
144 | | */ |
145 | 0 | _Py_SetImmortal(boolean); |
146 | 0 | } |
147 | | |
148 | | /* The type object for bool. Note that this cannot be subclassed! */ |
149 | | |
150 | | PyTypeObject PyBool_Type = { |
151 | | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
152 | | "bool", |
153 | | offsetof(struct _longobject, long_value.ob_digit), /* tp_basicsize */ |
154 | | sizeof(digit), /* tp_itemsize */ |
155 | | bool_dealloc, /* tp_dealloc */ |
156 | | 0, /* tp_vectorcall_offset */ |
157 | | 0, /* tp_getattr */ |
158 | | 0, /* tp_setattr */ |
159 | | 0, /* tp_as_async */ |
160 | | 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 | | 0, /* tp_str */ |
167 | | 0, /* tp_getattro */ |
168 | | 0, /* tp_setattro */ |
169 | | 0, /* tp_as_buffer */ |
170 | | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
171 | | bool_new__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 | | &PyLong_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 | | .tp_vectorcall = bool_vectorcall, |
190 | | }; |
191 | | |
192 | | /* The objects representing bool values False and True */ |
193 | | |
194 | | struct _longobject _Py_FalseStruct = { |
195 | | PyObject_HEAD_INIT(&PyBool_Type) |
196 | | { .lv_tag = _PyLong_FALSE_TAG, |
197 | | { 0 } |
198 | | } |
199 | | }; |
200 | | |
201 | | struct _longobject _Py_TrueStruct = { |
202 | | PyObject_HEAD_INIT(&PyBool_Type) |
203 | | { .lv_tag = _PyLong_TRUE_TAG, |
204 | | { 1 } |
205 | | } |
206 | | }; |