]> granicus.if.org Git - python/commitdiff
only accept AttributeError as indicating no __prepare__ attribute on a metaclass...
authorBenjamin Peterson <benjamin@python.org>
Sat, 27 Feb 2010 17:40:01 +0000 (17:40 +0000)
committerBenjamin Peterson <benjamin@python.org>
Sat, 27 Feb 2010 17:40:01 +0000 (17:40 +0000)
Lib/test/test_metaclass.py
Misc/NEWS
Python/bltinmodule.c

index 000bcf5c55081785d5766ee3bf4e038222049787..219ab99840b150c88c46e242efec3e2ef87c4317 100644 (file)
@@ -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}
index ee43e631717d59f1be3e142d8c30f02079d9f4c1..a04e068cb5ef5c364d34aa3655c15c8087942673 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -12,6 +12,8 @@ What's New in Python 3.2 Alpha 1?
 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.
 
index 7fe164f5deeeba4fe7b493d07939b5ad191146fe..5c7138ec60bb1d9d4c0b791dbe1a8e06449243de 100644 (file)
@@ -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);