subprocess equivalent for "os.execvp()"?

Eryk Sun eryksun at gmail.com
Mon Jan 9 05:32:16 EST 2023


On 1/9/23, c.buhtz at posteo.jp <c.buhtz at 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.


More information about the Python-list mailing list