blob: 59a66d52601f378e7dcc4c7088e2d4918c1f997a [file] [log] [blame]
Guido van Rossum5f820362002-04-03 23:01:45 +00001/* Boolean type, a subtype of int */
2
3#include "Python.h"
Guido van Rossumddefaf32007-01-14 03:31:43 +00004#include "longintrepr.h"
Guido van Rossum5f820362002-04-03 23:01:45 +00005
Guido van Rossum5f820362002-04-03 23:01:45 +00006/* We define bool_repr to return "False" or "True" */
7
8static PyObject *false_str = NULL;
9static PyObject *true_str = NULL;
10
Neal Norwitz657d2222002-08-06 22:12:52 +000011static PyObject *
Guido van Rossumddefaf32007-01-14 03:31:43 +000012bool_repr(PyObject *self)
Guido van Rossum5f820362002-04-03 23:01:45 +000013{
14 PyObject *s;
15
Guido van Rossumddefaf32007-01-14 03:31:43 +000016 if (self == Py_True)
Guido van Rossum5f820362002-04-03 23:01:45 +000017 s = true_str ? true_str :
Walter Dörwald16807132007-05-25 13:52:07 +000018 (true_str = PyUnicode_InternFromString("True"));
Guido van Rossum5f820362002-04-03 23:01:45 +000019 else
20 s = false_str ? false_str :
Walter Dörwald16807132007-05-25 13:52:07 +000021 (false_str = PyUnicode_InternFromString("False"));
Guido van Rossum5f820362002-04-03 23:01:45 +000022 Py_XINCREF(s);
23 return s;
24}
25
26/* Function to return a bool from a C long */
27
28PyObject *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 Norwitz657d2222002-08-06 22:12:52 +000042static PyObject *
Guido van Rossum5f820362002-04-03 23:01:45 +000043bool_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
44{
Martin v. Löwis15e62742006-02-27 16:46:16 +000045 static char *kwlist[] = {"x", 0};
Guido van Rossumaa86e352003-04-19 18:15:10 +000046 PyObject *x = Py_False;
Guido van Rossum5f820362002-04-03 23:01:45 +000047 long ok;
48
Guido van Rossumaa86e352003-04-19 18:15:10 +000049 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:bool", kwlist, &x))
Guido van Rossum5f820362002-04-03 23:01:45 +000050 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
59static PyObject *
60bool_and(PyObject *a, PyObject *b)
61{
62 if (!PyBool_Check(a) || !PyBool_Check(b))
Guido van Rossumddefaf32007-01-14 03:31:43 +000063 return PyLong_Type.tp_as_number->nb_and(a, b);
64 return PyBool_FromLong((a == Py_True) & (b == Py_True));
Guido van Rossum5f820362002-04-03 23:01:45 +000065}
66
67static PyObject *
68bool_or(PyObject *a, PyObject *b)
69{
70 if (!PyBool_Check(a) || !PyBool_Check(b))
Guido van Rossumddefaf32007-01-14 03:31:43 +000071 return PyLong_Type.tp_as_number->nb_or(a, b);
72 return PyBool_FromLong((a == Py_True) | (b == Py_True));
Guido van Rossum5f820362002-04-03 23:01:45 +000073}
74
75static PyObject *
76bool_xor(PyObject *a, PyObject *b)
77{
78 if (!PyBool_Check(a) || !PyBool_Check(b))
Guido van Rossumddefaf32007-01-14 03:31:43 +000079 return PyLong_Type.tp_as_number->nb_xor(a, b);
80 return PyBool_FromLong((a == Py_True) ^ (b == Py_True));
Guido van Rossum5f820362002-04-03 23:01:45 +000081}
82
83/* Doc string */
84
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000085PyDoc_STRVAR(bool_doc,
Guido van Rossum5f820362002-04-03 23:01:45 +000086"bool(x) -> bool\n\
87\n\
88Returns True when the argument x is true, False otherwise.\n\
89The builtins True and False are the only two instances of the class bool.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000090The class bool is a subclass of the class int, and cannot be subclassed.");
Guido van Rossum5f820362002-04-03 23:01:45 +000091
92/* Arithmetic methods -- only so we can override &, |, ^. */
93
94static PyNumberMethods bool_as_number = {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000095 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 Diederich4dafcc42006-11-28 19:15:13 +0000104 0, /* nb_bool */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000105 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 Wouters49fd7fa2006-04-21 10:40:58 +0000111 0, /* nb_int */
112 0, /* nb_long */
113 0, /* nb_float */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000114 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 Rossumddefaf32007-01-14 03:31:43 +0000128 0, /* nb_index */
Guido van Rossum5f820362002-04-03 23:01:45 +0000129};
130
131/* The type object for bool. Note that this cannot be subclassed! */
132
133PyTypeObject PyBool_Type = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000134 PyVarObject_HEAD_INIT(&PyType_Type, 0)
Guido van Rossum5f820362002-04-03 23:01:45 +0000135 "bool",
Guido van Rossumddefaf32007-01-14 03:31:43 +0000136 sizeof(struct _longobject),
Guido van Rossum5f820362002-04-03 23:01:45 +0000137 0,
138 0, /* tp_dealloc */
Guido van Rossum04dbf3b2007-08-07 19:51:00 +0000139 0, /* tp_print */
Guido van Rossum5f820362002-04-03 23:01:45 +0000140 0, /* tp_getattr */
141 0, /* tp_setattr */
142 0, /* tp_compare */
Guido van Rossumddefaf32007-01-14 03:31:43 +0000143 bool_repr, /* tp_repr */
Guido van Rossum5f820362002-04-03 23:01:45 +0000144 &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 Rossumddefaf32007-01-14 03:31:43 +0000149 bool_repr, /* tp_str */
Guido van Rossum5f820362002-04-03 23:01:45 +0000150 0, /* tp_getattro */
151 0, /* tp_setattro */
152 0, /* tp_as_buffer */
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000153 Py_TPFLAGS_DEFAULT, /* tp_flags */
Guido van Rossum5f820362002-04-03 23:01:45 +0000154 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 Rossumddefaf32007-01-14 03:31:43 +0000164 &PyLong_Type, /* tp_base */
Guido van Rossum5f820362002-04-03 23:01:45 +0000165 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/* Named Zero for link-level compatibility */
Guido van Rossumddefaf32007-01-14 03:31:43 +0000177struct _longobject _Py_FalseStruct = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000178 PyVarObject_HEAD_INIT(&PyBool_Type, 0)
179 { 0 }
Guido van Rossum5f820362002-04-03 23:01:45 +0000180};
181
Guido van Rossumddefaf32007-01-14 03:31:43 +0000182struct _longobject _Py_TrueStruct = {
Martin v. Löwis9f2e3462007-07-21 17:22:18 +0000183 PyVarObject_HEAD_INIT(&PyBool_Type, 1)
184 { 1 }
Guido van Rossum5f820362002-04-03 23:01:45 +0000185};