From 224205fde2af8fdbbd11de0b219880df9a0edabd Mon Sep 17 00:00:00 2001 From: Benjamin Peterson Date: Fri, 8 May 2009 03:25:19 +0000 Subject: [PATCH] Merged revisions 72461 via svnmerge from 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 | 1 + Lib/test/test_descr.py | 52 ++++++++++++++++++++++++++++++++++++++++++ Objects/object.c | 11 ++------- Objects/typeobject.c | 8 +++++++ 4 files changed, 63 insertions(+), 9 deletions(-) diff --git a/Include/object.h b/Include/object.h index d79b6aba8be..772bbb398c6 100644 --- a/Include/object.h +++ b/Include/object.h @@ -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 *); diff --git a/Lib/test/test_descr.py b/Lib/test/test_descr.py index 4558b9835fe..7137095774d 100644 --- a/Lib/test/test_descr.py +++ b/Lib/test/test_descr.py @@ -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(""); @@ -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); } diff --git a/Objects/typeobject.c b/Objects/typeobject.c index 06d600ead9a..7e81ce66270 100644 --- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -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. */