[Haskell] ANNOUNCE: silently-0.0.1 (prevent IO actions from writing to stdout)

Bas van Dijk v.dijk.bas at gmail.com
Mon Mar 7 09:18:55 CET 2011


On 7 March 2011 02:28, Trystan Spangler <trystan.s at comcast.net> wrote:
> Announcing Silently, a package with two simple functions to run an IO action
> while preventing it from writing to stdout (or the given handle)

Useful package!

I tiny remark about the code:

-- | Run an IO action while ignoring all output to stdout.
silently :: IO a -> IO a
silently = hSilently stdout

-- | Run an IO action while ignoring all output to the given handle.
hSilently :: Handle -> IO a -> IO a
hSilently handle action = do
  oldHandle <- hDuplicate handle
  (tmpFile, tmpHandle) <- openTempFile "." "silently"
  hDuplicateTo tmpHandle handle
  finally action (hDuplicateTo oldHandle handle >> hClose tmpHandle >>
removeFile tmpFile)

Think about what happens if an asynchronous exception is thrown to the
thread executing hSilently just after opening the temp file. It will
abort the computation which causes the temp file to be left on your
system and keep the tmpHandle open.

You need to use bracket to ensure the temp file we always be closed and deleted.

I send you a pull-request.

Regards,

Bas



More information about the Haskell mailing list