[Haskell-cafe] hopefully, a very quick question about ByteString lib

[1 of 1] Compiling Main ( sha1.lhs, interpreted ) import qualified Data.ByteString as B main = B.getLine >>= B.putStrLn Ok, modules loaded: Main. *Main> :main *** Exception: no buffering And just to make sure it's not something to do with GHCi or anything like that main = getLine >>= putStrLn [1 of 1] Compiling Main ( sha1.lhs, interpreted ) Ok, modules loaded: Main. *Main> :main hello hello Why am I getting an error when using the ByteString library? Iain

Iain,
If you wanted to make sure it didn't have to do with ghci then you
should have compiled the original code, not tested different
functions. The code you provided works fine compiled and I too am
curious what is going on when you run it in ghci.
Thomas
On Tue, Oct 27, 2009 at 8:26 AM, Iain Barnett
[1 of 1] Compiling Main ( sha1.lhs, interpreted ) import qualified Data.ByteString as B
main = B.getLine >>= B.putStrLn
Ok, modules loaded: Main. *Main> :main *** Exception: no buffering
And just to make sure it's not something to do with GHCi or anything like that
main = getLine >>= putStrLn
[1 of 1] Compiling Main ( sha1.lhs, interpreted ) Ok, modules loaded: Main. *Main> :main hello hello
Why am I getting an error when using the ByteString library?
Iain _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Am Dienstag 27 Oktober 2009 16:47:12 schrieb Thomas DuBuisson:
Iain, If you wanted to make sure it didn't have to do with ghci then you should have compiled the original code, not tested different functions. The code you provided works fine compiled and I too am curious what is going on when you run it in ghci.
Thomas
Look at the sources: hGetLine :: Handle -> IO ByteString #if !defined(__GLASGOW_HASKELL__) hGetLine h = System.IO.hGetLine h >>= return . pack . P.map c2w #else hGetLine h = wantReadableHandle "Data.ByteString.hGetLine" h $ \ handle_ -> do case haBufferMode handle_ of NoBuffering -> error "no buffering" _other -> hGetLineBuffered handle_ where ... So, in ghci, stdin isn't buffered and ByteString can't cope with that. Why exactly, I don't know. Change code to main = hSetBuffering stdin LineBuffering >> B.getLine >>= B.putStrLn and it works.

On Tue, 2009-10-27 at 17:00 +0100, Daniel Fischer wrote:
Look at the sources:
hGetLine :: Handle -> IO ByteString hGetLine h = wantReadableHandle "Data.ByteString.hGetLine" h $ \ handle_ -> do case haBufferMode handle_ of NoBuffering -> error "no buffering" _other -> hGetLineBuffered handle_
where ...
So, in ghci, stdin isn't buffered and ByteString can't cope with that. Why exactly, I don't know.
By comparison if we look at System.IO.hGetLine we see: hGetLine :: Handle -> IO String hGetLine h = do m <- wantReadableHandle "hGetLine" h $ \ handle_ -> do case haBufferMode handle_ of NoBuffering -> return Nothing LineBuffering -> do l <- hGetLineBuffered handle_ return (Just l) BlockBuffering _ -> do l <- hGetLineBuffered handle_ return (Just l) case m of Nothing -> hGetLineUnBuffered h Just l -> return l So there is something special about an unbuffered hGetLine. I can't remember exactly but when we implemented hGetLine for ByteString I guess we didn't understand / couldn't be bothered with the weird unbuffered case. The System.IO code in ghc-6.10 also notes: -- ToDo: the unbuffered case is wrong: it doesn't lock the handle for -- the duration. In ghc-6.12 I think the unbuffered mode for input was simply abolished so there is no longer a hGetLineUnBuffered case. Duncan

duncan.coutts:
On Tue, 2009-10-27 at 17:00 +0100, Daniel Fischer wrote:
Look at the sources:
hGetLine :: Handle -> IO ByteString hGetLine h = wantReadableHandle "Data.ByteString.hGetLine" h $ \ handle_ -> do case haBufferMode handle_ of NoBuffering -> error "no buffering" _other -> hGetLineBuffered handle_
where ...
So, in ghci, stdin isn't buffered and ByteString can't cope with that. Why exactly, I don't know.
By comparison if we look at System.IO.hGetLine we see:
hGetLine :: Handle -> IO String hGetLine h = do m <- wantReadableHandle "hGetLine" h $ \ handle_ -> do case haBufferMode handle_ of NoBuffering -> return Nothing LineBuffering -> do l <- hGetLineBuffered handle_ return (Just l) BlockBuffering _ -> do l <- hGetLineBuffered handle_ return (Just l) case m of Nothing -> hGetLineUnBuffered h Just l -> return l
So there is something special about an unbuffered hGetLine. I can't remember exactly but when we implemented hGetLine for ByteString I guess we didn't understand / couldn't be bothered with the weird unbuffered case.
The System.IO code in ghc-6.10 also notes:
-- ToDo: the unbuffered case is wrong: it doesn't lock the handle for -- the duration.
In ghc-6.12 I think the unbuffered mode for input was simply abolished so there is no longer a hGetLineUnBuffered case.
Pretty sure that stuff has been added since we wrote the bytestring code. -- Don

On 27 Oct 2009, at 15:47, Thomas DuBuisson wrote:
If you wanted to make sure it didn't have to do with ghci then you should have compiled the original code, not tested different functions.
That is a very good point! On 27 Oct 2009, at 16:00, Daniel Fischer wrote:
Look at the sources:
I'd still have needed you to look at the sources for me, I'm just learning.
Change code to
main = hSetBuffering stdin LineBuffering >> B.getLine >>= B.putStrLn
and it works.
Thankyou very much, I appreciate it. Iain
participants (5)
-
Daniel Fischer
-
Don Stewart
-
Duncan Coutts
-
Iain Barnett
-
Thomas DuBuisson