◐ Shell
clean mode source ↗

Message 261562 - Python tracker

Extract of test.libregrtest.setup_tests():

    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__)

Because of this code, it's not possible to store test files outside Lib/test/. For the issue #26295 (test_regrtest), I would like to create a temporary directory and then a subdirectory test/ to create temporary test files.

Attached patch adds _NamespacePath.__setitem__() method and modify setup_tests() to keep the _NamespacePath type of module.__path__.

Maybe we should move this abspath() code somewhere in importlib. The site module already contains similar code, abs_paths() function:

    for m in set(sys.modules.values()):
        if (getattr(getattr(m, '__loader__', None), '__module__', None) not in
                ('_frozen_importlib', '_frozen_importlib_external')):
            continue   # don't mess with a PEP 302-supplied __file__
        try:
            m.__file__ = os.path.abspath(m.__file__)
        except (AttributeError, OSError):
            pass
        try:
            m.__cached__ = os.path.abspath(m.__cached__)
        except (AttributeError, OSError):
            pass

Since this code looks to depend on the implementation of importlib (the __loader__ test), IMHO it makes sense to move the code directly somewhere in importlib.