Skip to content

Commit

Permalink
Bracket changes to locale encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
Bodigrim committed Dec 22, 2024
1 parent c8d6743 commit e13e23a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,13 @@ look for another way to speed up generation of Fibonacci numbers.
<stdout>: commitBuffer: invalid argument (cannot encode character '\177')
```

or

```
Uncaught exception ghc-internal:GHC.Internal.IO.Exception.IOException:
<stdout>: commitBuffer: invalid argument (cannot encode character '\956')
```

it means that your locale does not support UTF-8. `tasty-bench` makes an effort
to force locale to UTF-8, but sometimes, when benchmarks are a part of
a larger application, it's [impossible](https://gitlab.haskell.org/ghc/ghc/-/issues/23606)
Expand Down
23 changes: 19 additions & 4 deletions src/Test/Tasty/Bench.hs
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,11 @@ another way to speed up generation of Fibonacci numbers.
> <stdout>: commitBuffer: invalid argument (cannot encode character '\177')
or
> Uncaught exception ghc-internal:GHC.Internal.IO.Exception.IOException:
> <stdout>: commitBuffer: invalid argument (cannot encode character '\956')
it means that your locale does not support UTF-8. @tasty-bench@
makes an effort to force locale to UTF-8, but sometimes, when
benchmarks are a part of a larger application, it’s
Expand Down Expand Up @@ -706,7 +711,7 @@ import qualified Prelude
import Control.Applicative
import Control.Arrow (first, second)
import Control.DeepSeq (NFData, force, rnf)
import Control.Exception (bracket, evaluate)
import Control.Exception (bracket, bracket_, evaluate)
import Control.Monad (void, unless, guard, (>=>), when)
import Data.Foldable (foldMap, traverse_)
import Data.Int (Int64)
Expand Down Expand Up @@ -1307,12 +1312,22 @@ type Benchmark = TestTree
defaultMain :: [Benchmark] -> IO ()
defaultMain bs = do
let act = defaultMain' bs
setLocaleEncoding utf8
bracketUtf8 act

bracketUtf8 :: IO a -> IO a
bracketUtf8 act = do
prevLocaleEnc <- getLocaleEncoding
#if defined(mingw32_HOST_OS)
codePage <- getConsoleOutputCP
bracket (setConsoleOutputCP 65001) (const $ setConsoleOutputCP codePage) (const act)
bracket_
(setLocaleEncoding utf8 >> setConsoleOutputCP 65001)
(setLocaleEncoding prevLocaleEnc >> setConsoleOutputCP codePage)
act
#else
act
bracket_
(setLocaleEncoding utf8)
(setLocaleEncoding prevLocaleEnc)
act
#endif

defaultMain' :: [Benchmark] -> IO ()
Expand Down

0 comments on commit e13e23a

Please sign in to comment.