[issue32642] add support for path-like objects in sys.path

Serhiy Storchaka report at bugs.python.org
Sat Mar 26 01:34:01 EDT 2022


Serhiy Storchaka <storchaka+cpython at gmail.com> added the comment:

Are there any problems with converting a Path to string before adding it to sys.path? You do this one time, and any users of sys.path will not need to bother about conversion. It is better even for performance.

Otherwise we will need to revise and update every code that uses sys.path, and many bugs will left in third-party code for years. For example, in unittest the code

        if not top_level_dir in sys.path:
            sys.path.insert(0, top_level_dir)

should be replaced with more cumbersome

        for path in sys.path:
            if os.fsdecode(path) == top_level_dir:
                break
        else:
            sys.path.insert(0, top_level_dir)

In multiprocessing the code

    sys_path=sys.path.copy()
    try:
        i = sys_path.index('')
    except ValueError:
        pass
    else:
        sys_path[i] = process.ORIGINAL_DIR

should be replaced with

    sys_path=sys.path.copy()
    for i, path in enumerate(sys_path):
        if os.fsdecode(path) == '':
            sys_path[i] = process.ORIGINAL_DIR
            break

It is just two examples. I did not review the whole stdlib, and there should be more third-party code.

----------
nosy: +serhiy.storchaka

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue32642>
_______________________________________


More information about the Python-bugs-list mailing list