Skip to content

Commit

Permalink
Change resolve to restrict search to modules, if specified
Browse files Browse the repository at this point in the history
  • Loading branch information
RobRuana committed Feb 15, 2015
1 parent 8344994 commit e544951
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
12 changes: 7 additions & 5 deletions pockets/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,13 @@ def resolve(name, modules=None):

obj_path = name.split('.')
search_paths = []
for module_path in listify(modules):
search_paths.append(module_path.split('.') + obj_path)
search_paths.append(obj_path)
caller = inspect.getouterframes(inspect.currentframe())[1][0].f_globals
search_paths.append(caller['__name__'].split('.') + obj_path)
if modules:
for module_path in listify(modules):
search_paths.append(module_path.split('.') + obj_path)
else:
search_paths.append(obj_path)
caller = inspect.getouterframes(inspect.currentframe())[1][0].f_globals
search_paths.append(caller['__name__'].split('.') + obj_path)

for path in search_paths:
try:
Expand Down
4 changes: 3 additions & 1 deletion tests/test_inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ def test_object(self):

def test_local_module(self):
self.assertTrue(resolve("TestResolve") is self.__class__)
self.assertTrue(resolve("TestResolve", "pockets") is self.__class__)
self.assertTrue(resolve("TestResolve", "tests.test_inspect") is
self.__class__)
self.assertRaises(ValueError, resolve, "TestResolve", "pockets")

def test_modules_none(self):
self.assertTrue(resolve("pockets") is pockets)
Expand Down

0 comments on commit e544951

Please sign in to comment.