[Python-projects] sys.path cleanup in modutils

ashley walsh amwlists at gmail.com
Wed Nov 17 02:04:11 CET 2004


Hi,

There's a bug in modutils.get_module_part() in common-0.7.1, so that
it doesn't clean up sys.path except if an import fails because of an
ImportError.  I patched it with a try/finally:

146,160c146,158
<     try:
<         while parts and hasattr(module, '__path__'):
<             name = parts[0]
<             sys.path.insert(0, module.__path__[0])
<             to_pop += 1
<             try:
<                 mp_file, mp_filename, mp_desc = find_module(name,
module.__path__)
<                 module = load_module(name, mp_file, mp_filename, mp_desc)
<             except ImportError, ex:
<                 break
<             result.append(parts.pop(0))
<     finally:
<         while to_pop:
<             sys.path.pop(0)
<             to_pop -= 1
---
>     while parts and hasattr(module, '__path__'):
>         name = parts[0]
>         sys.path.insert(0, module.__path__[0])
>         to_pop += 1
>         try:
>             mp_file, mp_filename, mp_desc = find_module(name, module.__path__)
>             module = load_module(name, mp_file, mp_filename, mp_desc)
>         except ImportError, ex:
>             break
>         result.append(parts.pop(0))
>     while to_pop:
>         sys.path.pop(0)
>         to_pop -= 1

I fount this using pylint with some twisted-dependent code, where
get_module_part() was trying to reimport twisted.spread.pb.  This
failed with a ValueError because it tries to reregister an adapter.

Cheers,
Ashley


More information about the Python-Projects mailing list