[PATCH 02/16] Modify collect so it works with directories with missing __init__.py

Chris Lumens clumens at redhat.com
Thu Dec 13 18:42:12 UTC 2012


> @@ -400,12 +400,25 @@ def collect(module_pattern, path, pred):
>  
>      retval = []
>      for module_file in os.listdir(path):
> -        if not module_file.endswith(".py") or module_file == "__init__.py":
> +        if module_file == "__init__.py":
>              continue
>  
> -        mod_name = module_file[:-3]
> -        module = importlib.import_module(module_pattern % mod_name)
> -
> +        try:
> +            mod_name = module_file[:module_file.rindex(".")]
> +        except ValueError:
> +            mod_name = module_file
> +
> +        try:    
> +            imp.acquire_lock()
> +            mod_info = imp.find_module(mod_name, path)
> +            module = imp.load_module(module_pattern % mod_name, *mod_info)
> +            imp.release_lock()
> +        except ImportError:
> +            continue
> +        finally:
> +            if mod_info[0]:
> +                mod_info[0].close()
> +        

I wish the imp module locking thing worked more like other lock-related
code so you could do:

    with imp.lock do:
       ...

but that's hardly a knock on your patch.

- Chris


More information about the anaconda-patches mailing list