473,406 Members | 2,705 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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

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?

- nas

Feb 20 '06 #1
8 3720
Atanas Banov:
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.


Not here.

Python 2.4.2 (#67, Sep 28 2005, 12:41:11) [MSC v.1310 32 bit (Intel)]
Type "help", "copyright", "credits" or "license" for more information.
import os
print os.popen('exit 1').close()

1

--
René Pijlman
Feb 20 '06 #2

my gripe is about exit with MINUS ONE, not +1. see my post again.

yes, i know one cannot return -1 in unix (since returned value is
exitcode % 256 * 256), and no, i am not interested in unix behavior.

Rene Pijlman wrote:
Atanas Banov:
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.


Not here.

Python 2.4.2 (#67, Sep 28 2005, 12:41:11) [MSC v.1310 32 bit (Intel)]
Type "help", "copyright", "credits" or "license" for more information.
import os
print os.popen('exit 1').close()

1


Feb 20 '06 #3
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;
}
Feb 20 '06 #4

"Atanas Banov" <en****@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...

my gripe is about exit with MINUS ONE, not +1. see my post again.


FWIW, I reproduced the error message for -1 (and the no error for -2, -3)
in the Idle Python Shell uder Win XP home.

tjr

Feb 20 '06 #5
Jeffrey Schwab wrote:
_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.
yes, that's what i thought the root of the problem is.
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.
.... /* 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.
*/


the piece you quoted is from the unix #ifdef part, i think. there is
another version of the pypclose for windows below that.

in any event i think such behaviour is a bug - just because in unix
exit codes are limited to 0..255 (and returned multiplied by 256)
doesnt mean other OSes should suffer because of design flow in
_PyPclose, right?

throwing an IOError "no error" doesnt help.

is there a bug database for python where i can check if this was
discussed?

Feb 21 '06 #6
Atanas Banov wrote:
Jeffrey Schwab wrote:
_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.

yes, that's what i thought the root of the problem is.

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.


....
/* 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.
*/

the piece you quoted is from the unix #ifdef part, i think. there is
another version of the pypclose for windows below that.

in any event i think such behaviour is a bug - just because in unix
exit codes are limited to 0..255 (and returned multiplied by 256)
doesnt mean other OSes should suffer because of design flow in
_PyPclose, right?

throwing an IOError "no error" doesnt help.

is there a bug database for python where i can check if this was
discussed?


Yes, there's a bug database linked from python.org; search the main page
for "Bugs".

Here's the most (seemingly) relevant bug report I can find:
http://sourceforge.net/tracker/index...70&atid=105470
Feb 22 '06 #7
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 ***
Mar 2 '06 #8
Tobiah wrote:
phase:toby:~> echo 'exit -1' | bash
phase:toby:~> echo $?
255


http://www.linuxtopia.org/online_boo...exitcodes.html
Exit Code Number: 255 [1]
Meaning: Exit status out of range
Example: exit -1
Comments: exit takes only integer args in the range 0 - 255
[1] Out of range exit values can result in unexpected exit codes. An
exit value greater than 255 returns an exit code modulo 256. For
example, exit 3809 gives an exit code of 225 (3809 % 256 = 225).
Mar 3 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: zapazap | last post by:
Dear Snake Charming Gurus, (Was: http://mail.python.org/pipermail/python-list/2004-January/204454.html) First, a thank you to Tim Golden, Thomas Heller, and Mark Hammond for your earlier help...
2
by: Tim Black | last post by:
In my recent experience, popen os pipes always fail when cwd is a UNC path. Can anyone shed any light on this? Although I've seen lots of UNC path-related problems in this newsgroup, I've not been...
0
by: Mark Sandler | last post by:
Hi, When i create a process using popen and then if i decide that i am not interested in keeping the process pipes anymore, and I forget the correspond variables . Then, if python tries to destroy...
24
by: David Mathog | last post by:
If this: int i,sum; int *array; for(sum=0, i=0; i<len; i++){ sum += array; } is converted to this (never mind why for the moment):
1
by: rveloso | last post by:
Hi all, i'm having a really nasty problem with popen. I have the following code : --------------------- .... FILE *PD; .... sprintf(fname,"/usr/bin/gzip -dc %s/%s",dirname,dp->d_name); .......
3
by: Greg Strong | last post by:
Hello All, Is there any way to close an ODBC connection via DSN without completely closing the Access front-end? I'm doing some testing with using Access as a front-end to Oracle 10g Express...
3
by: Doru Moisa | last post by:
Hello, How can I capture the output of a long runnning process which I open with popen() ? I tried reading line by line, char by char, but the result always comes when the process finishes. (I...
19
by: Angus | last post by:
I have a socket class CTestClientSocket which I am using to simulate load testing. I create multiple instances of the client like this: for (int i = 0; i < 5; i++) { CTestClientSocket* pTemp...
2
by: revenant81 | last post by:
I'm writing a program which has to execute a command, get its output and show it on a treeview. This command runs for a very long time. I want to end the execution of the command when the user...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.