Nick Coghlan added the comment:

Ah, interesting, I didn't know there was a difference between the platforms in 
when the placeholder got resolved to a full path.

However, after browsing the code and running some local tests, it seems that 
injecting sys.path[0] isn't handled by Py_GetPath() or PySys_SetPath(), 
regardless of OS.

Instead, it's handled as a side effect of calling PySys_SetArgV(), with 
PySys_SetArgVEx() adding a flag to disable the side effect (for the benefit of 
the isolated mode implementation).

The actual side effect itself is implemented in sys_update_path: 
https://github.com/python/cpython/blob/3.6/Python/sysmodule.c#L2162

So in the case of running `./python Tools` (which exercises the paths of 
interest here) with some strategically placed printf() and PySys_FormatStdout() 
calls, I get:

```
$ ./python Tools
module_search_path: 
/usr/local/lib/python36.zip:/home/ncoghlan/devel/py36/Lib:/home/ncoghlan/devel/py36/Lib:/home/ncoghlan/devel/py36/build/lib.linux-x86_64-3.6
sys.path[0]: '/home/ncoghlan/devel/py36'
sys.path: ['/home/ncoghlan/devel/py36', '/usr/local/lib/python36.zip', 
'/home/ncoghlan/devel/py36/Lib', 
'/home/ncoghlan/devel/py36/build/lib.linux-x86_64-3.6', 
'/home/ncoghlan/.local/lib/python3.6/site-packages']
/home/ncoghlan/devel/py36/python: can't find '__main__' module in 'Tools'
```

The first line is from `Py_GetPath()`, the second is from `sys_update_path()`, 
the third is from `RunMainFromImporter` (before it makes any sys.path changes), 
and the last is the expected error because our `Tools` directory isn't 
executable.

In this scenario, we want the "Tools" entry to *overwrite* sys.path[0].

While in isolated mode I get:

```
$ ./python -I Tools
module_search_path: 
/usr/local/lib/python36.zip:/home/ncoghlan/devel/py36/Lib:/home/ncoghlan/devel/py36/Lib:/home/ncoghlan/devel/py36/build/lib.linux-x86_64-3.6
sys.path: ['/usr/local/lib/python36.zip', '/home/ncoghlan/devel/py36/Lib', 
'/home/ncoghlan/devel/py36/build/lib.linux-x86_64-3.6']
/home/ncoghlan/devel/py36/python: can't find '__main__' module in 'Tools'
```

In this scenario, we want the "Tools" entry to be inserted *before* sys.path[0].

Note that this has been buggy since the -I switch was introduced, but the entry 
we've been overwriting has been the one for the stdlib-as-a-zip-archive, so it 
didn't cause any problems for anyone using the default directory-based 
installation layout.

However, we can't reliably figure out which to do based on the contents of 
sys.path, we need to look at Py_IsolatedFlag instead. (I'm not sure why the 
current check is working on Windows, as sys_update_path() attempts to resolve 
an absolute path entry from the archive or directory name there as well)

----------

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

Reply via email to