[Python-Dev] Weird error handling in os._execvpe

Guido van Rossum guido@python.org
Thu, 01 Aug 2002 13:23:03 -0400


> While testing my tempfile.py rewrite I ran into this mess in os.py:
> 
> def _execvpe(file, args, env=None):
>     # ...
>     if not _notfound:
>         if sys.platform[:4] == 'beos':
>             #  Process handling (fork, wait) under BeOS (up to 5.0)
>             #  doesn't interoperate reliably with the thread interlocking
>             #  that happens during an import.  The actual error we need
>             #  is the same on BeOS for posix.open() et al., ENOENT.
>             try: unlink('/_#.# ## #.#')
>             except error, _notfound: pass
>         else:
>             import tempfile
>             t = tempfile.mktemp()
>             # Exec a file that is guaranteed not to exist
>             try: execv(t, ('blah',))
>             except error, _notfound: pass
>     exc, arg = error, _notfound
>     for dir in PATH:
>         fullname = path.join(dir, file)
>         try:
>             apply(func, (fullname,) + argrest)
>         except error, (errno, msg):
>             if errno != arg[0]:
>                 exc, arg = error, (errno, msg)
>     raise exc, arg
> 
> This appears to be an overcomplicated, unreliable way of writing
> 
> import errno
> 
> def _execvpe(file, args, env=None):
>     # ...
>     for dir in PATH:
> 	fullname = path.join(dir, file)
> 	try:
> 	    apply(func, (fullname,) + argrest)
> 	except error, (err, msg):
> 	    if err != errno.ENOENT: # and err != errno.ENOTDIR, maybe
> 	       raise
>     raise error, (err, msg)
> 
> Can anyone explain why it is done this way?

Because not all systems report the same error for this error condition
(attempting to execute a file that doesn't exist).

--Guido van Rossum (home page: http://www.python.org/~guido/)