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.

185 lines
6.6 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. /* Arithmetic operations redefined to return bool if both args are bool. */
  47. static PyObject *
  48. bool_and(PyObject *a, PyObject *b)
  49. {
  50. if (!PyBool_Check(a) || !PyBool_Check(b))
  51. return PyLong_Type.tp_as_number->nb_and(a, b);
  52. return PyBool_FromLong((a == Py_True) & (b == Py_True));
  53. }
  54. static PyObject *
  55. bool_or(PyObject *a, PyObject *b)
  56. {
  57. if (!PyBool_Check(a) || !PyBool_Check(b))
  58. return PyLong_Type.tp_as_number->nb_or(a, b);
  59. return PyBool_FromLong((a == Py_True) | (b == Py_True));
  60. }
  61. static PyObject *
  62. bool_xor(PyObject *a, PyObject *b)
  63. {
  64. if (!PyBool_Check(a) || !PyBool_Check(b))
  65. return PyLong_Type.tp_as_number->nb_xor(a, b);
  66. return PyBool_FromLong((a == Py_True) ^ (b == Py_True));
  67. }
  68. /* Doc string */
  69. PyDoc_STRVAR(bool_doc,
  70. "bool(x) -> bool\n\
  71. \n\
  72. Returns True when the argument x is true, False otherwise.\n\
  73. The builtins True and False are the only two instances of the class bool.\n\
  74. The class bool is a subclass of the class int, and cannot be subclassed.");
  75. /* Arithmetic methods -- only so we can override &, |, ^. */
  76. static PyNumberMethods bool_as_number = {
  77. 0, /* nb_add */
  78. 0, /* nb_subtract */
  79. 0, /* nb_multiply */
  80. 0, /* nb_remainder */
  81. 0, /* nb_divmod */
  82. 0, /* nb_power */
  83. 0, /* nb_negative */
  84. 0, /* nb_positive */
  85. 0, /* nb_absolute */
  86. 0, /* nb_bool */
  87. 0, /* nb_invert */
  88. 0, /* nb_lshift */
  89. 0, /* nb_rshift */
  90. bool_and, /* nb_and */
  91. bool_xor, /* nb_xor */
  92. bool_or, /* nb_or */
  93. 0, /* nb_int */
  94. 0, /* nb_reserved */
  95. 0, /* nb_float */
  96. 0, /* nb_inplace_add */
  97. 0, /* nb_inplace_subtract */
  98. 0, /* nb_inplace_multiply */
  99. 0, /* nb_inplace_remainder */
  100. 0, /* nb_inplace_power */
  101. 0, /* nb_inplace_lshift */
  102. 0, /* nb_inplace_rshift */
  103. 0, /* nb_inplace_and */
  104. 0, /* nb_inplace_xor */
  105. 0, /* nb_inplace_or */
  106. 0, /* nb_floor_divide */
  107. 0, /* nb_true_divide */
  108. 0, /* nb_inplace_floor_divide */
  109. 0, /* nb_inplace_true_divide */
  110. 0, /* nb_index */
  111. };
  112. /* The type object for bool. Note that this cannot be subclassed! */
  113. PyTypeObject PyBool_Type = {
  114. PyVarObject_HEAD_INIT(&PyType_Type, 0)
  115. "bool",
  116. sizeof(struct _longobject),
  117. 0,
  118. 0, /* tp_dealloc */
  119. 0, /* tp_vectorcall_offset */
  120. 0, /* tp_getattr */
  121. 0, /* tp_setattr */
  122. 0, /* tp_as_async */
  123. bool_repr, /* tp_repr */
  124. &bool_as_number, /* tp_as_number */
  125. 0, /* tp_as_sequence */
  126. 0, /* tp_as_mapping */
  127. 0, /* tp_hash */
  128. 0, /* tp_call */
  129. 0, /* tp_str */
  130. 0, /* tp_getattro */
  131. 0, /* tp_setattro */
  132. 0, /* tp_as_buffer */
  133. Py_TPFLAGS_DEFAULT, /* tp_flags */
  134. bool_doc, /* tp_doc */
  135. 0, /* tp_traverse */
  136. 0, /* tp_clear */
  137. 0, /* tp_richcompare */
  138. 0, /* tp_weaklistoffset */
  139. 0, /* tp_iter */
  140. 0, /* tp_iternext */
  141. 0, /* tp_methods */
  142. 0, /* tp_members */
  143. 0, /* tp_getset */
  144. &PyLong_Type, /* tp_base */
  145. 0, /* tp_dict */
  146. 0, /* tp_descr_get */
  147. 0, /* tp_descr_set */
  148. 0, /* tp_dictoffset */
  149. 0, /* tp_init */
  150. 0, /* tp_alloc */
  151. bool_new, /* tp_new */
  152. };
  153. /* The objects representing bool values False and True */
  154. struct _longobject _Py_FalseStruct = {
  155. PyVarObject_HEAD_INIT(&PyBool_Type, 0)
  156. { 0 }
  157. };
  158. struct _longobject _Py_TrueStruct = {
  159. PyVarObject_HEAD_INIT(&PyBool_Type, 1)
  160. { 1 }
  161. };