| 1 | n/a | /* Boolean type, a subtype of int */ |
|---|
| 2 | n/a | |
|---|
| 3 | n/a | #include "Python.h" |
|---|
| 4 | n/a | #include "longintrepr.h" |
|---|
| 5 | n/a | |
|---|
| 6 | n/a | /* We define bool_repr to return "False" or "True" */ |
|---|
| 7 | n/a | |
|---|
| 8 | n/a | static PyObject *false_str = NULL; |
|---|
| 9 | n/a | static PyObject *true_str = NULL; |
|---|
| 10 | n/a | |
|---|
| 11 | n/a | static PyObject * |
|---|
| 12 | n/a | bool_repr(PyObject *self) |
|---|
| 13 | n/a | { |
|---|
| 14 | n/a | PyObject *s; |
|---|
| 15 | n/a | |
|---|
| 16 | n/a | if (self == Py_True) |
|---|
| 17 | n/a | s = true_str ? true_str : |
|---|
| 18 | n/a | (true_str = PyUnicode_InternFromString("True")); |
|---|
| 19 | n/a | else |
|---|
| 20 | n/a | s = false_str ? false_str : |
|---|
| 21 | n/a | (false_str = PyUnicode_InternFromString("False")); |
|---|
| 22 | n/a | Py_XINCREF(s); |
|---|
| 23 | n/a | return s; |
|---|
| 24 | n/a | } |
|---|
| 25 | n/a | |
|---|
| 26 | n/a | /* Function to return a bool from a C long */ |
|---|
| 27 | n/a | |
|---|
| 28 | n/a | PyObject *PyBool_FromLong(long ok) |
|---|
| 29 | n/a | { |
|---|
| 30 | n/a | PyObject *result; |
|---|
| 31 | n/a | |
|---|
| 32 | n/a | if (ok) |
|---|
| 33 | n/a | result = Py_True; |
|---|
| 34 | n/a | else |
|---|
| 35 | n/a | result = Py_False; |
|---|
| 36 | n/a | Py_INCREF(result); |
|---|
| 37 | n/a | return result; |
|---|
| 38 | n/a | } |
|---|
| 39 | n/a | |
|---|
| 40 | n/a | /* We define bool_new to always return either Py_True or Py_False */ |
|---|
| 41 | n/a | |
|---|
| 42 | n/a | static PyObject * |
|---|
| 43 | n/a | bool_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
|---|
| 44 | n/a | { |
|---|
| 45 | n/a | static char *kwlist[] = {"x", 0}; |
|---|
| 46 | n/a | PyObject *x = Py_False; |
|---|
| 47 | n/a | long ok; |
|---|
| 48 | n/a | |
|---|
| 49 | n/a | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:bool", kwlist, &x)) |
|---|
| 50 | n/a | return NULL; |
|---|
| 51 | n/a | ok = PyObject_IsTrue(x); |
|---|
| 52 | n/a | if (ok < 0) |
|---|
| 53 | n/a | return NULL; |
|---|
| 54 | n/a | return PyBool_FromLong(ok); |
|---|
| 55 | n/a | } |
|---|
| 56 | n/a | |
|---|
| 57 | n/a | /* Arithmetic operations redefined to return bool if both args are bool. */ |
|---|
| 58 | n/a | |
|---|
| 59 | n/a | static PyObject * |
|---|
| 60 | n/a | bool_and(PyObject *a, PyObject *b) |
|---|
| 61 | n/a | { |
|---|
| 62 | n/a | if (!PyBool_Check(a) || !PyBool_Check(b)) |
|---|
| 63 | n/a | return PyLong_Type.tp_as_number->nb_and(a, b); |
|---|
| 64 | n/a | return PyBool_FromLong((a == Py_True) & (b == Py_True)); |
|---|
| 65 | n/a | } |
|---|
| 66 | n/a | |
|---|
| 67 | n/a | static PyObject * |
|---|
| 68 | n/a | bool_or(PyObject *a, PyObject *b) |
|---|
| 69 | n/a | { |
|---|
| 70 | n/a | if (!PyBool_Check(a) || !PyBool_Check(b)) |
|---|
| 71 | n/a | return PyLong_Type.tp_as_number->nb_or(a, b); |
|---|
| 72 | n/a | return PyBool_FromLong((a == Py_True) | (b == Py_True)); |
|---|
| 73 | n/a | } |
|---|
| 74 | n/a | |
|---|
| 75 | n/a | static PyObject * |
|---|
| 76 | n/a | bool_xor(PyObject *a, PyObject *b) |
|---|
| 77 | n/a | { |
|---|
| 78 | n/a | if (!PyBool_Check(a) || !PyBool_Check(b)) |
|---|
| 79 | n/a | return PyLong_Type.tp_as_number->nb_xor(a, b); |
|---|
| 80 | n/a | return PyBool_FromLong((a == Py_True) ^ (b == Py_True)); |
|---|
| 81 | n/a | } |
|---|
| 82 | n/a | |
|---|
| 83 | n/a | /* Doc string */ |
|---|
| 84 | n/a | |
|---|
| 85 | n/a | PyDoc_STRVAR(bool_doc, |
|---|
| 86 | n/a | "bool(x) -> bool\n\ |
|---|
| 87 | n/a | \n\ |
|---|
| 88 | n/a | Returns True when the argument x is true, False otherwise.\n\ |
|---|
| 89 | n/a | The builtins True and False are the only two instances of the class bool.\n\ |
|---|
| 90 | n/a | The class bool is a subclass of the class int, and cannot be subclassed."); |
|---|
| 91 | n/a | |
|---|
| 92 | n/a | /* Arithmetic methods -- only so we can override &, |, ^. */ |
|---|
| 93 | n/a | |
|---|
| 94 | n/a | static PyNumberMethods bool_as_number = { |
|---|
| 95 | n/a | 0, /* nb_add */ |
|---|
| 96 | n/a | 0, /* nb_subtract */ |
|---|
| 97 | n/a | 0, /* nb_multiply */ |
|---|
| 98 | n/a | 0, /* nb_remainder */ |
|---|
| 99 | n/a | 0, /* nb_divmod */ |
|---|
| 100 | n/a | 0, /* nb_power */ |
|---|
| 101 | n/a | 0, /* nb_negative */ |
|---|
| 102 | n/a | 0, /* nb_positive */ |
|---|
| 103 | n/a | 0, /* nb_absolute */ |
|---|
| 104 | n/a | 0, /* nb_bool */ |
|---|
| 105 | n/a | 0, /* nb_invert */ |
|---|
| 106 | n/a | 0, /* nb_lshift */ |
|---|
| 107 | n/a | 0, /* nb_rshift */ |
|---|
| 108 | n/a | bool_and, /* nb_and */ |
|---|
| 109 | n/a | bool_xor, /* nb_xor */ |
|---|
| 110 | n/a | bool_or, /* nb_or */ |
|---|
| 111 | n/a | 0, /* nb_int */ |
|---|
| 112 | n/a | 0, /* nb_reserved */ |
|---|
| 113 | n/a | 0, /* nb_float */ |
|---|
| 114 | n/a | 0, /* nb_inplace_add */ |
|---|
| 115 | n/a | 0, /* nb_inplace_subtract */ |
|---|
| 116 | n/a | 0, /* nb_inplace_multiply */ |
|---|
| 117 | n/a | 0, /* nb_inplace_remainder */ |
|---|
| 118 | n/a | 0, /* nb_inplace_power */ |
|---|
| 119 | n/a | 0, /* nb_inplace_lshift */ |
|---|
| 120 | n/a | 0, /* nb_inplace_rshift */ |
|---|
| 121 | n/a | 0, /* nb_inplace_and */ |
|---|
| 122 | n/a | 0, /* nb_inplace_xor */ |
|---|
| 123 | n/a | 0, /* nb_inplace_or */ |
|---|
| 124 | n/a | 0, /* nb_floor_divide */ |
|---|
| 125 | n/a | 0, /* nb_true_divide */ |
|---|
| 126 | n/a | 0, /* nb_inplace_floor_divide */ |
|---|
| 127 | n/a | 0, /* nb_inplace_true_divide */ |
|---|
| 128 | n/a | 0, /* nb_index */ |
|---|
| 129 | n/a | }; |
|---|
| 130 | n/a | |
|---|
| 131 | n/a | /* The type object for bool. Note that this cannot be subclassed! */ |
|---|
| 132 | n/a | |
|---|
| 133 | n/a | PyTypeObject PyBool_Type = { |
|---|
| 134 | n/a | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
|---|
| 135 | n/a | "bool", |
|---|
| 136 | n/a | sizeof(struct _longobject), |
|---|
| 137 | n/a | 0, |
|---|
| 138 | n/a | 0, /* tp_dealloc */ |
|---|
| 139 | n/a | 0, /* tp_print */ |
|---|
| 140 | n/a | 0, /* tp_getattr */ |
|---|
| 141 | n/a | 0, /* tp_setattr */ |
|---|
| 142 | n/a | 0, /* tp_reserved */ |
|---|
| 143 | n/a | bool_repr, /* tp_repr */ |
|---|
| 144 | n/a | &bool_as_number, /* tp_as_number */ |
|---|
| 145 | n/a | 0, /* tp_as_sequence */ |
|---|
| 146 | n/a | 0, /* tp_as_mapping */ |
|---|
| 147 | n/a | 0, /* tp_hash */ |
|---|
| 148 | n/a | 0, /* tp_call */ |
|---|
| 149 | n/a | bool_repr, /* tp_str */ |
|---|
| 150 | n/a | 0, /* tp_getattro */ |
|---|
| 151 | n/a | 0, /* tp_setattro */ |
|---|
| 152 | n/a | 0, /* tp_as_buffer */ |
|---|
| 153 | n/a | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
|---|
| 154 | n/a | bool_doc, /* tp_doc */ |
|---|
| 155 | n/a | 0, /* tp_traverse */ |
|---|
| 156 | n/a | 0, /* tp_clear */ |
|---|
| 157 | n/a | 0, /* tp_richcompare */ |
|---|
| 158 | n/a | 0, /* tp_weaklistoffset */ |
|---|
| 159 | n/a | 0, /* tp_iter */ |
|---|
| 160 | n/a | 0, /* tp_iternext */ |
|---|
| 161 | n/a | 0, /* tp_methods */ |
|---|
| 162 | n/a | 0, /* tp_members */ |
|---|
| 163 | n/a | 0, /* tp_getset */ |
|---|
| 164 | n/a | &PyLong_Type, /* tp_base */ |
|---|
| 165 | n/a | 0, /* tp_dict */ |
|---|
| 166 | n/a | 0, /* tp_descr_get */ |
|---|
| 167 | n/a | 0, /* tp_descr_set */ |
|---|
| 168 | n/a | 0, /* tp_dictoffset */ |
|---|
| 169 | n/a | 0, /* tp_init */ |
|---|
| 170 | n/a | 0, /* tp_alloc */ |
|---|
| 171 | n/a | bool_new, /* tp_new */ |
|---|
| 172 | n/a | }; |
|---|
| 173 | n/a | |
|---|
| 174 | n/a | /* The objects representing bool values False and True */ |
|---|
| 175 | n/a | |
|---|
| 176 | n/a | struct _longobject _Py_FalseStruct = { |
|---|
| 177 | n/a | PyVarObject_HEAD_INIT(&PyBool_Type, 0) |
|---|
| 178 | n/a | { 0 } |
|---|
| 179 | n/a | }; |
|---|
| 180 | n/a | |
|---|
| 181 | n/a | struct _longobject _Py_TrueStruct = { |
|---|
| 182 | n/a | PyVarObject_HEAD_INIT(&PyBool_Type, 1) |
|---|
| 183 | n/a | { 1 } |
|---|
| 184 | n/a | }; |
|---|