Skip to content
Permalink
Browse files

Merge pull request #678 from krekoten/class_allocate

Class#allocate
  • Loading branch information
alex committed May 5, 2013
2 parents 6bab1ea + 7ff661c commit dca6296818e65d41fb09b794ab61d5b608918ce8
Showing with 23 additions and 18 deletions.
  1. +0 −2 spec/tags/core/class/allocate_tags.txt
  2. +23 −16 topaz/objects/classobject.py
@@ -1,3 +1 @@
fails:Class#allocate returns a fully-formed instance of Module
fails:Class#allocate does not call initialize on the new instance
fails:Class#allocate raises TypeError for #superclass
@@ -6,7 +6,7 @@


class W_ClassObject(W_ModuleObject):
_immutable_fields_ = ["superclass"]
_immutable_fields_ = ["superclass?"]

classdef = ClassDef("Class", W_ModuleObject.classdef, filepath=__file__)

@@ -95,19 +95,8 @@ def method_undefined(self, space, w_name):
W_ModuleObject.method_undefined(self, space, w_name)

@classdef.singleton_method("allocate")
def singleton_method_allocate(self, space, w_superclass=None):
if w_superclass is not None:
if not isinstance(w_superclass, W_ClassObject):
raise space.error(space.w_TypeError,
"superclass must be a Class (%s given)" % space.obj_to_s(space.getclass(w_superclass))
)
if w_superclass.is_singleton:
raise space.error(space.w_TypeError,
"can't make subclass of singleton class"
)
else:
w_superclass = space.w_object
return space.newclass(None, w_superclass)
def singleton_method_allocate(self, space, args_w):
return space.newclass(None, None)

@classdef.method("new")
def method_new(self, space, args_w, block):
@@ -120,12 +109,30 @@ def method_allocate(self, space, args_w):
return W_Object(space, self)

@classdef.method("initialize")
def method_initialize(self, space, args_w, block):
def method_initialize(self, space, w_superclass=None, block=None):
if w_superclass is not None:
if not isinstance(w_superclass, W_ClassObject):
raise space.error(space.w_TypeError,
"superclass must be a Class (%s given)" % space.obj_to_s(space.getclass(w_superclass))
)
if w_superclass.is_singleton:
raise space.error(space.w_TypeError,
"can't make subclass of singleton class"
)
else:
w_superclass = space.w_object
self.superclass = w_superclass
self.superclass.inherited(space, self)
self.getsingletonclass(space)
space.send_super(space.getclassfor(W_ClassObject), self, space.newsymbol("initialize"), [], block=block)

@classdef.method("superclass")
def method_superclass(self, space):
return self.superclass if self.superclass is not None else space.w_nil
if self.superclass is not None:
return self.superclass
if self is space.w_basicobject:
return space.w_nil
raise space.error(space.w_TypeError, "uninitialized class")

@classdef.method("class_variables")
def method_class_variables(self, space):

0 comments on commit dca6296

Please sign in to comment.