[Python-3000-checkins] r56672 - in python/branches/py3k-struni: Lib/test/test_metaclass.py Objects/typeobject.c

guido.van.rossum python-3000-checkins at python.org
Thu Aug 2 18:48:18 CEST 2007


Author: guido.van.rossum
Date: Thu Aug  2 18:48:17 2007
New Revision: 56672

Modified:
   python/branches/py3k-struni/Lib/test/test_metaclass.py
   python/branches/py3k-struni/Objects/typeobject.c
Log:
Add a default __prepare__() method to 'type', so it can be called
using super().  (See recent conversation on python-3000 with Talin
and Phillip Eby about PEP 3115 chaining rules.)


Modified: python/branches/py3k-struni/Lib/test/test_metaclass.py
==============================================================================
--- python/branches/py3k-struni/Lib/test/test_metaclass.py	(original)
+++ python/branches/py3k-struni/Lib/test/test_metaclass.py	Thu Aug  2 18:48:17 2007
@@ -207,6 +207,29 @@
     kw: [('other', 'booh')]
     >>>
 
+The default metaclass must define a __prepare__() method.
+
+    >>> type.__prepare__()
+    {}
+    >>>
+
+Make sure it works with subclassing.
+
+    >>> class M(type):
+    ...     @classmethod
+    ...     def __prepare__(cls, *args, **kwds):
+    ...         d = super().__prepare__(*args, **kwds)
+    ...         d["hello"] = 42
+    ...         return d
+    ...
+    >>> class C(metaclass=M):
+    ...     print(hello)
+    ...
+    42
+    >>> print(C.hello)
+    42
+    >>>
+
 """
 
 __test__ = {'doctests' : doctests}

Modified: python/branches/py3k-struni/Objects/typeobject.c
==============================================================================
--- python/branches/py3k-struni/Objects/typeobject.c	(original)
+++ python/branches/py3k-struni/Objects/typeobject.c	Thu Aug  2 18:48:17 2007
@@ -2200,11 +2200,21 @@
 	return list;
 }
 
+static PyObject *
+type_prepare(PyObject *self, PyObject *args, PyObject *kwds)
+{
+	return PyDict_New();
+}
+
 static PyMethodDef type_methods[] = {
 	{"mro", (PyCFunction)mro_external, METH_NOARGS,
 	 PyDoc_STR("mro() -> list\nreturn a type's method resolution order")},
 	{"__subclasses__", (PyCFunction)type_subclasses, METH_NOARGS,
 	 PyDoc_STR("__subclasses__() -> list of immediate subclasses")},
+        {"__prepare__", (PyCFunction)type_prepare,
+	 METH_VARARGS | METH_KEYWORDS | METH_CLASS,
+         PyDoc_STR("__prepare__() -> dict\n"
+                   "used to create the namespace for the class statement")},
 	{0}
 };
 


More information about the Python-3000-checkins mailing list