REQ: add a non-closing version of handleToFd

Volker Wysk pf3 at
Sat Oct 1 13:31:13 CEST 2011


The function to extract a file descriptor from a handle, handleToFd, closes 
the handle as a side effect. This appears to be necessary in the general 
case, otherwise the implementers wouldn't have made it this way.

But there are cases in which it isn't necessary to close the handle.

For instance, this little function needs to extract the file descriptor, but 
there is no need to close, or change, the handle.

isatty :: Handle        -- ^ Handle to check
       -> IO Bool       -- ^ Whether the handle is connected to a terminal
isatty h = do
   fd <- handleToFd_noclose h
   isterm <- {# call isatty as hssh_c_isatty #}    -- this is c2hs
                   ((fromIntegral fd) :: CInt)
   return (isterm /= (0::CInt))

handleToFd_noclose :: Handle -> IO Fd
-- to be implemented

The isatty above can't be implemented with things the way they are. To me, 
this is a serious drawback.

The cases, in which the handle needs to be closed, should be identified, and 
documented. Then, a version of handleToFd, which doesn't close the handle can 
be implemented.


More information about the Glasgow-haskell-users mailing list