Steve Dower added the comment:

Found it in Modules/main.c in RunMainFromImporter():

    /* argv0 is usable as an import source, so put it in sys.path[0]
       and import __main__ */
    sys_path = PySys_GetObject("path");
    if (sys_path == NULL) {
        PyErr_SetString(PyExc_RuntimeError, "unable to get sys.path");
        goto error;
    }
    if (PyList_SetItem(sys_path, 0, argv0)) {
        argv0 = NULL;
        goto error;
    }
    Py_INCREF(argv0);

When running with a ._pth file, we force the -I option, which removes the empty 
entry at sys.path[0]. Instead, it becomes the path to the .zip file with the 
standard library, so when RunMainFromImporter overwrites it blindly, it's 
cutting out the only hope it has of finding runpy.

You can see this in a normal install by doing:

    py -I -c "import sys; print(sys.path)"
    [correct output]

    py -Ii test.pyz
    [output from test.pyz]
    >>> import sys; sys.path
    [incorrect output]

So we need to stop blindly overwriting sys.path[0] here. I'm not sure what the 
best approach would be, but maybe Nick has a preference? Perhaps we should pass 
the zip file path into runpy._run_module_as_main so it can initialize __path__ 
with it rather than making a global change? Or maybe an insert rather than a 
set is the right way and I'm over-thinking this.

----------
assignee: steve.dower -> 
nosy: +ncoghlan
stage:  -> needs patch
versions: +Python 3.7

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue29319>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to