argv[0] manipulation

Erik Max Francis max at alcyone.com
Sun Dec 15 19:44:03 EST 2002


Bernhard Herzog wrote:

> Not exactly. It says for the case where Python has been started with a
> script filename as argument:
> 
>     argv[0] is the script name (it is operating system dependent
>     whether
>     this is a full pathname or not).

It would be useful to include the description in its enterity:

	argv[0] is the script name (it is operating system
	dependent whether this is a full pathname or not). If the
	command was executed using the -c command line option
	to the interpreter, argv[0] is set to the string '-c'. If no 
	script name was passed to the Python interpreter, argv has
	zero length.

The sentences you didn't include make it clear that there may not be a
filename at all in argv[0]; in fact, it's conceivable there might not
even be an argv[0] element depending on how the interpreter was invoked.

> It's not clear to me that this means that it's not useful for
> anything.
> It only says that it's OS dependend whether it's a "full pathname".
> What's a "full pathname"? An "absolute pathname"?

It doesn't even guarantee that it's a _relative_ pathname, it could
simply be the base name file that's being ineterpreter without any path
information, relative or otherwise.  You simply shouldn't rely on any
behavior here in a portable program.

Your own analysis demonstrates the problem:  You were clearly talking
about UNIX-specific systems only.  It may well be that Python behaves
the way you described in most, or even all, UNIX systems, simply because
that's by all accounts the easiest way to proceed.  But what the
documentation is telling you is that you shouldn't _rely_ on this
behavior.  Do you really think it works the same on Windows (and in
different versions, no less), or pre-OS X MacOS?  On the contrary, it
most _certainly_ won't behave the same way.

Your own analysis demonstrates that you are not thinking portably.

> The operating system *has* to pass a usable absoulte or relative
> filename to the Python interpreter. Whether Python puts that filename
> or
> a modified version of it into sys.argv[0] is up to Python and
> independend of the OS Python is running on, so python could make some
> guarantees about it.

But it does not.  So you should not rely upon any.  Relying upon
behavior which is not only not guaranteed but explicitly disclaimed away
is asking for portability problems.

> Furthermore, Python itself uses the original filename it was given to
> put the directory containing the script into sys.path and the
> documentation doesn't qualify that with "operating system dependend":
> 
>    The first item of this list, path[0], is the directory containing
>    the
>    script that was used to invoke the Python interpreter.

Again, you're short-quoting:

	The first item of this list, path[0], is the directory
	containing the script that was used to invoke the Python
	interpreter. If the script directory is not available (e.g. if
	the interpreter is invoked interactively or if the script is
	read from standard input), path[0] is the empty string, which
	directs Python to search modules in the current directory
	first.

It explicitly includes the case where the script directory is not
available.  It gives examples of when this may happen, but does not
claim to list them exhaustively.  You are, again, relying upon behavior
which is not guaranteed strongly enough.

-- 
 Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
 __ San Jose, CA, USA / 37 20 N 121 53 W / &tSftDotIotE
/  \ There are no dull subjects.  There are only dull writers.
\__/ H.L. Mencken
    Official Omega page / http://www.alcyone.com/max/projects/omega/
 The official distribution page for the popular Roguelike, Omega.



More information about the Python-list mailing list