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 | |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 6 | /* We define bool_repr to return "False" or "True" */ |
| 7 | |
| 8 | static PyObject *false_str = NULL; |
| 9 | static PyObject *true_str = NULL; |
| 10 | |
Neal Norwitz | 657d222 | 2002-08-06 22:12:52 +0000 | [diff] [blame] | 11 | static PyObject * |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 12 | bool_repr(PyObject *self) |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 13 | { |
| 14 | PyObject *s; |
| 15 | |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 16 | if (self == Py_True) |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 17 | s = true_str ? true_str : |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 18 | (true_str = PyUnicode_InternFromString("True")); |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 19 | else |
| 20 | s = false_str ? false_str : |
Walter Dörwald | 1680713 | 2007-05-25 13:52:07 +0000 | [diff] [blame] | 21 | (false_str = PyUnicode_InternFromString("False")); |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 22 | Py_XINCREF(s); |
| 23 | return s; |
| 24 | } |
| 25 | |
| 26 | /* Function to return a bool from a C long */ |
| 27 | |
| 28 | PyObject *PyBool_FromLong(long ok) |
| 29 | { |
| 30 | PyObject *result; |
| 31 | |
| 32 | if (ok) |
| 33 | result = Py_True; |
| 34 | else |
| 35 | result = Py_False; |
| 36 | Py_INCREF(result); |
| 37 | return result; |
| 38 | } |
| 39 | |
| 40 | /* We define bool_new to always return either Py_True or Py_False */ |
| 41 | |
Neal Norwitz | 657d222 | 2002-08-06 22:12:52 +0000 | [diff] [blame] | 42 | static PyObject * |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 43 | bool_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 44 | { |
Martin v. Löwis | 15e6274 | 2006-02-27 16:46:16 +0000 | [diff] [blame] | 45 | static char *kwlist[] = {"x", 0}; |
Guido van Rossum | aa86e35 | 2003-04-19 18:15:10 +0000 | [diff] [blame] | 46 | PyObject *x = Py_False; |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 47 | long ok; |
| 48 | |
Guido van Rossum | aa86e35 | 2003-04-19 18:15:10 +0000 | [diff] [blame] | 49 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:bool", kwlist, &x)) |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 50 | return NULL; |
| 51 | ok = PyObject_IsTrue(x); |
| 52 | if (ok < 0) |
| 53 | return NULL; |
| 54 | return PyBool_FromLong(ok); |
| 55 | } |
| 56 | |
| 57 | /* Arithmetic operations redefined to return bool if both args are bool. */ |
| 58 | |
| 59 | static PyObject * |
| 60 | bool_and(PyObject *a, PyObject *b) |
| 61 | { |
| 62 | if (!PyBool_Check(a) || !PyBool_Check(b)) |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 63 | return PyLong_Type.tp_as_number->nb_and(a, b); |
| 64 | return PyBool_FromLong((a == Py_True) & (b == Py_True)); |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | static PyObject * |
| 68 | bool_or(PyObject *a, PyObject *b) |
| 69 | { |
| 70 | if (!PyBool_Check(a) || !PyBool_Check(b)) |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 71 | return PyLong_Type.tp_as_number->nb_or(a, b); |
| 72 | return PyBool_FromLong((a == Py_True) | (b == Py_True)); |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | static PyObject * |
| 76 | bool_xor(PyObject *a, PyObject *b) |
| 77 | { |
| 78 | if (!PyBool_Check(a) || !PyBool_Check(b)) |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 79 | return PyLong_Type.tp_as_number->nb_xor(a, b); |
| 80 | return PyBool_FromLong((a == Py_True) ^ (b == Py_True)); |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | /* Doc string */ |
| 84 | |
Martin v. Löwis | 14f8b4c | 2002-06-13 20:33:02 +0000 | [diff] [blame] | 85 | PyDoc_STRVAR(bool_doc, |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 86 | "bool(x) -> bool\n\ |
| 87 | \n\ |
| 88 | Returns True when the argument x is true, False otherwise.\n\ |
| 89 | 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] | 90 | 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] | 91 | |
| 92 | /* Arithmetic methods -- only so we can override &, |, ^. */ |
| 93 | |
| 94 | static PyNumberMethods bool_as_number = { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 95 | 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 */ |
Jack Diederich | 4dafcc4 | 2006-11-28 19:15:13 +0000 | [diff] [blame] | 104 | 0, /* nb_bool */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 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 */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 111 | 0, /* nb_int */ |
Mark Dickinson | 8055afd | 2009-01-17 10:04:45 +0000 | [diff] [blame] | 112 | 0, /* nb_reserved */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 113 | 0, /* nb_float */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 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 */ |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 128 | 0, /* nb_index */ |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 129 | }; |
| 130 | |
| 131 | /* The type object for bool. Note that this cannot be subclassed! */ |
| 132 | |
| 133 | PyTypeObject PyBool_Type = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 134 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 135 | "bool", |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 136 | sizeof(struct _longobject), |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 137 | 0, |
| 138 | 0, /* tp_dealloc */ |
Guido van Rossum | 04dbf3b | 2007-08-07 19:51:00 +0000 | [diff] [blame] | 139 | 0, /* tp_print */ |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 140 | 0, /* tp_getattr */ |
| 141 | 0, /* tp_setattr */ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 142 | 0, /* tp_reserved */ |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 143 | bool_repr, /* tp_repr */ |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 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 */ |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 149 | bool_repr, /* tp_str */ |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 150 | 0, /* tp_getattro */ |
| 151 | 0, /* tp_setattro */ |
| 152 | 0, /* tp_as_buffer */ |
Guido van Rossum | 3cf5b1e | 2006-07-27 21:53:35 +0000 | [diff] [blame] | 153 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 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 */ |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 164 | &PyLong_Type, /* tp_base */ |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 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 */ |
| 172 | }; |
| 173 | |
| 174 | /* The objects representing bool values False and True */ |
| 175 | |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 176 | struct _longobject _Py_FalseStruct = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 177 | PyVarObject_HEAD_INIT(&PyBool_Type, 0) |
| 178 | { 0 } |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 179 | }; |
| 180 | |
Guido van Rossum | ddefaf3 | 2007-01-14 03:31:43 +0000 | [diff] [blame] | 181 | struct _longobject _Py_TrueStruct = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 182 | PyVarObject_HEAD_INIT(&PyBool_Type, 1) |
| 183 | { 1 } |
Guido van Rossum | 5f82036 | 2002-04-03 23:01:45 +0000 | [diff] [blame] | 184 | }; |