Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 23 additions & 44 deletions Objects/boolobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@

#include "Python.h"
#include "pycore_long.h" // FALSE_TAG TRUE_TAG
#include "pycore_modsupport.h" // _PyArg_NoKwnames()
#include "pycore_object.h" // _Py_FatalRefcountError()
#include "pycore_runtime.h" // _Py_ID()

#include <stddef.h>

/*[clinic input]
class bool "PyObject *" "&PyBool_Type"
[clinic start generated code]*/
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=ee492546880a882f]*/

#include "clinic/boolobject.c.h"

/* We define bool_repr to return "False" or "True" */

static PyObject *
Expand All @@ -23,45 +29,28 @@ PyObject *PyBool_FromLong(long ok)
return ok ? Py_True : Py_False;
}

/* We define bool_new to always return either Py_True or Py_False */
/*[clinic input]
@vectorcall
@classmethod
bool.__new__ as bool_new
object as x: object = False
/

static PyObject *
bool_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
PyObject *x = Py_False;
long ok;
Returns True when the argument is true, False otherwise.

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);
}
The builtins True and False are the only two instances of the class
bool. The class bool is a subclass of the class int, and cannot be
subclassed.
[clinic start generated code]*/

static PyObject *
bool_vectorcall(PyObject *type, PyObject * const*args,
size_t nargsf, PyObject *kwnames)
bool_new_impl(PyTypeObject *type, PyObject *x)
/*[clinic end generated code: output=ec434408d43a3773 input=d96e25fce5a04f73]*/
{
long ok = 0;
if (!_PyArg_NoKwnames("bool", kwnames)) {
return NULL;
}

Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
if (!_PyArg_CheckPositional("bool", nargs, 0, 1)) {
int ok = PyObject_IsTrue(x);
if (ok < 0) {
return NULL;
}

assert(PyType_Check(type));
if (nargs) {
ok = PyObject_IsTrue(args[0]);
if (ok < 0) {
return NULL;
}
}
return PyBool_FromLong(ok);
}

Expand Down Expand Up @@ -107,16 +96,6 @@ bool_xor(PyObject *a, PyObject *b)
return PyBool_FromLong((a == Py_True) ^ (b == Py_True));
}

/* Doc string */

PyDoc_STRVAR(bool_doc,
"bool(object=False, /)\n\
--\n\
\n\
Returns True when the argument 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.");

/* Arithmetic methods -- only so we can override &, |, ^. */

static PyNumberMethods bool_as_number = {
Expand Down Expand Up @@ -189,7 +168,7 @@ PyTypeObject PyBool_Type = {
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT, /* tp_flags */
bool_doc, /* tp_doc */
bool_new__doc__, /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
Expand Down
73 changes: 73 additions & 0 deletions Objects/clinic/boolobject.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading