Merged revisions 78505-78506 via svnmerge from
authorBenjamin Peterson <benjamin@python.org>
Sat, 27 Feb 2010 17:56:22 +0000 (17:56 +0000)
committerBenjamin Peterson <benjamin@python.org>
Sat, 27 Feb 2010 17:56:22 +0000 (17:56 +0000)
svn+ssh://pythondev@svn.python.org/python/branches/py3k

........
  r78505 | benjamin.peterson | 2010-02-27 11:40:01 -0600 (Sat, 27 Feb 2010) | 1 line

  only accept AttributeError as indicating no __prepare__ attribute on a metaclass, allowing lookup errors to propogate
........
  r78506 | benjamin.peterson | 2010-02-27 11:41:13 -0600 (Sat, 27 Feb 2010) | 1 line

  check PyDict_New() for error
........

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 c42cef545c14e9af2876a366b8e53427ee32590e..d53bcb48f7a6f834d6857e009e322dc4949ff720 100644 (file)
--- 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.
 
index e03b49f9f4f9df54788cc9d7862b4a7198738a40..069d7908c7896a03b337b71f75e84c943a26c6ed 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);
@@ -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) {