blob: af6150d974323291cfe0785dac57100ecbd10b80 [file] [log] [blame]
Guido van Rossum5f820362002-04-03 23:01:45 +00001/* Boolean type, a subtype of int */
2
3#include "Python.h"
4
5/* We need to define bool_print to override int_print */
6
7static int
8bool_print(PyBoolObject *self, FILE *fp, int flags)
9{
Guido van Rossum645a22e2002-04-04 01:00:42 +000010 fputs(self->ob_ival == 0 ? "False" : "True", fp);
Guido van Rossum5f820362002-04-03 23:01:45 +000011 return 0;
12}
13
14/* We define bool_repr to return "False" or "True" */
15
16static PyObject *false_str = NULL;
17static PyObject *true_str = NULL;
18
Neal Norwitz657d2222002-08-06 22:12:52 +000019static PyObject *
Guido van Rossum5f820362002-04-03 23:01:45 +000020bool_repr(PyBoolObject *self)
21{
22 PyObject *s;
23
24 if (self->ob_ival)
25 s = true_str ? true_str :
26 (true_str = PyString_InternFromString("True"));
27 else
28 s = false_str ? false_str :
29 (false_str = PyString_InternFromString("False"));
30 Py_XINCREF(s);
31 return s;
32}
33
34/* Function to return a bool from a C long */
35
36PyObject *PyBool_FromLong(long ok)
37{
38 PyObject *result;
39
40 if (ok)
41 result = Py_True;
42 else
43 result = Py_False;
44 Py_INCREF(result);
45 return result;
46}
47
48/* We define bool_new to always return either Py_True or Py_False */
49
Neal Norwitz657d2222002-08-06 22:12:52 +000050static PyObject *
Guido van Rossum5f820362002-04-03 23:01:45 +000051bool_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
52{
53 static char *kwlist[] = {"x", 0};
54 PyObject *x;
55 long ok;
56
57 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:bool", kwlist, &x))
58 return NULL;
59 ok = PyObject_IsTrue(x);
60 if (ok < 0)
61 return NULL;
62 return PyBool_FromLong(ok);
63}
64
65/* Arithmetic operations redefined to return bool if both args are bool. */
66
67static PyObject *
68bool_and(PyObject *a, PyObject *b)
69{
70 if (!PyBool_Check(a) || !PyBool_Check(b))
71 return PyInt_Type.tp_as_number->nb_and(a, b);
72 return PyBool_FromLong(
73 ((PyBoolObject *)a)->ob_ival & ((PyBoolObject *)b)->ob_ival);
74}
75
76static PyObject *
77bool_or(PyObject *a, PyObject *b)
78{
79 if (!PyBool_Check(a) || !PyBool_Check(b))
80 return PyInt_Type.tp_as_number->nb_or(a, b);
81 return PyBool_FromLong(
82 ((PyBoolObject *)a)->ob_ival | ((PyBoolObject *)b)->ob_ival);
83}
84
85static PyObject *
86bool_xor(PyObject *a, PyObject *b)
87{
88 if (!PyBool_Check(a) || !PyBool_Check(b))
89 return PyInt_Type.tp_as_number->nb_xor(a, b);
90 return PyBool_FromLong(
91 ((PyBoolObject *)a)->ob_ival ^ ((PyBoolObject *)b)->ob_ival);
92}
93
94/* Doc string */
95
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000096PyDoc_STRVAR(bool_doc,
Guido van Rossum5f820362002-04-03 23:01:45 +000097"bool(x) -> bool\n\
98\n\
99Returns True when the argument x is true, False otherwise.\n\
100The builtins True and False are the only two instances of the class bool.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000101The class bool is a subclass of the class int, and cannot be subclassed.");
Guido van Rossum5f820362002-04-03 23:01:45 +0000102
103/* Arithmetic methods -- only so we can override &, |, ^. */
104
105static PyNumberMethods bool_as_number = {
Guido van Rossumc95cc872002-04-25 20:01:10 +0000106 0, /* nb_add */
107 0, /* nb_subtract */
108 0, /* nb_multiply */
109 0, /* nb_divide */
110 0, /* nb_remainder */
111 0, /* nb_divmod */
112 0, /* nb_power */
113 0, /* nb_negative */
114 0, /* nb_positive */
115 0, /* nb_absolute */
116 0, /* nb_nonzero */
117 0, /* nb_invert */
118 0, /* nb_lshift */
119 0, /* nb_rshift */
120 (binaryfunc)bool_and, /* nb_and */
121 (binaryfunc)bool_xor, /* nb_xor */
122 (binaryfunc)bool_or, /* nb_or */
123 0, /* nb_coerce */
124 0, /* nb_int */
125 0, /* nb_long */
126 0, /* nb_float */
127 0, /* nb_oct */
128 0, /* nb_hex */
129 0, /* nb_inplace_add */
130 0, /* nb_inplace_subtract */
131 0, /* nb_inplace_multiply */
132 0, /* nb_inplace_divide */
133 0, /* nb_inplace_remainder */
134 0, /* nb_inplace_power */
135 0, /* nb_inplace_lshift */
136 0, /* nb_inplace_rshift */
137 0, /* nb_inplace_and */
138 0, /* nb_inplace_xor */
139 0, /* nb_inplace_or */
140 0, /* nb_floor_divide */
141 0, /* nb_true_divide */
142 0, /* nb_inplace_floor_divide */
143 0, /* nb_inplace_true_divide */
Guido van Rossum5f820362002-04-03 23:01:45 +0000144};
145
146/* The type object for bool. Note that this cannot be subclassed! */
147
148PyTypeObject PyBool_Type = {
149 PyObject_HEAD_INIT(&PyType_Type)
150 0,
151 "bool",
152 sizeof(PyIntObject),
153 0,
154 0, /* tp_dealloc */
155 (printfunc)bool_print, /* tp_print */
156 0, /* tp_getattr */
157 0, /* tp_setattr */
158 0, /* tp_compare */
159 (reprfunc)bool_repr, /* tp_repr */
160 &bool_as_number, /* tp_as_number */
161 0, /* tp_as_sequence */
162 0, /* tp_as_mapping */
163 0, /* tp_hash */
164 0, /* tp_call */
165 (reprfunc)bool_repr, /* tp_str */
166 0, /* tp_getattro */
167 0, /* tp_setattro */
168 0, /* tp_as_buffer */
169 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES, /* tp_flags */
170 bool_doc, /* tp_doc */
171 0, /* tp_traverse */
172 0, /* tp_clear */
173 0, /* tp_richcompare */
174 0, /* tp_weaklistoffset */
175 0, /* tp_iter */
176 0, /* tp_iternext */
177 0, /* tp_methods */
178 0, /* tp_members */
179 0, /* tp_getset */
180 &PyInt_Type, /* tp_base */
181 0, /* tp_dict */
182 0, /* tp_descr_get */
183 0, /* tp_descr_set */
184 0, /* tp_dictoffset */
185 0, /* tp_init */
186 0, /* tp_alloc */
187 bool_new, /* tp_new */
188};
189
190/* The objects representing bool values False and True */
191
192/* Named Zero for link-level compatibility */
193PyIntObject _Py_ZeroStruct = {
194 PyObject_HEAD_INIT(&PyBool_Type)
195 0
196};
197
198PyIntObject _Py_TrueStruct = {
199 PyObject_HEAD_INIT(&PyBool_Type)
200 1
201};