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.

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