boolobject.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /* Boolean type, a subtype of int */
  2. #include "Python.h"
  3. #include "pycore_object.h" // _Py_FatalRefcountError()
  4. #include "pycore_long.h" // FALSE_TAG TRUE_TAG
  5. #include "pycore_runtime.h" // _Py_ID()
  6. #include <stddef.h>
  7. /* We define bool_repr to return "False" or "True" */
  8. static PyObject *
  9. bool_repr(PyObject *self)
  10. {
  11. PyObject *res = self == Py_True ? &_Py_ID(True) : &_Py_ID(False);
  12. return Py_NewRef(res);
  13. }
  14. /* Function to return a bool from a C long */
  15. PyObject *PyBool_FromLong(long ok)
  16. {
  17. return ok ? Py_True : Py_False;
  18. }
  19. /* We define bool_new to always return either Py_True or Py_False */
  20. static PyObject *
  21. bool_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
  22. {
  23. PyObject *x = Py_False;
  24. long ok;
  25. if (!_PyArg_NoKeywords("bool", kwds))
  26. return NULL;
  27. if (!PyArg_UnpackTuple(args, "bool", 0, 1, &x))
  28. return NULL;
  29. ok = PyObject_IsTrue(x);
  30. if (ok < 0)
  31. return NULL;
  32. return PyBool_FromLong(ok);
  33. }
  34. static PyObject *
  35. bool_vectorcall(PyObject *type, PyObject * const*args,
  36. size_t nargsf, PyObject *kwnames)
  37. {
  38. long ok = 0;
  39. if (!_PyArg_NoKwnames("bool", kwnames)) {
  40. return NULL;
  41. }
  42. Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
  43. if (!_PyArg_CheckPositional("bool", nargs, 0, 1)) {
  44. return NULL;
  45. }
  46. assert(PyType_Check(type));
  47. if (nargs) {
  48. ok = PyObject_IsTrue(args[0]);
  49. if (ok < 0) {
  50. return NULL;
  51. }
  52. }
  53. return PyBool_FromLong(ok);
  54. }
  55. /* Arithmetic operations redefined to return bool if both args are bool. */
  56. static PyObject *
  57. bool_invert(PyObject *v)
  58. {
  59. if (PyErr_WarnEx(PyExc_DeprecationWarning,
  60. "Bitwise inversion '~' on bool is deprecated. This "
  61. "returns the bitwise inversion of the underlying int "
  62. "object and is usually not what you expect from negating "
  63. "a bool. Use the 'not' operator for boolean negation or "
  64. "~int(x) if you really want the bitwise inversion of the "
  65. "underlying int.",
  66. 1) < 0) {
  67. return NULL;
  68. }
  69. return PyLong_Type.tp_as_number->nb_invert(v);
  70. }
  71. static PyObject *
  72. bool_and(PyObject *a, PyObject *b)
  73. {
  74. if (!PyBool_Check(a) || !PyBool_Check(b))
  75. return PyLong_Type.tp_as_number->nb_and(a, b);
  76. return PyBool_FromLong((a == Py_True) & (b == Py_True));
  77. }
  78. static PyObject *
  79. bool_or(PyObject *a, PyObject *b)
  80. {
  81. if (!PyBool_Check(a) || !PyBool_Check(b))
  82. return PyLong_Type.tp_as_number->nb_or(a, b);
  83. return PyBool_FromLong((a == Py_True) | (b == Py_True));
  84. }
  85. static PyObject *
  86. bool_xor(PyObject *a, PyObject *b)
  87. {
  88. if (!PyBool_Check(a) || !PyBool_Check(b))
  89. return PyLong_Type.tp_as_number->nb_xor(a, b);
  90. return PyBool_FromLong((a == Py_True) ^ (b == Py_True));
  91. }
  92. /* Doc string */
  93. PyDoc_STRVAR(bool_doc,
  94. "bool(x) -> bool\n\
  95. \n\
  96. Returns True when the argument x is true, False otherwise.\n\
  97. The builtins True and False are the only two instances of the class bool.\n\
  98. The class bool is a subclass of the class int, and cannot be subclassed.");
  99. /* Arithmetic methods -- only so we can override &, |, ^. */
  100. static PyNumberMethods bool_as_number = {
  101. 0, /* nb_add */
  102. 0, /* nb_subtract */
  103. 0, /* nb_multiply */
  104. 0, /* nb_remainder */
  105. 0, /* nb_divmod */
  106. 0, /* nb_power */
  107. 0, /* nb_negative */
  108. 0, /* nb_positive */
  109. 0, /* nb_absolute */
  110. 0, /* nb_bool */
  111. (unaryfunc)bool_invert, /* nb_invert */
  112. 0, /* nb_lshift */
  113. 0, /* nb_rshift */
  114. bool_and, /* nb_and */
  115. bool_xor, /* nb_xor */
  116. bool_or, /* nb_or */
  117. 0, /* nb_int */
  118. 0, /* nb_reserved */
  119. 0, /* nb_float */
  120. 0, /* nb_inplace_add */
  121. 0, /* nb_inplace_subtract */
  122. 0, /* nb_inplace_multiply */
  123. 0, /* nb_inplace_remainder */
  124. 0, /* nb_inplace_power */
  125. 0, /* nb_inplace_lshift */
  126. 0, /* nb_inplace_rshift */
  127. 0, /* nb_inplace_and */
  128. 0, /* nb_inplace_xor */
  129. 0, /* nb_inplace_or */
  130. 0, /* nb_floor_divide */
  131. 0, /* nb_true_divide */
  132. 0, /* nb_inplace_floor_divide */
  133. 0, /* nb_inplace_true_divide */
  134. 0, /* nb_index */
  135. };
  136. static void
  137. bool_dealloc(PyObject *boolean)
  138. {
  139. /* This should never get called, but we also don't want to SEGV if
  140. * we accidentally decref Booleans out of existence. Instead,
  141. * since bools are immortal, re-set the reference count.
  142. */
  143. _Py_SetImmortal(boolean);
  144. }
  145. /* The type object for bool. Note that this cannot be subclassed! */
  146. PyTypeObject PyBool_Type = {
  147. PyVarObject_HEAD_INIT(&PyType_Type, 0)
  148. "bool",
  149. offsetof(struct _longobject, long_value.ob_digit), /* tp_basicsize */
  150. sizeof(digit), /* tp_itemsize */
  151. bool_dealloc, /* tp_dealloc */
  152. 0, /* tp_vectorcall_offset */
  153. 0, /* tp_getattr */
  154. 0, /* tp_setattr */
  155. 0, /* tp_as_async */
  156. bool_repr, /* tp_repr */
  157. &bool_as_number, /* tp_as_number */
  158. 0, /* tp_as_sequence */
  159. 0, /* tp_as_mapping */
  160. 0, /* tp_hash */
  161. 0, /* tp_call */
  162. 0, /* tp_str */
  163. 0, /* tp_getattro */
  164. 0, /* tp_setattro */
  165. 0, /* tp_as_buffer */
  166. Py_TPFLAGS_DEFAULT, /* tp_flags */
  167. bool_doc, /* tp_doc */
  168. 0, /* tp_traverse */
  169. 0, /* tp_clear */
  170. 0, /* tp_richcompare */
  171. 0, /* tp_weaklistoffset */
  172. 0, /* tp_iter */
  173. 0, /* tp_iternext */
  174. 0, /* tp_methods */
  175. 0, /* tp_members */
  176. 0, /* tp_getset */
  177. &PyLong_Type, /* tp_base */
  178. 0, /* tp_dict */
  179. 0, /* tp_descr_get */
  180. 0, /* tp_descr_set */
  181. 0, /* tp_dictoffset */
  182. 0, /* tp_init */
  183. 0, /* tp_alloc */
  184. bool_new, /* tp_new */
  185. .tp_vectorcall = bool_vectorcall,
  186. };
  187. /* The objects representing bool values False and True */
  188. struct _longobject _Py_FalseStruct = {
  189. PyObject_HEAD_INIT(&PyBool_Type)
  190. { .lv_tag = _PyLong_FALSE_TAG,
  191. { 0 }
  192. }
  193. };
  194. struct _longobject _Py_TrueStruct = {
  195. PyObject_HEAD_INIT(&PyBool_Type)
  196. { .lv_tag = _PyLong_TRUE_TAG,
  197. { 1 }
  198. }
  199. };