New submission from Eryk Sun <eryk...@gmail.com>:

I've occasionally seen requests to find the path of the LNK shortcut that was 
used to run a script -- for whatever reason. This can be reliably supported in 
most cases by calling WinAPI GetStartupInfo. If the flag STARTF_TITLEISLINKNAME 
is set, then the lpTitle field is the path to the LNK file. (This is how the 
console host process knows to use the shortcut file for its properties instead 
of the registry.)

However, if .py scripts are associated with the py launcher, the STARTUPINFO 
that has the required information is in the launcher process instead of the 
python.exe process. Since to some extent the launcher is acting as a proxy for 
python.exe, I think it should initialize Python's STARTUPINFO from a copy of 
its own values as returned by GetStartupInfo. This solves the dwFlags and 
lpTitle problem.

Additionally, the C runtime spawn family of functions supports inheriting file 
descriptors by passing them in the lpReserved2 field of STARTUPINFO. Currently 
this doesn't work when spawning Python via the launcher, since it calls 
CreateProcess instead of a spawn function. By inheriting the launcher's 
STARTUPINFO, this feature is restored as well. 

For example, in `run_child` in PC/launcher.c, I replaced `si.cb = sizeof(si)` 
with GetStartupInfoW(&si). Then I tested passing inherited file descriptors 
through the launcher as follows:

grandparent:

    >>> import os
    >>> os.pipe()
    (3, 4)
    >>> os.set_inheritable(4, True)
    >>> os.spawnl(os.P_WAIT, 'amd64/py_d.exe', 'py_d')

grandchild (py_d.exe => python.exe):

    Python 3.7.0a4 (v3.7.0a4:07c9d85, Jan  9 2018, 07:07:02)
    [MSC v.1900 64 bit (AMD64)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import os
    >>> os.write(4, b'spam')
    4
    >>> ^Z

grandparent:

    0
    >>> os.read(3, 4)
    b'spam'

----------
components: Windows
keywords: easy (C)
messages: 310016
nosy: eryksun, paul.moore, steve.dower, tim.golden, zach.ware
priority: normal
severity: normal
stage: needs patch
status: open
title: inherit the py launcher's STARTUPINFO
type: enhancement
versions: Python 3.8

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

Reply via email to