You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

210 lines
7.1 KiB

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