Skip to content

Commit

Permalink
Merge pull request #697 from krekoten/module_constants
Browse files Browse the repository at this point in the history
Module#constants and Module.constants
  • Loading branch information
alex committed May 11, 2013
2 parents 77ef2dc + 2180504 commit 3a25b84
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 9 deletions.
7 changes: 0 additions & 7 deletions spec/tags/core/module/constants_tags.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1 @@
fails:Module.constants returns an array of the names of all toplevel constants
fails:Module.constants returns an array of Symbol names
fails:Module.constants returns Module's constants when given a parameter
fails:Module#constants returns all constants including inherited when passed true
fails:Module#constants returns all constants including inherited when passed some object
fails:Module#constants doesn't returns inherited constants when passed false
fails:Module#constants doesn't returns inherited constants when passed nil
fails:Module#constants returns only public constants
12 changes: 12 additions & 0 deletions topaz/objects/classobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,18 @@ def find_const(self, space, name):
w_res = self.superclass.find_const(space, name)
return w_res

def inherited_constants(self, space):
consts = {}
for const in W_ModuleObject.local_constants(self, space):
consts[const] = None
w_cls = self.superclass
while w_cls is not None:
for const in w_cls.local_constants(space):
consts[const] = None
w_cls = w_cls.superclass

return consts.keys()

def find_method(self, space, name):
method = W_ModuleObject.find_method(self, space, name)
if method is None and self.superclass is not None:
Expand Down
34 changes: 32 additions & 2 deletions topaz/objects/moduleobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,25 @@ def included_constants(self, space):
consts[const] = None
return consts.keys()

def lexical_constants(self, space):
consts = {}
frame = space.getexecutioncontext().gettoprubyframe()
scope = frame.lexical_scope

while scope is not None:
assert isinstance(scope, W_ModuleObject)
for const in scope.w_mod.constants_w:
consts[const] = None
scope = scope.backscope

return consts.keys()

def local_constants(self, space):
return self.constants_w.keys()

def inherited_constants(self, space):
return self.local_constants(space)

def find_local_const(self, space, name):
return self._find_const_pure(name, self.version)

Expand Down Expand Up @@ -474,8 +493,19 @@ def method_private_constant(self, space, args_w):
pass

@classdef.method("constants")
def method_constants(self, space):
return space.newarray([space.newsymbol(n) for n in self.included_constants(space)])
def method_constants(self, space, w_inherit=None):
if self is space.w_module and w_inherit is None:
consts = {}
for const in self.lexical_constants(space):
consts[const] = None
for const in self.inherited_constants(space):
consts[const] = None
return space.newarray([space.newsymbol(n) for n in consts.keys()])

if w_inherit is None or space.is_true(w_inherit):
return space.newarray([space.newsymbol(n) for n in self.included_constants(space)])
else:
return space.newarray([space.newsymbol(n) for n in self.constants_w.keys()])

@classdef.method("const_missing", name="symbol")
def method_const_missing(self, space, name):
Expand Down

0 comments on commit 3a25b84

Please sign in to comment.