summaryrefslogtreecommitdiffstats
path: root/Objects/abstract.c
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2001-10-07 20:54:12 (GMT)
committerGuido van Rossum <guido@python.org>2001-10-07 20:54:12 (GMT)
commit03290ecbf1661c0192e6abdbe00ae163af461d77 (patch)
treefc324d37b87c8d229d726f41de8dfc3bc33fda3a /Objects/abstract.c
parent1f733baa04a56eed0a5823158205fc04502e3050 (diff)
downloadcpython-03290ecbf1661c0192e6abdbe00ae163af461d77.zip
cpython-03290ecbf1661c0192e6abdbe00ae163af461d77.tar.gz
cpython-03290ecbf1661c0192e6abdbe00ae163af461d77.tar.bz2
Implement isinstance(x, (A, B, ...)). Note that we only allow tuples,
not other sequences (then we'd have to except strings, and we'd still be susceptible to recursive attacks).
Diffstat (limited to 'Objects/abstract.c')
-rw-r--r--Objects/abstract.c17
1 files changed, 16 insertions, 1 deletions
diff --git a/Objects/abstract.c b/Objects/abstract.c
index 4891622..33991a8 100644
--- a/Objects/abstract.c
+++ b/Objects/abstract.c
@@ -1805,6 +1805,20 @@ PyObject_IsInstance(PyObject *inst, PyObject *cls)
else if (PyType_Check(cls)) {
retval = PyObject_TypeCheck(inst, (PyTypeObject *)cls);
}
+ else if (PyTuple_Check(cls)) {
+ /* Not a general sequence -- that opens up the road to
+ recursion and stack overflow. */
+ int i, n;
+
+ n = PyTuple_GET_SIZE(cls);
+ for (i = 0; i < n; i++) {
+ retval = PyObject_IsInstance(
+ inst, PyTuple_GET_ITEM(cls, i));
+ if (retval != 0)
+ break;
+ }
+ return retval;
+ }
else if (!PyInstance_Check(inst)) {
if (__class__ == NULL) {
__class__ = PyString_FromString("__class__");
@@ -1827,7 +1841,8 @@ PyObject_IsInstance(PyObject *inst, PyObject *cls)
if (retval < 0) {
PyErr_SetString(PyExc_TypeError,
- "isinstance() arg 2 must be a class or type");
+ "isinstance() arg 2 must be a class or type "
+ "or tuple of those");
}
return retval;
}