[pypy-commit] pypy remove-remaining-smm: Make W_ObjectObject a W_Root.

Manuel Jacob noreply at buildbot.pypy.org
Mon Feb 24 18:20:42 CET 2014


Author: Manuel Jacob
Branch: remove-remaining-smm
Changeset: r69356:15baebca729e
Date: 2014-02-24 18:19 +0100
http://bitbucket.org/pypy/pypy/changeset/15baebca729e/

Log:	Make W_ObjectObject a W_Root.

diff --git a/pypy/objspace/std/model.py b/pypy/objspace/std/model.py
--- a/pypy/objspace/std/model.py
+++ b/pypy/objspace/std/model.py
@@ -31,7 +31,6 @@
         self.config = config
         # All the Python types that we want to provide in this StdObjSpace
         class result:
-            from pypy.objspace.std.objecttype import object_typedef
             from pypy.objspace.std.typeobject   import type_typedef
         self.pythontypes = [value for key, value in result.__dict__.items()
                             if not key.startswith('_')]   # don't look
@@ -65,6 +64,7 @@
 
         # not-multimethod based types
 
+        self.pythontypes.append(objectobject.W_ObjectObject.typedef)
         self.pythontypes.append(noneobject.W_NoneObject.typedef)
         self.pythontypes.append(tupleobject.W_TupleObject.typedef)
         self.pythontypes.append(listobject.W_ListObject.typedef)
diff --git a/pypy/objspace/std/objectobject.py b/pypy/objspace/std/objectobject.py
--- a/pypy/objspace/std/objectobject.py
+++ b/pypy/objspace/std/objectobject.py
@@ -1,13 +1,231 @@
-from pypy.objspace.std.model import W_Object
-from pypy.objspace.std.register_all import register_all
+from pypy.interpreter.baseobjspace import W_Root
+from pypy.interpreter.error import OperationError, oefmt
+from pypy.interpreter.gateway import applevel, interp2app, unwrap_spec
+from pypy.interpreter.typedef import GetSetProperty, default_identity_hash
+from pypy.objspace.descroperation import Object
+from pypy.objspace.std.stdtypedef import StdTypeDef
 
 
-class W_ObjectObject(W_Object):
+app = applevel(r'''
+def _abstract_method_error(typ):
+    methods = ", ".join(sorted(typ.__abstractmethods__))
+    err = "Can't instantiate abstract class %s with abstract methods %s"
+    raise TypeError(err % (typ.__name__, methods))
+
+def reduce_1(obj, proto):
+    import copy_reg
+    return copy_reg._reduce_ex(obj, proto)
+
+def reduce_2(obj):
+    cls = obj.__class__
+
+    try:
+        getnewargs = obj.__getnewargs__
+    except AttributeError:
+        args = ()
+    else:
+        args = getnewargs()
+        if not isinstance(args, tuple):
+            raise TypeError, "__getnewargs__ should return a tuple"
+
+    try:
+        getstate = obj.__getstate__
+    except AttributeError:
+        state = getattr(obj, "__dict__", None)
+        names = slotnames(cls) # not checking for list
+        if names is not None:
+            slots = {}
+            for name in names:
+                try:
+                    value = getattr(obj, name)
+                except AttributeError:
+                    pass
+                else:
+                    slots[name] =  value
+            if slots:
+                state = state, slots
+    else:
+        state = getstate()
+
+    if isinstance(obj, list):
+        listitems = iter(obj)
+    else:
+        listitems = None
+
+    if isinstance(obj, dict):
+        dictitems = obj.iteritems()
+    else:
+        dictitems = None
+
+    import copy_reg
+    newobj = copy_reg.__newobj__
+
+    args2 = (cls,) + args
+    return newobj, args2, state, listitems, dictitems
+
+def slotnames(cls):
+    if not isinstance(cls, type):
+        return None
+
+    try:
+        return cls.__dict__["__slotnames__"]
+    except KeyError:
+        pass
+
+    import copy_reg
+    slotnames = copy_reg._slotnames(cls)
+    if not isinstance(slotnames, list) and slotnames is not None:
+        raise TypeError, "copy_reg._slotnames didn't return a list or None"
+    return slotnames
+''', filename=__file__)
+
+_abstract_method_error = app.interphook("_abstract_method_error")
+reduce_1 = app.interphook('reduce_1')
+reduce_2 = app.interphook('reduce_2')
+
+
+class W_ObjectObject(W_Root):
     """Instances of this class are what the user can directly see with an
     'object()' call."""
