Skip to content
Snippets Groups Projects
Select Git revision
  • 84d58ad17baec0dda8a5ffd8d925343391e45b3e
  • main default protected
  • 3.10
  • 3.11
  • revert-15688-bpo-38031-_io-FileIO-opener-crash
  • 3.8
  • 3.9
  • 3.7
  • enum-fix_auto
  • branch-v3.11.0
  • backport-c3648f4-3.11
  • gh-93963/remove-importlib-resources-abcs
  • refactor-wait_for
  • shared-testcase
  • v3.12.0a2
  • v3.12.0a1
  • v3.11.0
  • v3.8.15
  • v3.9.15
  • v3.10.8
  • v3.7.15
  • v3.11.0rc2
  • v3.8.14
  • v3.9.14
  • v3.7.14
  • v3.10.7
  • v3.11.0rc1
  • v3.10.6
  • v3.11.0b5
  • v3.11.0b4
  • v3.10.5
  • v3.11.0b3
  • v3.11.0b2
  • v3.9.13
34 results

boolobject.c

Blame
  • 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);
    }