[issue26538] regrtest: setup_tests() must not replace module.__path__ (_NamespacePath) with a simple list // importlib & abspath

STINNER Victor report at bugs.python.org
Fri Mar 11 12:55:49 EST 2016


STINNER Victor added the comment:

Longer extract of setup_tests(), with the long comment:

    # Some times __path__ and __file__ are not absolute (e.g. while running from
    # Lib/) and, if we change the CWD to run the tests in a temporary dir, some
    # imports might fail.  This affects only the modules imported before os.chdir().
    # These modules are searched first in sys.path[0] (so '' -- the CWD) and if
    # they are found in the CWD their __file__ and __path__ will be relative (this
    # happens before the chdir).  All the modules imported after the chdir, are
    # not found in the CWD, and since the other paths in sys.path[1:] are absolute
    # (site.py absolutize them), the __file__ and __path__ will be absolute too.
    # Therefore it is necessary to absolutize manually the __file__ and __path__ of
    # the packages to prevent later imports to fail when the CWD is different.
    for module in sys.modules.values():
        if hasattr(module, '__path__'):
            module.__path__ = [os.path.abspath(path)
                               for path in module.__path__]
        if hasattr(module, '__file__'):
            module.__file__ = os.path.abspath(module.__file__)

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue26538>
_______________________________________


More information about the Python-bugs-list mailing list