mirror of
https://github.com/python/cpython.git
synced 2025-12-21 08:01:36 +00:00
completely ignore old-style stuff for type checking overloading
This commit is contained in:
parent
94eaba78b7
commit
a27dbc68f0
2 changed files with 42 additions and 61 deletions
|
|
@ -29,10 +29,6 @@ class SubInt(Integer):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
class Evil:
|
|
||||||
def __instancecheck__(self, inst): return False
|
|
||||||
|
|
||||||
|
|
||||||
class TypeChecksTest(unittest.TestCase):
|
class TypeChecksTest(unittest.TestCase):
|
||||||
|
|
||||||
def testIsSubclassInternal(self):
|
def testIsSubclassInternal(self):
|
||||||
|
|
@ -75,11 +71,18 @@ class TypeChecksTest(unittest.TestCase):
|
||||||
self.assertEqual(isinstance(42, SubInt), False)
|
self.assertEqual(isinstance(42, SubInt), False)
|
||||||
self.assertEqual(isinstance(42, (SubInt,)), False)
|
self.assertEqual(isinstance(42, (SubInt,)), False)
|
||||||
|
|
||||||
def testInfiniteRecursionCaughtProperly(self):
|
def test_oldstyle(self):
|
||||||
e = Evil()
|
# These should just be ignored.
|
||||||
# This invokes isinstance() recursively, until the stack is exhausted.
|
class X:
|
||||||
self.assertRaises(RuntimeError, isinstance, e, Evil)
|
def __instancecheck__(self, inst):
|
||||||
# XXX How to check the same situation for issubclass()?
|
return True
|
||||||
|
def __subclasscheck__(self, cls):
|
||||||
|
return True
|
||||||
|
class Sub(X): pass
|
||||||
|
self.assertFalse(isinstance(3, X))
|
||||||
|
self.assertTrue(isinstance(X(), X))
|
||||||
|
self.assertFalse(issubclass(int, X))
|
||||||
|
self.assertTrue(issubclass(Sub, X))
|
||||||
|
|
||||||
|
|
||||||
def test_main():
|
def test_main():
|
||||||
|
|
|
||||||
|
|
@ -2902,7 +2902,6 @@ int
|
||||||
PyObject_IsInstance(PyObject *inst, PyObject *cls)
|
PyObject_IsInstance(PyObject *inst, PyObject *cls)
|
||||||
{
|
{
|
||||||
static PyObject *name = NULL;
|
static PyObject *name = NULL;
|
||||||
PyObject *checker;
|
|
||||||
|
|
||||||
/* Quick test for an exact match */
|
/* Quick test for an exact match */
|
||||||
if (Py_TYPE(inst) == (PyTypeObject *)cls)
|
if (Py_TYPE(inst) == (PyTypeObject *)cls)
|
||||||
|
|
@ -2927,33 +2926,25 @@ PyObject_IsInstance(PyObject *inst, PyObject *cls)
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (PyClass_Check(cls) || PyInstance_Check(cls)) {
|
if (!(PyClass_Check(cls) || PyInstance_Check(cls))) {
|
||||||
checker = PyObject_GetAttrString(cls, "__instancecheck__");
|
PyObject *checker;
|
||||||
if (checker == NULL) {
|
|
||||||
if (PyErr_ExceptionMatches(PyExc_AttributeError))
|
|
||||||
PyErr_Clear();
|
|
||||||
else
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
checker = _PyObject_LookupSpecial(cls, "__instancecheck__", &name);
|
checker = _PyObject_LookupSpecial(cls, "__instancecheck__", &name);
|
||||||
}
|
if (checker != NULL) {
|
||||||
if (checker != NULL) {
|
PyObject *res;
|
||||||
PyObject *res;
|
int ok = -1;
|
||||||
int ok = -1;
|
if (Py_EnterRecursiveCall(" in __instancecheck__")) {
|
||||||
if (Py_EnterRecursiveCall(" in __instancecheck__")) {
|
Py_DECREF(checker);
|
||||||
|
return ok;
|
||||||
|
}
|
||||||
|
res = PyObject_CallFunctionObjArgs(checker, inst, NULL);
|
||||||
|
Py_LeaveRecursiveCall();
|
||||||
Py_DECREF(checker);
|
Py_DECREF(checker);
|
||||||
|
if (res != NULL) {
|
||||||
|
ok = PyObject_IsTrue(res);
|
||||||
|
Py_DECREF(res);
|
||||||
|
}
|
||||||
return ok;
|
return ok;
|
||||||
}
|
}
|
||||||
res = PyObject_CallFunctionObjArgs(checker, inst, NULL);
|
|
||||||
Py_LeaveRecursiveCall();
|
|
||||||
Py_DECREF(checker);
|
|
||||||
if (res != NULL) {
|
|
||||||
ok = PyObject_IsTrue(res);
|
|
||||||
Py_DECREF(res);
|
|
||||||
}
|
|
||||||
return ok;
|
|
||||||
}
|
}
|
||||||
return recursive_isinstance(inst, cls);
|
return recursive_isinstance(inst, cls);
|
||||||
}
|
}
|
||||||
|
|
@ -2992,8 +2983,6 @@ int
|
||||||
PyObject_IsSubclass(PyObject *derived, PyObject *cls)
|
PyObject_IsSubclass(PyObject *derived, PyObject *cls)
|
||||||
{
|
{
|
||||||
static PyObject *name = NULL;
|
static PyObject *name = NULL;
|
||||||
PyObject *t, *v, *tb;
|
|
||||||
PyObject *checker;
|
|
||||||
|
|
||||||
if (PyTuple_Check(cls)) {
|
if (PyTuple_Check(cls)) {
|
||||||
Py_ssize_t i;
|
Py_ssize_t i;
|
||||||
|
|
@ -3013,36 +3002,25 @@ PyObject_IsSubclass(PyObject *derived, PyObject *cls)
|
||||||
Py_LeaveRecursiveCall();
|
Py_LeaveRecursiveCall();
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
if (PyClass_Check(cls) || PyInstance_Check(cls)) {
|
if (!(PyClass_Check(cls) || PyInstance_Check(cls))) {
|
||||||
PyErr_Fetch(&t, &v, &tb);
|
PyObject *checker;
|
||||||
checker = PyObject_GetAttr(cls, name);
|
|
||||||
if (checker == NULL &&
|
|
||||||
!PyErr_ExceptionMatches(PyExc_AttributeError)) {
|
|
||||||
Py_XDECREF(t);
|
|
||||||
Py_XDECREF(v);
|
|
||||||
Py_XDECREF(tb);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
PyErr_Restore(t, v, tb);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
checker = _PyObject_LookupSpecial(cls, "__subclasscheck__", &name);
|
checker = _PyObject_LookupSpecial(cls, "__subclasscheck__", &name);
|
||||||
}
|
if (checker != NULL) {
|
||||||
if (checker != NULL) {
|
PyObject *res;
|
||||||
PyObject *res;
|
int ok = -1;
|
||||||
int ok = -1;
|
if (Py_EnterRecursiveCall(" in __subclasscheck__")) {
|
||||||
if (Py_EnterRecursiveCall(" in __subclasscheck__")) {
|
Py_DECREF(checker);
|
||||||
|
return ok;
|
||||||
|
}
|
||||||
|
res = PyObject_CallFunctionObjArgs(checker, derived, NULL);
|
||||||
|
Py_LeaveRecursiveCall();
|
||||||
Py_DECREF(checker);
|
Py_DECREF(checker);
|
||||||
|
if (res != NULL) {
|
||||||
|
ok = PyObject_IsTrue(res);
|
||||||
|
Py_DECREF(res);
|
||||||
|
}
|
||||||
return ok;
|
return ok;
|
||||||
}
|
}
|
||||||
res = PyObject_CallFunctionObjArgs(checker, derived, NULL);
|
|
||||||
Py_LeaveRecursiveCall();
|
|
||||||
Py_DECREF(checker);
|
|
||||||
if (res != NULL) {
|
|
||||||
ok = PyObject_IsTrue(res);
|
|
||||||
Py_DECREF(res);
|
|
||||||
}
|
|
||||||
return ok;
|
|
||||||
}
|
}
|
||||||
return recursive_issubclass(derived, cls);
|
return recursive_issubclass(derived, cls);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue