From 5b172978bbf44af7832c3638afe071b10751e664 Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Mon, 8 Apr 2002 13:31:12 +0000 Subject: Add bool(), True, False (as ints) for backwards compatibility. --- Misc/NEWS | 6 +++++- Python/bltinmodule.c | 23 +++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/Misc/NEWS b/Misc/NEWS index fc3902b..108ae17 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -2,7 +2,11 @@ What's New in Python 2.2.1 final? Release date: XX-Apr-2002 ================================= -Core +Core and builtins + +- Added new builtin function bool() and new builtin constants True and + False to ease backporting of code developed for Python 2.3. In 2.2, + bool() returns 1 or 0, True == 1, and False == 0. - Fixed super() to work correctly with class methods. [SF bug #535444] diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c index 41e906a..03b9418 100644 --- a/Python/bltinmodule.c +++ b/Python/bltinmodule.c @@ -107,6 +107,26 @@ Note that classes are callable, as are instances with a __call__() method."; static PyObject * +builtin_bool(PyObject *self, PyObject *x) +{ + long b = PyObject_IsTrue(x); + if (b < 0) + return NULL; + if (b) + x = Py_True; + else + x = Py_False; + Py_INCREF(x); + return x; +} + +static char bool_doc[] = +"bool(x) -> integer\n\ +\n\ +Normalize Boolean: return True (1) when x is true, False (0) otherwise."; + + +static PyObject * builtin_buffer(PyObject *self, PyObject *args) { PyObject *ob; @@ -1773,6 +1793,7 @@ static PyMethodDef builtin_methods[] = { {"__import__", builtin___import__, METH_VARARGS, import_doc}, {"abs", builtin_abs, METH_O, abs_doc}, {"apply", builtin_apply, METH_VARARGS, apply_doc}, + {"bool", builtin_bool, METH_O, bool_doc}, {"buffer", builtin_buffer, METH_VARARGS, buffer_doc}, {"callable", builtin_callable, METH_O, callable_doc}, {"chr", builtin_chr, METH_VARARGS, chr_doc}, @@ -1844,6 +1865,8 @@ _PyBuiltin_Init(void) SETBUILTIN("None", Py_None); SETBUILTIN("Ellipsis", Py_Ellipsis); SETBUILTIN("NotImplemented", Py_NotImplemented); + SETBUILTIN("True", Py_True); + SETBUILTIN("False", Py_False); SETBUILTIN("classmethod", &PyClassMethod_Type); #ifndef WITHOUT_COMPLEX SETBUILTIN("complex", &PyComplex_Type); -- cgit v0.12