| 
									
										
										
										
											2002-04-03 23:01:45 +00:00
										 |  |  | /* Boolean type, a subtype of int */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "Python.h"
 | 
					
						
							| 
									
										
										
										
											2022-04-19 14:02:19 -04:00
										 |  |  | #include "pycore_object.h"      // _Py_FatalRefcountError()
 | 
					
						
							| 
									
										
										
										
											2022-02-22 17:23:51 -07:00
										 |  |  | #include "pycore_runtime.h"       // _Py_ID()
 | 
					
						
							| 
									
										
										
										
											2002-04-03 23:01:45 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* We define bool_repr to return "False" or "True" */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-08-06 22:12:52 +00:00
										 |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2007-01-14 03:31:43 +00:00
										 |  |  | bool_repr(PyObject *self) | 
					
						
							| 
									
										
										
										
											2002-04-03 23:01:45 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-07-20 11:53:30 +05:30
										 |  |  |     PyObject *res = self == Py_True ? &_Py_ID(True) : &_Py_ID(False); | 
					
						
							|  |  |  |     return Py_NewRef(res); | 
					
						
							| 
									
										
										
										
											2002-04-03 23:01:45 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Function to return a bool from a C long */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PyObject *PyBool_FromLong(long ok) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     PyObject *result; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (ok) | 
					
						
							|  |  |  |         result = Py_True; | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |         result = Py_False; | 
					
						
							| 
									
										
										
										
											2022-11-10 16:27:32 +01:00
										 |  |  |     return Py_NewRef(result); | 
					
						
							| 
									
										
										
										
											2002-04-03 23:01:45 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* We define bool_new to always return either Py_True or Py_False */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-08-06 22:12:52 +00:00
										 |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2002-04-03 23:01:45 +00:00
										 |  |  | bool_new(PyTypeObject *type, PyObject *args, PyObject *kwds) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     PyObject *x = Py_False; | 
					
						
							|  |  |  |     long ok; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-08 14:41:19 +03:00
										 |  |  |     if (!_PyArg_NoKeywords("bool", kwds)) | 
					
						
							| 
									
										
										
										
											2017-03-06 17:01:06 +02:00
										 |  |  |         return NULL; | 
					
						
							|  |  |  |     if (!PyArg_UnpackTuple(args, "bool", 0, 1, &x)) | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |         return NULL; | 
					
						
							|  |  |  |     ok = PyObject_IsTrue(x); | 
					
						
							|  |  |  |     if (ok < 0) | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     return PyBool_FromLong(ok); | 
					
						
							| 
									
										
										
										
											2002-04-03 23:01:45 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-29 01:16:21 +09:00
										 |  |  | static PyObject * | 
					
						
							|  |  |  | bool_vectorcall(PyObject *type, PyObject * const*args, | 
					
						
							|  |  |  |                 size_t nargsf, PyObject *kwnames) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     long ok = 0; | 
					
						
							|  |  |  |     if (!_PyArg_NoKwnames("bool", kwnames)) { | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); | 
					
						
							|  |  |  |     if (!_PyArg_CheckPositional("bool", nargs, 0, 1)) { | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     assert(PyType_Check(type)); | 
					
						
							|  |  |  |     if (nargs) { | 
					
						
							|  |  |  |         ok = PyObject_IsTrue(args[0]); | 
					
						
							| 
									
										
										
										
											2020-10-01 13:50:40 +09:00
										 |  |  |         if (ok < 0) { | 
					
						
							|  |  |  |             return NULL; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-09-29 01:16:21 +09:00
										 |  |  |     } | 
					
						
							|  |  |  |     return PyBool_FromLong(ok); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-04-03 23:01:45 +00:00
										 |  |  | /* Arithmetic operations redefined to return bool if both args are bool. */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							|  |  |  | bool_and(PyObject *a, PyObject *b) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     if (!PyBool_Check(a) || !PyBool_Check(b)) | 
					
						
							|  |  |  |         return PyLong_Type.tp_as_number->nb_and(a, b); | 
					
						
							|  |  |  |     return PyBool_FromLong((a == Py_True) & (b == Py_True)); | 
					
						
							| 
									
										
										
										
											2002-04-03 23:01:45 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							|  |  |  | bool_or(PyObject *a, PyObject *b) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     if (!PyBool_Check(a) || !PyBool_Check(b)) | 
					
						
							|  |  |  |         return PyLong_Type.tp_as_number->nb_or(a, b); | 
					
						
							|  |  |  |     return PyBool_FromLong((a == Py_True) | (b == Py_True)); | 
					
						
							| 
									
										
										
										
											2002-04-03 23:01:45 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							|  |  |  | bool_xor(PyObject *a, PyObject *b) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     if (!PyBool_Check(a) || !PyBool_Check(b)) | 
					
						
							|  |  |  |         return PyLong_Type.tp_as_number->nb_xor(a, b); | 
					
						
							|  |  |  |     return PyBool_FromLong((a == Py_True) ^ (b == Py_True)); | 
					
						
							| 
									
										
										
										
											2002-04-03 23:01:45 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Doc string */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-06-13 20:33:02 +00:00
										 |  |  | PyDoc_STRVAR(bool_doc, | 
					
						
							| 
									
										
										
										
											2002-04-03 23:01:45 +00:00
										 |  |  | "bool(x) -> bool\n\
 | 
					
						
							|  |  |  | \n\ | 
					
						
							|  |  |  | Returns True when the argument x is true, False otherwise.\n\ | 
					
						
							|  |  |  | The builtins True and False are the only two instances of the class bool.\n\ | 
					
						
							| 
									
										
										
										
											2002-06-13 20:33:02 +00:00
										 |  |  | The class bool is a subclass of the class int, and cannot be subclassed."); | 
					
						
							| 
									
										
										
										
											2002-04-03 23:01:45 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* Arithmetic methods -- only so we can override &, |, ^. */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyNumberMethods bool_as_number = { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     0,                          /* nb_add */ | 
					
						
							|  |  |  |     0,                          /* nb_subtract */ | 
					
						
							|  |  |  |     0,                          /* nb_multiply */ | 
					
						
							|  |  |  |     0,                          /* nb_remainder */ | 
					
						
							|  |  |  |     0,                          /* nb_divmod */ | 
					
						
							|  |  |  |     0,                          /* nb_power */ | 
					
						
							|  |  |  |     0,                          /* nb_negative */ | 
					
						
							|  |  |  |     0,                          /* nb_positive */ | 
					
						
							|  |  |  |     0,                          /* nb_absolute */ | 
					
						
							|  |  |  |     0,                          /* nb_bool */ | 
					
						
							|  |  |  |     0,                          /* nb_invert */ | 
					
						
							|  |  |  |     0,                          /* nb_lshift */ | 
					
						
							|  |  |  |     0,                          /* nb_rshift */ | 
					
						
							|  |  |  |     bool_and,                   /* nb_and */ | 
					
						
							|  |  |  |     bool_xor,                   /* nb_xor */ | 
					
						
							|  |  |  |     bool_or,                    /* nb_or */ | 
					
						
							|  |  |  |     0,                          /* nb_int */ | 
					
						
							|  |  |  |     0,                          /* nb_reserved */ | 
					
						
							|  |  |  |     0,                          /* nb_float */ | 
					
						
							|  |  |  |     0,                          /* nb_inplace_add */ | 
					
						
							|  |  |  |     0,                          /* nb_inplace_subtract */ | 
					
						
							|  |  |  |     0,                          /* nb_inplace_multiply */ | 
					
						
							|  |  |  |     0,                          /* nb_inplace_remainder */ | 
					
						
							|  |  |  |     0,                          /* nb_inplace_power */ | 
					
						
							|  |  |  |     0,                          /* nb_inplace_lshift */ | 
					
						
							|  |  |  |     0,                          /* nb_inplace_rshift */ | 
					
						
							|  |  |  |     0,                          /* nb_inplace_and */ | 
					
						
							|  |  |  |     0,                          /* nb_inplace_xor */ | 
					
						
							|  |  |  |     0,                          /* nb_inplace_or */ | 
					
						
							|  |  |  |     0,                          /* nb_floor_divide */ | 
					
						
							|  |  |  |     0,                          /* nb_true_divide */ | 
					
						
							|  |  |  |     0,                          /* nb_inplace_floor_divide */ | 
					
						
							|  |  |  |     0,                          /* nb_inplace_true_divide */ | 
					
						
							|  |  |  |     0,                          /* nb_index */ | 
					
						
							| 
									
										
										
										
											2002-04-03 23:01:45 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-31 18:05:15 +02:00
										 |  |  | static void _Py_NO_RETURN | 
					
						
							|  |  |  | bool_dealloc(PyObject* Py_UNUSED(ignore)) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2021-09-21 23:04:34 +02:00
										 |  |  |     _Py_FatalRefcountError("deallocating True or False"); | 
					
						
							| 
									
										
										
										
											2021-08-31 18:05:15 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-04-03 23:01:45 +00:00
										 |  |  | /* The type object for bool.  Note that this cannot be subclassed! */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PyTypeObject PyBool_Type = { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     PyVarObject_HEAD_INIT(&PyType_Type, 0) | 
					
						
							|  |  |  |     "bool", | 
					
						
							|  |  |  |     sizeof(struct _longobject), | 
					
						
							|  |  |  |     0, | 
					
						
							| 
									
										
										
										
											2021-08-31 18:05:15 +02:00
										 |  |  |     bool_dealloc,                               /* tp_dealloc */ | 
					
						
							| 
									
										
										
										
											2019-05-31 04:13:39 +02:00
										 |  |  |     0,                                          /* tp_vectorcall_offset */ | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     0,                                          /* tp_getattr */ | 
					
						
							|  |  |  |     0,                                          /* tp_setattr */ | 
					
						
							| 
									
										
										
										
											2019-05-31 04:13:39 +02:00
										 |  |  |     0,                                          /* tp_as_async */ | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     bool_repr,                                  /* tp_repr */ | 
					
						
							|  |  |  |     &bool_as_number,                            /* tp_as_number */ | 
					
						
							|  |  |  |     0,                                          /* tp_as_sequence */ | 
					
						
							|  |  |  |     0,                                          /* tp_as_mapping */ | 
					
						
							|  |  |  |     0,                                          /* tp_hash */ | 
					
						
							|  |  |  |     0,                                          /* tp_call */ | 
					
						
							| 
									
										
										
										
											2019-05-06 22:29:40 +03:00
										 |  |  |     0,                                          /* tp_str */ | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     0,                                          /* tp_getattro */ | 
					
						
							|  |  |  |     0,                                          /* tp_setattro */ | 
					
						
							|  |  |  |     0,                                          /* tp_as_buffer */ | 
					
						
							|  |  |  |     Py_TPFLAGS_DEFAULT,                         /* tp_flags */ | 
					
						
							|  |  |  |     bool_doc,                                   /* tp_doc */ | 
					
						
							|  |  |  |     0,                                          /* tp_traverse */ | 
					
						
							|  |  |  |     0,                                          /* tp_clear */ | 
					
						
							|  |  |  |     0,                                          /* tp_richcompare */ | 
					
						
							|  |  |  |     0,                                          /* tp_weaklistoffset */ | 
					
						
							|  |  |  |     0,                                          /* tp_iter */ | 
					
						
							|  |  |  |     0,                                          /* tp_iternext */ | 
					
						
							|  |  |  |     0,                                          /* tp_methods */ | 
					
						
							|  |  |  |     0,                                          /* tp_members */ | 
					
						
							|  |  |  |     0,                                          /* tp_getset */ | 
					
						
							|  |  |  |     &PyLong_Type,                               /* tp_base */ | 
					
						
							|  |  |  |     0,                                          /* tp_dict */ | 
					
						
							|  |  |  |     0,                                          /* tp_descr_get */ | 
					
						
							|  |  |  |     0,                                          /* tp_descr_set */ | 
					
						
							|  |  |  |     0,                                          /* tp_dictoffset */ | 
					
						
							|  |  |  |     0,                                          /* tp_init */ | 
					
						
							|  |  |  |     0,                                          /* tp_alloc */ | 
					
						
							|  |  |  |     bool_new,                                   /* tp_new */ | 
					
						
							| 
									
										
										
										
											2020-09-29 01:16:21 +09:00
										 |  |  |     .tp_vectorcall = bool_vectorcall, | 
					
						
							| 
									
										
										
										
											2002-04-03 23:01:45 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* The objects representing bool values False and True */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-01-14 03:31:43 +00:00
										 |  |  | struct _longobject _Py_FalseStruct = { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     PyVarObject_HEAD_INIT(&PyBool_Type, 0) | 
					
						
							|  |  |  |     { 0 } | 
					
						
							| 
									
										
										
										
											2002-04-03 23:01:45 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2007-01-14 03:31:43 +00:00
										 |  |  | struct _longobject _Py_TrueStruct = { | 
					
						
							| 
									
										
										
										
											2010-05-09 15:52:27 +00:00
										 |  |  |     PyVarObject_HEAD_INIT(&PyBool_Type, 1) | 
					
						
							|  |  |  |     { 1 } | 
					
						
							| 
									
										
										
										
											2002-04-03 23:01:45 +00:00
										 |  |  | }; |