Select Git revision
boolobject.c
boolobject.c 7.08 KiB
/* Boolean type, a subtype of int */
#include "Python.h"
#include "pycore_object.h" // _Py_FatalRefcountError()
#include "pycore_runtime.h" // _Py_ID()
/* We define bool_repr to return "False" or "True" */
static PyObject *
bool_repr(PyObject *self)
{
PyObject *res = self == Py_True ? &_Py_ID(True) : &_Py_ID(False);
return Py_NewRef(res);
}
/* Function to return a bool from a C long */
PyObject *PyBool_FromLong(long ok)
{
PyObject *result;
if (ok)
result = Py_True;
else
result = Py_False;
Py_INCREF(result);
return result;
}
/* We define bool_new to always return either Py_True or 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);
}