use PYTHONUNBUFFERED to force python to use sane stdout buffering

Works around https://github.com/warner/magic-wormhole/issues/108

See http://stackoverflow.com/questions/107705/disable-output-buffering
for the gory details. Why a scripting language would chose a default
stdout buffering that differs between terminal and piped output, and
tends to introduce this kind of bug, I don't know.
This commit is contained in:
Joey Hess 2016-12-17 17:28:08 -04:00
parent fe6f36d9f3
commit 399d0f1929
No known key found for this signature in database
GPG key ID: C910D9222512E3C7

View file

@ -12,6 +12,7 @@ import Utility.SafeCommand
import Utility.Monad
import Utility.Misc
import Utility.FileSystemEncoding
import Utility.Env
import System.IO
import System.Exit
@ -58,13 +59,14 @@ sendCode = putMVar
--
-- A request to make the code available in machine-parsable form is here:
-- https://github.com/warner/magic-wormhole/issues/104
--
-- XXX This currently fails due to
-- https://github.com/warner/magic-wormhole/issues/108
sendFile :: FilePath -> CodeObserver -> WormHoleParams -> IO Bool
sendFile f o ps = runWormHoleProcess p $ \_hin hout -> do
fileEncoding hout
findcode =<< words <$> hGetContents hout
sendFile f o ps = do
-- Work around stupid stdout buffering behavior of python.
-- See https://github.com/warner/magic-wormhole/issues/108
environ <- addEntry "PYTHONUNBUFFERED" "1" <$> getEnvironment
runWormHoleProcess p { env = Just environ} $ \_hin hout -> do
fileEncoding hout
findcode =<< words <$> hGetContents hout
where
p = wormHoleProcess (Param "send" : ps ++ [File f])
findcode [] = return False