Skip to content

Commit

Permalink
start tracking method visibility
Browse files Browse the repository at this point in the history
  • Loading branch information
alex committed Jun 15, 2013
1 parent b28a136 commit 15ec282
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
4 changes: 2 additions & 2 deletions tests/objects/test_moduleobject.py
Expand Up @@ -452,7 +452,7 @@ class X
def test_private_class_method(self, space):
space.execute("""
class X
def m
def self.m
end
private_class_method :m
end
Expand All @@ -461,7 +461,7 @@ def m
def test_public_class_method(self, space):
space.execute("""
class X
def m
def self.m
end
public_class_method :m
end
Expand Down
16 changes: 12 additions & 4 deletions topaz/objects/functionobject.py
Expand Up @@ -5,11 +5,16 @@


class W_FunctionObject(W_BaseObject):
_immutable_fields_ = ["name", "w_class"]
_immutable_fields_ = ["name", "w_class", "visibility"]

def __init__(self, name, w_class=None):
PUBLIC = 0
PROTECTED = 1
PRIVATE = 2

def __init__(self, name, w_class=None, visibility=PUBLIC):
self.name = name
self.w_class = w_class
self.visibility = visibility

def __deepcopy__(self, memo):
obj = super(W_FunctionObject, self).__deepcopy__(memo)
Expand All @@ -24,8 +29,8 @@ def arity(self, space):
class W_UserFunction(W_FunctionObject):
_immutable_fields_ = ["bytecode", "lexical_scope"]

def __init__(self, name, bytecode, lexical_scope):
W_FunctionObject.__init__(self, name)
def __init__(self, name, bytecode, lexical_scope, visibility=W_FunctionObject.PUBLIC):
W_FunctionObject.__init__(self, name, visibility)
self.bytecode = bytecode
self.lexical_scope = lexical_scope

Expand All @@ -35,6 +40,9 @@ def __deepcopy__(self, memo):
obj.lexical_scope = copy.deepcopy(self.lexical_scope, memo)
return obj

def change_visibility(self, visibility):
return W_UserFunction(self.name, self.bytecode, self.lexical_scope, self.visibility)

def call(self, space, w_receiver, args_w, block):
frame = space.create_frame(
self.bytecode,
Expand Down
15 changes: 10 additions & 5 deletions topaz/objects/moduleobject.py
Expand Up @@ -323,7 +323,13 @@ def set_default_visibility(self, space, visibility):
pass

def set_method_visibility(self, space, name, visibility):
pass
w_method = self.find_method(space, name)
if w_method is None or isinstance(w_method, UndefMethod):
cls_name = space.obj_to_s(self)
raise space.error(space.w_NameError,
"undefined method `%s' for class `%s'" % (name, cls_name)
)
self.define_method(space, name, w_method.change_visibility(visibility))

def method_added(self, space, w_name):
space.send(self, "method_added", [w_name])
Expand Down Expand Up @@ -496,15 +502,15 @@ def method_name(self, space):

@classdef.method("private")
def method_private(self, space, args_w):
self.set_visibility(space, args_w, "private")
self.set_visibility(space, args_w, W_FunctionObject.PRIVATE)

@classdef.method("public")
def method_public(self, space, args_w):
self.set_visibility(space, args_w, "public")
self.set_visibility(space, args_w, W_FunctionObject.PUBLIC)

@classdef.method("protected")
def method_protected(self, space, args_w):
self.set_visibility(space, args_w, "protected")
self.set_visibility(space, args_w, W_FunctionObject.PROTECTED)

@classdef.method("private_constant")
def method_private_constant(self, space, args_w):
Expand Down Expand Up @@ -673,7 +679,6 @@ def method_comparison(self, space, w_other):
else:
return space.newint(1)


@classdef.method("instance_method", name="symbol")
def method_instance_method(self, space, name):
return space.newmethod(name, self)
Expand Down

0 comments on commit 15ec282

Please sign in to comment.