From: Guido van Rossum Date: Mon, 8 Apr 2002 13:31:12 +0000 (+0000) Subject: Add bool(), True, False (as ints) for backwards compatibility. X-Git-Tag: v2.2.1~5 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=5b172978bbf44af7832c3638afe071b10751e664;p=thirdparty%2FPython%2Fcpython.git Add bool(), True, False (as ints) for backwards compatibility. --- diff --git a/Misc/NEWS b/Misc/NEWS index fc3902bd348c..108ae170a53e 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 41e906adb3e8..03b941828b99 100644 --- a/Python/bltinmodule.c +++ b/Python/bltinmodule.c @@ -106,6 +106,26 @@ and keyword arguments taken from the optional dictionary kwargs.\n\ 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) { @@ -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);