annotate cos/python/Objects/boolobject.c @ 114:f42268da614f

Connected to stm32f4discovery
author Windel Bouwman
date Sat, 05 Jan 2013 19:07:14 +0100
parents 7f74363f4c82
children
rev   line source
27
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
1 /* Boolean type, a subtype of int */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
2
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
3 #include "Python.h"
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
4 #include "longintrepr.h"
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
5
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
6 /* We define bool_repr to return "False" or "True" */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
7
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
8 static PyObject *false_str = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
9 static PyObject *true_str = NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
10
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
11 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
12 bool_repr(PyObject *self)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
13 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
14 PyObject *s;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
15
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
16 if (self == Py_True)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
17 s = true_str ? true_str :
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
18 (true_str = PyUnicode_InternFromString("True"));
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
19 else
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
20 s = false_str ? false_str :
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
21 (false_str = PyUnicode_InternFromString("False"));
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
22 Py_XINCREF(s);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
23 return s;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
24 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
25
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
26 /* Function to return a bool from a C long */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
27
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
28 PyObject *PyBool_FromLong(long ok)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
29 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
30 PyObject *result;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
31
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
32 if (ok)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
33 result = Py_True;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
34 else
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
35 result = Py_False;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
36 Py_INCREF(result);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
37 return result;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
38 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
39
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
40 /* We define bool_new to always return either Py_True or Py_False */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
41
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
42 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
43 bool_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
44 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
45 static char *kwlist[] = {"x", 0};
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
46 PyObject *x = Py_False;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
47 long ok;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
48
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
49 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:bool", kwlist, &x))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
50 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
51 ok = PyObject_IsTrue(x);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
52 if (ok < 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
53 return NULL;
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
54 return PyBool_FromLong(ok);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
55 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
56
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
57 /* Arithmetic operations redefined to return bool if both args are bool. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
58
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
59 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
60 bool_and(PyObject *a, PyObject *b)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
61 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
62 if (!PyBool_Check(a) || !PyBool_Check(b))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
63 return PyLong_Type.tp_as_number->nb_and(a, b);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
64 return PyBool_FromLong((a == Py_True) & (b == Py_True));
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
65 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
66
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
67 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
68 bool_or(PyObject *a, PyObject *b)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
69 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
70 if (!PyBool_Check(a) || !PyBool_Check(b))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
71 return PyLong_Type.tp_as_number->nb_or(a, b);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
72 return PyBool_FromLong((a == Py_True) | (b == Py_True));
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
73 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
74
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
75 static PyObject *
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
76 bool_xor(PyObject *a, PyObject *b)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
77 {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
78 if (!PyBool_Check(a) || !PyBool_Check(b))
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
79 return PyLong_Type.tp_as_number->nb_xor(a, b);
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
80 return PyBool_FromLong((a == Py_True) ^ (b == Py_True));
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
81 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
82
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
83 /* Doc string */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
84
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
85 PyDoc_STRVAR(bool_doc,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
86 "bool(x) -> bool\n\
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
87 \n\
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
88 Returns True when the argument x is true, False otherwise.\n\
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
89 The builtins True and False are the only two instances of the class bool.\n\
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
90 The class bool is a subclass of the class int, and cannot be subclassed.");
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
91
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
92 /* Arithmetic methods -- only so we can override &, |, ^. */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
93
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
94 static PyNumberMethods bool_as_number = {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
95 0, /* nb_add */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
96 0, /* nb_subtract */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
97 0, /* nb_multiply */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
98 0, /* nb_remainder */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
99 0, /* nb_divmod */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
100 0, /* nb_power */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
101 0, /* nb_negative */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
102 0, /* nb_positive */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
103 0, /* nb_absolute */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
104 0, /* nb_bool */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
105 0, /* nb_invert */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
106 0, /* nb_lshift */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
107 0, /* nb_rshift */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
108 bool_and, /* nb_and */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
109 bool_xor, /* nb_xor */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
110 bool_or, /* nb_or */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
111 0, /* nb_int */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
112 0, /* nb_reserved */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
113 0, /* nb_float */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
114 0, /* nb_inplace_add */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
115 0, /* nb_inplace_subtract */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
116 0, /* nb_inplace_multiply */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
117 0, /* nb_inplace_remainder */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
118 0, /* nb_inplace_power */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
119 0, /* nb_inplace_lshift */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
120 0, /* nb_inplace_rshift */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
121 0, /* nb_inplace_and */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
122 0, /* nb_inplace_xor */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
123 0, /* nb_inplace_or */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
124 0, /* nb_floor_divide */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
125 0, /* nb_true_divide */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
126 0, /* nb_inplace_floor_divide */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
127 0, /* nb_inplace_true_divide */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
128 0, /* nb_index */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
129 };
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
130
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
131 /* The type object for bool. Note that this cannot be subclassed! */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
132
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
133 PyTypeObject PyBool_Type = {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
134 PyVarObject_HEAD_INIT(&PyType_Type, 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
135 "bool",
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
136 sizeof(struct _longobject),
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
137 0,
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
138 0, /* tp_dealloc */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
139 0, /* tp_print */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
140 0, /* tp_getattr */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
141 0, /* tp_setattr */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
142 0, /* tp_reserved */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
143 bool_repr, /* tp_repr */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
144 &bool_as_number, /* tp_as_number */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
145 0, /* tp_as_sequence */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
146 0, /* tp_as_mapping */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
147 0, /* tp_hash */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
148 0, /* tp_call */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
149 bool_repr, /* tp_str */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
150 0, /* tp_getattro */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
151 0, /* tp_setattro */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
152 0, /* tp_as_buffer */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
153 Py_TPFLAGS_DEFAULT, /* tp_flags */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
154 bool_doc, /* tp_doc */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
155 0, /* tp_traverse */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
156 0, /* tp_clear */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
157 0, /* tp_richcompare */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
158 0, /* tp_weaklistoffset */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
159 0, /* tp_iter */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
160 0, /* tp_iternext */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
161 0, /* tp_methods */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
162 0, /* tp_members */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
163 0, /* tp_getset */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
164 &PyLong_Type, /* tp_base */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
165 0, /* tp_dict */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
166 0, /* tp_descr_get */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
167 0, /* tp_descr_set */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
168 0, /* tp_dictoffset */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
169 0, /* tp_init */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
170 0, /* tp_alloc */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
171 bool_new, /* tp_new */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
172 };
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
173
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
174 /* The objects representing bool values False and True */
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
175
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
176 struct _longobject _Py_FalseStruct = {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
177 PyVarObject_HEAD_INIT(&PyBool_Type, 0)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
178 { 0 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
179 };
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
180
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
181 struct _longobject _Py_TrueStruct = {
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
182 PyVarObject_HEAD_INIT(&PyBool_Type, 1)
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
183 { 1 }
7f74363f4c82 Added some files for the python port
windel
parents:
diff changeset
184 };