Skip to content

Commit

Permalink
fix Module.getattr and rename "absolute_modname" to "relative_to_abso…
Browse files Browse the repository at this point in the history
…lute_name"

This renaming is done because there is actually no simple way to know if we
are on an absolute import or on an implicit relative import.
And the "absolute_import_activated" killed
the relative import we just wanted in case of "relative_only" option.
  • Loading branch information
Emile Anclin committed Oct 21, 2010
1 parent 75d0203 commit 6d59ad0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
4 changes: 3 additions & 1 deletion mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,9 @@ def do_import_module(self, modname):
# XXX: no more needed ?
mymodule = self.root()
level = getattr(self, 'level', None) # Import as no level
if mymodule.absolute_modname(modname, level) == mymodule.name:
# XXX we should investigate deeper if we really want to check
# importing itself: modname and mymodule.name be relative or absolute
if mymodule.relative_to_absolute_name(modname, level) == mymodule.name:
# FIXME: I don't know what to do here...
raise InferenceError('module importing itself: %s' % modname)
try:
Expand Down
18 changes: 12 additions & 6 deletions scoped_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,20 +331,23 @@ def absolute_import_activated(self):

def import_module(self, modname, relative_only=False, level=None):
"""import the given module considering self as context"""
absmodname = self.relative_to_absolute_name(modname, level)
try:
absmodname = self.absolute_modname(modname, level)
return MANAGER.astng_from_module_name(absmodname)
except ASTNGBuildingException:
# we only want to import a sub module or package of this module,
# skip here
if relative_only:
raise
module = MANAGER.astng_from_module_name(modname)
return module
return MANAGER.astng_from_module_name(modname)

def absolute_modname(self, modname, level):
if self.absolute_import_activated() and not level:
return modname
def relative_to_absolute_name(self, modname, level):
"""return the absolute module name for a relative import.
The relative import can be implicit or explicit.
"""
# XXX this returns non sens when called on an absolute import
# like 'pylint.checkers.logilab.astng.utils'
if level:
parts = self.name.split('.')
if self.package:
Expand All @@ -355,9 +358,12 @@ def absolute_modname(self, modname, level):
else:
package_name = '.'.join(self.name.split('.')[:-1])
if package_name:
if not modname:
return package_name
return '%s.%s' % (package_name, modname)
return modname


def wildcard_import_names(self):
"""return the list of imported names when this module is 'wildcard
imported'
Expand Down

0 comments on commit 6d59ad0

Please sign in to comment.