-    from pypy.objspace.std.objecttype import object_typedef as typedef
 
-# ____________________________________________________________
 
+def descr__new__(space, w_type, __args__):
+    from pypy.objspace.std.objectobject import W_ObjectObject
+    from pypy.objspace.std.typeobject import _precheck_for_new
+    # don't allow arguments if the default object.__init__() is about
+    # to be called
+    w_type = _precheck_for_new(space, w_type)
+    w_parentinit, w_ignored = w_type.lookup_where('__init__')
+    if w_parentinit is space.w_object:
+        try:
+            __args__.fixedunpack(0)
+        except ValueError:
+            raise OperationError(space.w_TypeError,
+                                 space.wrap("default __new__ takes "
+                                            "no parameters"))
+    if w_type.is_abstract():
+        _abstract_method_error(space, w_type)
+    w_obj = space.allocate_instance(W_ObjectObject, w_type)
+    return w_obj
 
-register_all(vars())
+
+def descr___subclasshook__(space, __args__):
+    return space.w_NotImplemented
+
+
+def descr__init__(space, w_obj, __args__):
+    # don't allow arguments unless __new__ is overridden
+    w_type = space.type(w_obj)
+    w_parent_new, _ = w_type.lookup_where('__new__')
+    if w_parent_new is space.w_object:
+        try:
+            __args__.fixedunpack(0)
+        except ValueError:
+            raise OperationError(space.w_TypeError,
+                space.wrap("object.__init__() takes no parameters"))
+
+
+def descr_get___class__(space, w_obj):
+    return space.type(w_obj)
+
+
+def descr_set___class__(space, w_obj, w_newcls):
+    from pypy.objspace.std.typeobject import W_TypeObject
+    if not isinstance(w_newcls, W_TypeObject):
+        raise oefmt(space.w_TypeError,
+                    "__class__ must be set to new-style class, not '%T' "
+                    "object", w_newcls)
+    if not w_newcls.is_heaptype():
+        raise OperationError(space.w_TypeError,
+                             space.wrap("__class__ assignment: only for heap types"))
+    w_oldcls = space.type(w_obj)
+    assert isinstance(w_oldcls, W_TypeObject)
+    if w_oldcls.get_full_instance_layout() == w_newcls.get_full_instance_layout():
+        w_obj.setclass(space, w_newcls)
+    else:
+        raise oefmt(space.w_TypeError,
+                    "__class__ assignment: '%N' object layout differs from "
+                    "'%N'", w_oldcls, w_newcls)
+
+
+def descr__repr__(space, w_obj):
+    w_type = space.type(w_obj)
+    classname = w_type.getname(space)
+    w_module = w_type.lookup("__module__")
+    if w_module is not None:
+        try:
+            modulename = space.str_w(w_module)
+        except OperationError, e:
+            if not e.match(space, space.w_TypeError):
+                raise
+        else:
+            classname = '%s.%s' % (modulename, classname)
+    return w_obj.getrepr(space, '%s object' % (classname,))
+
+
+def descr__str__(space, w_obj):
+    w_type = space.type(w_obj)
+    w_impl = w_type.lookup("__repr__")
+    if w_impl is None:
+        raise OperationError(space.w_TypeError,      # can it really occur?
+                             space.wrap("operand does not support unary str"))
+    return space.get_and_call_function(w_impl, w_obj)
+
+
+ at unwrap_spec(proto=int)
+def descr__reduce__(space, w_obj, proto=0):
+    if proto >= 2:
+        return reduce_2(space, w_obj)
+    w_proto = space.wrap(proto)
+    return reduce_1(space, w_obj, w_proto)
+
+ at unwrap_spec(proto=int)
+def descr__reduce_ex__(space, w_obj, proto=0):
+    w_st_reduce = space.wrap('__reduce__')
+    w_reduce = space.findattr(w_obj, w_st_reduce)
+    if w_reduce is not None:
+        w_cls = space.getattr(w_obj, space.wrap('__class__'))
+        w_cls_reduce_meth = space.getattr(w_cls, w_st_reduce)
+        w_cls_reduce = space.getattr(w_cls_reduce_meth, space.wrap('im_func'))
+        w_objtype = space.w_object
+        w_obj_dict = space.getattr(w_objtype, space.wrap('__dict__'))
+        w_obj_reduce = space.getitem(w_obj_dict, w_st_reduce)
+        override = not space.is_w(w_cls_reduce, w_obj_reduce)
+        # print 'OVR', override, w_cls_reduce, w_obj_reduce
+        if override:
+            return space.call(w_reduce, space.newtuple([]))
+    return descr__reduce__(space, w_obj, proto)
+
+def descr___format__(space, w_obj, w_format_spec):
+    if space.isinstance_w(w_format_spec, space.w_unicode):
+        w_as_str = space.call_function(space.w_unicode, w_obj)
+    elif space.isinstance_w(w_format_spec, space.w_str):
+        w_as_str = space.str(w_obj)
+    else:
+        msg = "format_spec must be a string"
+        raise OperationError(space.w_TypeError, space.wrap(msg))
+    if space.len_w(w_format_spec) > 0:
+        msg = "object.__format__ with a non-empty format string is deprecated"
+        space.warn(space.wrap(msg), space.w_PendingDeprecationWarning)
+    return space.format(w_as_str, w_format_spec)
+
+
+W_ObjectObject.typedef = StdTypeDef("object",
+    __doc__ = "The most base type",
+    __new__ = interp2app(descr__new__),
+    __subclasshook__ = interp2app(descr___subclasshook__, as_classmethod=True),
+
+    # these are actually implemented in pypy.objspace.descroperation
+    __getattribute__ = interp2app(Object.descr__getattribute__.im_func),
+    __setattr__ = interp2app(Object.descr__setattr__.im_func),
+    __delattr__ = interp2app(Object.descr__delattr__.im_func),
+
+    __init__ = interp2app(descr__init__),
+    __class__ = GetSetProperty(descr_get___class__, descr_set___class__),
+    __repr__ = interp2app(descr__repr__),
+    __str__ = interp2app(descr__str__),
+    __hash__ = interp2app(default_identity_hash),
+    __reduce__ = interp2app(descr__reduce__),
+    __reduce_ex__ = interp2app(descr__reduce_ex__),
+    __format__ = interp2app(descr___format__),
+)
diff --git a/pypy/objspace/std/objecttype.py b/pypy/objspace/std/objecttype.py
deleted file mode 100644
--- a/pypy/objspace/std/objecttype.py
+++ /dev/null
@@ -1,221 +0,0 @@
-from pypy.interpreter.error import OperationError, oefmt
-from pypy.interpreter.typedef import GetSetProperty, default_identity_hash
-from pypy.interpreter import gateway
-from pypy.objspace.descroperation import Object
-from pypy.objspace.std.stdtypedef import StdTypeDef
-
-def descr__repr__(space, w_obj):
-    w_type = space.type(w_obj)
-    classname = w_type.getname(space)
-    w_module = w_type.lookup("__module__")
-    if w_module is not None:
-        try:
-            modulename = space.str_w(w_module)
-        except OperationError, e:
-            if not e.match(space, space.w_TypeError):
-                raise
-        else:
-            classname = '%s.%s' % (modulename, classname)
-    return w_obj.getrepr(space, '%s object' % (classname,))
-
-def descr__str__(space, w_obj):
-    w_type = space.type(w_obj)
-    w_impl = w_type.lookup("__repr__")
-    if w_impl is None:
-        raise OperationError(space.w_TypeError,      # can it really occur?
-                             space.wrap("operand does not support unary str"))
-    return space.get_and_call_function(w_impl, w_obj)
-
-def descr__class__(space, w_obj):
-    return space.type(w_obj)
-
-def descr_set___class__(space, w_obj, w_newcls):
-    from pypy.objspace.std.typeobject import W_TypeObject
-    if not isinstance(w_newcls, W_TypeObject):
-        raise oefmt(space.w_TypeError,
-                    "__class__ must be set to new-style class, not '%T' "
-                    "object", w_newcls)
-    if not w_newcls.is_heaptype():
-        raise OperationError(space.w_TypeError,
-                             space.wrap("__class__ assignment: only for heap types"))
-    w_oldcls = space.type(w_obj)
-    assert isinstance(w_oldcls, W_TypeObject)
-    if w_oldcls.get_full_instance_layout() == w_newcls.get_full_instance_layout():
-        w_obj.setclass(space, w_newcls)
-    else:
-        raise oefmt(space.w_TypeError,
-                    "__class__ assignment: '%N' object layout differs from "
-                    "'%N'", w_oldcls, w_newcls)
-
-
-app = gateway.applevel("""
-def _abstract_method_error(typ):
-    methods = ", ".join(sorted(typ.__abstractmethods__))
-    err = "Can't instantiate abstract class %s with abstract methods %s"
-    raise TypeError(err % (typ.__name__, methods))
-""")
-_abstract_method_error = app.interphook("_abstract_method_error")
-
-
-def descr__new__(space, w_type, __args__):
-    from pypy.objspace.std.objectobject import W_ObjectObject
-    from pypy.objspace.std.typeobject import _precheck_for_new
-    # don't allow arguments if the default object.__init__() is about
-    # to be called
-    w_type = _precheck_for_new(space, w_type)
-    w_parentinit, w_ignored = w_type.lookup_where('__init__')
-    if w_parentinit is space.w_object:
-        try:
-            __args__.fixedunpack(0)
-        except ValueError:
-            raise OperationError(space.w_TypeError,
-                                 space.wrap("default __new__ takes "
-                                            "no parameters"))
-    if w_type.is_abstract():
-        _abstract_method_error(space, w_type)
-    w_obj = space.allocate_instance(W_ObjectObject, w_type)
-    return w_obj
-
-def descr__init__(space, w_obj, __args__):
-    # don't allow arguments unless __new__ is overridden
-    w_type = space.type(w_obj)
-    w_parent_new, _ = w_type.lookup_where('__new__')
-    if w_parent_new is space.w_object:
-        try:
-            __args__.fixedunpack(0)
-        except ValueError:
-            raise OperationError(space.w_TypeError,
-                space.wrap("object.__init__() takes no parameters"))
-
-
- at gateway.unwrap_spec(proto=int)
-def descr__reduce__(space, w_obj, proto=0):
-    if proto >= 2:
-        return reduce_2(space, w_obj)
-    w_proto = space.wrap(proto)
-    return reduce_1(space, w_obj, w_proto)
-
- at gateway.unwrap_spec(proto=int)
-def descr__reduce_ex__(space, w_obj, proto=0):
-    w_st_reduce = space.wrap('__reduce__')
-    w_reduce = space.findattr(w_obj, w_st_reduce)
-    if w_reduce is not None:
-        w_cls = space.getattr(w_obj, space.wrap('__class__'))
-        w_cls_reduce_meth = space.getattr(w_cls, w_st_reduce)
-        w_cls_reduce = space.getattr(w_cls_reduce_meth, space.wrap('im_func'))
-        w_objtype = space.w_object
-        w_obj_dict = space.getattr(w_objtype, space.wrap('__dict__'))
-        w_obj_reduce = space.getitem(w_obj_dict, w_st_reduce)
-        override = not space.is_w(w_cls_reduce, w_obj_reduce)
-        # print 'OVR', override, w_cls_reduce, w_obj_reduce
-        if override:
-            return space.call(w_reduce, space.newtuple([]))
-    return descr__reduce__(space, w_obj, proto)
-
-def descr___format__(space, w_obj, w_format_spec):
-    if space.isinstance_w(w_format_spec, space.w_unicode):
-        w_as_str = space.call_function(space.w_unicode, w_obj)
-    elif space.isinstance_w(w_format_spec, space.w_str):
-        w_as_str = space.str(w_obj)
-    else:
-        msg = "format_spec must be a string"
-        raise OperationError(space.w_TypeError, space.wrap(msg))
-    if space.len_w(w_format_spec) > 0:
-        msg = "object.__format__ with a non-empty format string is deprecated"
-        space.warn(space.wrap(msg), space.w_PendingDeprecationWarning)
-    return space.format(w_as_str, w_format_spec)
-
-def descr___subclasshook__(space, __args__):
-    return space.w_NotImplemented
-
-
-app = gateway.applevel(r'''
-def reduce_1(obj, proto):
-    import copy_reg
-    return copy_reg._reduce_ex(obj, proto)
-
-def reduce_2(obj):
-    cls = obj.__class__
-
-    try:
-        getnewargs = obj.__getnewargs__
-    except AttributeError:
-        args = ()
-    else:
-        args = getnewargs()
-        if not isinstance(args, tuple):
-            raise TypeError, "__getnewargs__ should return a tuple"
-
-    try:
-        getstate = obj.__getstate__
-    except AttributeError:
-        state = getattr(obj, "__dict__", None)
-        names = slotnames(cls) # not checking for list
-        if names is not None:
-            slots = {}
-            for name in names:
-                try:
-                    value = getattr(obj, name)
-                except AttributeError:
-                    pass
-                else:
-                    slots[name] =  value
-            if slots:
-                state = state, slots
-    else:
-        state = getstate()
-
-    if isinstance(obj, list):
-        listitems = iter(obj)
-    else:
-        listitems = None
-
-    if isinstance(obj, dict):
-        dictitems = obj.iteritems()
-    else:
-        dictitems = None
-
-    import copy_reg
-    newobj = copy_reg.__newobj__
-
-    args2 = (cls,) + args
-    return newobj, args2, state, listitems, dictitems
-
-def slotnames(cls):
-    if not isinstance(cls, type):
-        return None
-
-    try:
-        return cls.__dict__["__slotnames__"]
-    except KeyError:
-        pass
-
-    import copy_reg
-    slotnames = copy_reg._slotnames(cls)
-    if not isinstance(slotnames, list) and slotnames is not None:
-        raise TypeError, "copy_reg._slotnames didn't return a list or None"
-    return slotnames
-''', filename=__file__)
-
-reduce_1 = app.interphook('reduce_1')
-reduce_2 = app.interphook('reduce_2')
-
-# ____________________________________________________________
-
-object_typedef = StdTypeDef("object",
-    __getattribute__ = gateway.interp2app(Object.descr__getattribute__.im_func),
-    __setattr__ = gateway.interp2app(Object.descr__setattr__.im_func),
-    __delattr__ = gateway.interp2app(Object.descr__delattr__.im_func),
-    __str__ = gateway.interp2app(descr__str__),
-    __repr__ = gateway.interp2app(descr__repr__),
-    __class__ = GetSetProperty(descr__class__, descr_set___class__),
-    __doc__ = '''The most base type''',
-    __new__ = gateway.interp2app(descr__new__),
-    __hash__ = gateway.interp2app(default_identity_hash),
-    __reduce_ex__ = gateway.interp2app(descr__reduce_ex__),
-    __reduce__ = gateway.interp2app(descr__reduce__),
-    __format__ = gateway.interp2app(descr___format__),
-    __subclasshook__ = gateway.interp2app(descr___subclasshook__,
-                                          as_classmethod=True),
-    __init__ = gateway.interp2app(descr__init__),
-    )
diff --git a/pypy/objspace/std/stdtypedef.py b/pypy/objspace/std/stdtypedef.py
--- a/pypy/objspace/std/stdtypedef.py
+++ b/pypy/objspace/std/stdtypedef.py
@@ -29,8 +29,8 @@
 
 @jit.unroll_safe
 def issubtypedef(a, b):
-    from pypy.objspace.std.objecttype import object_typedef
-    if b is object_typedef:
+    from pypy.objspace.std.objectobject import W_ObjectObject
+    if b is W_ObjectObject.typedef:
         return True
     if a is None:
         return False
@@ -56,7 +56,7 @@
         "NOT_RPYTHON: initialization-time only."
         # build a W_TypeObject from this StdTypeDef
         from pypy.objspace.std.typeobject import W_TypeObject
-        from pypy.objspace.std.objecttype import object_typedef
+        from pypy.objspace.std.objectobject import W_ObjectObject
 
         space = cache.space
         w = space.wrap
@@ -75,10 +75,10 @@
                 lazyloaders[name] = loader
 
         # compute the bases
-        if typedef is object_typedef:
+        if typedef is W_ObjectObject.typedef:
             bases_w = []
         else:
-            bases = typedef.bases or [object_typedef]
+            bases = typedef.bases or [W_ObjectObject.typedef]
             bases_w = [space.gettypeobject(base) for base in bases]
 
         # wrap everything


More information about the pypy-commit mailing list