Index: Makefile.pre.in =================================================================== RCS file: /cvsroot/python/python/dist/src/Makefile.pre.in,v retrieving revision 1.76 diff -c -r1.76 Makefile.pre.in *** Makefile.pre.in 28 Feb 2002 19:26:08 -0000 1.76 --- Makefile.pre.in 10 Mar 2002 05:40:22 -0000 *************** *** 245,250 **** --- 245,251 ---- # Objects OBJECT_OBJS= \ Objects/abstract.o \ + Objects/boolobject.o \ Objects/bufferobject.o \ Objects/cellobject.o \ Objects/classobject.o \ *************** *** 435,440 **** --- 436,442 ---- 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 -c -r1.102 libfuncs.tex *** Doc/lib/libfuncs.tex 6 Mar 2002 02:29:30 -0000 1.102 --- Doc/lib/libfuncs.tex 10 Mar 2002 05:40:29 -0000 *************** *** 77,82 **** --- 77,92 ---- 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.81 diff -c -r1.81 libstdtypes.tex *** Doc/lib/libstdtypes.tex 26 Dec 2001 20:06:40 -0000 1.81 --- Doc/lib/libstdtypes.tex 10 Mar 2002 05:40:32 -0000 *************** *** 2,11 **** 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. \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 --- 2,9 ---- 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. \indexii{built-in}{types} Some operations are supported by several object types; in particular, all objects can be compared, tested for truth value, and converted to *************** *** 30,35 **** --- 28,36 ---- \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,60 **** \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.) ! \subsection{Boolean Operations \label{boolean}} --- 51,62 ---- \index{true} Operations and built-in functions that have a Boolean result always ! 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,74 **** {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)} \end{tableiii} \opindex{and} \opindex{or} --- 70,76 ---- {if \var{x} is false, then \var{x}, else \var{y}}{(1)} \hline \lineiii{not \var{x}} ! {if \var{x} is false, then \code{True}, else \code{False}}{(2)} \end{tableiii} \opindex{and} \opindex{or} *************** *** 161,168 **** \subsection{Numeric Types \label{typesnumeric}} ! There are four numeric types: \dfn{plain integers}, \dfn{long integers}, \dfn{floating point numbers}, and \dfn{complex numbers}. 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 --- 163,172 ---- \subsection{Numeric Types \label{typesnumeric}} ! 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,175 **** --- 174,180 ---- 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} *************** *** 1384,1389 **** --- 1389,1410 ---- \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 -c -r1.82 ref3.tex *** Doc/ref/ref3.tex 14 Dec 2001 22:52:41 -0000 1.82 --- Doc/ref/ref3.tex 10 Mar 2002 05:40:37 -0000 *************** *** 162,168 **** These represent elements from the mathematical set of whole numbers. \obindex{integer} ! There are two types of integers: \begin{description} --- 162,168 ---- These represent elements from the mathematical set of whole numbers. \obindex{integer} ! There are three types of integers: \begin{description} *************** *** 187,192 **** --- 187,203 ---- 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,227 **** --- 233,239 ---- \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,1078 **** \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, if it is defined (see below). If a class defines neither \method{__len__()} nor \method{__nonzero__()}, all its instances are considered true. --- 1083,1092 ---- \end{methoddesc} \begin{methoddesc}[object]{__nonzero__}{self} ! 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.41 diff -c -r2.41 Python.h *** Include/Python.h 12 Jan 2002 11:05:03 -0000 2.41 --- Include/Python.h 10 Mar 2002 05:40:37 -0000 *************** *** 70,75 **** --- 70,76 ---- #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 10 Mar 2002 05:40:37 -0000 *************** *** 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 -c -r2.24 intobject.h *** Include/intobject.h 10 Sep 2001 20:52:47 -0000 2.24 --- Include/intobject.h 10 Mar 2002 05:40:38 -0000 *************** *** 38,58 **** 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) --- 38,43 ---- 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 10 Mar 2002 05:40:40 -0000 *************** *** 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: Modules/operator.c =================================================================== RCS file: /cvsroot/python/python/dist/src/Modules/operator.c,v retrieving revision 2.18 diff -c -r2.18 operator.c *** Modules/operator.c 9 Aug 2001 20:14:34 -0000 2.18 --- Modules/operator.c 10 Mar 2002 05:40:45 -0000 *************** *** 102,108 **** PyObject *a1; long r; \ if(! PyArg_ParseTuple(a,"O:" #OP,&a1)) return NULL; \ if(-1 == (r=AOP(a1))) return NULL; \ ! return PyInt_FromLong(r); } #define spami2(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \ PyObject *a1, *a2; long r; \ --- 102,108 ---- PyObject *a1; long r; \ if(! PyArg_ParseTuple(a,"O:" #OP,&a1)) return NULL; \ if(-1 == (r=AOP(a1))) return NULL; \ ! return PyBool_FromLong(r); } #define spami2(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \ PyObject *a1, *a2; long r; \ *************** *** 110,115 **** --- 110,121 ---- 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,146 **** spami(isSequenceType , PySequence_Check) spam2(op_concat , PySequence_Concat) spamoi(op_repeat , PySequence_Repeat) ! spami2(op_contains , PySequence_Contains) ! spami2(sequenceIncludes, PySequence_Contains) spami2(indexOf , PySequence_Index) spami2(countOf , PySequence_Count) spami(isMappingType , PyMapping_Check) --- 145,152 ---- spami(isSequenceType , PySequence_Check) spam2(op_concat , PySequence_Concat) spamoi(op_repeat , PySequence_Repeat) ! spami2b(op_contains , PySequence_Contains) ! spami2b(sequenceIncludes, PySequence_Contains) spami2(indexOf , PySequence_Index) spami2(countOf , PySequence_Count) spami(isMappingType , PyMapping_Check) *************** *** 208,218 **** spam1(isCallable, "isCallable(a) -- Same as callable(a).") spam1(isNumberType, ! "isNumberType(a) -- Return 1 if a has a numeric type, and zero otherwise.") spam1(isSequenceType, ! "isSequenceType(a) -- Return 1 if a has a sequence type, and zero otherwise.") spam1(truth, ! "truth(a) -- Return 1 if a is true, and 0 otherwise.") spam2(contains,__contains__, "contains(a, b) -- Same as b in a (note reversed operands).") spam1(sequenceIncludes, --- 214,224 ---- spam1(isCallable, "isCallable(a) -- Same as callable(a).") spam1(isNumberType, ! "isNumberType(a) -- Return True if a has a numeric type, False otherwise.") spam1(isSequenceType, ! "isSequenceType(a) -- Return True if a has a sequence type, False otherwise.") spam1(truth, ! "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,228 **** 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.") spam2(add,__add__, "add(a, b) -- Same as a + b.") spam2(sub,__sub__, "sub(a, b) -- Same as a - b.") --- 228,234 ---- spam1(countOf, "countOf(a, b) -- Return the number of times b occurs in a.") spam1(isMappingType, ! "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/boolobject.c =================================================================== RCS file: Objects/boolobject.c diff -N Objects/boolobject.c *** /dev/null 1 Jan 1970 00:00:00 -0000 --- Objects/boolobject.c 10 Mar 2002 05:40:46 -0000 *************** *** 0 **** --- 1,221 ---- + /* 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) + { + if (self->ob_ival) + return true_str ? true_str : + (true_str = PyString_InternFromString("True")); + else + return false_str ? false_str : + (false_str = PyString_InternFromString("False")); + } + + /* We define bool_str to return "0" or "1" */ + + PyObject * + bool_str(PyBoolObject *self) + { + if (self->ob_ival) + return true_str ? true_str : + (true_str = PyString_InternFromString("True")); + else + return false_str ? false_str : + (false_str = PyString_InternFromString("False")); + } + + /* 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_str, /* 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 + }; Index: Objects/dictobject.c =================================================================== RCS file: /cvsroot/python/python/dist/src/Objects/dictobject.c,v retrieving revision 2.119 diff -c -r2.119 dictobject.c *** Objects/dictobject.c 11 Dec 2001 18:51:08 -0000 2.119 --- Objects/dictobject.c 10 Mar 2002 05:40:50 -0000 *************** *** 1446,1452 **** return NULL; } ok = (mp->ma_lookup)(mp, key, hash)->me_value != NULL; ! return PyInt_FromLong(ok); } static PyObject * --- 1446,1452 ---- return NULL; } ok = (mp->ma_lookup)(mp, key, hash)->me_value != NULL; ! return PyBool_FromLong(ok); } static PyObject * Index: Objects/fileobject.c =================================================================== RCS file: /cvsroot/python/python/dist/src/Objects/fileobject.c,v retrieving revision 2.144 diff -c -r2.144 fileobject.c *** Objects/fileobject.c 26 Feb 2002 11:36:35 -0000 2.144 --- Objects/fileobject.c 10 Mar 2002 05:40:53 -0000 *************** *** 1378,1384 **** 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" "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."; --- 1378,1384 ---- 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" "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."; *************** *** 1422,1432 **** static PyObject * get_closed(PyFileObject *f, void *closure) { ! return PyInt_FromLong((long)(f->f_fp == 0)); } static PyGetSetDef file_getsetlist[] = { ! {"closed", (getter)get_closed, NULL, "flag set if the file is closed"}, {0}, }; --- 1422,1432 ---- static PyObject * get_closed(PyFileObject *f, void *closure) { ! return PyBool_FromLong((long)(f->f_fp == 0)); } static PyGetSetDef file_getsetlist[] = { ! {"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 -c -r2.80 intobject.c *** Objects/intobject.c 1 Feb 2002 15:34:10 -0000 2.80 --- Objects/intobject.c 10 Mar 2002 05:40:55 -0000 *************** *** 10,27 **** 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) --- 10,15 ---- Index: Objects/object.c =================================================================== RCS file: /cvsroot/python/python/dist/src/Objects/object.c,v retrieving revision 2.164 diff -c -r2.164 object.c *** Objects/object.c 2 Mar 2002 08:43:19 -0000 2.164 --- Objects/object.c 10 Mar 2002 05:40:59 -0000 *************** *** 1755,1760 **** --- 1755,1763 ---- 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.150 diff -c -r2.150 stringobject.c *** Objects/stringobject.c 28 Feb 2002 11:38:24 -0000 2.150 --- Objects/stringobject.c 10 Mar 2002 05:41:06 -0000 *************** *** 2030,2038 **** static char startswith__doc__[] = ! "S.startswith(prefix[, start[, end]]) -> int\n\ \n\ ! Return 1 if S starts with the specified prefix, otherwise return 0. With\n\ optional start, test S beginning at that position. With optional end, stop\n\ comparing S at that position."; --- 2030,2038 ---- static char startswith__doc__[] = ! "S.startswith(prefix[, start[, end]]) -> bool\n\ \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."; *************** *** 2062,2068 **** if (rc == -1) return NULL; else ! return PyInt_FromLong((long) rc); } #endif else if (PyObject_AsCharBuffer(subobj, &prefix, &plen)) --- 2062,2068 ---- if (rc == -1) return NULL; else ! return PyBool_FromLong((long) rc); } #endif else if (PyObject_AsCharBuffer(subobj, &prefix, &plen)) *************** *** 2073,2097 **** * the empty string. */ if (start < 0 || start+plen > len) ! return PyInt_FromLong(0); if (!memcmp(str+start, prefix, plen)) { /* did the match end after the specified end? */ if (end < 0) ! return PyInt_FromLong(1); else if (end - start < plen) ! return PyInt_FromLong(0); else ! return PyInt_FromLong(1); } ! else return PyInt_FromLong(0); } static char endswith__doc__[] = ! "S.endswith(suffix[, start[, end]]) -> int\n\ \n\ ! Return 1 if S ends with the specified suffix, otherwise return 0. With\n\ optional start, test S beginning at that position. With optional end, stop\n\ comparing S at that position."; --- 2073,2097 ---- * the empty string. */ if (start < 0 || start+plen > len) ! return PyBool_FromLong(0); if (!memcmp(str+start, prefix, plen)) { /* did the match end after the specified end? */ if (end < 0) ! return PyBool_FromLong(1); else if (end - start < plen) ! return PyBool_FromLong(0); else ! return PyBool_FromLong(1); } ! else return PyBool_FromLong(0); } static char endswith__doc__[] = ! "S.endswith(suffix[, start[, end]]) -> bool\n\ \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."; *************** *** 2122,2142 **** if (rc == -1) return NULL; else ! return PyInt_FromLong((long) rc); } #endif else if (PyObject_AsCharBuffer(subobj, &suffix, &slen)) return NULL; if (start < 0 || start > len || slen > len) ! return PyInt_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); } --- 2122,2142 ---- if (rc == -1) return NULL; else ! return PyBool_FromLong((long) rc); } #endif else if (PyObject_AsCharBuffer(subobj, &suffix, &slen)) return NULL; if (start < 0 || start > len || slen > len) ! 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 PyBool_FromLong(1); ! else return PyBool_FromLong(0); } *************** *** 2341,2350 **** } static char isspace__doc__[] = ! "S.isspace() -> int\n" "\n" ! "Return 1 if there are only whitespace characters in S,\n" ! "0 otherwise."; static PyObject* string_isspace(PyStringObject *self) --- 2341,2350 ---- } static char isspace__doc__[] = ! "S.isspace() -> bool\n" "\n" ! "Return True if there are only whitespace characters in S,\n" ! "False otherwise."; static PyObject* string_isspace(PyStringObject *self) *************** *** 2356,2381 **** /* Shortcut for single character strings */ if (PyString_GET_SIZE(self) == 1 && isspace(*p)) ! return PyInt_FromLong(1); /* Special case for empty strings */ if (PyString_GET_SIZE(self) == 0) ! return PyInt_FromLong(0); e = p + PyString_GET_SIZE(self); for (; p < e; p++) { if (!isspace(*p)) ! return PyInt_FromLong(0); } ! return PyInt_FromLong(1); } static char isalpha__doc__[] = ! "S.isalpha() -> int\n\ \n\ ! Return 1 if all characters in S are alphabetic\n\ ! and there is at least one character in S, 0 otherwise."; static PyObject* string_isalpha(PyStringObject *self) --- 2356,2381 ---- /* Shortcut for single character strings */ if (PyString_GET_SIZE(self) == 1 && isspace(*p)) ! return PyBool_FromLong(1); /* Special case for empty strings */ if (PyString_GET_SIZE(self) == 0) ! return PyBool_FromLong(0); e = p + PyString_GET_SIZE(self); for (; p < e; p++) { if (!isspace(*p)) ! return PyBool_FromLong(0); } ! return PyBool_FromLong(1); } static char isalpha__doc__[] = ! "S.isalpha() -> bool\n\ \n\ ! 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) *************** *** 2387,2412 **** /* Shortcut for single character strings */ if (PyString_GET_SIZE(self) == 1 && isalpha(*p)) ! return PyInt_FromLong(1); /* Special case for empty strings */ if (PyString_GET_SIZE(self) == 0) ! return PyInt_FromLong(0); e = p + PyString_GET_SIZE(self); for (; p < e; p++) { if (!isalpha(*p)) ! return PyInt_FromLong(0); } ! return PyInt_FromLong(1); } static char isalnum__doc__[] = ! "S.isalnum() -> int\n\ \n\ ! Return 1 if all characters in S are alphanumeric\n\ ! and there is at least one character in S, 0 otherwise."; static PyObject* string_isalnum(PyStringObject *self) --- 2387,2412 ---- /* Shortcut for single character strings */ if (PyString_GET_SIZE(self) == 1 && isalpha(*p)) ! return PyBool_FromLong(1); /* Special case for empty strings */ if (PyString_GET_SIZE(self) == 0) ! return PyBool_FromLong(0); e = p + PyString_GET_SIZE(self); for (; p < e; p++) { if (!isalpha(*p)) ! return PyBool_FromLong(0); } ! return PyBool_FromLong(1); } static char isalnum__doc__[] = ! "S.isalnum() -> bool\n\ \n\ ! 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) *************** *** 2418,2443 **** /* Shortcut for single character strings */ if (PyString_GET_SIZE(self) == 1 && isalnum(*p)) ! return PyInt_FromLong(1); /* Special case for empty strings */ if (PyString_GET_SIZE(self) == 0) ! return PyInt_FromLong(0); e = p + PyString_GET_SIZE(self); for (; p < e; p++) { if (!isalnum(*p)) ! return PyInt_FromLong(0); } ! return PyInt_FromLong(1); } static char isdigit__doc__[] = ! "S.isdigit() -> int\n\ \n\ ! Return 1 if there are only digit characters in S,\n\ ! 0 otherwise."; static PyObject* string_isdigit(PyStringObject *self) --- 2418,2443 ---- /* Shortcut for single character strings */ if (PyString_GET_SIZE(self) == 1 && isalnum(*p)) ! return PyBool_FromLong(1); /* Special case for empty strings */ if (PyString_GET_SIZE(self) == 0) ! return PyBool_FromLong(0); e = p + PyString_GET_SIZE(self); for (; p < e; p++) { if (!isalnum(*p)) ! return PyBool_FromLong(0); } ! return PyBool_FromLong(1); } static char isdigit__doc__[] = ! "S.isdigit() -> bool\n\ \n\ ! Return True if there are only digit characters in S,\n\ ! False otherwise."; static PyObject* string_isdigit(PyStringObject *self) *************** *** 2449,2474 **** /* Shortcut for single character strings */ if (PyString_GET_SIZE(self) == 1 && isdigit(*p)) ! return PyInt_FromLong(1); /* Special case for empty strings */ if (PyString_GET_SIZE(self) == 0) ! return PyInt_FromLong(0); e = p + PyString_GET_SIZE(self); for (; p < e; p++) { if (!isdigit(*p)) ! return PyInt_FromLong(0); } ! return PyInt_FromLong(1); } static char islower__doc__[] = ! "S.islower() -> int\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."; static PyObject* string_islower(PyStringObject *self) --- 2449,2474 ---- /* Shortcut for single character strings */ if (PyString_GET_SIZE(self) == 1 && isdigit(*p)) ! return PyBool_FromLong(1); /* Special case for empty strings */ if (PyString_GET_SIZE(self) == 0) ! return PyBool_FromLong(0); e = p + PyString_GET_SIZE(self); for (; p < e; p++) { if (!isdigit(*p)) ! return PyBool_FromLong(0); } ! return PyBool_FromLong(1); } static char islower__doc__[] = ! "S.islower() -> bool\n\ \n\ ! 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) *************** *** 2480,2508 **** /* Shortcut for single character strings */ if (PyString_GET_SIZE(self) == 1) ! return PyInt_FromLong(islower(*p) != 0); /* Special case for empty strings */ if (PyString_GET_SIZE(self) == 0) ! return PyInt_FromLong(0); e = p + PyString_GET_SIZE(self); cased = 0; for (; p < e; p++) { if (isupper(*p)) ! return PyInt_FromLong(0); else if (!cased && islower(*p)) cased = 1; } ! return PyInt_FromLong(cased); } static char isupper__doc__[] = ! "S.isupper() -> int\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."; static PyObject* string_isupper(PyStringObject *self) --- 2480,2508 ---- /* Shortcut for single character strings */ if (PyString_GET_SIZE(self) == 1) ! return PyBool_FromLong(islower(*p) != 0); /* Special case for empty strings */ if (PyString_GET_SIZE(self) == 0) ! return PyBool_FromLong(0); e = p + PyString_GET_SIZE(self); cased = 0; for (; p < e; p++) { if (isupper(*p)) ! return PyBool_FromLong(0); else if (!cased && islower(*p)) cased = 1; } ! return PyBool_FromLong(cased); } static char isupper__doc__[] = ! "S.isupper() -> bool\n\ \n\ ! 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) *************** *** 2514,2543 **** /* Shortcut for single character strings */ if (PyString_GET_SIZE(self) == 1) ! return PyInt_FromLong(isupper(*p) != 0); /* Special case for empty strings */ if (PyString_GET_SIZE(self) == 0) ! return PyInt_FromLong(0); e = p + PyString_GET_SIZE(self); cased = 0; for (; p < e; p++) { if (islower(*p)) ! return PyInt_FromLong(0); else if (!cased && isupper(*p)) cased = 1; } ! return PyInt_FromLong(cased); } static char istitle__doc__[] = ! "S.istitle() -> int\n\ \n\ ! Return 1 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."; static PyObject* string_istitle(PyStringObject *self, PyObject *uncased) --- 2514,2543 ---- /* Shortcut for single character strings */ if (PyString_GET_SIZE(self) == 1) ! return PyBool_FromLong(isupper(*p) != 0); /* Special case for empty strings */ if (PyString_GET_SIZE(self) == 0) ! return PyBool_FromLong(0); e = p + PyString_GET_SIZE(self); cased = 0; for (; p < e; p++) { if (islower(*p)) ! return PyBool_FromLong(0); else if (!cased && isupper(*p)) cased = 1; } ! return PyBool_FromLong(cased); } static char istitle__doc__[] = ! "S.istitle() -> bool\n\ \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 False otherwise."; static PyObject* string_istitle(PyStringObject *self, PyObject *uncased) *************** *** 2549,2559 **** /* Shortcut for single character strings */ if (PyString_GET_SIZE(self) == 1) ! return PyInt_FromLong(isupper(*p) != 0); /* Special case for empty strings */ if (PyString_GET_SIZE(self) == 0) ! return PyInt_FromLong(0); e = p + PyString_GET_SIZE(self); cased = 0; --- 2549,2559 ---- /* Shortcut for single character strings */ if (PyString_GET_SIZE(self) == 1) ! return PyBool_FromLong(isupper(*p) != 0); /* Special case for empty strings */ if (PyString_GET_SIZE(self) == 0) ! return PyBool_FromLong(0); e = p + PyString_GET_SIZE(self); cased = 0; *************** *** 2563,2582 **** if (isupper(ch)) { if (previous_is_cased) ! return PyInt_FromLong(0); previous_is_cased = 1; cased = 1; } else if (islower(ch)) { if (!previous_is_cased) ! return PyInt_FromLong(0); previous_is_cased = 1; cased = 1; } else previous_is_cased = 0; } ! return PyInt_FromLong(cased); } --- 2563,2582 ---- if (isupper(ch)) { if (previous_is_cased) ! return PyBool_FromLong(0); previous_is_cased = 1; cased = 1; } else if (islower(ch)) { if (!previous_is_cased) ! return PyBool_FromLong(0); previous_is_cased = 1; cased = 1; } else previous_is_cased = 0; } ! return PyBool_FromLong(cased); } Index: Objects/unicodeobject.c =================================================================== RCS file: /cvsroot/python/python/dist/src/Objects/unicodeobject.c,v retrieving revision 2.130 diff -c -r2.130 unicodeobject.c *** Objects/unicodeobject.c 28 Feb 2002 11:38:24 -0000 2.130 --- Objects/unicodeobject.c 10 Mar 2002 05:41:16 -0000 *************** *** 4099,4108 **** } static char islower__doc__[] = ! "S.islower() -> int\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."; static PyObject* unicode_islower(PyUnicodeObject *self) --- 4099,4108 ---- } static char islower__doc__[] = ! "S.islower() -> bool\n\ \n\ ! 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) *************** *** 4113,4123 **** /* Shortcut for single character strings */ if (PyUnicode_GET_SIZE(self) == 1) ! return PyInt_FromLong(Py_UNICODE_ISLOWER(*p) != 0); /* Special case for empty strings */ if (PyString_GET_SIZE(self) == 0) ! return PyInt_FromLong(0); e = p + PyUnicode_GET_SIZE(self); cased = 0; --- 4113,4123 ---- /* Shortcut for single character strings */ if (PyUnicode_GET_SIZE(self) == 1) ! return PyBool_FromLong(Py_UNICODE_ISLOWER(*p)); /* Special case for empty strings */ if (PyString_GET_SIZE(self) == 0) ! return PyBool_FromLong(0); e = p + PyUnicode_GET_SIZE(self); cased = 0; *************** *** 4125,4142 **** register const Py_UNICODE ch = *p; if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) ! return PyInt_FromLong(0); else if (!cased && Py_UNICODE_ISLOWER(ch)) cased = 1; } ! return PyInt_FromLong(cased); } static char isupper__doc__[] = ! "S.isupper() -> int\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."; static PyObject* unicode_isupper(PyUnicodeObject *self) --- 4125,4142 ---- register const Py_UNICODE ch = *p; if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) ! return PyBool_FromLong(0); else if (!cased && Py_UNICODE_ISLOWER(ch)) cased = 1; } ! return PyBool_FromLong(cased); } static char isupper__doc__[] = ! "S.isupper() -> bool\n\ \n\ ! 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) *************** *** 4147,4157 **** /* Shortcut for single character strings */ if (PyUnicode_GET_SIZE(self) == 1) ! return PyInt_FromLong(Py_UNICODE_ISUPPER(*p) != 0); /* Special case for empty strings */ if (PyString_GET_SIZE(self) == 0) ! return PyInt_FromLong(0); e = p + PyUnicode_GET_SIZE(self); cased = 0; --- 4147,4157 ---- /* Shortcut for single character strings */ if (PyUnicode_GET_SIZE(self) == 1) ! return PyBool_FromLong(Py_UNICODE_ISUPPER(*p) != 0); /* Special case for empty strings */ if (PyString_GET_SIZE(self) == 0) ! return PyBool_FromLong(0); e = p + PyUnicode_GET_SIZE(self); cased = 0; *************** *** 4159,4177 **** register const Py_UNICODE ch = *p; if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) ! return PyInt_FromLong(0); else if (!cased && Py_UNICODE_ISUPPER(ch)) cased = 1; } ! return PyInt_FromLong(cased); } static char istitle__doc__[] = ! "S.istitle() -> int\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."; static PyObject* unicode_istitle(PyUnicodeObject *self) --- 4159,4177 ---- register const Py_UNICODE ch = *p; if (Py_UNICODE_ISLOWER(ch) || Py_UNICODE_ISTITLE(ch)) ! return PyBool_FromLong(0); else if (!cased && Py_UNICODE_ISUPPER(ch)) cased = 1; } ! return PyBool_FromLong(cased); } static char istitle__doc__[] = ! "S.istitle() -> bool\n\ \n\ ! 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) *************** *** 4182,4193 **** /* Shortcut for single character strings */ if (PyUnicode_GET_SIZE(self) == 1) ! return PyInt_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); e = p + PyUnicode_GET_SIZE(self); cased = 0; --- 4182,4193 ---- /* Shortcut for single character strings */ if (PyUnicode_GET_SIZE(self) == 1) ! 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 PyBool_FromLong(0); e = p + PyUnicode_GET_SIZE(self); cased = 0; *************** *** 4197,4223 **** if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { if (previous_is_cased) ! return PyInt_FromLong(0); previous_is_cased = 1; cased = 1; } else if (Py_UNICODE_ISLOWER(ch)) { if (!previous_is_cased) ! return PyInt_FromLong(0); previous_is_cased = 1; cased = 1; } else previous_is_cased = 0; } ! return PyInt_FromLong(cased); } static char isspace__doc__[] = ! "S.isspace() -> int\n\ \n\ ! Return 1 if there are only whitespace characters in S,\n\ ! 0 otherwise."; static PyObject* unicode_isspace(PyUnicodeObject *self) --- 4197,4223 ---- if (Py_UNICODE_ISUPPER(ch) || Py_UNICODE_ISTITLE(ch)) { if (previous_is_cased) ! return PyBool_FromLong(0); previous_is_cased = 1; cased = 1; } else if (Py_UNICODE_ISLOWER(ch)) { if (!previous_is_cased) ! return PyBool_FromLong(0); previous_is_cased = 1; cased = 1; } else previous_is_cased = 0; } ! return PyBool_FromLong(cased); } static char isspace__doc__[] = ! "S.isspace() -> bool\n\ \n\ ! Return True if there are only whitespace characters in S,\n\ ! False otherwise."; static PyObject* unicode_isspace(PyUnicodeObject *self) *************** *** 4228,4252 **** /* Shortcut for single character strings */ if (PyUnicode_GET_SIZE(self) == 1 && Py_UNICODE_ISSPACE(*p)) ! return PyInt_FromLong(1); /* Special case for empty strings */ if (PyString_GET_SIZE(self) == 0) ! return PyInt_FromLong(0); e = p + PyUnicode_GET_SIZE(self); for (; p < e; p++) { if (!Py_UNICODE_ISSPACE(*p)) ! return PyInt_FromLong(0); } ! return PyInt_FromLong(1); } static char isalpha__doc__[] = ! "S.isalpha() -> int\n\ \n\ ! Return 1 if all characters in S are alphabetic\n\ ! and there is at least one character in S, 0 otherwise."; static PyObject* unicode_isalpha(PyUnicodeObject *self) --- 4228,4252 ---- /* Shortcut for single character strings */ if (PyUnicode_GET_SIZE(self) == 1 && Py_UNICODE_ISSPACE(*p)) ! return PyBool_FromLong(1); /* Special case for empty strings */ if (PyString_GET_SIZE(self) == 0) ! return PyBool_FromLong(0); e = p + PyUnicode_GET_SIZE(self); for (; p < e; p++) { if (!Py_UNICODE_ISSPACE(*p)) ! return PyBool_FromLong(0); } ! return PyBool_FromLong(1); } static char isalpha__doc__[] = ! "S.isalpha() -> bool\n\ \n\ ! 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) *************** *** 4257,4281 **** /* Shortcut for single character strings */ if (PyUnicode_GET_SIZE(self) == 1 && Py_UNICODE_ISALPHA(*p)) ! return PyInt_FromLong(1); /* Special case for empty strings */ if (PyString_GET_SIZE(self) == 0) ! return PyInt_FromLong(0); e = p + PyUnicode_GET_SIZE(self); for (; p < e; p++) { if (!Py_UNICODE_ISALPHA(*p)) ! return PyInt_FromLong(0); } ! return PyInt_FromLong(1); } static char isalnum__doc__[] = ! "S.isalnum() -> int\n\ \n\ ! Return 1 if all characters in S are alphanumeric\n\ ! and there is at least one character in S, 0 otherwise."; static PyObject* unicode_isalnum(PyUnicodeObject *self) --- 4257,4281 ---- /* Shortcut for single character strings */ if (PyUnicode_GET_SIZE(self) == 1 && Py_UNICODE_ISALPHA(*p)) ! return PyBool_FromLong(1); /* Special case for empty strings */ if (PyString_GET_SIZE(self) == 0) ! return PyBool_FromLong(0); e = p + PyUnicode_GET_SIZE(self); for (; p < e; p++) { if (!Py_UNICODE_ISALPHA(*p)) ! return PyBool_FromLong(0); } ! return PyBool_FromLong(1); } static char isalnum__doc__[] = ! "S.isalnum() -> bool\n\ \n\ ! 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) *************** *** 4286,4310 **** /* Shortcut for single character strings */ if (PyUnicode_GET_SIZE(self) == 1 && Py_UNICODE_ISALNUM(*p)) ! return PyInt_FromLong(1); /* Special case for empty strings */ if (PyString_GET_SIZE(self) == 0) ! return PyInt_FromLong(0); e = p + PyUnicode_GET_SIZE(self); for (; p < e; p++) { if (!Py_UNICODE_ISALNUM(*p)) ! return PyInt_FromLong(0); } ! return PyInt_FromLong(1); } static char isdecimal__doc__[] = ! "S.isdecimal() -> int\n\ \n\ ! Return 1 if there are only decimal characters in S,\n\ ! 0 otherwise."; static PyObject* unicode_isdecimal(PyUnicodeObject *self) --- 4286,4310 ---- /* Shortcut for single character strings */ if (PyUnicode_GET_SIZE(self) == 1 && Py_UNICODE_ISALNUM(*p)) ! return PyBool_FromLong(1); /* Special case for empty strings */ if (PyString_GET_SIZE(self) == 0) ! return PyBool_FromLong(0); e = p + PyUnicode_GET_SIZE(self); for (; p < e; p++) { if (!Py_UNICODE_ISALNUM(*p)) ! return PyBool_FromLong(0); } ! return PyBool_FromLong(1); } static char isdecimal__doc__[] = ! "S.isdecimal() -> bool\n\ \n\ ! Return True if there are only decimal characters in S,\n\ ! False otherwise."; static PyObject* unicode_isdecimal(PyUnicodeObject *self) *************** *** 4315,4339 **** /* Shortcut for single character strings */ if (PyUnicode_GET_SIZE(self) == 1 && Py_UNICODE_ISDECIMAL(*p)) ! return PyInt_FromLong(1); /* Special case for empty strings */ if (PyString_GET_SIZE(self) == 0) ! return PyInt_FromLong(0); e = p + PyUnicode_GET_SIZE(self); for (; p < e; p++) { if (!Py_UNICODE_ISDECIMAL(*p)) ! return PyInt_FromLong(0); } ! return PyInt_FromLong(1); } static char isdigit__doc__[] = ! "S.isdigit() -> int\n\ \n\ ! Return 1 if there are only digit characters in S,\n\ ! 0 otherwise."; static PyObject* unicode_isdigit(PyUnicodeObject *self) --- 4315,4339 ---- /* Shortcut for single character strings */ if (PyUnicode_GET_SIZE(self) == 1 && Py_UNICODE_ISDECIMAL(*p)) ! return PyBool_FromLong(1); /* Special case for empty strings */ if (PyString_GET_SIZE(self) == 0) ! return PyBool_FromLong(0); e = p + PyUnicode_GET_SIZE(self); for (; p < e; p++) { if (!Py_UNICODE_ISDECIMAL(*p)) ! return PyBool_FromLong(0); } ! return PyBool_FromLong(1); } static char isdigit__doc__[] = ! "S.isdigit() -> bool\n\ \n\ ! Return True if there are only digit characters in S,\n\ ! False otherwise."; static PyObject* unicode_isdigit(PyUnicodeObject *self) *************** *** 4344,4368 **** /* Shortcut for single character strings */ if (PyUnicode_GET_SIZE(self) == 1 && Py_UNICODE_ISDIGIT(*p)) ! return PyInt_FromLong(1); /* Special case for empty strings */ if (PyString_GET_SIZE(self) == 0) ! return PyInt_FromLong(0); e = p + PyUnicode_GET_SIZE(self); for (; p < e; p++) { if (!Py_UNICODE_ISDIGIT(*p)) ! return PyInt_FromLong(0); } ! return PyInt_FromLong(1); } static char isnumeric__doc__[] = ! "S.isnumeric() -> int\n\ \n\ ! Return 1 if there are only numeric characters in S,\n\ ! 0 otherwise."; static PyObject* unicode_isnumeric(PyUnicodeObject *self) --- 4344,4368 ---- /* Shortcut for single character strings */ if (PyUnicode_GET_SIZE(self) == 1 && Py_UNICODE_ISDIGIT(*p)) ! return PyBool_FromLong(1); /* Special case for empty strings */ if (PyString_GET_SIZE(self) == 0) ! return PyBool_FromLong(0); e = p + PyUnicode_GET_SIZE(self); for (; p < e; p++) { if (!Py_UNICODE_ISDIGIT(*p)) ! return PyBool_FromLong(0); } ! return PyBool_FromLong(1); } static char isnumeric__doc__[] = ! "S.isnumeric() -> bool\n\ \n\ ! Return True if there are only numeric characters in S,\n\ ! False otherwise."; static PyObject* unicode_isnumeric(PyUnicodeObject *self) *************** *** 4373,4390 **** /* Shortcut for single character strings */ if (PyUnicode_GET_SIZE(self) == 1 && Py_UNICODE_ISNUMERIC(*p)) ! return PyInt_FromLong(1); /* Special case for empty strings */ if (PyString_GET_SIZE(self) == 0) ! return PyInt_FromLong(0); e = p + PyUnicode_GET_SIZE(self); for (; p < e; p++) { if (!Py_UNICODE_ISNUMERIC(*p)) ! return PyInt_FromLong(0); } ! return PyInt_FromLong(1); } static char join__doc__[] = --- 4373,4390 ---- /* Shortcut for single character strings */ if (PyUnicode_GET_SIZE(self) == 1 && Py_UNICODE_ISNUMERIC(*p)) ! return PyBool_FromLong(1); /* Special case for empty strings */ if (PyString_GET_SIZE(self) == 0) ! return PyBool_FromLong(0); e = p + PyUnicode_GET_SIZE(self); for (; p < e; p++) { if (!Py_UNICODE_ISNUMERIC(*p)) ! return PyBool_FromLong(0); } ! return PyBool_FromLong(1); } static char join__doc__[] = *************** *** 4852,4860 **** #endif static char startswith__doc__[] = ! "S.startswith(prefix[, start[, end]]) -> int\n\ \n\ ! Return 1 if S starts with the specified prefix, otherwise return 0. With\n\ optional start, test S beginning at that position. With optional end, stop\n\ comparing S at that position."; --- 4852,4860 ---- #endif static char startswith__doc__[] = ! "S.startswith(prefix[, start[, end]]) -> bool\n\ \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."; *************** *** 4875,4881 **** if (substring == NULL) return NULL; ! result = PyInt_FromLong(tailmatch(self, substring, start, end, -1)); Py_DECREF(substring); return result; --- 4875,4881 ---- if (substring == NULL) return NULL; ! result = PyBool_FromLong(tailmatch(self, substring, start, end, -1)); Py_DECREF(substring); return result; *************** *** 4883,4891 **** static char endswith__doc__[] = ! "S.endswith(suffix[, start[, end]]) -> int\n\ \n\ ! Return 1 if S ends with the specified suffix, otherwise return 0. With\n\ optional start, test S beginning at that position. With optional end, stop\n\ comparing S at that position."; --- 4883,4891 ---- static char endswith__doc__[] = ! "S.endswith(suffix[, start[, end]]) -> bool\n\ \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."; *************** *** 4906,4912 **** if (substring == NULL) return NULL; ! result = PyInt_FromLong(tailmatch(self, substring, start, end, +1)); Py_DECREF(substring); return result; --- 4906,4912 ---- if (substring == NULL) return NULL; ! 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 -c -r2.249 bltinmodule.c *** Python/bltinmodule.c 9 Mar 2002 12:07:51 -0000 2.249 --- Python/bltinmodule.c 10 Mar 2002 05:41:20 -0000 *************** *** 130,140 **** static PyObject * builtin_callable(PyObject *self, PyObject *v) { ! return PyInt_FromLong((long)PyCallable_Check(v)); } static char callable_doc[] = ! "callable(object) -> Boolean\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."; --- 130,140 ---- static PyObject * builtin_callable(PyObject *self, PyObject *v) { ! return PyBool_FromLong((long)PyCallable_Check(v)); } static char callable_doc[] = ! "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,719 **** } static char hasattr_doc[] = ! "hasattr(object, name) -> Boolean\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.)"; --- 713,719 ---- } static char hasattr_doc[] = ! "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,1676 **** retval = PyObject_IsInstance(inst, cls); if (retval < 0) return NULL; ! return PyInt_FromLong(retval); } static char isinstance_doc[] = ! "isinstance(object, class-or-type-or-tuple) -> Boolean\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\ --- 1666,1676 ---- retval = PyObject_IsInstance(inst, cls); if (retval < 0) return NULL; ! return PyBool_FromLong(retval); } static char isinstance_doc[] = ! "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,1701 **** retval = PyObject_IsSubclass(derived, cls); if (retval < 0) return NULL; ! return PyInt_FromLong(retval); } static char issubclass_doc[] = ! "issubclass(C, B) -> Boolean\n\ \n\ Return whether class C is a subclass (i.e., a derived class) of class B."; --- 1691,1701 ---- retval = PyObject_IsSubclass(derived, cls); if (retval < 0) return NULL; ! return PyBool_FromLong(retval); } static char issubclass_doc[] = ! "issubclass(C, B) -> bool\n\ \n\ Return whether class C is a subclass (i.e., a derived class) of class B."; *************** *** 1856,1861 **** --- 1856,1864 ---- 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,1885 **** #ifdef Py_USING_UNICODE SETBUILTIN("unicode", &PyUnicode_Type); #endif ! debug = PyInt_FromLong(Py_OptimizeFlag == 0); if (PyDict_SetItemString(dict, "__debug__", debug) < 0) { Py_XDECREF(debug); return NULL; --- 1882,1888 ---- #ifdef Py_USING_UNICODE SETBUILTIN("unicode", &PyUnicode_Type); #endif ! debug = PyBool_FromLong(Py_OptimizeFlag == 0); if (PyDict_SetItemString(dict, "__debug__", debug) < 0) { Py_XDECREF(debug); return NULL;