[issue31038] test_runpy causes running all Python tests when run directly

Nick Coghlan report at bugs.python.org
Wed Jul 26 10:45:35 EDT 2017


Nick Coghlan added the comment:

This is a result of a known quirk in the way sys.path entry execution works: the search for "__main__.py" isn't constrained specifically to sys.path[0].

That's almost entirely a bad thing, but I'd been ignoring it because I hadn't thought of a nice way of fixing it that didn't require some substantial changes to the import system APIs.

However, it just occurred to me that I may have been overcomplicating matters: we don't need to keep runpy from *finding* a __main__.py from outside sys.path[0] in this case, we just need to keep it from *running* it.

That means that after we find the candidate module spec for __main__, we can introduce a new constraint:

    if os.path.commonpath([sys.path[0], spec.origin]) != sys.path[0]:
        raise RuntimeError(...)

It might still be a little fiddly to decide exactly when to enforce the constraint, but it should still be much easier than attempting to constrain the search for the spec directly.

----------
nosy: +brett.cannon, eric.snow

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


More information about the Python-bugs-list mailing list