]> granicus.if.org Git - python/commitdiff
Merged revisions 72461 via svnmerge from
authorBenjamin Peterson <benjamin@python.org>
Fri, 8 May 2009 03:25:19 +0000 (03:25 +0000)
committerBenjamin Peterson <benjamin@python.org>
Fri, 8 May 2009 03:25:19 +0000 (03:25 +0000)
svn+ssh://pythondev@svn.python.org/python/trunk

........
  r72461 | benjamin.peterson | 2009-05-07 22:06:00 -0500 (Thu, 07 May 2009) | 1 line

  add _PyObject_LookupSpecial to handle fetching special method lookup
........

Include/object.h
Lib/test/test_descr.py
Objects/object.c
Objects/typeobject.c

index d79b6aba8befd8837861f3577de0b5ea148a57e6..772bbb398c66c716d27e1629b9b4358deca3175b 100644 (file)
@@ -414,6 +414,7 @@ PyAPI_FUNC(PyObject *) PyType_GenericAlloc(PyTypeObject *, Py_ssize_t);
 PyAPI_FUNC(PyObject *) PyType_GenericNew(PyTypeObject *,
                                               PyObject *, PyObject *);
 PyAPI_FUNC(PyObject *) _PyType_Lookup(PyTypeObject *, PyObject *);
+PyAPI_FUNC(PyObject *) _PyObject_LookupSpecial(PyObject *, char *, PyObject **);
 PyAPI_FUNC(unsigned int) PyType_ClearCache(void);
 PyAPI_FUNC(void) PyType_Modified(PyTypeObject *);
 
index 4558b9835fe7fd5a2e23f50f504bf88bece0dcd3..7137095774da55b912e6a7c58855a600755ce339 100644 (file)
@@ -1538,6 +1538,58 @@ order (MRO) for bases """
         self.assertEqual(E().foo.__func__, C.foo) # i.e., unbound
         self.assert_(repr(C.foo.__get__(C(1))).startswith("<bound method "))
 
+    def test_special_method_lookup(self):
+        # The lookup of special methods bypasses __getattr__ and
+        # __getattribute__, but they still can be descriptors.
+
+        def run_context(manager):
+            with manager:
+                pass
+        def iden(self):
+            return self
+        def hello(self):
+            return b"hello"
+
+        # 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 = [
+            ("__bytes__", bytes, hello),
+            # 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.
+            # ("__enter__", run_context, iden),
+            # ("__exit__", run_context, iden),
+            ]
+
+        class Checker(object):
+            def __getattr__(self, attr, test=self):
+                test.fail("__getattr__ called with {0}".format(attr))
+            def __getattribute__(self, attr, test=self):
+                test.fail("__getattribute__ called with {0}".format(attr))
+        class SpecialDescr(object):
+            def __init__(self, impl):
+                self.impl = impl
+            def __get__(self, obj, owner):
+                record.append(1)
+                return self
+            def __call__(self, *args):
+                return self.impl(*args)
+
+
+        for name, runner, meth_impl in specials:
+            class X(Checker):
+                pass
+            setattr(X, name, staticmethod(meth_impl))
+            runner(X())
+
+            record = []
+            class X(Checker):
+                pass
+            setattr(X, name, SpecialDescr(meth_impl))
+            runner(X())
+            self.assertEqual(record, [1], name)
+
     def test_specials(self):
         # Testing special operators...
         # Test operators like __hash__ for which a built-in default exists
index 57b4906089c81315e6c393f7520b91f5c683ec50..d8fc91e38fedbda57b6a5c932886b61602a85714 100644 (file)
@@ -474,12 +474,6 @@ PyObject_Bytes(PyObject *v)
        PyObject *result, *func;
        static PyObject *bytesstring = NULL;
 
-       if (bytesstring == NULL) {
-               bytesstring = PyUnicode_InternFromString("__bytes__");
-               if (bytesstring == NULL)
-                       return NULL;
-       }
-
        if (v == NULL)
                return PyBytes_FromString("<NULL>");
 
@@ -488,10 +482,10 @@ PyObject_Bytes(PyObject *v)
                return v;
        }
 
-        /* Doesn't create a reference */
-       func = _PyType_Lookup(Py_TYPE(v), bytesstring);
+       func = _PyObject_LookupSpecial(v, "__bytes__", &bytesstring);
        if (func != NULL) {
             result = PyObject_CallFunctionObjArgs(func, v, NULL);
+           Py_DECREF(func);
             if (result == NULL)
                return NULL;
             if (!PyBytes_Check(result)) {
@@ -503,7 +497,6 @@ PyObject_Bytes(PyObject *v)
             }
             return result;
        }
-        PyErr_Clear();
        return PyBytes_FromObject(v);
 }
 
index 06d600ead9a4e0e58a1ce41a46bae6d6c665463e..7e81ce66270d23e98ec95a28b4bc6604366d22eb 100644 (file)
@@ -1125,6 +1125,8 @@ PyType_IsSubtype(PyTypeObject *a, PyTypeObject *b)
      when the _PyType_Lookup() call fails;
 
    - lookup_method() always raises an exception upon errors.
+
+   - _PyObject_LookupSpecial() exported for the benefit of other places.
 */
 
 static PyObject *
@@ -1157,6 +1159,12 @@ lookup_method(PyObject *self, char *attrstr, PyObject **attrobj)
        return res;
 }
 
+PyObject *
+_PyObject_LookupSpecial(PyObject *self, char *attrstr, PyObject **attrobj)
+{
+       return lookup_maybe(self, attrstr, attrobj);
+}
+
 /* A variation of PyObject_CallMethod that uses lookup_method()
    instead of PyObject_GetAttrString().         This uses the same convention
    as lookup_method to cache the interned name string object. */