diff options
author | Guido van Rossum <guido@python.org> | 2002-04-08 13:31:12 +0000 |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 2002-04-08 13:31:12 +0000 |
commit | 5b172978bbf44af7832c3638afe071b10751e664 (patch) | |
tree | 0162e8a57915405e1b06d8fd2c2167a13d426b5a | |
parent | Do not call "knee" a standard module. (diff) | |
download | cpython-5b172978bbf44af7832c3638afe071b10751e664.tar.gz cpython-5b172978bbf44af7832c3638afe071b10751e664.tar.bz2 cpython-5b172978bbf44af7832c3638afe071b10751e664.zip |
Add bool(), True, False (as ints) for backwards compatibility.
-rw-r--r-- | Misc/NEWS | 6 | ||||
-rw-r--r-- | Python/bltinmodule.c | 23 |
2 files changed, 28 insertions, 1 deletions
diff --git a/Misc/NEWS b/Misc/NEWS index fc3902bd348..108ae170a53 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 41e906adb3e..03b941828b9 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); |