Showing the changes to sys/src/cmd/python/Objects/boolobject.c (1 of 3706 files). Show all
Unified · Split
| @@ -0,0 +1,201 @@ | |||
| 1 | /* Boolean type, a subtype of int */ | ||
| 2 | |||
| 3 | #include "Python.h" | ||
| 4 | |||
| 5 | /* We need to define bool_print to override int_print */ | ||
| 6 | |||
| 7 | static int | ||
| 8 | bool_print(PyBoolObject *self, FILE *fp, int flags) | ||
| 9 | { | ||
| 10 | fputs(self->ob_ival == 0 ? "False" : "True", fp); | ||
| 11 | return 0; | ||
| 12 | } | ||
| 13 | |||
| 14 | /* We define bool_repr to return "False" or "True" */ | ||
| 15 | |||
| 16 | static PyObject *false_str = NULL; | ||
| 17 | static PyObject *true_str = NULL; | ||
| 18 | |||
| 19 | static PyObject * | ||
| 20 | bool_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 | |||
| 36 | PyObject *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 | |||
| 50 | static PyObject * | ||
| 51 | bool_new(PyTypeObject *type, PyObject *args, PyObject *kwds) | ||
| 52 | { | ||
| 53 | static char *kwlist[] = {"x", 0}; | ||
| 54 | PyObject *x = Py_False; | ||
| 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 | |||
| 67 | static PyObject * | ||
| 68 | bool_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 | |||
| 76 | static PyObject * | ||
| 77 | bool_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 | |||
| 85 | static PyObject * | ||
| 86 | bool_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 | |||
| 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."); | ||
| 102 | |||
| 103 | /* Arithmetic methods -- only so we can override &, |, ^. */ | ||
| 104 | |||
| 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 */ | ||
| 144 | }; | ||
| 145 | |||
| 146 | /* The type object for bool. Note that this cannot be subclassed! */ | ||
| 147 | |||
| 148 | PyTypeObject 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 */ | ||
| 193 | PyIntObject _Py_ZeroStruct = { | ||
| 194 | PyObject_HEAD_INIT(&PyBool_Type) | ||
| 195 | 0 | ||
| 196 | }; | ||
| 197 | |||
| 198 | PyIntObject _Py_TrueStruct = { | ||
| 199 | PyObject_HEAD_INIT(&PyBool_Type) | ||
| 200 | 1 | ||
| 201 | }; | ||