[pypy-svn] r49349 - in pypy/dist/pypy: interpreter module/__builtin__

arigo at codespeak.net arigo at codespeak.net
Tue Dec 4 18:12:48 CET 2007


Author: arigo
Date: Tue Dec  4 18:12:48 2007
New Revision: 49349

Modified:
   pypy/dist/pypy/interpreter/function.py
   pypy/dist/pypy/interpreter/typedef.py
   pypy/dist/pypy/module/__builtin__/__init__.py
   pypy/dist/pypy/module/__builtin__/descriptor.py
Log:
Move ClassMethod together with StaticMethod into function.py, where it
is more easily importable.  It should be possible to just use it to
create TypeDefs with classmethods.


Modified: pypy/dist/pypy/interpreter/function.py
==============================================================================
--- pypy/dist/pypy/interpreter/function.py	(original)
+++ pypy/dist/pypy/interpreter/function.py	Tue Dec  4 18:12:48 2007
@@ -451,8 +451,7 @@
         return space.newtuple([new_inst, space.newtuple(tup)])
         
 class StaticMethod(Wrappable):
-    """A static method.  Note that there is one class staticmethod at
-    app-level too currently; this is only used for __new__ methods."""
+    """The staticmethod objects."""
 
     def __init__(self, w_function):
         self.w_function = w_function
@@ -464,6 +463,24 @@
     def descr_staticmethod__new__(space, w_type, w_function):
         return space.wrap(StaticMethod(w_function))
 
+class ClassMethod(Wrappable):
+    """The classmethod objects."""
+
+    def __init__(self, w_function):
+        self.w_function = w_function
+
+    def descr_classmethod_get(self, space, w_obj, w_klass=None):
+        if space.is_w(w_klass, space.w_None):
+            w_klass = space.type(w_obj)
+        return space.wrap(Method(space, self.w_function, w_klass, space.w_None))
+
+    def descr_classmethod__new__(space, w_type, w_function):
+        if not space.is_true(space.callable(w_function)):
+            typename = space.type(w_function).getname(space, '?')
+            raise OperationError(space.w_TypeError, space.wrap(
+                                 "'%s' object is not callable" % typename))
+        return space.wrap(ClassMethod(w_function))
+
 class BuiltinFunction(Function):
 
     def __init__(self, func):

Modified: pypy/dist/pypy/interpreter/typedef.py
==============================================================================
--- pypy/dist/pypy/interpreter/typedef.py	(original)
+++ pypy/dist/pypy/interpreter/typedef.py	Tue Dec  4 18:12:48 2007
@@ -482,6 +482,7 @@
 from pypy.interpreter.pyopcode import SuspendedUnroller
 from pypy.interpreter.module import Module
 from pypy.interpreter.function import Function, Method, StaticMethod
+from pypy.interpreter.function import ClassMethod
 from pypy.interpreter.function import BuiltinFunction, descr_function_get
 from pypy.interpreter.pytraceback import PyTraceback
 from pypy.interpreter.generator import GeneratorIterator
@@ -696,6 +697,30 @@
                          unwrap_spec = [ObjSpace, W_Root, W_Root]),
     )
 
+ClassMethod.typedef = TypeDef(
+    'classmethod',
+    __new__ = interp2app(ClassMethod.descr_classmethod__new__.im_func,
+                         unwrap_spec = [ObjSpace, W_Root, W_Root]),
+    __get__ = interp2app(ClassMethod.descr_classmethod_get,
+                         unwrap_spec = ['self', ObjSpace, W_Root, W_Root]),
+    __doc__ = """classmethod(function) -> class method
+
+Convert a function to be a class method.
+
+A class method receives the class as implicit first argument,
+just like an instance method receives the instance.
+To declare a class method, use this idiom:
+
+  class C:
+      def f(cls, arg1, arg2, ...): ...
+      f = classmethod(f)
+
+It can be called either on the class (e.g. C.f()) or on an instance
+(e.g. C().f()).  The instance is ignored except for its class.
+If a class method is called for a derived class, the derived class
+object is passed as the implied first argument.""",
+)
+
 def always_none(self, obj):
     return None
 BuiltinFunction.typedef = TypeDef("builtin_function",**Function.typedef.rawdict)

Modified: pypy/dist/pypy/module/__builtin__/__init__.py
==============================================================================
--- pypy/dist/pypy/module/__builtin__/__init__.py	(original)
+++ pypy/dist/pypy/module/__builtin__/__init__.py	Tue Dec  4 18:12:48 2007
@@ -124,7 +124,7 @@
         'any'           : 'functional.any',
         'super'         : 'descriptor.W_Super',
         'staticmethod'  : 'descriptor.StaticMethod',
-        'classmethod'   : 'descriptor.W_ClassMethod',
+        'classmethod'   : 'descriptor.ClassMethod',
         'property'      : 'descriptor.W_Property',
 
     }

Modified: pypy/dist/pypy/module/__builtin__/descriptor.py
==============================================================================
--- pypy/dist/pypy/module/__builtin__/descriptor.py	(original)
+++ pypy/dist/pypy/module/__builtin__/descriptor.py	Tue Dec  4 18:12:48 2007
@@ -5,7 +5,7 @@
 from pypy.interpreter.gateway import interp2app
 from pypy.interpreter.error import OperationError
 from pypy.interpreter.callmethod import object_getattribute
-from pypy.interpreter.function import StaticMethod, Method
+from pypy.interpreter.function import StaticMethod, ClassMethod
 from pypy.interpreter.typedef import GetSetProperty, descr_get_dict, \
      descr_set_dict, interp_attrproperty_w
 
@@ -92,46 +92,6 @@
         super(C, self).meth(arg)"""
 )
 
-class W_ClassMethod(Wrappable):
-    def __init__(self, w_function):
-        self.w_function = w_function
-
-    def new(space, w_type, w_function):
-        if not space.is_true(space.callable(w_function)):
-            name = space.getattr(space.type(w_function), space.wrap('__name__'))
-            raise OperationError(space.w_TypeError, space.wrap(
-                                 "'%s' object is not callable" % name))
-        return W_ClassMethod(w_function)
-
-    def get(self, space, w_obj, w_klass=None):
-        if space.is_w(w_klass, space.w_None):
-            w_klass = space.type(w_obj)
-        return space.wrap(Method(space, self.w_function, w_klass, space.w_None))
-
-W_ClassMethod.typedef = TypeDef(
-    'classmethod',
-    __new__ = interp2app(W_ClassMethod.new.im_func,
-                         unwrap_spec=[ObjSpace, W_Root, W_Root]),
-    __get__ = interp2app(W_ClassMethod.get,
-                         unwrap_spec=['self', ObjSpace, W_Root, W_Root]),
-    __doc__ = """classmethod(function) -> class method
-
-Convert a function to be a class method.
-
-A class method receives the class as implicit first argument,
-just like an instance method receives the instance.
-To declare a class method, use this idiom:
-
-  class C:
-      def f(cls, arg1, arg2, ...): ...
-      f = classmethod(f)
-
-It can be called either on the class (e.g. C.f()) or on an instance
-(e.g. C().f()).  The instance is ignored except for its class.
-If a class method is called for a derived class, the derived class
-object is passed as the implied first argument.""",
-)
-
 class W_Property(Wrappable):
     def __init__(self, space, w_fget, w_fset, w_fdel, w_doc):
         self.w_fget = w_fget


More information about the pypy-svn mailing list