]> granicus.if.org Git - python/commitdiff
Improve accuracy of sequence and mapping checks.
authorRaymond Hettinger <python@rcn.com>
Sun, 4 Apr 2004 08:51:41 +0000 (08:51 +0000)
committerRaymond Hettinger <python@rcn.com>
Sun, 4 Apr 2004 08:51:41 +0000 (08:51 +0000)
Misc/NEWS
Objects/abstract.c

index b1f2043b5c55285a751fe3880201b354ce9cf1ae..c51b74bdf89f065e178e27f35db43f4980624498 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -202,6 +202,9 @@ Core and builtins
 Extension modules
 -----------------
 
+- operator.isMappingType() and operator.isSequenceType() now give
+  fewer false positives.
+
 - socket.sslerror is now a subclass of socket.error .  Also added
   socket.error to the socket module's C API.
 
index bf60c750f1b3f6b0c8d60f79a52fa97c4b7294f3..307ef8650b0d81954585d7f6f950925d76da4e02 100644 (file)
@@ -1058,6 +1058,8 @@ PyNumber_Float(PyObject *o)
 int
 PySequence_Check(PyObject *s)
 {
+       if (PyInstance_Check(s))
+               return PyObject_HasAttrString(s, "__getitem__");
        return s != NULL && s->ob_type->tp_as_sequence &&
                s->ob_type->tp_as_sequence->sq_item != NULL;
 }
@@ -1600,8 +1602,12 @@ PySequence_Index(PyObject *s, PyObject *o)
 int
 PyMapping_Check(PyObject *o)
 {
-       return o && o->ob_type->tp_as_mapping &&
-               o->ob_type->tp_as_mapping->mp_subscript;
+       if (PyInstance_Check(o))
+               return PyObject_HasAttrString(o, "__getitem__");
+
+       return  o && o->ob_type->tp_as_mapping &&
+               o->ob_type->tp_as_mapping->mp_subscript &&
+               !PyObject_HasAttrString(o, "__getslice__");
 }
 
 int