ยปCore Development>Code coverage>Objects/boolobject.c

Python code coverage for Objects/boolobject.c

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