Show More
@@ -225,7 +225,7 b' def list_stdlib_modules():' | |||
|
225 | 225 | |
|
226 | 226 | stdlib_modules = set(list_stdlib_modules()) |
|
227 | 227 | |
|
228 | def imported_modules(source, modulename, localmods, ignore_nested=False): | |
|
228 | def imported_modules(source, modulename, f, localmods, ignore_nested=False): | |
|
229 | 229 | """Given the source of a file as a string, yield the names |
|
230 | 230 | imported by that file. |
|
231 | 231 | |
@@ -239,6 +239,7 b' def imported_modules(source, modulename,' | |||
|
239 | 239 | Returns: |
|
240 | 240 | A list of absolute module names imported by the given source. |
|
241 | 241 | |
|
242 | >>> f = 'foo/xxx.py' | |
|
242 | 243 | >>> modulename = 'foo.xxx' |
|
243 | 244 | >>> localmods = {'foo.__init__': True, |
|
244 | 245 | ... 'foo.foo1': True, 'foo.foo2': True, |
@@ -247,43 +248,43 b' def imported_modules(source, modulename,' | |||
|
247 | 248 | >>> # standard library (= not locally defined ones) |
|
248 | 249 | >>> sorted(imported_modules( |
|
249 | 250 | ... 'from stdlib1 import foo, bar; import stdlib2', |
|
250 | ... modulename, localmods)) | |
|
251 | ... modulename, f, localmods)) | |
|
251 | 252 | [] |
|
252 | 253 | >>> # relative importing |
|
253 | 254 | >>> sorted(imported_modules( |
|
254 | 255 | ... 'import foo1; from bar import bar1', |
|
255 | ... modulename, localmods)) | |
|
256 | ... modulename, f, localmods)) | |
|
256 | 257 | ['foo.bar.bar1', 'foo.foo1'] |
|
257 | 258 | >>> sorted(imported_modules( |
|
258 | 259 | ... 'from bar.bar1 import name1, name2, name3', |
|
259 | ... modulename, localmods)) | |
|
260 | ... modulename, f, localmods)) | |
|
260 | 261 | ['foo.bar.bar1'] |
|
261 | 262 | >>> # absolute importing |
|
262 | 263 | >>> sorted(imported_modules( |
|
263 | 264 | ... 'from baz import baz1, name1', |
|
264 | ... modulename, localmods)) | |
|
265 | ... modulename, f, localmods)) | |
|
265 | 266 | ['baz.__init__', 'baz.baz1'] |
|
266 | 267 | >>> # mixed importing, even though it shouldn't be recommended |
|
267 | 268 | >>> sorted(imported_modules( |
|
268 | 269 | ... 'import stdlib, foo1, baz', |
|
269 | ... modulename, localmods)) | |
|
270 | ... modulename, f, localmods)) | |
|
270 | 271 | ['baz.__init__', 'foo.foo1'] |
|
271 | 272 | >>> # ignore_nested |
|
272 | 273 | >>> sorted(imported_modules( |
|
273 | 274 | ... '''import foo |
|
274 | 275 | ... def wat(): |
|
275 | 276 | ... import bar |
|
276 | ... ''', modulename, localmods)) | |
|
277 | ... ''', modulename, f, localmods)) | |
|
277 | 278 | ['foo.__init__', 'foo.bar.__init__'] |
|
278 | 279 | >>> sorted(imported_modules( |
|
279 | 280 | ... '''import foo |
|
280 | 281 | ... def wat(): |
|
281 | 282 | ... import bar |
|
282 | ... ''', modulename, localmods, ignore_nested=True)) | |
|
283 | ... ''', modulename, f, localmods, ignore_nested=True)) | |
|
283 | 284 | ['foo.__init__'] |
|
284 | 285 | """ |
|
285 | 286 | fromlocal = fromlocalfunc(modulename, localmods) |
|
286 | for node in ast.walk(ast.parse(source)): | |
|
287 | for node in ast.walk(ast.parse(source, f)): | |
|
287 | 288 | if ignore_nested and getattr(node, 'col_offset', 0) > 0: |
|
288 | 289 | continue |
|
289 | 290 | if isinstance(node, ast.Import): |
@@ -589,7 +590,7 b' def main(argv):' | |||
|
589 | 590 | for src, modname in sources(source_path, localmodname): |
|
590 | 591 | try: |
|
591 | 592 | used_imports[modname] = sorted( |
|
592 | imported_modules(src, modname, localmods, | |
|
593 | imported_modules(src, modname, source_path, localmods, | |
|
593 | 594 | ignore_nested=True)) |
|
594 | 595 | for error, lineno in verify_import_convention(modname, src, |
|
595 | 596 | localmods): |
General Comments 0
You need to be logged in to leave comments.
Login now