• Re: subprocess equivalent for "os.execvp()"?

    From Chris Angelico@21:1/5 to c.buhtz@posteo.jp on Sun Jan 8 21:52:10 2023
    On Sun, 8 Jan 2023 at 21:51, <c.buhtz@posteo.jp> wrote:

    Hello,

    is there an equivalent in the subprocess module for "os.execvp()" to
    replace the current process with the new called one?

    It won't make a subprocess, so no. It's in the os module - under the
    name execvp. You found it already :)

    ChrisA

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Eryk Sun@21:1/5 to c.buhtz@posteo.jp on Sun Jan 8 10:22:33 2023
    On 1/8/23, c.buhtz@posteo.jp <c.buhtz@posteo.jp> wrote:

    is there an equivalent in the subprocess module for "os.execvp()" to
    replace the current process with the new called one?

    A note for Windows users

    Avoid using any of the `os.exec*` functions on Windows. There's no
    support for replacing a Windows process image, so the `exec*()`
    functions simply spawn a child process and terminate the current one.
    This is a mess in general because a waiting parent isn't aware of the
    spawned descendant. It's particularly bad for a console process that
    inherits the console session, since two processes will try to use
    console I/O simultaneously.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Eryk Sun@21:1/5 to c.buhtz@posteo.jp on Mon Jan 9 04:32:16 2023
    On 1/9/23, c.buhtz@posteo.jp <c.buhtz@posteo.jp> wrote:

    On Python for Windows what is the appropriate way how a process can call itself again?

    Let me give you an example [1]:
    There is a project "bitcli" having two entry points

    [project.scripts]
    bitcli = "bitcli.__main__:main"
    bitcli-root = "bitcli.__main__:run_main_as_root_via_policykit"

    The first is usual.

    But the second does call "bitcli" via "pkexec" to give it some root
    rights.

    This application is intended to be run as user or root by the user
    himself.

    def run_main_as_root_via_policykit():
    cmd = ['pkexec', '--disable-internal-agent', 'bitcli']

    # See https://github.com/python/cpython/issues/39569
    os.execvp(cmd[0], cmd)

    The nearest equivalent on Windows, if the current process doesn't have administrator access and high integrity level, would be to spawn a
    process with administrator access and elevated integrity level by
    calling ShellExecuteExW() with the "runas" operation. The original
    process should wait on the spawned process and proxy its exit status.
    It should also add the spawned process to a kill-on-close,
    silent-breakaway job, such that terminating the original process also terminates the spawned process.

    The standard library doesn't support calling ShellExecuteExW() or
    working with job objects, except via ctypes. Or use the PyWin32
    package.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)