2002-04-03 19:01:45 -04:00
|
|
|
/* Boolean type, a subtype of int */
|
|
|
|
|
|
|
|
#include "Python.h"
|
2007-01-13 23:31:43 -04:00
|
|
|
#include "longintrepr.h"
|
2002-04-03 19:01:45 -04:00
|
|
|
|
|
|
|
/* We define bool_repr to return "False" or "True" */
|
|
|
|
|
|
|
|
static PyObject *false_str = NULL;
|
|
|
|
static PyObject *true_str = NULL;
|
|
|
|
|
2002-08-06 19:12:52 -03:00
|
|
|
static PyObject *
|
2007-01-13 23:31:43 -04:00
|
|
|
bool_repr(PyObject *self)
|
2002-04-03 19:01:45 -04:00
|
|
|
{
|
|
|
|
PyObject *s;
|
|
|
|
|
2007-01-13 23:31:43 -04:00
|
|
|
if (self == Py_True)
|
2002-04-03 19:01:45 -04:00
|
|
|
s = true_str ? true_str :
|
2007-05-25 10:52:07 -03:00
|
|
|
(true_str = PyUnicode_InternFromString("True"));
|
2002-04-03 19:01:45 -04:00
|
|
|
else
|
|
|
|
s = false_str ? false_str :
|
2007-05-25 10:52:07 -03:00
|
|
|
(false_str = PyUnicode_InternFromString("False"));
|
2002-04-03 19:01:45 -04:00
|
|
|
Py_XINCREF(s);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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 */
|
|
|
|
|
2002-08-06 19:12:52 -03:00
|
|
|
static PyObject *
|
2002-04-03 19:01:45 -04:00
|
|
|
bool_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
|
|
|
{
|
2006-02-27 12:46:16 -04:00
|
|
|
static char *kwlist[] = {"x", 0};
|
2003-04-19 15:15:10 -03:00
|
|
|
PyObject *x = Py_False;
|
2002-04-03 19:01:45 -04:00
|
|
|
long ok;
|
|
|
|
|
2003-04-19 15:15:10 -03:00
|
|
|
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:bool", kwlist, &x))
|
2002-04-03 19:01:45 -04:00
|
|
|
return NULL;
|
|
|
|
ok = PyObject_IsTrue(x);
|
|
|
|
if (ok < 0)
|
|
|
|
return NULL;
|
|
|
|
return PyBool_FromLong(ok);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Arithmetic operations redefined to return bool if both args are bool. */
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
bool_and(PyObject *a, PyObject *b)
|
|
|
|
{
|
|
|
|
if (!PyBool_Check(a) || !PyBool_Check(b))
|
2007-01-13 23:31:43 -04:00
|
|
|
return PyLong_Type.tp_as_number->nb_and(a, b);
|
|
|
|
return PyBool_FromLong((a == Py_True) & (b == Py_True));
|
2002-04-03 19:01:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
bool_or(PyObject *a, PyObject *b)
|
|
|
|
{
|
|
|
|
if (!PyBool_Check(a) || !PyBool_Check(b))
|
2007-01-13 23:31:43 -04:00
|
|
|
return PyLong_Type.tp_as_number->nb_or(a, b);
|
|
|
|
return PyBool_FromLong((a == Py_True) | (b == Py_True));
|
2002-04-03 19:01:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
bool_xor(PyObject *a, PyObject *b)
|
|
|
|
{
|
|
|
|
if (!PyBool_Check(a) || !PyBool_Check(b))
|
2007-01-13 23:31:43 -04:00
|
|
|
return PyLong_Type.tp_as_number->nb_xor(a, b);
|
|
|
|
return PyBool_FromLong((a == Py_True) ^ (b == Py_True));
|
2002-04-03 19:01:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Doc string */
|
|
|
|
|
2002-06-13 17:33:02 -03:00
|
|
|
PyDoc_STRVAR(bool_doc,
|
2002-04-03 19:01:45 -04:00
|
|
|
"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\
|
2002-06-13 17:33:02 -03:00
|
|
|
The class bool is a subclass of the class int, and cannot be subclassed.");
|
2002-04-03 19:01:45 -04:00
|
|
|
|
|
|
|
/* Arithmetic methods -- only so we can override &, |, ^. */
|
|
|
|
|
|
|
|
static PyNumberMethods bool_as_number = {
|
2006-04-21 07:40:58 -03:00
|
|
|
0, /* nb_add */
|
|
|
|
0, /* nb_subtract */
|
|
|
|
0, /* nb_multiply */
|
|
|
|
0, /* nb_remainder */
|
|
|
|
0, /* nb_divmod */
|
|
|
|
0, /* nb_power */
|
|
|
|
0, /* nb_negative */
|
|
|
|
0, /* nb_positive */
|
|
|
|
0, /* nb_absolute */
|
2006-11-28 15:15:13 -04:00
|
|
|
0, /* nb_bool */
|
2006-04-21 07:40:58 -03:00
|
|
|
0, /* nb_invert */
|
|
|
|
0, /* nb_lshift */
|
|
|
|
0, /* nb_rshift */
|
|
|
|
bool_and, /* nb_and */
|
|
|
|
bool_xor, /* nb_xor */
|
|
|
|
bool_or, /* nb_or */
|
|
|
|
0, /* nb_coerce */
|
|
|
|
0, /* nb_int */
|
|
|
|
0, /* nb_long */
|
|
|
|
0, /* nb_float */
|
|
|
|
0, /* nb_oct */
|
|
|
|
0, /* nb_hex */
|
|
|
|
0, /* nb_inplace_add */
|
|
|
|
0, /* nb_inplace_subtract */
|
|
|
|
0, /* nb_inplace_multiply */
|
|
|
|
0, /* nb_inplace_remainder */
|
|
|
|
0, /* nb_inplace_power */
|
|
|
|
0, /* nb_inplace_lshift */
|
|
|
|
0, /* nb_inplace_rshift */
|
|
|
|
0, /* nb_inplace_and */
|
|
|
|
0, /* nb_inplace_xor */
|
|
|
|
0, /* nb_inplace_or */
|
|
|
|
0, /* nb_floor_divide */
|
|
|
|
0, /* nb_true_divide */
|
|
|
|
0, /* nb_inplace_floor_divide */
|
|
|
|
0, /* nb_inplace_true_divide */
|
2007-01-13 23:31:43 -04:00
|
|
|
0, /* nb_index */
|
2002-04-03 19:01:45 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
/* The type object for bool. Note that this cannot be subclassed! */
|
|
|
|
|
|
|
|
PyTypeObject PyBool_Type = {
|
Merged revisions 56467-56482 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/p3yk
................
r56477 | martin.v.loewis | 2007-07-21 09:04:38 +0200 (Sa, 21 Jul 2007) | 11 lines
Merged revisions 56466-56476 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r56476 | martin.v.loewis | 2007-07-21 08:55:02 +0200 (Sa, 21 Jul 2007) | 4 lines
PEP 3123: Provide forward compatibility with Python 3.0, while keeping
backwards compatibility. Add Py_Refcnt, Py_Type, Py_Size, and
PyVarObject_HEAD_INIT.
........
................
r56478 | martin.v.loewis | 2007-07-21 09:47:23 +0200 (Sa, 21 Jul 2007) | 2 lines
PEP 3123: Use proper C inheritance for PyObject.
................
r56479 | martin.v.loewis | 2007-07-21 10:06:55 +0200 (Sa, 21 Jul 2007) | 3 lines
Add longintrepr.h to Python.h, so that the compiler can
see that PyFalse is really some kind of PyObject*.
................
r56480 | martin.v.loewis | 2007-07-21 10:47:18 +0200 (Sa, 21 Jul 2007) | 2 lines
Qualify SHIFT, MASK, BASE.
................
r56482 | martin.v.loewis | 2007-07-21 19:10:57 +0200 (Sa, 21 Jul 2007) | 2 lines
Correctly refer to _ob_next.
................
2007-07-21 14:22:18 -03:00
|
|
|
PyVarObject_HEAD_INIT(&PyType_Type, 0)
|
2002-04-03 19:01:45 -04:00
|
|
|
"bool",
|
2007-01-13 23:31:43 -04:00
|
|
|
sizeof(struct _longobject),
|
2002-04-03 19:01:45 -04:00
|
|
|
0,
|
|
|
|
0, /* tp_dealloc */
|
2007-08-07 16:51:00 -03:00
|
|
|
0, /* tp_print */
|
2002-04-03 19:01:45 -04:00
|
|
|
0, /* tp_getattr */
|
|
|
|
0, /* tp_setattr */
|
|
|
|
0, /* tp_compare */
|
2007-01-13 23:31:43 -04:00
|
|
|
bool_repr, /* tp_repr */
|
2002-04-03 19:01:45 -04:00
|
|
|
&bool_as_number, /* tp_as_number */
|
|
|
|
0, /* tp_as_sequence */
|
|
|
|
0, /* tp_as_mapping */
|
|
|
|
0, /* tp_hash */
|
|
|
|
0, /* tp_call */
|
2007-01-13 23:31:43 -04:00
|
|
|
bool_repr, /* tp_str */
|
2002-04-03 19:01:45 -04:00
|
|
|
0, /* tp_getattro */
|
|
|
|
0, /* tp_setattro */
|
|
|
|
0, /* tp_as_buffer */
|
2006-07-27 18:53:35 -03:00
|
|
|
Py_TPFLAGS_DEFAULT, /* tp_flags */
|
2002-04-03 19:01:45 -04:00
|
|
|
bool_doc, /* tp_doc */
|
|
|
|
0, /* tp_traverse */
|
|
|
|
0, /* tp_clear */
|
|
|
|
0, /* tp_richcompare */
|
|
|
|
0, /* tp_weaklistoffset */
|
|
|
|
0, /* tp_iter */
|
|
|
|
0, /* tp_iternext */
|
|
|
|
0, /* tp_methods */
|
|
|
|
0, /* tp_members */
|
|
|
|
0, /* tp_getset */
|
2007-01-13 23:31:43 -04:00
|
|
|
&PyLong_Type, /* tp_base */
|
2002-04-03 19:01:45 -04:00
|
|
|
0, /* tp_dict */
|
|
|
|
0, /* tp_descr_get */
|
|
|
|
0, /* tp_descr_set */
|
|
|
|
0, /* tp_dictoffset */
|
|
|
|
0, /* tp_init */
|
|
|
|
0, /* tp_alloc */
|
|
|
|
bool_new, /* tp_new */
|
|
|
|
};
|
|
|
|
|
|
|
|
/* The objects representing bool values False and True */
|
|
|
|
|
|
|
|
/* Named Zero for link-level compatibility */
|
2007-01-13 23:31:43 -04:00
|
|
|
struct _longobject _Py_FalseStruct = {
|
Merged revisions 56467-56482 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/p3yk
................
r56477 | martin.v.loewis | 2007-07-21 09:04:38 +0200 (Sa, 21 Jul 2007) | 11 lines
Merged revisions 56466-56476 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r56476 | martin.v.loewis | 2007-07-21 08:55:02 +0200 (Sa, 21 Jul 2007) | 4 lines
PEP 3123: Provide forward compatibility with Python 3.0, while keeping
backwards compatibility. Add Py_Refcnt, Py_Type, Py_Size, and
PyVarObject_HEAD_INIT.
........
................
r56478 | martin.v.loewis | 2007-07-21 09:47:23 +0200 (Sa, 21 Jul 2007) | 2 lines
PEP 3123: Use proper C inheritance for PyObject.
................
r56479 | martin.v.loewis | 2007-07-21 10:06:55 +0200 (Sa, 21 Jul 2007) | 3 lines
Add longintrepr.h to Python.h, so that the compiler can
see that PyFalse is really some kind of PyObject*.
................
r56480 | martin.v.loewis | 2007-07-21 10:47:18 +0200 (Sa, 21 Jul 2007) | 2 lines
Qualify SHIFT, MASK, BASE.
................
r56482 | martin.v.loewis | 2007-07-21 19:10:57 +0200 (Sa, 21 Jul 2007) | 2 lines
Correctly refer to _ob_next.
................
2007-07-21 14:22:18 -03:00
|
|
|
PyVarObject_HEAD_INIT(&PyBool_Type, 0)
|
|
|
|
{ 0 }
|
2002-04-03 19:01:45 -04:00
|
|
|
};
|
|
|
|
|
2007-01-13 23:31:43 -04:00
|
|
|
struct _longobject _Py_TrueStruct = {
|
Merged revisions 56467-56482 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/p3yk
................
r56477 | martin.v.loewis | 2007-07-21 09:04:38 +0200 (Sa, 21 Jul 2007) | 11 lines
Merged revisions 56466-56476 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r56476 | martin.v.loewis | 2007-07-21 08:55:02 +0200 (Sa, 21 Jul 2007) | 4 lines
PEP 3123: Provide forward compatibility with Python 3.0, while keeping
backwards compatibility. Add Py_Refcnt, Py_Type, Py_Size, and
PyVarObject_HEAD_INIT.
........
................
r56478 | martin.v.loewis | 2007-07-21 09:47:23 +0200 (Sa, 21 Jul 2007) | 2 lines
PEP 3123: Use proper C inheritance for PyObject.
................
r56479 | martin.v.loewis | 2007-07-21 10:06:55 +0200 (Sa, 21 Jul 2007) | 3 lines
Add longintrepr.h to Python.h, so that the compiler can
see that PyFalse is really some kind of PyObject*.
................
r56480 | martin.v.loewis | 2007-07-21 10:47:18 +0200 (Sa, 21 Jul 2007) | 2 lines
Qualify SHIFT, MASK, BASE.
................
r56482 | martin.v.loewis | 2007-07-21 19:10:57 +0200 (Sa, 21 Jul 2007) | 2 lines
Correctly refer to _ob_next.
................
2007-07-21 14:22:18 -03:00
|
|
|
PyVarObject_HEAD_INIT(&PyBool_Type, 1)
|
|
|
|
{ 1 }
|
2002-04-03 19:01:45 -04:00
|
|
|
};
|