Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Why must path be in args in os.execv?

58 views
Skip to first unread message

Holger Jannsen

unread,
Jun 30, 1999, 3:00:00 AM6/30/99
to
High,

there's again something I don't understand. Hope you do.;-)

I'd like to start a windows-executable with some arguments.
Ok. 'os.execv' or anything like that is the thing.

But if I don't repeat the path with the arguments, the execution fails.
Better: Something happens, but not the correct execution.

For testing I constructed an easy batch named test.bat with only one command:
echo %0 %1 %2 %3 %4 %5 >result.log

Python 1.5.2 (#0, Apr 13 1999, 10:51:12) [MSC 32 bit (Intel)] on win32
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> import os
>>> os.chdir("c:\\mypython")
>>> os.execv("test.bat",["test.bat","1","2","3","4"])

When I start with:
>>> os.execv("test.bat",["1","2","3","4"])
only the interpreter ends. No Log-File has been written...

I really spent a long time since I found out. Perhaps the documentation
have to be changed?
>>>
execv (path, args)
Execute the executable path with argument list args, replacing the current process (i.e.,
the Python interpreter). The argument list may be a tuple or list of strings.
Availability: Unix, Windows.
>>>

Ciao,
Holger

Holger Jannsen

unread,
Jun 30, 1999, 3:00:00 AM6/30/99
to
From: Holger Jannsen <hol...@phoenix-edv.netzservice.de>

Aahz Maruch

unread,
Jul 1, 1999, 3:00:00 AM7/1/99
to
In article <377A2590...@phoenix-edv.netzservice.de>,

Holger Jannsen <hol...@phoenix-edv.netzservice.de> wrote:
>
>there's again something I don't understand. Hope you do.;-)
>
>I'd like to start a windows-executable with some arguments.
>Ok. 'os.execv' or anything like that is the thing.
>
>But if I don't repeat the path with the arguments, the execution fails.
>Better: Something happens, but not the correct execution.

If you look in a standard C-library docs, you'll see the same thing.
--
--- Aahz (@netcom.com)

Hugs and backrubs -- I break Rule 6 <*> http://www.rahul.net/aahz/
Androgynous poly kinky vanilla queer het

Michael P. Reilly

unread,
Jul 1, 1999, 3:00:00 AM7/1/99
to
Holger Jannsen <hol...@phoenix-edv.netzservice.de> wrote:
: High,

: there's again something I don't understand. Hope you do.;-)

: I'd like to start a windows-executable with some arguments.
: Ok. 'os.execv' or anything like that is the thing.

: But if I don't repeat the path with the arguments, the execution fails.
: Better: Something happens, but not the correct execution.

: For testing I constructed an easy batch named test.bat with only one command:


: echo %0 %1 %2 %3 %4 %5 >result.log

: Python 1.5.2 (#0, Apr 13 1999, 10:51:12) [MSC 32 bit (Intel)] on win32
: Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
:>>> import os
:>>> os.chdir("c:\\mypython")
:>>> os.execv("test.bat",["test.bat","1","2","3","4"])

: When I start with:
:>>> os.execv("test.bat",["1","2","3","4"])
: only the interpreter ends. No Log-File has been written...

: I really spent a long time since I found out. Perhaps the documentation
: have to be changed?
:>>>
: execv (path, args)
: Execute the executable path with argument list args, replacing the current process (i.e.,
: the Python interpreter). The argument list may be a tuple or list of strings.
: Availability: Unix, Windows.

:>>>

os.execv is a construct originating on UNIX, where the program name
does not always reflect the program file's name (with reason). It is
common to create an executable that can react differently when called
with a different name. As a simple example, gzip, gunzip and gzcat are
all the same physical file on a UNIX box, but called with those names,
it will compress, uncompress and uncompress-and-stream-output,
respectively.

Including the program name in the execv call allows this from a calling
process.

But in most cases, you can just use the program file:
def myexec(program, *args):
if len(args) == 1 and type(args[0]) == type(args):
args = args[0]
os.execv(program, (program,) + args)

This places the program filename in the arg0 position (see section 6.1.5
"Process Management" in the Python Library Reference.

-Arcege


Jeff Epler

unread,
Jul 1, 1999, 3:00:00 AM7/1/99
to
Holger,

On Unix, read the manpage for execve and friends. You will see that the
first argument is the path to the executable, and the subsequent arguments
describe the arguments (including argv[0]) and optionally the environment.
By convention, argv[0] is the basename of the executable when the
executable is on the PATH, and the full path otherwise. (Another unix
convention is that when the first letter of argv[0] is "-", the shell is a
login shell)

DOS doesn't really have execve. It's possible that the imitation is less
perfect than it could be. Instead, it may do the equivalent of entering
the string
string.join(argv, " ")
in the shell, i.e., ignoring the path argument.

This looks like a shortcoming in the DOS (windows 9x, NT) implementation of
exec*, in a way that makes it work differently from the Unix it attempts to
imitate. This wouldn't surprise me, since I recently discovered that NT's
spawnl takes a unix-like array of arguments (const char *argv[]) but when
any element contains a space, it is split into two arguments in the child
process. So, I had to break some code that had worked fine on Unix to
generate argv, by quoting each argument (something that would *not* work on
Unix) ..

Bad enough that Windows wants to copy Unix, they have to consistently get
it subtly wrong too.

Jeff

Aahz Maruch

unread,
Jul 1, 1999, 3:00:00 AM7/1/99
to
From: aa...@netcom.com (Aahz Maruch)

>there's again something I don't understand. Hope you do.;-)
>
>I'd like to start a windows-executable with some arguments.
>Ok. 'os.execv' or anything like that is the thing.
>
>But if I don't repeat the path with the arguments, the execution fails.
>Better: Something happens, but not the correct execution.

If you look in a standard C-library docs, you'll see the same thing.

Michael P. Reilly

unread,
Jul 1, 1999, 3:00:00 AM7/1/99
to
From: "Michael P. Reilly" <arc...@shore.net>

Holger Jannsen <hol...@phoenix-edv.netzservice.de> wrote:
: High,

: there's again something I don't understand. Hope you do.;-)

: I'd like to start a windows-executable with some arguments.
: Ok. 'os.execv' or anything like that is the thing.

: But if I don't repeat the path with the arguments, the execution fails.
: Better: Something happens, but not the correct execution.

: For testing I constructed an easy batch named test.bat with only one command:

Jeff Epler

unread,
Jul 1, 1999, 3:00:00 AM7/1/99
to
From: jep...@inetnebr.com (Jeff Epler)

Holger Jannsen

unread,
Jul 2, 1999, 3:00:00 AM7/2/99
to
Thanks, Jeff.

That's a good answer.

Ciao,
Holger

Jeff Epler schrieb:

Holger Jannsen

unread,
Jul 2, 1999, 3:00:00 AM7/2/99
to
From: Holger Jannsen <hol...@phoenix-edv.netzservice.de>
0 new messages