blob: b54b0525dfc8e7dfad53b52376dfe70204d3f8f4 [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{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000014 PyObject *s;
Guido van Rossum5f820362002-04-03 23:01:45 +000015
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000016 if (self == Py_True)
17 s = true_str ? true_str :
18 (true_str = PyUnicode_InternFromString("True"));
19 else
20 s = false_str ? false_str :
21 (false_str = PyUnicode_InternFromString("False"));
22 Py_XINCREF(s);
23 return s;
Guido van Rossum5f820362002-04-03 23:01:45 +000024}
25
26/* Function to return a bool from a C long */
27
28PyObject *PyBool_FromLong(long ok)
29{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000030 PyObject *result;
Guido van Rossum5f820362002-04-03 23:01:45 +000031
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000032 if (ok)
33 result = Py_True;
34 else
35 result = Py_False;
36 Py_INCREF(result);
37 return result;
Guido van Rossum5f820362002-04-03 23:01:45 +000038}
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{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000045 static char *kwlist[] = {"x", 0};
46 PyObject *x = Py_False;
47 long ok;
Guido van Rossum5f820362002-04-03 23:01:45 +000048
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000049 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:bool", kwlist, &x))
50 return NULL;
Serhiy Storchaka58d23e62017-03-06 00:53:39 +020051 if (kwds != NULL && PyDict_GET_SIZE(kwds) != 0) {
52 if (PyErr_Warn(PyExc_DeprecationWarning,
53 "Using 'x' as a keyword argument is deprecated; "
54 "specify the value as a positional argument instead") < 0)
55 return NULL;
56 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000057 ok = PyObject_IsTrue(x);
58 if (ok < 0)
59 return NULL;
60 return PyBool_FromLong(ok);
Guido van Rossum5f820362002-04-03 23:01:45 +000061}
62
63/* Arithmetic operations redefined to return bool if both args are bool. */
64
65static PyObject *
66bool_and(PyObject *a, PyObject *b)
67{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000068 if (!PyBool_Check(a) || !PyBool_Check(b))
69 return PyLong_Type.tp_as_number->nb_and(a, b);
70 return PyBool_FromLong((a == Py_True) & (b == Py_True));
Guido van Rossum5f820362002-04-03 23:01:45 +000071}
72
73static PyObject *
74bool_or(PyObject *a, PyObject *b)
75{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000076 if (!PyBool_Check(a) || !PyBool_Check(b))
77 return PyLong_Type.tp_as_number->nb_or(a, b);
78 return PyBool_FromLong((a == Py_True) | (b == Py_True));
Guido van Rossum5f820362002-04-03 23:01:45 +000079}
80
81static PyObject *
82bool_xor(PyObject *a, PyObject *b)
83{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000084 if (!PyBool_Check(a) || !PyBool_Check(b))
85 return PyLong_Type.tp_as_number->nb_xor(a, b);
86 return PyBool_FromLong((a == Py_True) ^ (b == Py_True));
Guido van Rossum5f820362002-04-03 23:01:45 +000087}
88
89/* Doc string */
90
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000091PyDoc_STRVAR(bool_doc,
Guido van Rossum5f820362002-04-03 23:01:45 +000092"bool(x) -> bool\n\
93\n\
94Returns True when the argument x is true, False otherwise.\n\
95The builtins True and False are the only two instances of the class bool.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000096The class bool is a subclass of the class int, and cannot be subclassed.");
Guido van Rossum5f820362002-04-03 23:01:45 +000097
98/* Arithmetic methods -- only so we can override &, |, ^. */
99
100static PyNumberMethods bool_as_number = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000101 0, /* nb_add */
102 0, /* nb_subtract */
103 0, /* nb_multiply */
104 0, /* nb_remainder */
105 0, /* nb_divmod */
106 0, /* nb_power */
107 0, /* nb_negative */
108 0, /* nb_positive */
109 0, /* nb_absolute */
110 0, /* nb_bool */
111 0, /* nb_invert */
112 0, /* nb_lshift */
113 0, /* nb_rshift */
114 bool_and, /* nb_and */
115 bool_xor, /* nb_xor */
116 bool_or, /* nb_or */
117 0, /* nb_int */
118 0, /* nb_reserved */
119 0, /* nb_float */
120 0, /* nb_inplace_add */
121 0, /* nb_inplace_subtract */
122 0, /* nb_inplace_multiply */
123 0, /* nb_inplace_remainder */
124 0, /* nb_inplace_power */
125 0, /* nb_inplace_lshift */
126 0, /* nb_inplace_rshift */
127 0, /* nb_inplace_and */
128 0, /* nb_inplace_xor */
129 0, /* nb_inplace_or */
130 0, /* nb_floor_divide */
131 0, /* nb_true_divide */
132 0, /* nb_inplace_floor_divide */
133 0, /* nb_inplace_true_divide */
134 0, /* nb_index */
Guido van Rossum5f820362002-04-03 23:01:45 +0000135};
136
137/* The type object for bool. Note that this cannot be subclassed! */
138
139PyTypeObject PyBool_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000140 PyVarObject_HEAD_INIT(&PyType_Type, 0)
141 "bool",
142 sizeof(struct _longobject),
143 0,
144 0, /* tp_dealloc */
145 0, /* tp_print */
146 0, /* tp_getattr */
147 0, /* tp_setattr */
148 0, /* tp_reserved */
149 bool_repr, /* tp_repr */
150 &bool_as_number, /* tp_as_number */
151 0, /* tp_as_sequence */
152 0, /* tp_as_mapping */
153 0, /* tp_hash */
154 0, /* tp_call */
155 bool_repr, /* tp_str */
156 0, /* tp_getattro */
157 0, /* tp_setattro */
158 0, /* tp_as_buffer */
159 Py_TPFLAGS_DEFAULT, /* tp_flags */
160 bool_doc, /* tp_doc */
161 0, /* tp_traverse */
162 0, /* tp_clear */
163 0, /* tp_richcompare */
164 0, /* tp_weaklistoffset */
165 0, /* tp_iter */
166 0, /* tp_iternext */
167 0, /* tp_methods */
168 0, /* tp_members */
169 0, /* tp_getset */
170 &PyLong_Type, /* tp_base */
171 0, /* tp_dict */
172 0, /* tp_descr_get */
173 0, /* tp_descr_set */
174 0, /* tp_dictoffset */
175 0, /* tp_init */
176 0, /* tp_alloc */
177 bool_new, /* tp_new */
Guido van Rossum5f820362002-04-03 23:01:45 +0000178};
179
180/* The objects representing bool values False and True */
181
Guido van Rossumddefaf32007-01-14 03:31:43 +0000182struct _longobject _Py_FalseStruct = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000183 PyVarObject_HEAD_INIT(&PyBool_Type, 0)
184 { 0 }
Guido van Rossum5f820362002-04-03 23:01:45 +0000185};
186
Guido van Rossumddefaf32007-01-14 03:31:43 +0000187struct _longobject _Py_TrueStruct = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000188 PyVarObject_HEAD_INIT(&PyBool_Type, 1)
189 { 1 }
Guido van Rossum5f820362002-04-03 23:01:45 +0000190};