[pypy-svn] r47933 - in pypy/dist/pypy/lang/smalltalk: . test
arigo at codespeak.net
arigo at codespeak.net
Thu Oct 25 15:59:44 CEST 2007
Author: arigo
Date: Thu Oct 25 15:59:44 2007
New Revision: 47933
Modified:
pypy/dist/pypy/lang/smalltalk/classtable.py
pypy/dist/pypy/lang/smalltalk/objtable.py
pypy/dist/pypy/lang/smalltalk/shadow.py
pypy/dist/pypy/lang/smalltalk/test/test_classtable.py
pypy/dist/pypy/lang/smalltalk/test/test_primitives.py
Log:
Put as classtable.w_Xxx globals only the classes that are known to the VM.
The other classes should not be used directly, because they will become
invalid after an image is loaded.
Modified: pypy/dist/pypy/lang/smalltalk/classtable.py
==============================================================================
--- pypy/dist/pypy/lang/smalltalk/classtable.py (original)
+++ pypy/dist/pypy/lang/smalltalk/classtable.py Thu Oct 25 15:59:44 2007
@@ -1,4 +1,4 @@
-from pypy.lang.smalltalk import shadow
+from pypy.lang.smalltalk import shadow, constants
from pypy.lang.smalltalk import constants
def bootstrap_class(instsize, w_superclass=None, w_metaclass=None,
@@ -31,7 +31,6 @@
w_metaclass=w_metaclass,
name=name[2:])
classtable[name] = shadow
- globals()[name] = shadow
return shadow
# Class Name Super class name
@@ -43,12 +42,15 @@
["w_Metaclass", "w_ClassDescription"],
]
define_core_cls("w_ProtoObjectClass", None, None)
+ w_ProtoObjectClass = classtable["w_ProtoObjectClass"]
define_core_cls("w_ProtoObject", None, w_ProtoObjectClass)
for (cls_nm, super_cls_nm) in cls_nm_tbl:
meta_nm = cls_nm + "Class"
meta_super_nm = super_cls_nm + "Class"
w_metacls = define_core_cls(meta_nm, classtable[meta_super_nm], None)
define_core_cls(cls_nm, classtable[super_cls_nm], w_metacls)
+ w_Class = classtable["w_Class"]
+ w_Metaclass = classtable["w_Metaclass"]
w_ProtoObjectClass.as_class_get_shadow().s_superclass = \
w_Class.as_class_get_shadow()
# at this point, all classes that still lack a w_metaclass are themselves
@@ -59,6 +61,11 @@
s.s_metaclass = w_Metaclass.as_class_get_shadow()
create_classtable()
+def copy_in_globals_classes_known_to_the_vm():
+ for name in constants.classes_in_special_object_table:
+ name = 'w_' + name
+ globals()[name] = classtable[name]
+
# ___________________________________________________________________________
# Other classes
@@ -66,12 +73,13 @@
assert cls_nm.startswith("w_")
meta_nm = cls_nm + "Class"
meta_super_nm = supercls_nm + "Class"
- w_meta_cls = globals()[meta_nm] = classtable[meta_nm] = \
+ w_Metaclass = classtable["w_Metaclass"]
+ w_meta_cls = classtable[meta_nm] = \
bootstrap_class(0, # XXX
classtable[meta_super_nm],
w_Metaclass,
name=meta_nm[2:])
- w_cls = globals()[cls_nm] = classtable[cls_nm] = \
+ w_cls = classtable[cls_nm] = \
bootstrap_class(instvarsize,
classtable[supercls_nm],
w_meta_cls,
@@ -87,15 +95,17 @@
define_cls("w_Collection", "w_Object")
define_cls("w_SequencableCollection", "w_Collection")
define_cls("w_ArrayedCollection", "w_SequencableCollection")
-define_cls("w_String", "w_ArrayedCollection")
-define_cls("w_ByteString", "w_String", format=shadow.BYTES)
+define_cls("w_String", "w_ArrayedCollection", format=shadow.BYTES)
define_cls("w_UndefinedObject", "w_Object")
define_cls("w_Boolean", "w_Object")
define_cls("w_True", "w_Boolean")
define_cls("w_False", "w_Boolean")
define_cls("w_ByteArray", "w_ArrayedCollection", format=shadow.BYTES)
define_cls("w_CompiledMethod", "w_ByteArray", format=shadow.COMPILED_METHOD)
+define_cls("w_MethodContext", "w_Object")
define_cls("w_ContextPart", "w_Object")
define_cls("w_MethodContext", "w_ContextPart")
define_cls("w_BlockContext", "w_ContextPart",
instvarsize=constants.BLKCTX_TEMP_FRAME_START)
+
+copy_in_globals_classes_known_to_the_vm()
Modified: pypy/dist/pypy/lang/smalltalk/objtable.py
==============================================================================
--- pypy/dist/pypy/lang/smalltalk/objtable.py (original)
+++ pypy/dist/pypy/lang/smalltalk/objtable.py Thu Oct 25 15:59:44 2007
@@ -14,7 +14,7 @@
return model.W_Float(i)
def wrap_string(str):
- w_inst = ct.w_ByteString.as_class_get_shadow().new(len(str))
+ w_inst = ct.w_String.as_class_get_shadow().new(len(str))
for i in range(len(str)):
w_inst.setbyte(i, ord(str[i]))
return w_inst
@@ -47,9 +47,9 @@
CharacterTable = [bld_char(i) for i in range(256)]
wrap_char_table()
-w_true = ct.w_True.as_class_get_shadow().new()
-w_false = ct.w_False.as_class_get_shadow().new()
-w_nil = ct.w_UndefinedObject.as_class_get_shadow().new()
+w_true = ct.classtable['w_True'].as_class_get_shadow().new()
+w_false = ct.classtable['w_False'].as_class_get_shadow().new()
+w_nil = ct.classtable['w_UndefinedObject'].as_class_get_shadow().new()
w_mone = wrap_int(-1)
w_zero = wrap_int(0)
w_one = wrap_int(1)
Modified: pypy/dist/pypy/lang/smalltalk/shadow.py
==============================================================================
--- pypy/dist/pypy/lang/smalltalk/shadow.py (original)
+++ pypy/dist/pypy/lang/smalltalk/shadow.py Thu Oct 25 15:59:44 2007
@@ -130,11 +130,6 @@
" Number of named instance variables for each instance of this class "
return self.instance_size
- def ismetaclass(self):
- "Heuristic to detect if this is the shadow of a metaclass."
- from pypy.lang.smalltalk import classtable
- return self.s_metaclass.w_self is classtable.w_Metaclass
-
def inherits_from(self, s_superclass):
classshadow = self
while classshadow is not None:
Modified: pypy/dist/pypy/lang/smalltalk/test/test_classtable.py
==============================================================================
--- pypy/dist/pypy/lang/smalltalk/test/test_classtable.py (original)
+++ pypy/dist/pypy/lang/smalltalk/test/test_classtable.py Thu Oct 25 15:59:44 2007
@@ -1,20 +1,26 @@
from pypy.lang.smalltalk import classtable
+def ismetaclass(shadow):
+ # Heuristic to detect if this is the shadow of a metaclass. Don't use
+ # apart from in this test file, because classtable['w_Metaclass'] is
+ # bogus after loading an image.
+ return shadow.s_metaclass.w_self is classtable.classtable['w_Metaclass']
+
def test_every_class_is_an_instance_of_a_metaclass():
for (nm, w_cls) in classtable.classtable.items():
shadow = w_cls.as_class_get_shadow()
- assert shadow.ismetaclass() or shadow.s_metaclass.ismetaclass()
+ assert ismetaclass(shadow) or ismetaclass(shadow.s_metaclass)
def test_every_metaclass_inherits_from_class_and_behavior():
- s_Class = classtable.w_Class.as_class_get_shadow()
- s_Behavior = classtable.w_Behavior.as_class_get_shadow()
+ s_Class = classtable.classtable['w_Class'].as_class_get_shadow()
+ s_Behavior = classtable.classtable['w_Behavior'].as_class_get_shadow()
for (nm, w_cls) in classtable.classtable.items():
shadow = w_cls.as_class_get_shadow()
- if shadow.ismetaclass():
+ if ismetaclass(shadow):
assert shadow.inherits_from(s_Class)
assert s_Class.inherits_from(s_Behavior)
def test_metaclass_of_metaclass_is_an_instance_of_metaclass():
- s_Metaclass = classtable.w_Metaclass.as_class_get_shadow()
+ s_Metaclass = classtable.classtable['w_Metaclass'].as_class_get_shadow()
assert s_Metaclass.s_metaclass.s_metaclass is s_Metaclass
Modified: pypy/dist/pypy/lang/smalltalk/test/test_primitives.py
==============================================================================
--- pypy/dist/pypy/lang/smalltalk/test/test_primitives.py (original)
+++ pypy/dist/pypy/lang/smalltalk/test/test_primitives.py Thu Oct 25 15:59:44 2007
@@ -217,19 +217,21 @@
assert prim(p.STRING_AT, [test_str, i]) == wrap(exp[i])
def test_new():
- w_res = prim(p.NEW, [classtable.w_Object])
- assert w_res.getclass() == classtable.w_Object
+ w_Object = classtable.classtable['w_Object']
+ w_res = prim(p.NEW, [w_Object])
+ assert w_res.getclass() is w_Object
def test_invalid_new():
- prim_fails(p.NEW, [classtable.w_ByteString])
+ prim_fails(p.NEW, [classtable.w_String])
def test_new_with_arg():
- w_res = prim(p.NEW_WITH_ARG, [classtable.w_ByteString, 20])
- assert w_res.getclass() == classtable.w_ByteString
+ w_res = prim(p.NEW_WITH_ARG, [classtable.w_String, 20])
+ assert w_res.getclass() == classtable.w_String
assert w_res.size() == 20
def test_invalid_new_with_arg():
- prim_fails(p.NEW_WITH_ARG, [classtable.w_Object, 20])
+ w_Object = classtable.classtable['w_Object']
+ prim_fails(p.NEW_WITH_ARG, [w_Object, 20])
def test_inst_var_at():
# I am not entirely sure if this is what this primitive is
More information about the Pypy-commit
mailing list