boolobject.c 4.38 KB
Newer Older
1 2 3
/* Boolean type, a subtype of int */

#include "Python.h"
4
#include "longintrepr.h"
5 6 7 8 9 10

/* We define bool_repr to return "False" or "True" */

static PyObject *false_str = NULL;
static PyObject *true_str = NULL;

11
static PyObject *
12
bool_repr(PyObject *self)
13 14 15
{
	PyObject *s;

16
	if (self == Py_True)
17
		s = true_str ? true_str :
18
			(true_str = PyUnicode_InternFromString("True"));
19 20
	else
		s = false_str ? false_str :
21
			(false_str = PyUnicode_InternFromString("False"));
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
	Py_XINCREF(s);
	return s;
}

/* Function to return a bool from a C long */

PyObject *PyBool_FromLong(long ok)
{
	PyObject *result;

	if (ok)
		result = Py_True;
	else
		result = Py_False;
	Py_INCREF(result);
	return result;
}

/* We define bool_new to always return either Py_True or Py_False */

42
static PyObject *
43 44
bool_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
45
	static char *kwlist[] = {"x", 0};
46
	PyObject *x = Py_False;
47 48
	long ok;

49
	if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:bool", kwlist, &x))
50 51 52 53 54 55 56 57 58 59 60 61 62
		return NULL;
	ok = PyObject_IsTrue(x);
	if (ok < 0)
		return NULL;
	return PyBool_FromLong(ok);
}

/* Arithmetic operations redefined to return bool if both args are bool. */

static PyObject *
bool_and(PyObject *a, PyObject *b)
{
	if (!PyBool_Check(a) || !PyBool_Check(b))
63 64
		return PyLong_Type.tp_as_number->nb_and(a, b);
	return PyBool_FromLong((a == Py_True) & (b == Py_True));
65 66 67 68 69 70
}

static PyObject *
bool_or(PyObject *a, PyObject *b)
{
	if (!PyBool_Check(a) || !PyBool_Check(b))
71 72
		return PyLong_Type.tp_as_number->nb_or(a, b);
	return PyBool_FromLong((a == Py_True) | (b == Py_True));
73 74 75 76 77 78
}

static PyObject *
bool_xor(PyObject *a, PyObject *b)
{
	if (!PyBool_Check(a) || !PyBool_Check(b))
79 80
		return PyLong_Type.tp_as_number->nb_xor(a, b);
	return PyBool_FromLong((a == Py_True) ^ (b == Py_True));
81 82 83 84
}

/* Doc string */

85
PyDoc_STRVAR(bool_doc,
86 87 88 89
"bool(x) -> bool\n\
\n\
Returns True when the argument x is true, False otherwise.\n\
The builtins True and False are the only two instances of the class bool.\n\
90
The class bool is a subclass of the class int, and cannot be subclassed.");
91 92 93 94

/* Arithmetic methods -- only so we can override &, |, ^. */

static PyNumberMethods bool_as_number = {
95 96 97 98 99 100 101 102 103
	0,			/* nb_add */
	0,			/* nb_subtract */
	0,			/* nb_multiply */
	0,			/* nb_remainder */
	0,			/* nb_divmod */
	0,			/* nb_power */
	0,			/* nb_negative */
	0,			/* nb_positive */
	0,			/* nb_absolute */
104
	0,			/* nb_bool */
105 106 107 108 109 110
	0,			/* nb_invert */
	0,			/* nb_lshift */
	0,			/* nb_rshift */
	bool_and,		/* nb_and */
	bool_xor,		/* nb_xor */
	bool_or,		/* nb_or */
111
	0,			/* nb_reserved */
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
	0,			/* nb_int */
	0,			/* nb_long */
	0,			/* nb_float */
	0,			/* nb_oct */
	0,		 	/* nb_hex */
	0,			/* nb_inplace_add */
	0,			/* nb_inplace_subtract */
	0,			/* nb_inplace_multiply */
	0,			/* nb_inplace_remainder */
	0,			/* nb_inplace_power */
	0,			/* nb_inplace_lshift */
	0,			/* nb_inplace_rshift */
	0,			/* nb_inplace_and */
	0,			/* nb_inplace_xor */
	0,			/* nb_inplace_or */
	0,			/* nb_floor_divide */
	0,			/* nb_true_divide */
	0,			/* nb_inplace_floor_divide */
	0,			/* nb_inplace_true_divide */
131
	0,			/* nb_index */
132 133 134 135 136
};

/* The type object for bool.  Note that this cannot be subclassed! */

PyTypeObject PyBool_Type = {
137
	PyVarObject_HEAD_INIT(&PyType_Type, 0)
138
	"bool",
139
	sizeof(struct _longobject),
140 141
	0,
	0,					/* tp_dealloc */
142
	0,					/* tp_print */
143 144 145
	0,					/* tp_getattr */
	0,					/* tp_setattr */
	0,					/* tp_compare */
146
	bool_repr,				/* tp_repr */
147 148 149 150 151
	&bool_as_number,			/* tp_as_number */
	0,					/* tp_as_sequence */
	0,					/* tp_as_mapping */
	0,					/* tp_hash */
        0,					/* tp_call */
152
        bool_repr,				/* tp_str */
153 154 155
	0,					/* tp_getattro */
	0,					/* tp_setattro */
	0,					/* tp_as_buffer */
156
	Py_TPFLAGS_DEFAULT,			/* tp_flags */
157 158 159 160 161 162 163 164 165 166
	bool_doc,				/* tp_doc */
	0,					/* tp_traverse */
	0,					/* tp_clear */
	0,					/* tp_richcompare */
	0,					/* tp_weaklistoffset */
	0,					/* tp_iter */
	0,					/* tp_iternext */
	0,					/* tp_methods */
	0,					/* tp_members */
	0,					/* tp_getset */
167
	&PyLong_Type,				/* tp_base */
168 169 170 171 172 173 174 175 176 177 178 179
	0,					/* tp_dict */
	0,					/* tp_descr_get */
	0,					/* tp_descr_set */
	0,					/* tp_dictoffset */
	0,					/* tp_init */
	0,					/* tp_alloc */
	bool_new,				/* tp_new */
};

/* The objects representing bool values False and True */

/* Named Zero for link-level compatibility */
180
struct _longobject _Py_FalseStruct = {
181 182
	PyVarObject_HEAD_INIT(&PyBool_Type, 0)
	{ 0 }
183 184
};

185
struct _longobject _Py_TrueStruct = {
186 187
	PyVarObject_HEAD_INIT(&PyBool_Type, 1)
	{ 1 }
188
};