why does close() fail miserably on popen with exit code -1 ?!

Tobiah toby at rcsreg.com
Thu Mar 2 15:29:18 EST 2006


Still:

phase:toby:~> echo 'exit -1' | bash
phase:toby:~> echo $?
255
phase:toby:~> 


Jeffrey Schwab wrote:
> Atanas Banov wrote:
> 
>> i ran onto this weirdness today: seems like close() on popen-ed
>> (pseudo)file fails miserably with exception instead of returning exit
>> code, when said exit code is -1.
>>
>> here is the simplest example (under Windows):
>>
>>
>>>>> print popen('exit 1').close()
>>
>>
>> 1
>>
>>>>> print popen('exit -1').close()
>>
>>
>> Traceback (most recent call last):
>>   File "<interactive input>", line 1, in ?
>> IOError: (0, 'Error')
>>
>>>>> print popen('exit -2').close()
>>
>>
>> -2
>>
>> has anyone have idea why is that?
> 
> 
> _PyPclose returns the exit status of the popened process (the popenee?), 
> or -1 on error.  Of course, if the status is supposed to be -1, there's 
> some confusion.
> 
> In the snippet of code below (from Modules/posixmodule.c), result has 
> been initialized to the output of fclose, which in your case is 0.  The 
> comment is particularly handy.
> 
>     if (result != EOF &&
>         waitpid(pipe_pid, &exit_code, 0) == pipe_pid)
>     {
>         /* extract exit status */
>         if (WIFEXITED(exit_code))
>         {
>             result = WEXITSTATUS(exit_code);
>         }
>         else
>         {
>             errno = EPIPE;
>             result = -1;
>         }
>     }
>     else
>     {
>         /* Indicate failure - this will cause the file object
>          * to raise an I/O error and translate the last
>          * error code from errno.  We do have a problem with
>          * last errors that overlap the normal errno table,
>          * but that's a consistent problem with the file object.
>          */
>         result = -1;
>     }
*** Free account sponsored by SecureIX.com ***
*** Encrypt your Internet usage with a free VPN account from http://www.SecureIX.com ***



More information about the Python-list mailing list