Skip to content

Commit

Permalink
Merge pull request #678 from krekoten/class_allocate
Browse files Browse the repository at this point in the history
Class#allocate
  • Loading branch information
alex committed May 5, 2013
2 parents 6bab1ea + 7ff661c commit dca6296
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
2 changes: 0 additions & 2 deletions spec/tags/core/class/allocate_tags.txt
Original file line number Diff line number Diff line change
@@ -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
39 changes: 23 additions & 16 deletions topaz/objects/classobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@


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

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

Expand Down Expand Up @@ -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):
Expand All @@ -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):
Expand Down

0 comments on commit dca6296

Please sign in to comment.