[Python-checkins] r72495 - in python/trunk: Lib/test/test_descr.py Objects/enumobject.c

benjamin.peterson python-checkins at python.org
Sat May 9 04:07:04 CEST 2009


Author: benjamin.peterson
Date: Sat May  9 04:07:04 2009
New Revision: 72495

Log:
lookup __reveresed__ correctly as a special method

Modified:
   python/trunk/Lib/test/test_descr.py
   python/trunk/Objects/enumobject.c

Modified: python/trunk/Lib/test/test_descr.py
==============================================================================
--- python/trunk/Lib/test/test_descr.py	(original)
+++ python/trunk/Lib/test/test_descr.py	Sat May  9 04:07:04 2009
@@ -1676,12 +1676,15 @@
             return self
         def hello(self):
             return "hello"
+        def empty_seq(self):
+            return []
 
         # It would be nice to have every special method tested here, but I'm
         # only listing the ones I can remember outside of typeobject.c, since it
         # does it right.
         specials = [
             ("__unicode__", unicode, hello),
+            ("__reversed__", reversed, empty_seq),
             # These two fail because the compiler generates LOAD_ATTR to look
             # them up.  We'd have to add a new opcode to fix this, and it's
             # probably not worth it.

Modified: python/trunk/Objects/enumobject.c
==============================================================================
--- python/trunk/Objects/enumobject.c	(original)
+++ python/trunk/Objects/enumobject.c	Sat May  9 04:07:04 2009
@@ -222,7 +222,8 @@
 reversed_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
 {
 	Py_ssize_t n;
-	PyObject *seq;
+	PyObject *seq, *reversed_meth;
+	static PyObject *reversed_cache = NULL;
 	reversedobject *ro;
 
 	if (type == &PyReversed_Type && !_PyArg_NoKeywords("reversed()", kwds))
@@ -231,8 +232,12 @@
 	if (!PyArg_UnpackTuple(args, "reversed", 1, 1, &seq) )
 		return NULL;
 
-	if (PyObject_HasAttrString(seq, "__reversed__"))
-		return PyObject_CallMethod(seq, "__reversed__", NULL);
+	reversed_meth = _PyObject_LookupSpecial(seq, "__reversed__", &reversed_cache);
+	if (reversed_meth != NULL) {
+		PyObject *res = PyObject_CallFunctionObjArgs(reversed_meth, NULL);
+		Py_DECREF(reversed_meth);
+		return res;
+	}
 
 	if (!PySequence_Check(seq)) {
 		PyErr_SetString(PyExc_TypeError,


More information about the Python-checkins mailing list