| 1 |
|
| 2 |
|
| 3 |
#include "Python.h" |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
static int |
| 8 |
bool_print(PyBoolObject *self, FILE *fp, int flags) |
| 9 |
{ |
| 10 |
Py_BEGIN_ALLOW_THREADS |
| 11 |
fputs(self->ob_ival == 0 ? "False" : "True", fp); |
| 12 |
Py_END_ALLOW_THREADS |
| 13 |
return 0; |
| 14 |
} |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
static PyObject *false_str = NULL; |
| 19 |
static PyObject *true_str = NULL; |
| 20 |
|
| 21 |
static PyObject * |
| 22 |
bool_repr(PyBoolObject *self) |
| 23 |
{ |
| 24 |
PyObject *s; |
| 25 |
|
| 26 |
if (self->ob_ival) |
| 27 |
s = true_str ? true_str : |
| 28 |
(true_str = PyString_InternFromString("True")); |
| 29 |
else |
| 30 |
s = false_str ? false_str : |
| 31 |
(false_str = PyString_InternFromString("False")); |
| 32 |
Py_XINCREF(s); |
| 33 |
return s; |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
PyObject *PyBool_FromLong(long ok) |
| 39 |
{ |
| 40 |
PyObject *result; |
| 41 |
|
| 42 |
if (ok) |
| 43 |
result = Py_True; |
| 44 |
else |
| 45 |
result = Py_False; |
| 46 |
Py_INCREF(result); |
| 47 |
return result; |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
static PyObject * |
| 53 |
bool_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 54 |
{ |
| 55 |
static char *kwlist[] = {"x", 0}; |
| 56 |
PyObject *x = Py_False; |
| 57 |
long ok; |
| 58 |
|
| 59 |
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:bool", kwlist, &x)) |
| 60 |
return NULL; |
| 61 |
ok = PyObject_IsTrue(x); |
| 62 |
if (ok < 0) |
| 63 |
return NULL; |
| 64 |
return PyBool_FromLong(ok); |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
static PyObject * |
| 70 |
bool_and(PyObject *a, PyObject *b) |
| 71 |
{ |
| 72 |
if (!PyBool_Check(a) || !PyBool_Check(b)) |
| 73 |
return PyInt_Type.tp_as_number->nb_and(a, b); |
| 74 |
return PyBool_FromLong( |
| 75 |
((PyBoolObject *)a)->ob_ival & ((PyBoolObject *)b)->ob_ival); |
| 76 |
} |
| 77 |
|
| 78 |
static PyObject * |
| 79 |
bool_or(PyObject *a, PyObject *b) |
| 80 |
{ |
| 81 |
if (!PyBool_Check(a) || !PyBool_Check(b)) |
| 82 |
return PyInt_Type.tp_as_number->nb_or(a, b); |
| 83 |
return PyBool_FromLong( |
| 84 |
((PyBoolObject *)a)->ob_ival | ((PyBoolObject *)b)->ob_ival); |
| 85 |
} |
| 86 |
|
| 87 |
static PyObject * |
| 88 |
bool_xor(PyObject *a, PyObject *b) |
| 89 |
{ |
| 90 |
if (!PyBool_Check(a) || !PyBool_Check(b)) |
| 91 |
return PyInt_Type.tp_as_number->nb_xor(a, b); |
| 92 |
return PyBool_FromLong( |
| 93 |
((PyBoolObject *)a)->ob_ival ^ ((PyBoolObject *)b)->ob_ival); |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
PyDoc_STRVAR(bool_doc, |
| 99 |
"bool(x) -> bool\n\ |
| 100 |
\n\ |
| 101 |
Returns True when the argument x is true, False otherwise.\n\ |
| 102 |
The builtins True and False are the only two instances of the class bool.\n\ |
| 103 |
The class bool is a subclass of the class int, and cannot be subclassed."); |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
static PyNumberMethods bool_as_number = { |
| 108 |
0, |
| 109 |
0, |
| 110 |
0, |
| 111 |
0, |
| 112 |
0, |
| 113 |
0, |
| 114 |
0, |
| 115 |
0, |
| 116 |
0, |
| 117 |
0, |
| 118 |
0, |
| 119 |
0, |
| 120 |
0, |
| 121 |
0, |
| 122 |
bool_and, |
| 123 |
bool_xor, |
| 124 |
bool_or, |
| 125 |
0, |
| 126 |
0, |
| 127 |
0, |
| 128 |
0, |
| 129 |
0, |
| 130 |
0, |
| 131 |
0, |
| 132 |
0, |
| 133 |
0, |
| 134 |
0, |
| 135 |
0, |
| 136 |
0, |
| 137 |
0, |
| 138 |
0, |
| 139 |
0, |
| 140 |
0, |
| 141 |
0, |
| 142 |
0, |
| 143 |
0, |
| 144 |
0, |
| 145 |
0, |
| 146 |
}; |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
PyTypeObject PyBool_Type = { |
| 151 |
PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 152 |
"bool", |
| 153 |
sizeof(PyIntObject), |
| 154 |
0, |
| 155 |
0, |
| 156 |
(printfunc)bool_print, |
| 157 |
0, |
| 158 |
0, |
| 159 |
0, |
| 160 |
(reprfunc)bool_repr, |
| 161 |
&bool_as_number, |
| 162 |
0, |
| 163 |
0, |
| 164 |
0, |
| 165 |
0, |
| 166 |
(reprfunc)bool_repr, |
| 167 |
0, |
| 168 |
0, |
| 169 |
0, |
| 170 |
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES, |
| 171 |
bool_doc, |
| 172 |
0, |
| 173 |
0, |
| 174 |
0, |
| 175 |
0, |
| 176 |
0, |
| 177 |
0, |
| 178 |
0, |
| 179 |
0, |
| 180 |
0, |
| 181 |
&PyInt_Type, |
| 182 |
0, |
| 183 |
0, |
| 184 |
0, |
| 185 |
0, |
| 186 |
0, |
| 187 |
0, |
| 188 |
bool_new, |
| 189 |
}; |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
PyIntObject _Py_ZeroStruct = { |
| 195 |
PyObject_HEAD_INIT(&PyBool_Type) |
| 196 |
0 |
| 197 |
}; |
| 198 |
|
| 199 |
PyIntObject _Py_TrueStruct = { |
| 200 |
PyObject_HEAD_INIT(&PyBool_Type) |
| 201 |
1 |
| 202 |
}; |