Show More
@@ -78,13 +78,26 b' def fromlocalfunc(modulename, localmods)' | |||||
78 | >>> # unknown = maybe standard library |
|
78 | >>> # unknown = maybe standard library | |
79 | >>> fromlocal('os') |
|
79 | >>> fromlocal('os') | |
80 | False |
|
80 | False | |
|
81 | >>> fromlocal(None, 1) | |||
|
82 | ('foo', 'foo.__init__', True) | |||
|
83 | >>> fromlocal2 = fromlocalfunc('foo.xxx.yyy', localmods) | |||
|
84 | >>> fromlocal2(None, 2) | |||
|
85 | ('foo', 'foo.__init__', True) | |||
81 | """ |
|
86 | """ | |
82 | prefix = '.'.join(modulename.split('.')[:-1]) |
|
87 | prefix = '.'.join(modulename.split('.')[:-1]) | |
83 | if prefix: |
|
88 | if prefix: | |
84 | prefix += '.' |
|
89 | prefix += '.' | |
85 | def fromlocal(name): |
|
90 | def fromlocal(name, level=0): | |
86 | # check relative name at first |
|
91 | # name is None when relative imports are used. | |
87 | for n in prefix + name, name: |
|
92 | if name is None: | |
|
93 | # If relative imports are used, level must not be absolute. | |||
|
94 | assert level > 0 | |||
|
95 | candidates = ['.'.join(modulename.split('.')[:-level])] | |||
|
96 | else: | |||
|
97 | # Check relative name first. | |||
|
98 | candidates = [prefix + name, name] | |||
|
99 | ||||
|
100 | for n in candidates: | |||
88 | if n in localmods: |
|
101 | if n in localmods: | |
89 | return (n, n, False) |
|
102 | return (n, n, False) | |
90 | dottedpath = n + '.__init__' |
|
103 | dottedpath = n + '.__init__' | |
@@ -239,7 +252,7 b' def imported_modules(source, modulename,' | |||||
239 | continue |
|
252 | continue | |
240 | yield found[1] |
|
253 | yield found[1] | |
241 | elif isinstance(node, ast.ImportFrom): |
|
254 | elif isinstance(node, ast.ImportFrom): | |
242 | found = fromlocal(node.module) |
|
255 | found = fromlocal(node.module, node.level) | |
243 | if not found: |
|
256 | if not found: | |
244 | # this should import standard library |
|
257 | # this should import standard library | |
245 | continue |
|
258 | continue |
General Comments 0
You need to be logged in to leave comments.
Login now