@@ -129,8 +129,8 @@ import Data.List(stripPrefix, isSuffixOf, uncons, dropWhileEnd)
129129#define STRING String
130130#define FILEPATH FilePath
131131#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 )
134134import Control.DeepSeq (force )
135135import GHC.IO (unsafePerformIO )
136136import qualified Data.Char as C
@@ -1270,15 +1270,31 @@ snoc :: String -> Char -> String
12701270snoc str = \ c -> str <> [c]
12711271
12721272#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
12731289#ifdef WINDOWS
12741290fromString :: P. String -> STRING
12751291fromString 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
12771293 evaluate $ force $ first displayException r
12781294#else
12791295fromString :: P. String -> STRING
12801296fromString 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
12821298 evaluate $ force $ first displayException r
12831299#endif
12841300
0 commit comments