]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Add bool(), True, False (as ints) for backwards compatibility.
authorGuido van Rossum <guido@python.org>
Mon, 8 Apr 2002 13:31:12 +0000 (13:31 +0000)
committerGuido van Rossum <guido@python.org>
Mon, 8 Apr 2002 13:31:12 +0000 (13:31 +0000)
Misc/NEWS
Python/bltinmodule.c

index fc3902bd348cdd7ddf8d71355b850f30f730be36..108ae170a53e1084d40914de527cfccd86ec6845 100644 (file)
--- 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]
 
index 41e906adb3e8dbc43a7acd41272dcd0d0ba4bfbc..03b941828b99dcb5c31617b08f02713de153044c 100644 (file)
@@ -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);