Skip to content

setLocaleEncoding does not have any effect after putStrLn

Summary

setLocaleEncoding does not seem to have any effect after any output has been written, even while getLocaleEncoding reports that the encoding has been successfully updated.

Steps to reproduce

module Main (main) where

import GHC.IO.Encoding

main :: IO ()
main = do
  setLocaleEncoding latin1
  putStrLn "foo"
  setLocaleEncoding utf8
  e <- getLocaleEncoding
  print e
  putStrLn "\x1234"

This outputs:

foo
UTF-8
bench: <stdout>: commitBuffer: invalid argument (cannot encode character '\4660')

Expected behavior

I'd expect the program to print

foo
UTF-8

If I comment out putStrLn "foo" line then everything works as expected.

I assume that it might be challenging to change encoding of an already open stdout handle, but in such case getLocaleEncoding should not lie that the encoding has been switched to UTF-8.

Environment

  • GHC version used: 9.6.1
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information