REQ: add a non-closing version of handleToFd
Simon Marlow
marlowsd at gmail.com
Thu Oct 6 14:58:23 CEST 2011
On 01/10/2011 12:31, Volker Wysk wrote:
> Hello
>
> 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.
There's another danger. A Handle has an associated finalizer that
closes the file descriptor when the Handle is no longer needed by the
program, so you can't make a handleToFd_noClose because the Handle might
be closed anyway. What you can do is make a withHandleFD:
withHandleFD :: Handle -> (FD -> IO a) -> IO a
it's still quite dodgy, depending on what you do with the FD. Perhaps
it should be called unsafeWithHandleFD.
Anyway, patches gratefully accepted...
Cheers,
Simon
More information about the Glasgow-haskell-users
mailing list