summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Lib/test/test_metaclass.py14
-rw-r--r--Misc/NEWS2
-rw-r--r--Python/bltinmodule.c24
3 files changed, 32 insertions, 8 deletions
diff --git a/Lib/test/test_metaclass.py b/Lib/test/test_metaclass.py
index 000bcf5..219ab99 100644
--- a/Lib/test/test_metaclass.py
+++ b/Lib/test/test_metaclass.py
@@ -230,6 +230,20 @@ Make sure it works with subclassing.
42
>>>
+Test failures in looking up the __prepare__ method work.
+ >>> class ObscureException(Exception):
+ ... pass
+ >>> class FailDescr:
+ ... def __get__(self, instance, owner):
+ ... raise ObscureException
+ >>> class Meta(type):
+ ... __prepare__ = FailDescr()
+ >>> class X(metaclass=Meta):
+ ... pass
+ Traceback (most recent call last):
+ [...]
+ test.test_metaclass.ObscureException
+
"""
__test__ = {'doctests' : doctests}
diff --git a/Misc/NEWS b/Misc/NEWS
index c42cef5..d53bcb4 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,8 @@ What's New in Python 3.1.2?
Core and Builtins
-----------------
+- Handle errors from looking up __prepare__ correctly.
+
- Issue #5939: Add additional runtime checking to ensure a valid capsule
in Modules/_ctypes/callproc.c.
diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c
index e03b49f..069d790 100644
--- a/Python/bltinmodule.c
+++ b/Python/bltinmodule.c
@@ -108,8 +108,16 @@ builtin___build_class__(PyObject *self, PyObject *args, PyObject *kwds)
}
prep = PyObject_GetAttrString(meta, "__prepare__");
if (prep == NULL) {
- PyErr_Clear();
- ns = PyDict_New();
+ if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
+ PyErr_Clear();
+ ns = PyDict_New();
+ }
+ else {
+ Py_DECREF(meta);
+ Py_XDECREF(mkw);
+ Py_DECREF(bases);
+ return NULL;
+ }
}
else {
PyObject *pargs = PyTuple_Pack(2, name, bases);
@@ -123,12 +131,12 @@ builtin___build_class__(PyObject *self, PyObject *args, PyObject *kwds)
ns = PyEval_CallObjectWithKeywords(prep, pargs, mkw);
Py_DECREF(pargs);
Py_DECREF(prep);
- if (ns == NULL) {
- Py_DECREF(meta);
- Py_XDECREF(mkw);
- Py_DECREF(bases);
- return NULL;
- }
+ }
+ if (ns == NULL) {
+ Py_DECREF(meta);
+ Py_XDECREF(mkw);
+ Py_DECREF(bases);
+ return NULL;
}
cell = PyObject_CallFunctionObjArgs(func, ns, NULL);
if (cell != NULL) {