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