[issue11432] webbrowser.open on unix fails.

Charles-Francois Natali report at bugs.python.org
Tue Mar 8 11:11:33 CET 2011


Charles-Francois Natali <neologix at free.fr> added the comment:

The problem lies here:

/* Close pipe fds. Make sure we don't close the same fd more than */
/* once, or standard fds. */
if (p2cread > 2) {
    POSIX_CALL(close(p2cread));
}
(c2pwrite > 2) {
    POSIX_CALL(close(c2pwrite));
}
if (errwrite != c2pwrite && errwrite > 2) {
    POSIX_CALL(close(errwrite));
}

If p2cread == c2pwrite (which is the case here since /dev/null is passed as stdin and stderr), we end up closing the same FD twice, hence the EBADF.
Just passing 
-(c2pwrite > 2) {
+(c2pwrite > 2 && c2pwrite != p2cread) {
    POSIX_CALL(close(c2pwrite));
}

Solves this (but you probably also want to check for (errwrite != p2cread) when closing c2pwrite).

Note that the Python implementation uses a set to avoid closing the same FD twice:

# Close pipe fds. Make sure we don't close the
# same fd more than once, or standard fds.
closed = set()
for fd in [p2cread, c2pwrite, errwrite]:
    if fd > 2 and fd not in closed:
        os.close(fd)
        closed.add(fd)

It might be cleaner to use a fd_set, i.e.:
fd_set set;
FD_ZERO(&set);
FD_SET(0, &set);
FD_SET(1, &set);
FD_SET(2, &set);
if (!FD_ISSET(p2cread, &set)) {
    POSIX_CALL(close(p2cread));
    FD_SET(p2cread, &fd);
}
if (!FD_ISSET(c2pwrite, &set)) {
    POSIX_CALL(close(c2pwrite));
    FD_SET(c2pwrite, &fd);
}
if (!FD_ISSET(errwrite, &set)) {
    POSIX_CALL(close(errwrite));
    FD_SET(errwrite, &fd);
}

But maybe it's just too much (and also, fd_set can be defined in different header files, and while I'm sure it's async-safe on Linux, I don't know if it's required as part of a standard...).

----------
nosy: +neologix

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue11432>
_______________________________________


More information about the Python-bugs-list mailing list