Patch 1114: fix compilation of curses module on 64-bit AIX, and any other LP64 platfo...
[pytest.git] / Objects / boolobject.c
blob37be295ac7d56eabca110a2c8735c4c3cbac807a
1 /* Boolean type, a subtype of int */
3 #include "Python.h"
5 /* We need to define bool_print to override int_print */
7 static int
8 bool_print(PyBoolObject *self, FILE *fp, int flags)
10 fputs(self->ob_ival == 0 ? "False" : "True", fp);
11 return 0;
14 /* We define bool_repr to return "False" or "True" */
16 static PyObject *false_str = NULL;
17 static PyObject *true_str = NULL;
19 static PyObject *
20 bool_repr(PyBoolObject *self)
22 PyObject *s;
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;
34 /* Function to return a bool from a C long */
36 PyObject *PyBool_FromLong(long ok)
38 PyObject *result;
40 if (ok)
41 result = Py_True;
42 else
43 result = Py_False;
44 Py_INCREF(result);
45 return result;
48 /* We define bool_new to always return either Py_True or Py_False */
50 static PyObject *
51 bool_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
53 static char *kwlist[] = {"x", 0};
54 PyObject *x = Py_False;
55 long ok;
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);
65 /* Arithmetic operations redefined to return bool if both args are bool. */
67 static PyObject *
68 bool_and(PyObject *a, PyObject *b)
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);
76 static PyObject *
77 bool_or(PyObject *a, PyObject *b)
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);
85 static PyObject *
86 bool_xor(PyObject *a, PyObject *b)
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);
94 /* Doc string */
96 PyDoc_STRVAR(bool_doc,
97 "bool(x) -> bool\n\
98 \n\
99 Returns True when the argument x is true, False otherwise.\n\
100 The builtins True and False are the only two instances of the class bool.\n\
101 The class bool is a subclass of the class int, and cannot be subclassed.");
103 /* Arithmetic methods -- only so we can override &, |, ^. */
105 static PyNumberMethods bool_as_number = {
106 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 bool_and, /* nb_and */
121 bool_xor, /* nb_xor */
122 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 */
146 /* The type object for bool. Note that this cannot be subclassed! */
148 PyTypeObject PyBool_Type = {
149 PyObject_HEAD_INIT(&PyType_Type)
151 "bool",
152 sizeof(PyIntObject),
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 */
190 /* The objects representing bool values False and True */
192 /* Named Zero for link-level compatibility */
193 PyIntObject _Py_ZeroStruct = {
194 PyObject_HEAD_INIT(&PyBool_Type)
198 PyIntObject _Py_TrueStruct = {
199 PyObject_HEAD_INIT(&PyBool_Type)