Line data Source code
1 : /* Boolean type, a subtype of int */
2 :
3 : #include "Python.h"
4 : #include "longintrepr.h"
5 :
6 : /* We define bool_repr to return "False" or "True" */
7 :
8 : static PyObject *false_str = NULL;
9 : static PyObject *true_str = NULL;
10 :
11 : static PyObject *
12 0 : bool_repr(PyObject *self)
13 : {
14 : PyObject *s;
15 :
16 0 : if (self == Py_True)
17 0 : s = true_str ? true_str :
18 0 : (true_str = PyUnicode_InternFromString("True"));
19 : else
20 0 : s = false_str ? false_str :
21 0 : (false_str = PyUnicode_InternFromString("False"));
22 0 : Py_XINCREF(s);
23 0 : return s;
24 : }
25 :
26 : /* Function to return a bool from a C long */
27 :
28 4936 : PyObject *PyBool_FromLong(long ok)
29 : {
30 : PyObject *result;
31 :
32 4936 : if (ok)
33 1509 : result = Py_True;
34 : else
35 3427 : result = Py_False;
36 4936 : Py_INCREF(result);
37 4936 : return result;
38 : }
39 :
40 : /* We define bool_new to always return either Py_True or Py_False */
41 :
42 : static PyObject *
43 0 : bool_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
44 : {
45 : static char *kwlist[] = {"x", 0};
46 0 : PyObject *x = Py_False;
47 : long ok;
48 :
49 0 : if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:bool", kwlist, &x))
50 0 : return NULL;
51 0 : ok = PyObject_IsTrue(x);
52 0 : if (ok < 0)
53 0 : return NULL;
54 0 : return PyBool_FromLong(ok);
55 : }
56 :
57 : /* Arithmetic operations redefined to return bool if both args are bool. */
58 :
59 : static PyObject *
60 0 : bool_and(PyObject *a, PyObject *b)
61 : {
62 0 : if (!PyBool_Check(a) || !PyBool_Check(b))
63 0 : return PyLong_Type.tp_as_number->nb_and(a, b);
64 0 : return PyBool_FromLong((a == Py_True) & (b == Py_True));
65 : }
66 :
67 : static PyObject *
68 0 : bool_or(PyObject *a, PyObject *b)
69 : {
70 0 : if (!PyBool_Check(a) || !PyBool_Check(b))
71 0 : return PyLong_Type.tp_as_number->nb_or(a, b);
72 0 : return PyBool_FromLong((a == Py_True) | (b == Py_True));
73 : }
74 :
75 : static PyObject *
76 0 : bool_xor(PyObject *a, PyObject *b)
77 : {
78 0 : if (!PyBool_Check(a) || !PyBool_Check(b))
79 0 : return PyLong_Type.tp_as_number->nb_xor(a, b);
80 0 : return PyBool_FromLong((a == Py_True) ^ (b == Py_True));
81 : }
82 :
83 : /* Doc string */
84 :
85 : PyDoc_STRVAR(bool_doc,
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\
90 : The class bool is a subclass of the class int, and cannot be subclassed.");
91 :
92 : /* Arithmetic methods -- only so we can override &, |, ^. */
93 :
94 : static PyNumberMethods bool_as_number = {
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 */
104 : 0, /* nb_bool */
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 */
111 : 0, /* nb_int */
112 : 0, /* nb_reserved */
113 : 0, /* nb_float */
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 */
128 : 0, /* nb_index */
129 : };
130 :
131 : /* The type object for bool. Note that this cannot be subclassed! */
132 :
133 : PyTypeObject PyBool_Type = {
134 : PyVarObject_HEAD_INIT(&PyType_Type, 0)
135 : "bool",
136 : sizeof(struct _longobject),
137 : 0,
138 : 0, /* tp_dealloc */
139 : 0, /* tp_print */
140 : 0, /* tp_getattr */
141 : 0, /* tp_setattr */
142 : 0, /* tp_reserved */
143 : bool_repr, /* tp_repr */
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 */
149 : bool_repr, /* tp_str */
150 : 0, /* tp_getattro */
151 : 0, /* tp_setattro */
152 : 0, /* tp_as_buffer */
153 : Py_TPFLAGS_DEFAULT, /* tp_flags */
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 */
164 : &PyLong_Type, /* tp_base */
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 :
176 : struct _longobject _Py_FalseStruct = {
177 : PyVarObject_HEAD_INIT(&PyBool_Type, 0)
178 : { 0 }
179 : };
180 :
181 : struct _longobject _Py_TrueStruct = {
182 : PyVarObject_HEAD_INIT(&PyBool_Type, 1)
183 : { 1 }
184 : };
|