[Python-checkins] CVS: python/dist/src/Objects abstract.c,2.72,2.73

Guido van Rossum gvanrossum@users.sourceforge.net
Fri, 07 Sep 2001 13:20:13 -0700


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

Modified Files:
	abstract.c 
Log Message:
PySequence_Check(), PyMapping_Check(): only return true if the
corresponding "getitem" operation (sq_item or mp_subscript) is
implemented.  I realize that "sequence-ness" and "mapping-ness" are
poorly defined (and the tests may still be wrong for user-defined
instances, which always have both slots filled), but I believe that a
sequence that doesn't support its getitem operation should not be
considered a sequence.  All other operations are optional though.

For example, the ZODB BTree tests crashed because PySequence_Check()
returned true for a dictionary!  (In 2.2, the dictionary type has a
tp_as_sequence pointer, but the only field filled is sq_contains, so
you can write "if key in dict".)  With this fix, all standalone ZODB
tests succeed.


Index: abstract.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/abstract.c,v
retrieving revision 2.72
retrieving revision 2.73
diff -C2 -d -r2.72 -r2.73
*** abstract.c	2001/08/17 18:39:25	2.72
--- abstract.c	2001/09/07 20:20:11	2.73
***************
*** 920,924 ****
  PySequence_Check(PyObject *s)
  {
! 	return s != NULL && s->ob_type->tp_as_sequence;
  }
  
--- 920,925 ----
  PySequence_Check(PyObject *s)
  {
! 	return s != NULL && s->ob_type->tp_as_sequence &&
! 		s->ob_type->tp_as_sequence->sq_item != NULL;
  }
  
***************
*** 1510,1514 ****
  PyMapping_Check(PyObject *o)
  {
! 	return o && o->ob_type->tp_as_mapping;
  }
  
--- 1511,1516 ----
  PyMapping_Check(PyObject *o)
  {
! 	return o && o->ob_type->tp_as_mapping &&
! 		o->ob_type->tp_as_mapping->mp_subscript;
  }