[Python-checkins] CVS: python/dist/src/Modules cPickle.c,2.78,2.79 operator.c,2.18,2.19

Guido van Rossum gvanrossum@users.sourceforge.net
Wed, 03 Apr 2002 14:41:53 -0800


Update of /cvsroot/python/python/dist/src/Modules
In directory usw-pr-cvs1:/tmp/cvs-serv31419/Modules

Modified Files:
	cPickle.c operator.c 
Log Message:
Add the 'bool' type and its values 'False' and 'True', as described in
PEP 285.  Everything described in the PEP is here, and there is even
some documentation.  I had to fix 12 unit tests; all but one of these
were printing Boolean outcomes that changed from 0/1 to False/True.
(The exception is test_unicode.py, which did a type(x) == type(y)
style comparison.  I could've fixed that with a single line using
issubtype(x, type(y)), but instead chose to be explicit about those
places where a bool is expected.

Still to do: perhaps more documentation; change standard library
modules to return False/True from predicates.



Index: cPickle.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Modules/cPickle.c,v
retrieving revision 2.78
retrieving revision 2.79
diff -C2 -d -r2.78 -r2.79
*** cPickle.c	1 Apr 2002 17:40:08 -0000	2.78
--- cPickle.c	3 Apr 2002 22:41:50 -0000	2.79
***************
*** 126,129 ****
--- 126,132 ----
  #define EMPTY_TUPLE ')'
  #define SETITEMS    'u'
+ #define TRUE        'Z'
+ #define FALSE       'z'
+ 
  
  static char MARKv = MARK;
***************
*** 979,982 ****
--- 982,996 ----
  }
  
+ static int
+ save_bool(Picklerobject *self, PyObject *args) 
+ {
+ 	static char buf[2] = {FALSE, TRUE};
+ 	long l = PyInt_AS_LONG((PyIntObject *)args);
+ 
+ 	if ((*self->write_func)(self, buf + l, 1) < 0)
+ 		return -1;
+ 
+ 	return 0;
+ }
  
  static int
***************
*** 1922,1925 ****
--- 1936,1945 ----
  
  	switch (type->tp_name[0]) {
+ 	case 'b':
+ 		if (args == Py_False || args == Py_True) {
+ 			res = save_bool(self, args);
+ 			goto finally;
+ 		}
+ 		break;
          case 'i':
  		if (type == &PyInt_Type) {
***************
*** 2637,2640 ****
--- 2657,2674 ----
  
  static int
+ load_false(Unpicklerobject *self) 
+ {
+ 	PDATA_APPEND(self->stack, Py_False, -1);
+ 	return 0;
+ }
+ 
+ static int
+ load_true(Unpicklerobject *self) 
+ {
+ 	PDATA_APPEND(self->stack, Py_True, -1);
+ 	return 0;
+ }
+ 
+ static int
  bad_readline(void) 
  {
***************
*** 3775,3778 ****
--- 3809,3822 ----
  		case NONE:
  			if (load_none(self) < 0)
+ 				break;
+ 			continue;
+ 
+ 		case FALSE:
+ 			if (load_false(self) < 0)
+ 				break;
+ 			continue;
+ 
+ 		case TRUE:
+ 			if (load_true(self) < 0)
  				break;
  			continue;

Index: operator.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Modules/operator.c,v
retrieving revision 2.18
retrieving revision 2.19
diff -C2 -d -r2.18 -r2.19
*** operator.c	9 Aug 2001 20:14:34 -0000	2.18
--- operator.c	3 Apr 2002 22:41:50 -0000	2.19
***************
*** 103,107 ****
    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) { \
--- 103,107 ----
    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) { \
***************
*** 111,114 ****
--- 111,120 ----
    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; \
***************
*** 140,145 ****
  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)
--- 146,151 ----
  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)
***************
*** 209,217 ****
   "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).")
--- 215,223 ----
   "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).")
***************
*** 223,227 ****
   "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.")
--- 229,233 ----
   "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.")