@@ -129,8 +129,8 @@ import Data.List(stripPrefix, isSuffixOf, uncons, dropWhileEnd)
129
129
#define STRING String
130
130
#define FILEPATH FilePath
131
131
#else
132
- import Prelude (fromIntegral )
133
- import Control.Exception ( SomeException , evaluate , try , displayException )
132
+ import Prelude (fromIntegral , return , IO , Either ( .. ) )
133
+ import Control.Exception ( catch , displayException , evaluate , fromException , toException , throwIO , Exception , SomeAsyncException ( .. ), SomeException )
134
134
import Control.DeepSeq (force )
135
135
import GHC.IO (unsafePerformIO )
136
136
import qualified Data.Char as C
@@ -1270,15 +1270,31 @@ snoc :: String -> Char -> String
1270
1270
snoc str = \ c -> str <> [c]
1271
1271
1272
1272
#else
1273
+ -- | Like 'try', but rethrows async exceptions.
1274
+ trySafe :: Exception e => IO a -> IO (Either e a )
1275
+ trySafe ioA = catch action eHandler
1276
+ where
1277
+ action = do
1278
+ v <- ioA
1279
+ return (Right v)
1280
+ eHandler e
1281
+ | isAsyncException e = throwIO e
1282
+ | otherwise = return (Left e)
1283
+
1284
+ isAsyncException :: Exception e => e -> Bool
1285
+ isAsyncException e =
1286
+ case fromException (toException e) of
1287
+ Just (SomeAsyncException _) -> True
1288
+ Nothing -> False
1273
1289
#ifdef WINDOWS
1274
1290
fromString :: P. String -> STRING
1275
1291
fromString str = P. either (P. error . P. show ) P. id $ unsafePerformIO $ do
1276
- r <- try @ SomeException $ GHC. withCStringLen (mkUTF16le ErrorOnCodingFailure ) str $ \ cstr -> packCStringLen cstr
1292
+ r <- trySafe @ SomeException $ GHC. withCStringLen (mkUTF16le ErrorOnCodingFailure ) str $ \ cstr -> packCStringLen cstr
1277
1293
evaluate $ force $ first displayException r
1278
1294
#else
1279
1295
fromString :: P. String -> STRING
1280
1296
fromString str = P. either (P. error . P. show ) P. id $ unsafePerformIO $ do
1281
- r <- try @ SomeException $ GHC. withCStringLen (mkUTF8 ErrorOnCodingFailure ) str $ \ cstr -> packCStringLen cstr
1297
+ r <- trySafe @ SomeException $ GHC. withCStringLen (mkUTF8 ErrorOnCodingFailure ) str $ \ cstr -> packCStringLen cstr
1282
1298
evaluate $ force $ first displayException r
1283
1299
#endif
1284
1300
0 commit comments