[pypy-svn] r4925 - in pypy/branch/src-newobjectmodel/pypy: interpreter objspace/std
arigo at codespeak.net
arigo at codespeak.net
Fri Jun 4 18:55:45 CEST 2004
Author: arigo
Date: Fri Jun 4 18:55:43 2004
New Revision: 4925
Modified:
pypy/branch/src-newobjectmodel/pypy/interpreter/debug.py
pypy/branch/src-newobjectmodel/pypy/interpreter/typedef.py
pypy/branch/src-newobjectmodel/pypy/objspace/std/dicttype.py
pypy/branch/src-newobjectmodel/pypy/objspace/std/floattype.py
pypy/branch/src-newobjectmodel/pypy/objspace/std/inttype.py
pypy/branch/src-newobjectmodel/pypy/objspace/std/listtype.py
pypy/branch/src-newobjectmodel/pypy/objspace/std/objecttype.py
pypy/branch/src-newobjectmodel/pypy/objspace/std/objspace.py
pypy/branch/src-newobjectmodel/pypy/objspace/std/slicetype.py
pypy/branch/src-newobjectmodel/pypy/objspace/std/stdtypedef.py
pypy/branch/src-newobjectmodel/pypy/objspace/std/stringtype.py
pypy/branch/src-newobjectmodel/pypy/objspace/std/tupletype.py
pypy/branch/src-newobjectmodel/pypy/objspace/std/typeobject.py
pypy/branch/src-newobjectmodel/pypy/objspace/std/typetype.py
Log:
Fixes fixes fixes.
Modified: pypy/branch/src-newobjectmodel/pypy/interpreter/debug.py
==============================================================================
--- pypy/branch/src-newobjectmodel/pypy/interpreter/debug.py (original)
+++ pypy/branch/src-newobjectmodel/pypy/interpreter/debug.py Fri Jun 4 18:55:43 2004
@@ -5,7 +5,7 @@
import pdb, sys
def fire(operationerr):
- if not operationerr.debug_tbs:
+ if not operationerr.debug_excs:
return
- tb = operationerr.debug_tbs[-1]
+ exc, val, tb = operationerr.debug_excs[-1]
pdb.post_mortem(tb)
Modified: pypy/branch/src-newobjectmodel/pypy/interpreter/typedef.py
==============================================================================
--- pypy/branch/src-newobjectmodel/pypy/interpreter/typedef.py (original)
+++ pypy/branch/src-newobjectmodel/pypy/interpreter/typedef.py Fri Jun 4 18:55:43 2004
@@ -27,10 +27,29 @@
self.doc = doc
def descr_property_get(space, w_property, w_obj, w_ignored):
- return space.unwrap(w_property).fget(space, w_obj)
+ if w_obj == space.w_None:
+ return w_property
+ else:
+ return space.unwrap(w_property).fget(space, w_obj)
+
+ def descr_property_set(space, w_property, w_obj, w_value):
+ fset = space.unwrap(w_property).fset
+ if fset is None:
+ complains
+ else:
+ do_it
+
+ def descr_property_del(space, w_property, w_obj):
+ fset = space.unwrap(w_property).fset
+ if fset is None:
+ complains
+ else:
+ do_it
typedef = TypeDef("GetSetProperty",
__get__ = interp2app(descr_property_get),
+ __set__ = interp2app(descr_property_set),
+ __delete__ = interp2app(descr_property_del),
)
def attrproperty(name):
Modified: pypy/branch/src-newobjectmodel/pypy/objspace/std/dicttype.py
==============================================================================
--- pypy/branch/src-newobjectmodel/pypy/objspace/std/dicttype.py (original)
+++ pypy/branch/src-newobjectmodel/pypy/objspace/std/dicttype.py Fri Jun 4 18:55:43 2004
@@ -96,7 +96,8 @@
def descr__new__(space, w_dicttype, *args_w, **kwds_w):
from pypy.objspace.std.dictobject import W_DictObject
- return W_DictObject(space, [])
+ w_obj = W_DictObject(space, [])
+ return space.w_dict.check_user_subclass(w_dicttype, w_obj)
# ____________________________________________________________
Modified: pypy/branch/src-newobjectmodel/pypy/objspace/std/floattype.py
==============================================================================
--- pypy/branch/src-newobjectmodel/pypy/objspace/std/floattype.py (original)
+++ pypy/branch/src-newobjectmodel/pypy/objspace/std/floattype.py Fri Jun 4 18:55:43 2004
@@ -4,12 +4,13 @@
def descr__new__(space, w_floattype, w_value=0):
if space.is_true(space.isinstance(w_value, space.w_str)):
try:
- return space.newfloat(float(space.unwrap(w_value)))
+ w_obj = space.newfloat(float(space.unwrap(w_value)))
except ValueError, e:
raise OperationError(space.w_ValueError,
space.wrap(str(e)))
else:
- return space.float(w_value)
+ w_obj = space.float(w_value)
+ return space.w_float.check_user_subclass(w_floattype, w_obj)
# ____________________________________________________________
Modified: pypy/branch/src-newobjectmodel/pypy/objspace/std/inttype.py
==============================================================================
--- pypy/branch/src-newobjectmodel/pypy/objspace/std/inttype.py (original)
+++ pypy/branch/src-newobjectmodel/pypy/objspace/std/inttype.py Fri Jun 4 18:55:43 2004
@@ -5,7 +5,7 @@
def descr__new__(space, w_inttype, w_value=0, w_base=None):
from intobject import W_IntObject
if w_base == space.w_None:
- return space.int(w_value)
+ w_obj = space.int(w_value)
else:
# XXX write the logic for int("str", base)
s = space.unwrap(w_value)
@@ -21,7 +21,8 @@
except OverflowError, e:
raise OperationError(space.w_OverflowError,
space.wrap(str(e)))
- return W_IntObject(value)
+ w_obj = W_IntObject(value)
+ return space.w_int.check_user_subclass(w_inttype, w_obj)
# ____________________________________________________________
Modified: pypy/branch/src-newobjectmodel/pypy/objspace/std/listtype.py
==============================================================================
--- pypy/branch/src-newobjectmodel/pypy/objspace/std/listtype.py (original)
+++ pypy/branch/src-newobjectmodel/pypy/objspace/std/listtype.py Fri Jun 4 18:55:43 2004
@@ -15,7 +15,8 @@
# ____________________________________________________________
def descr__new__(space, w_listtype, *args_w, **kwds_w):
- return space.newlist([])
+ w_obj = space.newlist([])
+ return space.w_list.check_user_subclass(w_listtype, w_obj)
# ____________________________________________________________
Modified: pypy/branch/src-newobjectmodel/pypy/objspace/std/objecttype.py
==============================================================================
--- pypy/branch/src-newobjectmodel/pypy/objspace/std/objecttype.py (original)
+++ pypy/branch/src-newobjectmodel/pypy/objspace/std/objecttype.py Fri Jun 4 18:55:43 2004
@@ -26,7 +26,8 @@
def descr__new__(space, w_type, *args_w, **kwds_w):
# XXX 2.2 behavior: ignoring all arguments
from objectobject import W_ObjectObject
- return W_ObjectObject(space)
+ w_obj = W_ObjectObject(space)
+ return space.w_object.check_user_subclass(w_type, w_obj)
# ____________________________________________________________
Modified: pypy/branch/src-newobjectmodel/pypy/objspace/std/objspace.py
==============================================================================
--- pypy/branch/src-newobjectmodel/pypy/objspace/std/objspace.py (original)
+++ pypy/branch/src-newobjectmodel/pypy/objspace/std/objspace.py Fri Jun 4 18:55:43 2004
@@ -84,7 +84,8 @@
'Exception',
[self.w_object],
{'__init__': w_init,
- '__str__': w_str})
+ '__str__': w_str},
+ )
done = {'Exception': self.w_Exception}
# some of the complexity of the following is due to the fact
@@ -111,7 +112,8 @@
self,
next,
[base],
- {})
+ {},
+ )
setattr(self,
'w_' + next,
newtype)
@@ -141,6 +143,8 @@
}
# types
+ from pypy.objspace.std.objecttype import object_typedef
+ self.object_typedef = object_typedef
self.types_w = {}
for typedef in self.standard_types():
w_type = self.gettypeobject(typedef)
@@ -243,8 +247,11 @@
return stringobject.W_StringObject(self, ''.join(chars))
def type(self, w_obj):
- assert w_obj.typedef, w_obj
- return self.gettypeobject(w_obj.typedef)
+ if hasattr(w_obj, 'w__class__'):
+ return w_obj.w__class__ # user-defined classes
+ else:
+ assert w_obj.typedef, w_obj
+ return self.gettypeobject(w_obj.typedef)
def lookup(self, w_obj, name):
from pypy.objspace.std.cpythonobject import W_CPythonObject
Modified: pypy/branch/src-newobjectmodel/pypy/objspace/std/slicetype.py
==============================================================================
--- pypy/branch/src-newobjectmodel/pypy/objspace/std/slicetype.py (original)
+++ pypy/branch/src-newobjectmodel/pypy/objspace/std/slicetype.py Fri Jun 4 18:55:43 2004
@@ -96,7 +96,8 @@
else:
raise OperationError(space.w_TypeError,
space.wrap("slice() takes at least 1 argument"))
- return space.newslice(w_start, w_stop, w_step)
+ w_obj = space.newslice(w_start, w_stop, w_step)
+ return space.w_slice.check_user_subclass(w_slicetype, w_obj)
# ____________________________________________________________
Modified: pypy/branch/src-newobjectmodel/pypy/objspace/std/stdtypedef.py
==============================================================================
--- pypy/branch/src-newobjectmodel/pypy/objspace/std/stdtypedef.py (original)
+++ pypy/branch/src-newobjectmodel/pypy/objspace/std/stdtypedef.py Fri Jun 4 18:55:43 2004
@@ -46,7 +46,6 @@
# get all the sliced multimethods
multimethods = slicemultimethods(space.__class__, typedef)
for name, code in multimethods.items():
- #print typedef.name, ':', name
fn = function.Function(space, code, defs_w=code.getdefaults(space))
assert name not in rawdict, 'name clash: %s in %s_typedef' % (
name, typedef.name)
@@ -61,7 +60,7 @@
for descrname, descrvalue in rawdict.items():
dict_w[descrname] = w(descrvalue)
- return W_TypeObject(space, typedef.name, bases_w, dict_w)
+ return W_TypeObject(space, typedef.name, bases_w, dict_w, typedef)
def hack_out_multimethods(ns):
result = []
Modified: pypy/branch/src-newobjectmodel/pypy/objspace/std/stringtype.py
==============================================================================
--- pypy/branch/src-newobjectmodel/pypy/objspace/std/stringtype.py (original)
+++ pypy/branch/src-newobjectmodel/pypy/objspace/std/stringtype.py Fri Jun 4 18:55:43 2004
@@ -37,7 +37,8 @@
# ____________________________________________________________
def descr__new__(space, w_stringtype, w_obj=''):
- return space.str(w_obj)
+ w_obj = space.str(w_obj)
+ return space.w_str.check_user_subclass(w_stringtype, w_obj)
# ____________________________________________________________
Modified: pypy/branch/src-newobjectmodel/pypy/objspace/std/tupletype.py
==============================================================================
--- pypy/branch/src-newobjectmodel/pypy/objspace/std/tupletype.py (original)
+++ pypy/branch/src-newobjectmodel/pypy/objspace/std/tupletype.py Fri Jun 4 18:55:43 2004
@@ -4,7 +4,8 @@
def descr__new__(space, w_tupletype, w_items=()):
tuple_w = space.unpackiterable(w_items)
- return space.newtuple(tuple_w)
+ w_obj = space.newtuple(tuple_w)
+ return space.w_tuple.check_user_subclass(w_tupletype, w_obj)
# ____________________________________________________________
Modified: pypy/branch/src-newobjectmodel/pypy/objspace/std/typeobject.py
==============================================================================
--- pypy/branch/src-newobjectmodel/pypy/objspace/std/typeobject.py (original)
+++ pypy/branch/src-newobjectmodel/pypy/objspace/std/typeobject.py Fri Jun 4 18:55:43 2004
@@ -4,11 +4,28 @@
class W_TypeObject(W_Object):
from pypy.objspace.std.typetype import type_typedef as typedef
- def __init__(w_self, space, name, bases_w, dict_w):
+ def __init__(w_self, space, name, bases_w, dict_w, overridetypedef=None):
W_Object.__init__(w_self, space)
w_self.name = name
w_self.bases_w = bases_w
w_self.dict_w = dict_w
+ w_self.needs_new_dict = False
+ if overridetypedef is not None:
+ w_self.instancetypedef = overridetypedef
+ else:
+ # find the most specific typedef
+ longest_mro = [space.object_typedef]
+ for w_base in bases_w:
+ mro = w_base.instancetypedef.mro(space)
+ if len(mro) > len(longest_mro):
+ longest_mro = mro
+ # check that it is a sub-typedef of all other ones
+ for w_base in bases_w:
+ if w_base.instancetypedef not in longest_mro:
+ raise OperationError(space.w_TypeError,
+ space.wrap("instance layout conflicts in "
+ "multiple inheritance"))
+ w_self.instancetypedef = longest_mro[0]
def getmro(w_self):
# XXX this is something that works not too bad right now
@@ -30,6 +47,30 @@
pass
return None
+ def check_user_subclass(w_self, w_subtype, w_obj):
+ """This morphs an object newly created by the w_self's __new__
+ function into an instance of a subclass of w_self if needed."""
+ space = w_self.space
+ if space.is_true(space.is_(w_self, w_subtype)):
+ return w_obj
+ if not space.is_true(space.issubtype(w_subtype, w_self)):
+ raise OperationError(space.w_TypeError,
+ "%s.__new__(%s): %s is not a subtype of %s" % (
+ w_self.name, w_subtype.name, w_subtype.name, w_self.name))
+ if w_self.instancetypedef is not w_subtype.instancetypedef:
+ raise OperationError(space.w_TypeError,
+ "%s.__new__(%s) is not safe, use %s.__new__()" % (
+ w_self.name, w_subtype.name, w_subtype.name))
+ if w_self.instancetypedef is not w_obj.typedef:
+ raise OperationError(space.w_TypeError,
+ "%s.__new__(): got an object of type %s instead of %s" % (
+ w_self.name, space.type(w_obj).name, w_self.name))
+ # stuff extra attributes into w_obj
+ w_obj.w__class__ = w_subtype
+ if w_subtype.needs_new_dict:
+ w_obj.w__dict__ = space.newdict([])
+ return w_obj
+
## def lookup_exactly_here(w_self, w_key):
## space = w_self.space
## multimethods = getmultimethods(space.__class__, w_self.__class__)
@@ -186,7 +227,7 @@
return space.newbool(w_type2 in w_type1.getmro())
def repr__Type(space, w_obj):
- return space.wrap("<pypy type '%s'>" % w_obj.typename) # XXX remove 'pypy'
+ return space.wrap("<pypy type '%s'>" % w_obj.name) # XXX remove 'pypy'
def getattr__Type_ANY(space, w_type, w_name):
name = space.unwrap(w_name)
Modified: pypy/branch/src-newobjectmodel/pypy/objspace/std/typetype.py
==============================================================================
--- pypy/branch/src-newobjectmodel/pypy/objspace/std/typetype.py (original)
+++ pypy/branch/src-newobjectmodel/pypy/objspace/std/typetype.py Fri Jun 4 18:55:43 2004
@@ -3,7 +3,7 @@
def descr__new__(space, w_typetype, w_name, w_bases, w_dict):
- # XXX staticmethod-ify w_dict['__new__']
+ "This is used to create user-defined classes only."
from pypy.objspace.std.typeobject import W_TypeObject
# XXX check types
name = space.unwrap(w_name)
@@ -15,7 +15,13 @@
key = space.unwrap(w_key)
assert isinstance(key, str)
dict_w[key] = space.getitem(w_dict, w_key)
- return W_TypeObject(space, name, bases_w or [space.w_object], dict_w)
+ # XXX classmethod-ify w_dict['__new__']
+ w_type = W_TypeObject(space, name, bases_w or [space.w_object], dict_w, None)
+ # provide a __dict__ for the instances if there isn't any yet
+ if w_type.lookup('__dict__') is None:
+ w_type.needs_new_dict = True
+ w_type.dict_w['__dict__'] = space.wrap(attrproperty_w('w__dict__'))
+ return space.w_type.check_user_subclass(w_typetype, w_type)
def descr_get__mro__(space, w_type):
# XXX this should be inside typeobject.py
More information about the Pypy-commit
mailing list