#include "Python.h"
#include "pycore_object.h"
#include "pycore_long.h"
#include "pycore_runtime.h"
#include <stddef.h>
static PyObject *
bool_repr(PyObject *self)
{
PyObject *res = self == Py_True ? &_Py_ID(True) : &_Py_ID(False);
return Py_NewRef(res);
}
PyObject *PyBool_FromLong(long ok)
{
return ok ? Py_True : Py_False;
}
static PyObject *
bool_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
PyObject *x = Py_False;
long ok;
if (!_PyArg_NoKeywords("bool", kwds))
return NULL;
if (!PyArg_UnpackTuple(args, "bool", 0, 1, &x))
return NULL;
ok = PyObject_IsTrue(x);
if (ok < 0)
return NULL;
return PyBool_FromLong(ok);
}
static PyObject *
bool_vectorcall(PyObject *type, PyObject * const*args,
size_t nargsf, PyObject *kwnames)
{
long ok = 0;
if (!_PyArg_NoKwnames("bool", kwnames)) {
return NULL;
}
Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
if (!_PyArg_CheckPositional("bool", nargs, 0, 1)) {
return NULL;
}
assert(PyType_Check(type));
if (nargs) {
ok = PyObject_IsTrue(args[0]);
if (ok < 0) {
return NULL;
}
}
return PyBool_FromLong(ok);
}
static PyObject *
bool_invert(PyObject *v)
{
if (PyErr_WarnEx(PyExc_DeprecationWarning,
"Bitwise inversion '~' on bool is deprecated. This "
"returns the bitwise inversion of the underlying int "
"object and is usually not what you expect from negating "
"a bool. Use the 'not' operator for boolean negation or "
"~int(x) if you really want the bitwise inversion of the "
"underlying int.",
1) < 0) {
return NULL;
}
return PyLong_Type.tp_as_number->nb_invert(v);
}
static PyObject *
bool_and(PyObject *a, PyObject *b)
{
if (!PyBool_Check(a) || !PyBool_Check(b))
return PyLong_Type.tp_as_number->nb_and(a, b);
return PyBool_FromLong((a == Py_True) & (b == Py_True));
}
static PyObject *
bool_or(PyObject *a, PyObject *b)
{
if (!PyBool_Check(a) || !PyBool_Check(b))
return PyLong_Type.tp_as_number->nb_or(a, b);
return PyBool_FromLong((a == Py_True) | (b == Py_True));
}
static PyObject *
bool_xor(PyObject *a, PyObject *b)
{
if (!PyBool_Check(a) || !PyBool_Check(b))
return PyLong_Type.tp_as_number->nb_xor(a, b);
return PyBool_FromLong((a == Py_True) ^ (b == Py_True));
}
PyDoc_STRVAR(bool_doc,
"bool(x) -> bool\n\
\n\
Returns True when the argument x is true, False otherwise.\n\
The builtins True and False are the only two instances of the class bool.\n\
The class bool is a subclass of the class int, and cannot be subclassed.");
static PyNumberMethods bool_as_number = {
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
(unaryfunc)bool_invert,
0,
0,
bool_and,
bool_xor,
bool_or,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
};
static void
bool_dealloc(PyObject *boolean)
{
_Py_SetImmortal(boolean);
}
PyTypeObject PyBool_Type = {
PyVarObject_HEAD_INIT(&PyType_Type, 0)
"bool",
offsetof(struct _longobject, long_value.ob_digit),
sizeof(digit),
bool_dealloc,
0,
0,
0,
0,
bool_repr,
&bool_as_number,
0,
0,
0,
0,
0,
0,
0,
0,
Py_TPFLAGS_DEFAULT,
bool_doc,
0,
0,
0,
0,
0,
0,
0,
0,
0,
&PyLong_Type,
0,
0,
0,
0,
0,
0,
bool_new,
.tp_vectorcall = bool_vectorcall,
};
struct _longobject _Py_FalseStruct = {
PyObject_HEAD_INIT(&PyBool_Type)
{ .lv_tag = _PyLong_FALSE_TAG,
{ 0 }
}
};
struct _longobject _Py_TrueStruct = {
PyObject_HEAD_INIT(&PyBool_Type)
{ .lv_tag = _PyLong_TRUE_TAG,
{ 1 }
}
};