From 94c65d9a8ff8ec56c2420f46a6622803f7f1b154 Mon Sep 17 00:00:00 2001 From: Benjamin Peterson Date: Mon, 25 May 2009 03:10:48 +0000 Subject: [PATCH] Merged revisions 72907 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r72907 | benjamin.peterson | 2009-05-24 21:40:21 -0500 (Sun, 24 May 2009) | 1 line handle errors from _PyObject_LookupSpecial when __get__ fails ........ --- Lib/test/test_descr.py | 18 +++++++++++++++++- Objects/abstract.c | 12 ++++++++++-- Objects/enumobject.c | 2 ++ Objects/object.c | 2 ++ Python/sysmodule.c | 10 ++++++---- 5 files changed, 37 insertions(+), 7 deletions(-) diff --git a/Lib/test/test_descr.py b/Lib/test/test_descr.py index 32f9090d60..adcb55f053 100644 --- a/Lib/test/test_descr.py +++ b/Lib/test/test_descr.py @@ -1595,7 +1595,11 @@ order (MRO) for bases """ def __get__(self, obj, owner): record.append(1) return self.impl.__get__(obj, owner) - + class MyException(Exception): + pass + class ErrDescr(object): + def __get__(self, obj, owner): + raise MyException for name, runner, meth_impl, ok, env in specials: class X(Checker): @@ -1614,6 +1618,18 @@ order (MRO) for bases """ runner(X()) self.assertEqual(record, [1], name) + class X(Checker): + pass + for attr, obj in env.items(): + setattr(X, attr, obj) + setattr(X, name, ErrDescr()) + try: + runner(X()) + except MyException: + pass + else: + self.fail("{0!r} didn't raise".format(name)) + def test_specials(self): # Testing special operators... # Test operators like __hash__ for which a built-in default exists diff --git a/Objects/abstract.c b/Objects/abstract.c index 56e5b717a6..3b2de9d6c1 100644 --- a/Objects/abstract.c +++ b/Objects/abstract.c @@ -90,8 +90,12 @@ _PyObject_LengthHint(PyObject *o, Py_ssize_t defaultvalue) /* try o.__length_hint__() */ hintmeth = _PyObject_LookupSpecial(o, "__length_hint__", &hintstrobj); - if (hintmeth == NULL) - return defaultvalue; + if (hintmeth == NULL) { + if (PyErr_Occurred()) + return -1; + else + return defaultvalue; + } ro = PyObject_CallFunctionObjArgs(hintmeth, NULL); Py_DECREF(hintmeth); if (ro == NULL) { @@ -2592,6 +2596,8 @@ PyObject_IsInstance(PyObject *inst, PyObject *cls) } return ok; } + else if (PyErr_Occurred()) + return -1; return recursive_isinstance(inst, cls); } @@ -2655,6 +2661,8 @@ PyObject_IsSubclass(PyObject *derived, PyObject *cls) } return ok; } + else if (PyErr_Occurred()) + return -1; return recursive_issubclass(derived, cls); } diff --git a/Objects/enumobject.c b/Objects/enumobject.c index f8e407282e..62e55bb88a 100644 --- a/Objects/enumobject.c +++ b/Objects/enumobject.c @@ -238,6 +238,8 @@ reversed_new(PyTypeObject *type, PyObject *args, PyObject *kwds) Py_DECREF(reversed_meth); return res; } + else if (PyErr_Occurred()) + return NULL; if (!PySequence_Check(seq)) { PyErr_SetString(PyExc_TypeError, diff --git a/Objects/object.c b/Objects/object.c index 58b70f09e7..a29c31a43d 100644 --- a/Objects/object.c +++ b/Objects/object.c @@ -497,6 +497,8 @@ PyObject_Bytes(PyObject *v) } return result; } + else if (PyErr_Occurred()) + return NULL; return PyBytes_FromObject(v); } diff --git a/Python/sysmodule.c b/Python/sysmodule.c index 6639acc7d1..89613ecbc2 100644 --- a/Python/sysmodule.c +++ b/Python/sysmodule.c @@ -652,10 +652,12 @@ sys_getsizeof(PyObject *self, PyObject *args, PyObject *kwds) method = _PyObject_LookupSpecial(o, "__sizeof__", &str__sizeof__); - if (method == NULL) - PyErr_Format(PyExc_TypeError, - "Type %.100s doesn't define __sizeof__", - Py_TYPE(o)->tp_name); + if (method == NULL) { + if (!PyErr_Occurred()) + PyErr_Format(PyExc_TypeError, + "Type %.100s doesn't define __sizeof__", + Py_TYPE(o)->tp_name); + } else { res = PyObject_CallFunctionObjArgs(method, NULL); Py_DECREF(method); -- 2.40.0