Index: Makefile.pre.in =================================================================== RCS file: /cvsroot/python/python/dist/src/Makefile.pre.in,v retrieving revision 1.78 diff -u -r1.78 Makefile.pre.in --- Makefile.pre.in 29 Mar 2002 16:28:30 -0000 1.78 +++ Makefile.pre.in 1 Apr 2002 02:38:18 -0000 @@ -249,6 +249,7 @@ # Objects OBJECT_OBJS= \ Objects/abstract.o \ + Objects/boolobject.o \ Objects/bufferobject.o \ Objects/cellobject.o \ Objects/classobject.o \ @@ -431,6 +432,7 @@ PYTHON_HEADERS= \ Include/Python.h \ Include/abstract.h \ + Include/boolobject.h \ Include/bufferobject.h \ Include/ceval.h \ Include/classobject.h \ Index: Doc/lib/libfuncs.tex =================================================================== RCS file: /cvsroot/python/python/dist/src/Doc/lib/libfuncs.tex,v retrieving revision 1.102 diff -u -r1.102 libfuncs.tex --- Doc/lib/libfuncs.tex 6 Mar 2002 02:29:30 -0000 1.102 +++ Doc/lib/libfuncs.tex 1 Apr 2002 02:38:22 -0000 @@ -77,6 +77,16 @@ to \code{\var{function}(*\var{args}, **\var{keywords})}. \end{funcdesc} +\begin{funcdesc}{bool}{x} + Convert a value to a Boolean, using the standard truth testing + procedure. If \code{x} is false, this returns \code{False}; + otherwise it returns \code{True}. \code{bool} is also a class, + which is a subclass of \code{int}. Class \code{bool} cannot be + subclassed further. Its only instances are \code{False} and + \code{True}. +\indexii{Boolean}{type} +\end{funcdesc} + \begin{funcdesc}{buffer}{object\optional{, offset\optional{, size}}} The \var{object} argument must be an object that supports the buffer call interface (such as strings, arrays, and buffers). A new buffer Index: Doc/lib/libstdtypes.tex =================================================================== RCS file: /cvsroot/python/python/dist/src/Doc/lib/libstdtypes.tex,v retrieving revision 1.83 diff -u -r1.83 libstdtypes.tex --- Doc/lib/libstdtypes.tex 12 Mar 2002 03:04:44 -0000 1.83 +++ Doc/lib/libstdtypes.tex 1 Apr 2002 02:38:27 -0000 @@ -2,10 +2,8 @@ The following sections describe the standard types that are built into the interpreter. These are the numeric types, sequence types, and -several others, including types themselves. There is no explicit -Boolean type; use integers instead. +several others, including types themselves. \indexii{built-in}{types} -\indexii{Boolean}{type} Some operations are supported by several object types; in particular, all objects can be compared, tested for truth value, and converted to @@ -30,6 +28,9 @@ \item \code{None} \withsubitem{(Built-in object)}{\ttindex{None}} +\item \code{False} + \withsubitem{(Built-in object)}{\ttindex{False}} + \item zero of any numeric type, for example, \code{0}, \code{0L}, \code{0.0}, \code{0j}. @@ -50,11 +51,12 @@ \index{true} Operations and built-in functions that have a Boolean result always -return \code{0} for false and \code{1} for true, unless otherwise -stated. (Important exception: the Boolean operations -\samp{or}\opindex{or} and \samp{and}\opindex{and} always return one of -their operands.) - +return \code{0} or \code{False} for false and \code{1} or \code{True} +for true, unless otherwise stated. (Important exception: the Boolean +operations \samp{or}\opindex{or} and \samp{and}\opindex{and} always +return one of their operands.) +\index{False} +\index{True} \subsection{Boolean Operations \label{boolean}} @@ -68,7 +70,7 @@ {if \var{x} is false, then \var{x}, else \var{y}}{(1)} \hline \lineiii{not \var{x}} - {if \var{x} is false, then \code{1}, else \code{0}}{(2)} + {if \var{x} is false, then \code{True}, else \code{False}}{(2)} \end{tableiii} \opindex{and} \opindex{or} @@ -161,8 +163,10 @@ \subsection{Numeric Types \label{typesnumeric}} -There are four numeric types: \dfn{plain integers}, \dfn{long integers}, +There are four distinct numeric types: \dfn{plain integers}, +\dfn{long integers}, \dfn{floating point numbers}, and \dfn{complex numbers}. +In addition, Booleans are a subtype of plain integers. Plain integers (also just called \dfn{integers}) are implemented using \ctype{long} in C, which gives them at least 32 bits of precision. Long integers have unlimited precision. Floating @@ -170,6 +174,7 @@ their precision are off unless you happen to know the machine you are working with. \obindex{numeric} +\obindex{Boolean} \obindex{integer} \obindex{long integer} \obindex{floating point} @@ -1388,6 +1393,22 @@ \constant{Ellipsis} (a built-in name). It is written as \code{Ellipsis}. + +\subsubsection{Boolean Values} + +Boolean values are the two constant objects \code{False} and +\code{True}. They are used to represent truth values (although other +values can also be considered false or true). In numeric contexts +(for example when used as the argument to an arithmetic operator), +they behave like the integers 0 and 1, respectively. The built-in +function \function{bool()} can be used to cast any value to a Boolean, +if the value can be interpreted as a truth value (see section Truth +Value Testing above). + +They are written as \code{False} and \code{True}, respectively. +\index{False} +\index{True} +\indexii{Boolean}{values} \subsubsection{Internal Objects \label{typesinternal}} Index: Doc/ref/ref3.tex =================================================================== RCS file: /cvsroot/python/python/dist/src/Doc/ref/ref3.tex,v retrieving revision 1.82 diff -u -r1.82 ref3.tex --- Doc/ref/ref3.tex 14 Dec 2001 22:52:41 -0000 1.82 +++ Doc/ref/ref3.tex 1 Apr 2002 02:38:32 -0000 @@ -162,7 +162,7 @@ These represent elements from the mathematical set of whole numbers. \obindex{integer} -There are two types of integers: +There are three types of integers: \begin{description} @@ -187,6 +187,17 @@ an infinite string of sign bits extending to the left. \obindex{long integer} +\item[Booleans] +These represent the truth values False and True. The two objects +representing the values False and True are the only Boolean objects. +The Boolean type is a subtype of plain integers, and Boolean values +behave like the values 0 and 1, respectively, in almost all contexts, +the exception being that when converted to a string, the strings +\code{"False"} or \code{"True"} are returned, respectively. +\obindex{Boolean} +\ttindex{False} +\ttindex{True} + \end{description} % Integers The rules for integer representation are intended to give the most @@ -222,6 +233,7 @@ \end{description} % Numbers + \item[Sequences] These represent finite ordered sets indexed by non-negative numbers. The built-in function \function{len()}\bifuncindex{len} returns the @@ -1071,8 +1083,10 @@ \end{methoddesc} \begin{methoddesc}[object]{__nonzero__}{self} -Called to implement truth value testing; should return \code{0} or -\code{1}. When this method is not defined, \method{__len__()} is +Called to implement truth value testing, and the built-in operation +\code{bool()}; should return \code{False} or \code{True}, or their +integer equivalents \code{0} or \code{1}. +When this method is not defined, \method{__len__()} is called, if it is defined (see below). If a class defines neither \method{__len__()} nor \method{__nonzero__()}, all its instances are considered true. Index: Include/Python.h =================================================================== RCS file: /cvsroot/python/python/dist/src/Include/Python.h,v retrieving revision 2.44 diff -u -r2.44 Python.h --- Include/Python.h 25 Mar 2002 22:21:58 -0000 2.44 +++ Include/Python.h 1 Apr 2002 02:38:33 -0000 @@ -79,6 +79,7 @@ #include "unicodeobject.h" #include "intobject.h" +#include "boolobject.h" #include "longobject.h" #include "floatobject.h" #ifndef WITHOUT_COMPLEX Index: Include/boolobject.h =================================================================== RCS file: Include/boolobject.h diff -N Include/boolobject.h --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ Include/boolobject.h 1 Apr 2002 02:38:33 -0000 @@ -0,0 +1,20 @@ +/* Boolean object interface */ + +typedef PyIntObject PyBoolObject; + +extern DL_IMPORT(PyTypeObject) PyBool_Type; + +#define PyBool_Check(x) ((x)->ob_type == &PyBool_Type) + +/* Py_False and Py_True are the only two bools in existence. +Don't forget to apply Py_INCREF() when returning either!!! */ + +/* Don't use these directly */ +extern DL_IMPORT(PyIntObject) _Py_ZeroStruct, _Py_TrueStruct; + +/* Use these macros */ +#define Py_False ((PyObject *) &_Py_ZeroStruct) +#define Py_True ((PyObject *) &_Py_TrueStruct) + +/* Function to return a bool from a C long */ +PyObject *PyBool_FromLong(long); Index: Include/intobject.h =================================================================== RCS file: /cvsroot/python/python/dist/src/Include/intobject.h,v retrieving revision 2.24 diff -u -r2.24 intobject.h --- Include/intobject.h 10 Sep 2001 20:52:47 -0000 2.24 +++ Include/intobject.h 1 Apr 2002 02:38:33 -0000 @@ -38,21 +38,6 @@ extern DL_IMPORT(long) PyInt_AsLong(PyObject *); extern DL_IMPORT(long) PyInt_GetMax(void); - -/* -False and True are special intobjects used by Boolean expressions. -All values of type Boolean must point to either of these; but in -contexts where integers are required they are integers (valued 0 and 1). -Hope these macros don't conflict with other people's. - -Don't forget to apply Py_INCREF() when returning True or False!!! -*/ - -extern DL_IMPORT(PyIntObject) _Py_ZeroStruct, _Py_TrueStruct; /* Don't use these directly */ - -#define Py_False ((PyObject *) &_Py_ZeroStruct) -#define Py_True ((PyObject *) &_Py_TrueStruct) - /* Macro, trading safety for speed */ #define PyInt_AS_LONG(op) (((PyIntObject *)(op))->ob_ival) Index: Include/object.h =================================================================== RCS file: /cvsroot/python/python/dist/src/Include/object.h,v retrieving revision 2.99 diff -u -r2.99 object.h --- Include/object.h 8 Dec 2001 18:02:50 -0000 2.99 +++ Include/object.h 1 Apr 2002 02:38:35 -0000 @@ -531,8 +531,8 @@ #define Py_DECREF(op) \ if (--_Py_RefTotal, 0 < (--((op)->ob_refcnt))) ; \ else if (0 == (op)->ob_refcnt) _Py_Dealloc( (PyObject*)(op)); \ - else (void)fprintf( stderr, "%s:%i negative ref count %i\n", \ - __FILE__, __LINE__, (op)->ob_refcnt) + else ((void)fprintf( stderr, "%s:%i negative ref count %i\n", \ + __FILE__, __LINE__, (op)->ob_refcnt), abort()) #else /* !Py_REF_DEBUG */ #ifdef COUNT_ALLOCS Index: Lib/test/test_bool.py =================================================================== RCS file: Lib/test/test_bool.py diff -N Lib/test/test_bool.py --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ Lib/test/test_bool.py 1 Apr 2002 02:38:38 -0000 @@ -0,0 +1,210 @@ +# Test properties of bool promised by PEP 285 + +from test_support import verbose, TestFailed, TESTFN, vereq + +def veris(a, b): + if a is not b: + raise TestFailed, "%r is %r" % (a, b) + +def verisnot(a, b): + if a is b: + raise TestFailed, "%r is %r" % (a, b) + +try: + class C(bool): + pass +except TypeError: + pass +else: + raise TestFailed, "bool should not be subclassable" + +try: + int.__new__(bool, 0) +except TypeError: + pass +else: + raise TestFailed, "should not be able to create new bool instances" + +vereq(int(False), 0) +verisnot(int(False), False) +vereq(int(True), 1) +verisnot(int(True), True) + +vereq(+False, 0) +verisnot(+False, False) +vereq(-False, 0) +verisnot(-False, False) +vereq(abs(False), 0) +verisnot(abs(False), False) +vereq(+True, 1) +verisnot(+True, True) +vereq(-True, -1) +vereq(abs(True), 1) +verisnot(abs(True), True) +vereq(~False, -1) +vereq(~True, -2) + +vereq(False+2, 2) +vereq(True+2, 3) +vereq(2+False, 2) +vereq(2+True, 3) + +vereq(False+False, 0) +verisnot(False+False, False) +vereq(False+True, 1) +verisnot(False+True, True) +vereq(True+False, 1) +verisnot(True+False, True) +vereq(True+True, 2) + +vereq(True-True, 0) +verisnot(True-True, False) +vereq(False-False, 0) +verisnot(False-False, False) +vereq(True-False, 1) +verisnot(True-False, True) +vereq(False-True, -1) + +vereq(True*1, 1) +vereq(False*1, 0) +verisnot(False*1, False) + +vereq(True/1, 1) +verisnot(True/1, True) +vereq(False/1, 0) +verisnot(False/1, False) + +for b in False, True: + for i in 0, 1, 2: + vereq(b**i, int(b)**i) + verisnot(b**i, bool(int(b)**i)) + +for a in False, True: + for b in False, True: + veris(a&b, bool(int(a)&int(b))) + veris(a|b, bool(int(a)|int(b))) + veris(a^b, bool(int(a)^int(b))) + vereq(a&int(b), int(a)&int(b)) + verisnot(a&int(b), bool(int(a)&int(b))) + vereq(a|int(b), int(a)|int(b)) + verisnot(a|int(b), bool(int(a)|int(b))) + vereq(a^int(b), int(a)^int(b)) + verisnot(a^int(b), bool(int(a)^int(b))) + vereq(int(a)&b, int(a)&int(b)) + verisnot(int(a)&b, bool(int(a)&int(b))) + vereq(int(a)|b, int(a)|int(b)) + verisnot(int(a)|b, bool(int(a)|int(b))) + vereq(int(a)^b, int(a)^int(b)) + verisnot(int(a)^b, bool(int(a)^int(b))) + +veris(1==1, True) +veris(1==0, False) +# XXX <, <=, >, >=, != + +x = [1] +veris(x is x, True) +veris(x is not x, False) + +veris(1 in x, True) +veris(0 in x, False) +veris(1 not in x, False) +veris(0 not in x, True) + +veris(not True, False) +veris(not False, True) + +veris(bool(10), True) +veris(bool(1), True) +veris(bool(-1), True) +veris(bool(0), False) +veris(bool("hello"), True) +veris(bool(""), False) + +veris(hasattr([], "append"), True) +veris(hasattr([], "wobble"), False) + +veris(callable(len), True) +veris(callable(1), False) + +veris(isinstance(True, bool), True) +veris(isinstance(False, bool), True) +veris(isinstance(True, int), True) +veris(isinstance(False, int), True) +veris(isinstance(1, bool), False) +veris(isinstance(0, bool), False) + +veris(issubclass(bool, int), True) +veris(issubclass(int, bool), False) + +veris({}.has_key(1), False) +veris({1:1}.has_key(1), True) + +veris("xyz".endswith("z"), True) +veris("xyz".endswith("x"), False) +veris("xyz0123".isalnum(), True) +veris("@#$%".isalnum(), False) +veris("xyz".isalpha(), True) +veris("@#$%".isalpha(), False) +veris("0123".isdigit(), True) +veris("xyz".isdigit(), False) +veris("xyz".islower(), True) +veris("XYZ".islower(), False) +veris(" ".isspace(), True) +veris("XYZ".isspace(), False) +veris("X".istitle(), True) +veris("x".istitle(), False) +veris("XYZ".isupper(), True) +veris("xyz".isupper(), False) +veris("xyz".startswith("x"), True) +veris("xyz".startswith("z"), False) + +veris(u"xyz".endswith(u"z"), True) +veris(u"xyz".endswith(u"x"), False) +veris(u"xyz0123".isalnum(), True) +veris(u"@#$%".isalnum(), False) +veris(u"xyz".isalpha(), True) +veris(u"@#$%".isalpha(), False) +veris(u"0123".isdecimal(), True) +veris(u"xyz".isdecimal(), False) +veris(u"0123".isdigit(), True) +veris(u"xyz".isdigit(), False) +veris(u"xyz".islower(), True) +veris(u"XYZ".islower(), False) +veris(u"0123".isnumeric(), True) +veris(u"xyz".isnumeric(), False) +veris(u" ".isspace(), True) +veris(u"XYZ".isspace(), False) +veris(u"X".istitle(), True) +veris(u"x".istitle(), False) +veris(u"XYZ".isupper(), True) +veris(u"xyz".isupper(), False) +veris(u"xyz".startswith(u"x"), True) +veris(u"xyz".startswith(u"z"), False) + +f = file(TESTFN, "w") +veris(f.closed, False) +f.close() +veris(f.closed, True) +import os +os.remove(TESTFN) + +import operator +veris(operator.truth(0), False) +veris(operator.truth(1), True) +veris(operator.isCallable(0), False) +veris(operator.isCallable(len), True) +veris(operator.isNumberType(None), False) +veris(operator.isNumberType(0), True) +veris(operator.not_(1), False) +veris(operator.not_(0), True) +veris(operator.isSequenceType(0), False) +veris(operator.isSequenceType([]), True) +veris(operator.contains([], 1), False) +veris(operator.contains([1], 1), True) +veris(operator.isMappingType([]), False) +veris(operator.isMappingType({}), True) +veris(operator.lt(0, 0), False) +veris(operator.lt(0, 1), True) + +if verbose: + print "All OK" Index: Lib/test/output/test_augassign =================================================================== RCS file: /cvsroot/python/python/dist/src/Lib/test/output/test_augassign,v retrieving revision 1.2 diff -u -r1.2 test_augassign --- Lib/test/output/test_augassign 29 Aug 2001 17:50:27 -0000 1.2 +++ Lib/test/output/test_augassign 1 Apr 2002 02:38:40 -0000 @@ -4,14 +4,14 @@ 6 [1, 2, 3, 4, 1, 2, 3, 4] [1, 2, 1, 2, 3] -1 -1 -1 +True +True +True 11 -1 +True 12 -1 -1 +True +True 13 __add__ called __radd__ called Index: Lib/test/output/test_grammar =================================================================== RCS file: /cvsroot/python/python/dist/src/Lib/test/output/test_grammar,v retrieving revision 1.18 diff -u -r1.18 test_grammar --- Lib/test/output/test_grammar 26 Sep 2001 12:43:39 -0000 1.18 +++ Lib/test/output/test_grammar 1 Apr 2002 02:38:40 -0000 @@ -60,6 +60,6 @@ [3, 4, 5] [(1, 'Apple'), (1, 'Banana'), (1, 'Coconut'), (2, 'Apple'), (2, 'Banana'), (2, 'Coconut'), (3, 'Apple'), (3, 'Banana'), (3, 'Coconut'), (4, 'Apple'), (4, 'Banana'), (4, 'Coconut'), (5, 'Apple'), (5, 'Banana'), (5, 'Coconut')] [(1, 'Banana'), (1, 'Coconut'), (2, 'Banana'), (2, 'Coconut'), (3, 'Banana'), (3, 'Coconut'), (4, 'Banana'), (4, 'Coconut'), (5, 'Banana'), (5, 'Coconut')] -[0, 0, 0] +[False, False, False] [[1, 2], [3, 4], [5, 6]] [('Boeing', 'Airliner'), ('Boeing', 'Engine'), ('Ford', 'Engine'), ('Macdonalds', 'Cheeseburger')] Index: Modules/operator.c =================================================================== RCS file: /cvsroot/python/python/dist/src/Modules/operator.c,v retrieving revision 2.18 diff -u -r2.18 operator.c --- Modules/operator.c 9 Aug 2001 20:14:34 -0000 2.18 +++ Modules/operator.c 1 Apr 2002 02:38:43 -0000 @@ -102,7 +102,7 @@ PyObject *a1; long r; \ if(! PyArg_ParseTuple(a,"O:" #OP,&a1)) return NULL; \ if(-1 == (r=AOP(a1))) return NULL; \ - return PyInt_FromLong(r); } + return PyBool_FromLong(r); } #define spami2(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \ PyObject *a1, *a2; long r; \ @@ -110,6 +110,12 @@ if(-1 == (r=AOP(a1,a2))) return NULL; \ return PyInt_FromLong(r); } +#define spami2b(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \ + PyObject *a1, *a2; long r; \ + if(! PyArg_ParseTuple(a,"OO:" #OP,&a1,&a2)) return NULL; \ + if(-1 == (r=AOP(a1,a2))) return NULL; \ + return PyBool_FromLong(r); } + #define spamrc(OP,A) static PyObject *OP(PyObject *s, PyObject *a) { \ PyObject *a1, *a2; \ if(! PyArg_ParseTuple(a,"OO:" #OP,&a1,&a2)) return NULL; \ @@ -139,8 +145,8 @@ spami(isSequenceType , PySequence_Check) spam2(op_concat , PySequence_Concat) spamoi(op_repeat , PySequence_Repeat) -spami2(op_contains , PySequence_Contains) -spami2(sequenceIncludes, PySequence_Contains) +spami2b(op_contains , PySequence_Contains) +spami2b(sequenceIncludes, PySequence_Contains) spami2(indexOf , PySequence_Index) spami2(countOf , PySequence_Count) spami(isMappingType , PyMapping_Check) @@ -208,11 +214,11 @@ spam1(isCallable, "isCallable(a) -- Same as callable(a).") spam1(isNumberType, - "isNumberType(a) -- Return 1 if a has a numeric type, and zero otherwise.") + "isNumberType(a) -- Return True if a has a numeric type, False otherwise.") spam1(isSequenceType, - "isSequenceType(a) -- Return 1 if a has a sequence type, and zero otherwise.") + "isSequenceType(a) -- Return True if a has a sequence type, False otherwise.") spam1(truth, - "truth(a) -- Return 1 if a is true, and 0 otherwise.") + "truth(a) -- Return True if a is true, False otherwise.") spam2(contains,__contains__, "contains(a, b) -- Same as b in a (note reversed operands).") spam1(sequenceIncludes, @@ -222,7 +228,7 @@ spam1(countOf, "countOf(a, b) -- Return the number of times b occurs in a.") spam1(isMappingType, - "isMappingType(a) -- Return 1 if a has a mapping type, and zero otherwise.") + "isMappingType(a) -- Return True if a has a mapping type, False otherwise.") spam2(add,__add__, "add(a, b) -- Same as a + b.") spam2(sub,__sub__, "sub(a, b) -- Same as a - b.") Index: Objects/dictobject.c =================================================================== RCS file: /cvsroot/python/python/dist/src/Objects/dictobject.c,v retrieving revision 2.122 diff -u -r2.122 dictobject.c --- Objects/dictobject.c 29 Mar 2002 03:29:07 -0000 2.122 +++ Objects/dictobject.c 1 Apr 2002 02:38:47 -0000 @@ -1429,7 +1429,7 @@ return NULL; } ok = (mp->ma_lookup)(mp, key, hash)->me_value != NULL; - return PyInt_FromLong(ok); + return PyBool_FromLong(ok); } static PyObject * Index: Objects/fileobject.c =================================================================== RCS file: /cvsroot/python/python/dist/src/Objects/fileobject.c,v retrieving revision 2.152 diff -u -r2.152 fileobject.c --- Objects/fileobject.c 23 Mar 2002 19:41:34 -0000 2.152 +++ Objects/fileobject.c 1 Apr 2002 02:38:50 -0000 @@ -1444,7 +1444,7 @@ static char close_doc[] = "close() -> None or (perhaps) an integer. Close the file.\n" "\n" -"Sets data attribute .closed to true. A closed file cannot be used for\n" +"Sets data attribute .closed to True. A closed file cannot be used for\n" "further I/O operations. close() may be called more than once without\n" "error. Some kinds of file objects (for example, opened by popen())\n" "may return an exit status upon closing."; @@ -1488,11 +1488,11 @@ static PyObject * get_closed(PyFileObject *f, void *closure) { - return PyInt_FromLong((long)(f->f_fp == 0)); + return PyBool_FromLong((long)(f->f_fp == 0)); } static PyGetSetDef file_getsetlist[] = { - {"closed", (getter)get_closed, NULL, "flag set if the file is closed"}, + {"closed", (getter)get_closed, NULL, "True if the file is closed"}, {0}, }; Index: Objects/intobject.c =================================================================== RCS file: /cvsroot/python/python/dist/src/Objects/intobject.c,v retrieving revision 2.80 diff -u -r2.80 intobject.c --- Objects/intobject.c 1 Feb 2002 15:34:10 -0000 2.80 +++ Objects/intobject.c 1 Apr 2002 02:38:53 -0000 @@ -10,18 +10,6 @@ return LONG_MAX; /* To initialize sys.maxint */ } -/* Standard Booleans */ - -PyIntObject _Py_ZeroStruct = { - PyObject_HEAD_INIT(&PyInt_Type) - 0 -}; - -PyIntObject _Py_TrueStruct = { - PyObject_HEAD_INIT(&PyInt_Type) - 1 -}; - /* Return 1 if exception raised, 0 if caller should retry using longs */ static int err_ovf(char *msg) Index: Objects/object.c =================================================================== RCS file: /cvsroot/python/python/dist/src/Objects/object.c,v retrieving revision 2.169 diff -u -r2.169 object.c --- Objects/object.c 29 Mar 2002 03:05:54 -0000 2.169 +++ Objects/object.c 1 Apr 2002 02:38:57 -0000 @@ -1763,6 +1763,9 @@ if (PyType_Ready(&PyType_Type) < 0) Py_FatalError("Can't initialize 'type'"); + if (PyType_Ready(&PyBool_Type) < 0) + Py_FatalError("Can't initialize 'bool'"); + if (PyType_Ready(&PyList_Type) < 0) Py_FatalError("Can't initialize 'list'"); Index: Objects/stringobject.c =================================================================== RCS file: /cvsroot/python/python/dist/src/Objects/stringobject.c,v retrieving revision 2.153 diff -u -r2.153 stringobject.c --- Objects/stringobject.c 30 Mar 2002 10:06:07 -0000 2.153 +++ Objects/stringobject.c 1 Apr 2002 02:39:02 -0000 @@ -2000,9 +2000,9 @@ static char startswith__doc__[] = -"S.startswith(prefix[, start[, end]]) -> int\n\ +"S.startswith(prefix[, start[, end]]) -> bool\n\ \n\ -Return 1 if S starts with the specified prefix, otherwise return 0. With\n\ +Return True if S starts with the specified prefix, False otherwise. With\n\ optional start, test S beginning at that position. With optional end, stop\n\ comparing S at that position."; @@ -2032,7 +2032,7 @@ if (rc == -1) return NULL; else - return PyInt_FromLong((long) rc); + return PyBool_FromLong((long) rc); } #endif else if (PyObject_AsCharBuffer(subobj, &prefix, &plen)) @@ -2043,25 +2043,25 @@ * the empty string. */ if (start < 0 || start+plen > len) - return PyInt_FromLong(0); + return PyBool_FromLong(0); if (!memcmp(str+start, prefix, plen)) { /* did the match end after the specified end? */ if (end < 0) - return PyInt_FromLong(1); + return PyBool_FromLong(1); else if (end - start < plen) - return PyInt_FromLong(0); + return PyBool_FromLong(0); else - return PyInt_FromLong(1); + return PyBool_FromLong(1); } - else return PyInt_FromLong(0); + else return PyBool_FromLong(0); } static char endswith__doc__[] = -"S.endswith(suffix[, start[, end]]) -> int\n\ +"S.endswith(suffix[, start[, end]]) -> bool\n\ \n\ -Return 1 if S ends with the specified suffix, otherwise return 0. With\n\ +Return True if S ends with the specified suffix, False otherwise. With\n\ optional start, test S beginning at that position. With optional end, stop\n\ comparing S at that position."; @@ -2092,21 +2092,21 @@ if (rc == -1) return NULL; else - return PyInt_FromLong((long) rc); + return PyBool_FromLong((long) rc); } #endif else if (PyObject_AsCharBuffer(subobj, &suffix, &slen)) return NULL; if (start < 0 || start > len || slen > len) - return PyInt_FromLong(0); + return PyBool_FromLong(0); upper = (end >= 0 && end <= len) ? end : len; lower = (upper - slen) > start ? (upper - slen) : start; if (upper-lower >= slen && !memcmp(str+lower, suffix, slen)) - return PyInt_FromLong(1); - else return PyInt_FromLong(0); + return PyBool_FromLong(1); + else return PyBool_FromLong(0); } @@ -2311,10 +2311,10 @@ } static char isspace__doc__[] = -"S.isspace() -> int\n" +"S.isspace() -> bool\n" "\n" -"Return 1 if there are only whitespace characters in S,\n" -"0 otherwise."; +"Return True if there are only whitespace characters in S,\n" +"False otherwise."; static PyObject* string_isspace(PyStringObject *self) @@ -2326,26 +2326,26 @@ /* Shortcut for single character strings */ if (PyString_GET_SIZE(self) == 1 && isspace(*p)) - return PyInt_FromLong(1); + return PyBool_FromLong(1); /* Special case for empty strings */ if (PyString_GET_SIZE(self) == 0) - return PyInt_FromLong(0); + return PyBool_FromLong(0); e = p + PyString_GET_SIZE(self); for (; p < e; p++) { if (!isspace(*p)) - return PyInt_FromLong(0); + return PyBool_FromLong(0); } - return PyInt_FromLong(1); + return PyBool_FromLong(1); } static char isalpha__doc__[] = -"S.isalpha() -> int\n\ +"S.isalpha() -> bool\n\ \n\ -Return 1 if all characters in S are alphabetic\n\ -and there is at least one character in S, 0 otherwise."; +Return True if all characters in S are alphabetic\n\ +and there is at least one character in S, False otherwise."; static PyObject* string_isalpha(PyStringObject *self) @@ -2357,26 +2357,26 @@ /* Shortcut for single character strings */ if (PyString_GET_SIZE(self) == 1 && isalpha(*p)) - return PyInt_FromLong(1); + return PyBool_FromLong(1); /* Special case for empty strings */ if (PyString_GET_SIZE(self) == 0) - return PyInt_FromLong(0); + return PyBool_FromLong(0); e = p + PyString_GET_SIZE(self); for (; p < e; p++) { if (!isalpha(*p)) - return PyInt_FromLong(0); + return PyBool_FromLong(0); } - return PyInt_FromLong(1); + return PyBool_FromLong(1); } static char isalnum__doc__[] = -"S.isalnum() -> int\n\ +"S.isalnum() -> bool\n\ \n\ -Return 1 if all characters in S are alphanumeric\n\ -and there is at least one character in S, 0 otherwise."; +Return True if all characters in S are alphanumeric\n\ +and there is at least one character in S, False otherwise."; static PyObject* string_isalnum(PyStringObject *self) @@ -2388,26 +2388,26 @@ /* Shortcut for single character strings */ if (PyString_GET_SIZE(self) == 1 && isalnum(*p)) - return PyInt_FromLong(1); + return PyBool_FromLong(1); /* Special case for empty strings */ if (PyString_GET_SIZE(self) == 0) - return PyInt_FromLong(0); + return PyBool_FromLong(0); e = p + PyString_GET_SIZE(self); for (; p < e; p++) { if (!isalnum(*p)) - return PyInt_FromLong(0); + return PyBool_FromLong(0); } - return PyInt_FromLong(1); + return PyBool_FromLong(1); } static char isdigit__doc__[] = -"S.isdigit() -> int\n\ +"S.isdigit() -> bool\n\ \n\ -Return 1 if there are only digit characters in S,\n\ -0 otherwise."; +Return True if there are only digit characters in S,\n\ +False otherwise."; static PyObject* string_isdigit(PyStringObject *self) @@ -2419,26 +2419,26 @@ /* Shortcut for single character strings */ if (PyString_GET_SIZE(self) == 1 && isdigit(*p)) - return PyInt_FromLong(1); + return PyBool_FromLong(1); /* Special case for empty strings */ if (PyString_GET_SIZE(self) == 0) - return PyInt_FromLong(0); + return PyBool_FromLong(0); e = p + PyString_GET_SIZE(self); for (; p < e; p++) { if (!isdigit(*p)) - return PyInt_FromLong(0); + return PyBool_FromLong(0); } - return PyInt_FromLong(1); + return PyBool_FromLong(1); } static char islower__doc__[] = -"S.islower() -> int\n\ +"S.islower() -> bool\n\ \n\ -Return 1 if all cased characters in S are lowercase and there is\n\ -at least one cased character in S, 0 otherwise."; +Return True if all cased characters in S are lowercase and there is\n\ +at least one cased character in S, False otherwise."; static PyObject* string_islower(PyStringObject *self) @@ -2450,29 +2450,29 @@ /* Shortcut for single character strings */ if (PyString_GET_SIZE(self) == 1) - return PyInt_FromLong(islower(*p) != 0); + return PyBool_FromLong(islower(*p) != 0); /* Special case for empty strings */ if (PyString_GET_SIZE(self) == 0) - return PyInt_FromLong(0); + return PyBool_FromLong(0); e = p + PyString_GET_SIZE(self); cased = 0; for (; p < e; p++) { if (isupper(*p)) - return PyInt_FromLong(0); + return PyBool_FromLong(0); else if (!cased && islower(*p)) cased = 1; } - return PyInt_FromLong(cased); + return PyBool_FromLong(cased); } static char isupper__doc__[] = -"S.isupper() -> int\n\ +"S.isupper() -> bool\n\ \n\ -Return 1 if all cased characters in S are uppercase and there is\n\ -at least one cased character in S, 0 otherwise."; +Return True if all cased characters in S are uppercase and there is\n\ +at least one cased character in S, False otherwise."; static PyObject* string_isupper(PyStringObject *self) @@ -2484,30 +2484,30 @@ /* Shortcut for single character strings */ if (PyString_GET_SIZE(self) == 1) - return PyInt_FromLong(isupper(*p) != 0); + return PyBool_FromLong(isupper(*p) != 0); /* Special case for empty strings */ if (PyString_GET_SIZE(self) == 0) - return PyInt_FromLong(0); + return PyBool_FromLong(0); e = p + PyString_GET_SIZE(self); cased = 0; for (; p < e; p++) { if (islower(*p)) - return PyInt_FromLong(0); + return PyBool_FromLong(0); else if (!cased && isupper(*p)) cased = 1; } - return PyInt_FromLong(cased); + return PyBool_FromLong(cased); } static char istitle__doc__[] = -"S.istitle() -> int\n\ +"S.istitle() -> bool\n\ \n\ -Return 1 if S is a titlecased string, i.e. uppercase characters\n\ +Return True if S is a titlecased string, i.e. uppercase characters\n\ may only follow uncased characters and lowercase characters only cased\n\ -ones. Return 0 otherwise."; +ones. Return False otherwise."; static PyObject* string_istitle(PyStringObject *self, PyObject *uncased) @@ -2519,11 +2519,11 @@ /* Shortcut for single character strings */ if (PyString_GET_SIZE(self) == 1) - return PyInt_FromLong(isupper(*p) != 0); + return PyBool_FromLong(isupper(*p) != 0); /* Special case for empty strings */ if (PyString_GET_SIZE(self) == 0) - return PyInt_FromLong(0); + return PyBool_FromLong(0); e = p + PyString_GET_SIZE(self); cased = 0; @@ -2533,20 +2533,20 @@ if (isupper(ch)) { if (previous_is_cased) - return PyInt_FromLong(0); + return PyBool_FromLong(0); previous_is_cased = 1; cased = 1; } else if (islower(ch)) { if (!previous_is_cased) - return PyInt_FromLong(0); + return PyBool_FromLong(0); previous_is_cased = 1; cased = 1; } else previous_is_cased = 0; } - return PyInt_FromLong(cased); + return PyBool_FromLong(cased); } Index: Objects/unicodeobject.c =================================================================== RCS file: /cvsroot/python/python/dist/src/Objects/unicodeobject.c,v retrieving revision 2.133 diff -u -r2.133 unicodeobject.c --- Objects/unicodeobject.c 25 Mar 2002 11:16:18 -0000 2.133 +++ Objects/unicodeobject.c 1 Apr 2002 02:39:13 -0000 @@ -4109,10 +4109,10 @@ } static char islower__doc__[] = -"S.islower() -> int\n\ +"S.islower() -> bool\n\ \n\ -Return 1 if all cased characters in S are lowercase and there is\n\ -at least one cased character in S, 0 otherwise."; +Return True if all cased characters in S are lowercase and there is\n\ +at least one cased character in S, False otherwise."; static PyObject* unicode_islower(PyUnicodeObject *self) @@ -4123,11 +4123,11 @@ /* Shortcut for single character strings */ if (PyUnicode_GET_SIZE(self) == 1) - return PyInt_FromLong(Py_UNICODE_ISLOWER(*p) != 0); + return PyBool_FromLong(Py_UNICODE_ISLOWER(*p)); /* Special case for empty strings */ if (PyString_GET_SIZE(self) == 0) - return PyInt_FromLong(0); + return PyBool_FromLong(0); e = p + PyUnicode_GET_SIZE(self); cased = 0; @@ -4135,18 +4135,18 @@ register const Py_UNICODE ch = *p; if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) - return PyInt_FromLong(0); + return PyBool_FromLong(0); else if (!cased && Py_UNICODE_ISLOWER(ch)) cased = 1; } - return PyInt_FromLong(cased); + return PyBool_FromLong(cased); } static char isupper__doc__[] = -"S.isupper() -> int\n\ +"S.isupper() -> bool\n\ \n\ -Return 1 if all cased characters in S are uppercase and there is\n\ -at least one cased character in S, 0 otherwise."; +Return True if all cased characters in S are uppercase and there is\n\ +at least one cased character in S, False otherwise."; static PyObject* unicode_isupper(PyUnicodeObject *self) @@ -4157,11 +4157,11 @@ /* Shortcut for single character strings */ if (PyUnicode_GET_SIZE(self) == 1) - return PyInt_FromLong(Py_UNICODE_ISUPPER(*p) != 0); + return PyBool_FromLong(Py_UNICODE_ISUPPER(*p) != 0); /* Special case for empty strings */ if (PyString_GET_SIZE(self) == 0) - return PyInt_FromLong(0); + return PyBool_FromLong(0); e = p + PyUnicode_GET_SIZE(self); cased = 0; @@ -4169,19 +4169,19 @@ register const Py_UNICODE ch = *p; if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) - return PyInt_FromLong(0); + return PyBool_FromLong(0); else if (!cased && Py_UNICODE_ISUPPER(ch)) cased = 1; } - return PyInt_FromLong(cased); + return PyBool_FromLong(cased); } static char istitle__doc__[] = -"S.istitle() -> int\n\ +"S.istitle() -> bool\n\ \n\ -Return 1 if S is a titlecased string, i.e. upper- and titlecase characters\n\ -may only follow uncased characters and lowercase characters only cased\n\ -ones. Return 0 otherwise."; +Return True if S is a titlecased string, i.e. upper- and titlecase\n\ +characters may only follow uncased characters and lowercase characters\n\ +only cased ones. Return False otherwise."; static PyObject* unicode_istitle(PyUnicodeObject *self) @@ -4192,12 +4192,12 @@ /* Shortcut for single character strings */ if (PyUnicode_GET_SIZE(self) == 1) - return PyInt_FromLong((Py_UNICODE_ISTITLE(*p) != 0) || - (Py_UNICODE_ISUPPER(*p) != 0)); + return PyBool_FromLong((Py_UNICODE_ISTITLE(*p) != 0) || + (Py_UNICODE_ISUPPER(*p) != 0)); /* Special case for empty strings */ if (PyString_GET_SIZE(self) == 0) - return PyInt_FromLong(0); + return PyBool_FromLong(0); e = p + PyUnicode_GET_SIZE(self); cased = 0; @@ -4207,27 +4207,27 @@ if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { if (previous_is_cased) - return PyInt_FromLong(0); + return PyBool_FromLong(0); previous_is_cased = 1; cased = 1; } else if (Py_UNICODE_ISLOWER(ch)) { if (!previous_is_cased) - return PyInt_FromLong(0); + return PyBool_FromLong(0); previous_is_cased = 1; cased = 1; } else previous_is_cased = 0; } - return PyInt_FromLong(cased); + return PyBool_FromLong(cased); } static char isspace__doc__[] = -"S.isspace() -> int\n\ +"S.isspace() -> bool\n\ \n\ -Return 1 if there are only whitespace characters in S,\n\ -0 otherwise."; +Return True if there are only whitespace characters in S,\n\ +False otherwise."; static PyObject* unicode_isspace(PyUnicodeObject *self) @@ -4238,25 +4238,25 @@ /* Shortcut for single character strings */ if (PyUnicode_GET_SIZE(self) == 1 && Py_UNICODE_ISSPACE(*p)) - return PyInt_FromLong(1); + return PyBool_FromLong(1); /* Special case for empty strings */ if (PyString_GET_SIZE(self) == 0) - return PyInt_FromLong(0); + return PyBool_FromLong(0); e = p + PyUnicode_GET_SIZE(self); for (; p < e; p++) { if (!Py_UNICODE_ISSPACE(*p)) - return PyInt_FromLong(0); + return PyBool_FromLong(0); } - return PyInt_FromLong(1); + return PyBool_FromLong(1); } static char isalpha__doc__[] = -"S.isalpha() -> int\n\ +"S.isalpha() -> bool\n\ \n\ -Return 1 if all characters in S are alphabetic\n\ -and there is at least one character in S, 0 otherwise."; +Return True if all characters in S are alphabetic\n\ +and there is at least one character in S, False otherwise."; static PyObject* unicode_isalpha(PyUnicodeObject *self) @@ -4267,25 +4267,25 @@ /* Shortcut for single character strings */ if (PyUnicode_GET_SIZE(self) == 1 && Py_UNICODE_ISALPHA(*p)) - return PyInt_FromLong(1); + return PyBool_FromLong(1); /* Special case for empty strings */ if (PyString_GET_SIZE(self) == 0) - return PyInt_FromLong(0); + return PyBool_FromLong(0); e = p + PyUnicode_GET_SIZE(self); for (; p < e; p++) { if (!Py_UNICODE_ISALPHA(*p)) - return PyInt_FromLong(0); + return PyBool_FromLong(0); } - return PyInt_FromLong(1); + return PyBool_FromLong(1); } static char isalnum__doc__[] = -"S.isalnum() -> int\n\ +"S.isalnum() -> bool\n\ \n\ -Return 1 if all characters in S are alphanumeric\n\ -and there is at least one character in S, 0 otherwise."; +Return True if all characters in S are alphanumeric\n\ +and there is at least one character in S, False otherwise."; static PyObject* unicode_isalnum(PyUnicodeObject *self) @@ -4296,25 +4296,25 @@ /* Shortcut for single character strings */ if (PyUnicode_GET_SIZE(self) == 1 && Py_UNICODE_ISALNUM(*p)) - return PyInt_FromLong(1); + return PyBool_FromLong(1); /* Special case for empty strings */ if (PyString_GET_SIZE(self) == 0) - return PyInt_FromLong(0); + return PyBool_FromLong(0); e = p + PyUnicode_GET_SIZE(self); for (; p < e; p++) { if (!Py_UNICODE_ISALNUM(*p)) - return PyInt_FromLong(0); + return PyBool_FromLong(0); } - return PyInt_FromLong(1); + return PyBool_FromLong(1); } static char isdecimal__doc__[] = -"S.isdecimal() -> int\n\ +"S.isdecimal() -> bool\n\ \n\ -Return 1 if there are only decimal characters in S,\n\ -0 otherwise."; +Return True if there are only decimal characters in S,\n\ +False otherwise."; static PyObject* unicode_isdecimal(PyUnicodeObject *self) @@ -4325,25 +4325,25 @@ /* Shortcut for single character strings */ if (PyUnicode_GET_SIZE(self) == 1 && Py_UNICODE_ISDECIMAL(*p)) - return PyInt_FromLong(1); + return PyBool_FromLong(1); /* Special case for empty strings */ if (PyString_GET_SIZE(self) == 0) - return PyInt_FromLong(0); + return PyBool_FromLong(0); e = p + PyUnicode_GET_SIZE(self); for (; p < e; p++) { if (!Py_UNICODE_ISDECIMAL(*p)) - return PyInt_FromLong(0); + return PyBool_FromLong(0); } - return PyInt_FromLong(1); + return PyBool_FromLong(1); } static char isdigit__doc__[] = -"S.isdigit() -> int\n\ +"S.isdigit() -> bool\n\ \n\ -Return 1 if there are only digit characters in S,\n\ -0 otherwise."; +Return True if there are only digit characters in S,\n\ +False otherwise."; static PyObject* unicode_isdigit(PyUnicodeObject *self) @@ -4354,25 +4354,25 @@ /* Shortcut for single character strings */ if (PyUnicode_GET_SIZE(self) == 1 && Py_UNICODE_ISDIGIT(*p)) - return PyInt_FromLong(1); + return PyBool_FromLong(1); /* Special case for empty strings */ if (PyString_GET_SIZE(self) == 0) - return PyInt_FromLong(0); + return PyBool_FromLong(0); e = p + PyUnicode_GET_SIZE(self); for (; p < e; p++) { if (!Py_UNICODE_ISDIGIT(*p)) - return PyInt_FromLong(0); + return PyBool_FromLong(0); } - return PyInt_FromLong(1); + return PyBool_FromLong(1); } static char isnumeric__doc__[] = -"S.isnumeric() -> int\n\ +"S.isnumeric() -> bool\n\ \n\ -Return 1 if there are only numeric characters in S,\n\ -0 otherwise."; +Return True if there are only numeric characters in S,\n\ +False otherwise."; static PyObject* unicode_isnumeric(PyUnicodeObject *self) @@ -4383,18 +4383,18 @@ /* Shortcut for single character strings */ if (PyUnicode_GET_SIZE(self) == 1 && Py_UNICODE_ISNUMERIC(*p)) - return PyInt_FromLong(1); + return PyBool_FromLong(1); /* Special case for empty strings */ if (PyString_GET_SIZE(self) == 0) - return PyInt_FromLong(0); + return PyBool_FromLong(0); e = p + PyUnicode_GET_SIZE(self); for (; p < e; p++) { if (!Py_UNICODE_ISNUMERIC(*p)) - return PyInt_FromLong(0); + return PyBool_FromLong(0); } - return PyInt_FromLong(1); + return PyBool_FromLong(1); } static char join__doc__[] = @@ -4862,9 +4862,9 @@ #endif static char startswith__doc__[] = -"S.startswith(prefix[, start[, end]]) -> int\n\ +"S.startswith(prefix[, start[, end]]) -> bool\n\ \n\ -Return 1 if S starts with the specified prefix, otherwise return 0. With\n\ +Return True if S starts with the specified prefix, False otherwise. With\n\ optional start, test S beginning at that position. With optional end, stop\n\ comparing S at that position."; @@ -4885,7 +4885,7 @@ if (substring == NULL) return NULL; - result = PyInt_FromLong(tailmatch(self, substring, start, end, -1)); + result = PyBool_FromLong(tailmatch(self, substring, start, end, -1)); Py_DECREF(substring); return result; @@ -4893,9 +4893,9 @@ static char endswith__doc__[] = -"S.endswith(suffix[, start[, end]]) -> int\n\ +"S.endswith(suffix[, start[, end]]) -> bool\n\ \n\ -Return 1 if S ends with the specified suffix, otherwise return 0. With\n\ +Return True if S ends with the specified suffix, False otherwise. With\n\ optional start, test S beginning at that position. With optional end, stop\n\ comparing S at that position."; @@ -4916,7 +4916,7 @@ if (substring == NULL) return NULL; - result = PyInt_FromLong(tailmatch(self, substring, start, end, +1)); + result = PyBool_FromLong(tailmatch(self, substring, start, end, +1)); Py_DECREF(substring); return result; Index: Python/bltinmodule.c =================================================================== RCS file: /cvsroot/python/python/dist/src/Python/bltinmodule.c,v retrieving revision 2.249 diff -u -r2.249 bltinmodule.c --- Python/bltinmodule.c 9 Mar 2002 12:07:51 -0000 2.249 +++ Python/bltinmodule.c 1 Apr 2002 02:39:16 -0000 @@ -130,11 +130,11 @@ static PyObject * builtin_callable(PyObject *self, PyObject *v) { - return PyInt_FromLong((long)PyCallable_Check(v)); + return PyBool_FromLong((long)PyCallable_Check(v)); } static char callable_doc[] = -"callable(object) -> Boolean\n\ +"callable(object) -> bool\n\ \n\ Return whether the object is callable (i.e., some kind of function).\n\ Note that classes are callable, as are instances with a __call__() method."; @@ -713,7 +713,7 @@ } static char hasattr_doc[] = -"hasattr(object, name) -> Boolean\n\ +"hasattr(object, name) -> bool\n\ \n\ Return whether the object has an attribute with the given name.\n\ (This is done by calling getattr(object, name) and catching exceptions.)"; @@ -1666,11 +1666,11 @@ retval = PyObject_IsInstance(inst, cls); if (retval < 0) return NULL; - return PyInt_FromLong(retval); + return PyBool_FromLong(retval); } static char isinstance_doc[] = -"isinstance(object, class-or-type-or-tuple) -> Boolean\n\ +"isinstance(object, class-or-type-or-tuple) -> bool\n\ \n\ Return whether an object is an instance of a class or of a subclass thereof.\n\ With a type as second argument, return whether that is the object's type.\n\ @@ -1691,11 +1691,11 @@ retval = PyObject_IsSubclass(derived, cls); if (retval < 0) return NULL; - return PyInt_FromLong(retval); + return PyBool_FromLong(retval); } static char issubclass_doc[] = -"issubclass(C, B) -> Boolean\n\ +"issubclass(C, B) -> bool\n\ \n\ Return whether class C is a subclass (i.e., a derived class) of class B."; @@ -1856,6 +1856,9 @@ SETBUILTIN("None", Py_None); SETBUILTIN("Ellipsis", Py_Ellipsis); SETBUILTIN("NotImplemented", Py_NotImplemented); + SETBUILTIN("False", Py_False); + SETBUILTIN("True", Py_True); + SETBUILTIN("bool", &PyBool_Type); SETBUILTIN("classmethod", &PyClassMethod_Type); #ifndef WITHOUT_COMPLEX SETBUILTIN("complex", &PyComplex_Type); @@ -1879,7 +1882,7 @@ #ifdef Py_USING_UNICODE SETBUILTIN("unicode", &PyUnicode_Type); #endif - debug = PyInt_FromLong(Py_OptimizeFlag == 0); + debug = PyBool_FromLong(Py_OptimizeFlag == 0); if (PyDict_SetItemString(dict, "__debug__", debug) < 0) { Py_XDECREF(debug); return NULL; Index: Objects/boolobject.c =================================================================== RCS file: Objects/boolobject.c diff -N Objects/boolobject.c --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ Objects/boolobject.c 1 Apr 2002 02:42:12 -0000 @@ -0,0 +1,212 @@ +/* Boolean type, a subtype of int */ + +#include "Python.h" + +/* We need to define bool_print to override int_print */ + +static int +bool_print(PyBoolObject *self, FILE *fp, int flags) +{ + if (flags & Py_PRINT_RAW) { + if (self->ob_ival == 0) + fputs("False", fp); + else + fputs("True", fp); + } + else { + if (self->ob_ival == 0) + fputs("False", fp); + else + fputs("True", fp); + } + return 0; +} + +/* We define bool_repr to return "False" or "True" */ + +static PyObject *false_str = NULL; +static PyObject *true_str = NULL; + +PyObject * +bool_repr(PyBoolObject *self) +{ + PyObject *s; + + if (self->ob_ival) + s = true_str ? true_str : + (true_str = PyString_InternFromString("True")); + else + s = false_str ? false_str : + (false_str = PyString_InternFromString("False")); + 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 */ + +PyObject * +bool_new(PyTypeObject *type, PyObject *args, PyObject *kwds) +{ + static char *kwlist[] = {"x", 0}; + PyObject *x; + long ok; + + if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:bool", kwlist, &x)) + 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)) + return PyInt_Type.tp_as_number->nb_and(a, b); + return PyBool_FromLong( + ((PyBoolObject *)a)->ob_ival & ((PyBoolObject *)b)->ob_ival); +} + +static PyObject * +bool_or(PyObject *a, PyObject *b) +{ + if (!PyBool_Check(a) || !PyBool_Check(b)) + return PyInt_Type.tp_as_number->nb_or(a, b); + return PyBool_FromLong( + ((PyBoolObject *)a)->ob_ival | ((PyBoolObject *)b)->ob_ival); +} + +static PyObject * +bool_xor(PyObject *a, PyObject *b) +{ + if (!PyBool_Check(a) || !PyBool_Check(b)) + return PyInt_Type.tp_as_number->nb_xor(a, b); + return PyBool_FromLong( + ((PyBoolObject *)a)->ob_ival ^ ((PyBoolObject *)b)->ob_ival); +} + +/* Doc string */ + +static char bool_doc[] = +"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\ +The class bool is a subclass of the class int, and cannot be subclassed."; + +/* Arithmetic methods -- only so we can override &, |, ^. */ + +static PyNumberMethods bool_as_number = { + 0, /*nb_add*/ + 0, /*nb_subtract*/ + 0, /*nb_multiply*/ + 0, /*nb_divide*/ + 0, /*nb_remainder*/ + 0, /*nb_divmod*/ + 0, /*nb_power*/ + 0, /*nb_negative*/ + 0, /*nb_positive*/ + 0, /*nb_absolute*/ + 0, /*nb_nonzero*/ + 0, /*nb_invert*/ + 0, /*nb_lshift*/ + 0, /*nb_rshift*/ + (binaryfunc)bool_and, /*nb_and*/ + (binaryfunc)bool_xor, /*nb_xor*/ + (binaryfunc)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_divide*/ + 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 */ +}; + +/* The type object for bool. Note that this cannot be subclassed! */ + +PyTypeObject PyBool_Type = { + PyObject_HEAD_INIT(&PyType_Type) + 0, + "bool", + sizeof(PyIntObject), + 0, + 0, /* tp_dealloc */ + (printfunc)bool_print, /* tp_print */ + 0, /* tp_getattr */ + 0, /* tp_setattr */ + 0, /* tp_compare */ + (reprfunc)bool_repr, /* tp_repr */ + &bool_as_number, /* tp_as_number */ + 0, /* tp_as_sequence */ + 0, /* tp_as_mapping */ + 0, /* tp_hash */ + 0, /* tp_call */ + (reprfunc)bool_repr, /* tp_str */ + 0, /* tp_getattro */ + 0, /* tp_setattro */ + 0, /* tp_as_buffer */ + Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES, /* tp_flags */ + 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 */ + &PyInt_Type, /* tp_base */ + 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 */ +PyIntObject _Py_ZeroStruct = { + PyObject_HEAD_INIT(&PyBool_Type) + 0 +}; + +PyIntObject _Py_TrueStruct = { + PyObject_HEAD_INIT(&PyBool_Type) + 1 +};