tcalmant / ipopo

iPOPO: a Service-Oriented Component Model for Python
https://ipopo.readthedocs.io/
Apache License 2.0
69 stars 28 forks source link

Bug in FrameworkFactory.get_framework() due to calling buggy normalize_path() function #76

Closed tbr closed 7 years ago

tbr commented 7 years ago

Python's sys.path grows each time when calling the FrameworkFactory.get_framework() method.

This is caused by the call tonormalize_path(), which (to my opinion), is bugged. The normalize_path() function inserts '' andos.getcwd() to the sys.path variable, without checking the contents of sys.path and whether cwd is already in sys.path. Thus, sys.path keeps growing over time while using the ipopo framework.

The following code fixes normalize_path() in framework.py:

def normalize_path():
    """
    Normalizes sys.path to avoid the use of relative folders
    """
    # Normalize Python paths
    whole_path = [os.path.abspath(path) for path in sys.path if os.path.exists(path)]

    # Keep the "dynamic" current folder indicator and add the "static" current path
    sys.path = [ '', os.getcwd() ]

    # Add original path entries
    for path in whole_path:
        if path not in sys.path:
            sys.path.append(path)

    # Normalize paths in loaded modules
    for name, module in sys.modules.items():
        try:
            module.__path__ = [
                os.path.abspath(path) for path in module.__path__
                if _package_exists(path)]
        except AttributeError:
            # builtin modules don't have a __path__
            pass
        except ImportError:
            pass

However, my feeling is also that FrameworkFactory.get_framework() shouldn't be calling normalize_path() at all. This can be done either inside the if cls.__singleton is None: block, or maybe even in the Framework.__init__ constructor.

tcalmant commented 7 years ago

Hi, Thanks for the report.

I'd prefer to move the call to normalize_path() in the if cls.__singleton is None block as the idea was to normalize the environment before creating a framework (therefore, before calling __init__.

In any case, your version of normalize_path() will help to avoid the bug: could you make a pull request for the fix, to keep paternity of your code ?

tcalmant commented 7 years ago

Fixed by #77