Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 1 | /* Boolean type, a subtype of int */ |
| 2 | |
| 3 | #include "Python.h" |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 4 | #include "longintrepr.h" |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 5 | |
| 6 | /* We need to define bool_print to override int_print */ |
| 7 | |
| 8 | static int |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 9 | bool_print(PyObject *self, FILE *fp, int flags) |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 10 | { |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 11 | fputs(self == Py_False ? "False" : "True", fp); |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 12 | return 0; |
| 13 | } |
| 14 | |
| 15 | /* We define bool_repr to return "False" or "True" */ |
| 16 | |
| 17 | static PyObject *false_str = NULL; |
| 18 | static PyObject *true_str = NULL; |
| 19 | |
Neal Norwitz | 657d222 | 2002-08-06 22:12:52 +0000 | [diff] [blame] | 20 | static PyObject * |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 21 | bool_repr(PyObject *self) |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 22 | { |
| 23 | PyObject *s; |
| 24 | |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 25 | if (self == Py_True) |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 26 | 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 | |
| 37 | PyObject *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 Norwitz | 657d222 | 2002-08-06 22:12:52 +0000 | [diff] [blame] | 51 | static PyObject * |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 52 | bool_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 53 | { |
Martin v. Löwis | 15e6274 | 2006-02-27 16:46:16 +0000 | [diff] [blame] | 54 | static char *kwlist[] = {"x", 0}; |
Guido van Rossum | aa86e35 | 2003-04-19 18:15:10 +0000 | [diff] [blame] | 55 | PyObject *x = Py_False; |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 56 | long ok; |
| 57 | |
Guido van Rossum | aa86e35 | 2003-04-19 18:15:10 +0000 | [diff] [blame] | 58 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:bool", kwlist, &x)) |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 59 | 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 | |
| 68 | static PyObject * |
| 69 | bool_and(PyObject *a, PyObject *b) |
| 70 | { |
| 71 | if (!PyBool_Check(a) || !PyBool_Check(b)) |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 72 | return PyLong_Type.tp_as_number->nb_and(a, b); |
| 73 | return PyBool_FromLong((a == Py_True) & (b == Py_True)); |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | static PyObject * |
| 77 | bool_or(PyObject *a, PyObject *b) |
| 78 | { |
| 79 | if (!PyBool_Check(a) || !PyBool_Check(b)) |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 80 | return PyLong_Type.tp_as_number->nb_or(a, b); |
| 81 | return PyBool_FromLong((a == Py_True) | (b == Py_True)); |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | static PyObject * |
| 85 | bool_xor(PyObject *a, PyObject *b) |
| 86 | { |
| 87 | if (!PyBool_Check(a) || !PyBool_Check(b)) |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 88 | return PyLong_Type.tp_as_number->nb_xor(a, b); |
| 89 | return PyBool_FromLong((a == Py_True) ^ (b == Py_True)); |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | /* Doc string */ |
| 93 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 94 | PyDoc_STRVAR(bool_doc, |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 95 | "bool(x) -> bool\n\ |
| 96 | \n\ |
| 97 | Returns True when the argument x is true, False otherwise.\n\ |
| 98 | The builtins True and False are the only two instances of the class bool.\n\ |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 99 | The class bool is a subclass of the class int, and cannot be subclassed."); |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 100 | |
| 101 | /* Arithmetic methods -- only so we can override &, |, ^. */ |
| 102 | |
| 103 | static PyNumberMethods bool_as_number = { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 104 | 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 Diederich | 4dafcc4 | 2006-11-28 19:15:13 +0000 | [diff] [blame] | 113 | 0, /* nb_bool */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 114 | 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 Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 140 | 0, /* nb_index */ |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 141 | }; |
| 142 | |
| 143 | /* The type object for bool. Note that this cannot be subclassed! */ |
| 144 | |
| 145 | PyTypeObject PyBool_Type = { |
| 146 | PyObject_HEAD_INIT(&PyType_Type) |
| 147 | 0, |
| 148 | "bool", |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 149 | sizeof(struct _longobject), |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 150 | 0, |
| 151 | 0, /* tp_dealloc */ |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 152 | bool_print, /* tp_print */ |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 153 | 0, /* tp_getattr */ |
| 154 | 0, /* tp_setattr */ |
| 155 | 0, /* tp_compare */ |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 156 | bool_repr, /* tp_repr */ |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 157 | &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 Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 162 | bool_repr, /* tp_str */ |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 163 | 0, /* tp_getattro */ |
| 164 | 0, /* tp_setattro */ |
| 165 | 0, /* tp_as_buffer */ |
Guido van Rossum | 3cf5b1e | 2006-07-27 21:53:35 +0000 | [diff] [blame] | 166 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 167 | 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 Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 177 | &PyLong_Type, /* tp_base */ |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 178 | 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 Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 190 | struct _longobject _Py_FalseStruct = { |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 191 | PyObject_HEAD_INIT(&PyBool_Type) |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 192 | 0, { 0 } |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 193 | }; |
| 194 | |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 195 | struct _longobject _Py_TrueStruct = { |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 196 | PyObject_HEAD_INIT(&PyBool_Type) |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 197 | 1, { 1 } |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 198 | }; |