[Python-checkins] r55623 - in python/branches/cpy_merge: Lib/test/test_inspect.py Lib/test/test_typechecks.py Misc/NEWS Objects/abstract.c

alexandre.vassalotti python-checkins at python.org
Mon May 28 19:08:27 CEST 2007


Author: alexandre.vassalotti
Date: Mon May 28 19:08:21 2007
New Revision: 55623

Added:
   python/branches/cpy_merge/Lib/test/test_typechecks.py
      - copied unchanged from r55599, python/branches/p3yk/Lib/test/test_typechecks.py
Modified:
   python/branches/cpy_merge/   (props changed)
   python/branches/cpy_merge/Lib/test/test_inspect.py
   python/branches/cpy_merge/Misc/NEWS
   python/branches/cpy_merge/Objects/abstract.c
Log:
Merged revisions 55529-55622 via svnmerge from 
svn+ssh://pythondev@svn.python.org/python/branches/p3yk

........
  r55544 | guido.van.rossum | 2007-05-23 22:23:53 -0400 (Wed, 23 May 2007) | 2 lines
  
  Fix test_inspect.  It seems my previous "fix" was due to a stale .pyc file.
........
  r55587 | guido.van.rossum | 2007-05-25 13:37:01 -0400 (Fri, 25 May 2007) | 2 lines
  
  Implement isinstance and issubclass overriding, a la PEP 3119.
........
  r55598 | neal.norwitz | 2007-05-25 22:44:02 -0400 (Fri, 25 May 2007) | 1 line
  
  Fix refleak on infinite recursion
........
  r55599 | neal.norwitz | 2007-05-25 22:47:45 -0400 (Fri, 25 May 2007) | 1 line
  
  Add news entry about overidding isinstance/issubclass (PEP 3119)
........


Modified: python/branches/cpy_merge/Lib/test/test_inspect.py
==============================================================================
--- python/branches/cpy_merge/Lib/test/test_inspect.py	(original)
+++ python/branches/cpy_merge/Lib/test/test_inspect.py	Mon May 28 19:08:21 2007
@@ -218,7 +218,7 @@
     fodderFile = mod2
 
     def test_wrapped_decorator(self):
-        self.assertSourceEqual(mod2.wrapped, 14, 17)
+        self.assertSourceEqual(mod2.wrapped, 16, 17)
 
     def test_replacing_decorator(self):
         self.assertSourceEqual(mod2.gone, 9, 10)

Modified: python/branches/cpy_merge/Misc/NEWS
==============================================================================
--- python/branches/cpy_merge/Misc/NEWS	(original)
+++ python/branches/cpy_merge/Misc/NEWS	Mon May 28 19:08:21 2007
@@ -26,6 +26,8 @@
 Core and Builtins
 -----------------
 
+- PEP 3119: isinstance() and issubclass() can be overridden.
+
 - Remove BaseException.message.
 
 - Remove tuple parameter unpacking (PEP 3113).

Modified: python/branches/cpy_merge/Objects/abstract.c
==============================================================================
--- python/branches/cpy_merge/Objects/abstract.c	(original)
+++ python/branches/cpy_merge/Objects/abstract.c	Mon May 28 19:08:21 2007
@@ -2132,7 +2132,27 @@
 int
 PyObject_IsInstance(PyObject *inst, PyObject *cls)
 {
-    return recursive_isinstance(inst, cls, Py_GetRecursionLimit());
+	PyObject *t, *v, *tb;
+	PyErr_Fetch(&t, &v, &tb);
+	PyObject *checker = PyObject_GetAttrString(cls, "__instancecheck__");
+	PyErr_Restore(t, v, tb);
+	if (checker != NULL) {
+		PyObject *res;
+		int ok = -1;
+		if (Py_EnterRecursiveCall(" in __instancecheck__")) {
+			Py_DECREF(checker);
+			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, Py_GetRecursionLimit());
 }
 
 static  int
@@ -2182,7 +2202,25 @@
 int
 PyObject_IsSubclass(PyObject *derived, PyObject *cls)
 {
-    return recursive_issubclass(derived, cls, Py_GetRecursionLimit());
+	PyObject *t, *v, *tb;
+	PyErr_Fetch(&t, &v, &tb);
+	PyObject *checker = PyObject_GetAttrString(cls, "__subclasscheck__");
+	PyErr_Restore(t, v, tb);
+	if (checker != NULL) {
+		PyObject *res;
+		int ok = -1;
+		if (Py_EnterRecursiveCall(" in __subclasscheck__"))
+			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, Py_GetRecursionLimit());
 }
 
 


More information about the Python-checkins mailing list