Skip to content

Commit 4a1d1ba

Browse files
committed
Merge branch 'issue-22'
2 parents 75f9fd4 + 42b0b12 commit 4a1d1ba

File tree

5 files changed

+34
-11
lines changed

5 files changed

+34
-11
lines changed

System/OsString/Common.hs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ module System.OsString.MODULE_NAME
143143
where
144144

145145

146-
146+
import System.OsString.Internal.Exception
147147
import System.OsString.Internal.Types (
148148
#ifdef WINDOWS
149149
WindowsString(..), WindowsChar(..)
@@ -236,15 +236,15 @@ encodeWith :: TextEncoding -- ^ text encoding (wide char)
236236
-> String
237237
-> Either EncodingException PLATFORM_STRING
238238
encodeWith enc str = unsafePerformIO $ do
239-
r <- try @SomeException $ GHC.withCStringLen enc str $ \cstr -> WindowsString <$> BS8.packCStringLen cstr
239+
r <- trySafe @SomeException $ GHC.withCStringLen enc str $ \cstr -> WindowsString <$> BS8.packCStringLen cstr
240240
evaluate $ force $ first (flip EncodingError Nothing . displayException) r
241241
#else
242242
-- | Encode a 'String' with the specified encoding.
243243
encodeWith :: TextEncoding
244244
-> String
245245
-> Either EncodingException PLATFORM_STRING
246246
encodeWith enc str = unsafePerformIO $ do
247-
r <- try @SomeException $ GHC.withCStringLen enc str $ \cstr -> PosixString <$> BSP.packCStringLen cstr
247+
r <- trySafe @SomeException $ GHC.withCStringLen enc str $ \cstr -> PosixString <$> BSP.packCStringLen cstr
248248
evaluate $ force $ first (flip EncodingError Nothing . displayException) r
249249
#endif
250250

@@ -340,7 +340,7 @@ decodeWith :: TextEncoding
340340
-> PLATFORM_STRING
341341
-> Either EncodingException String
342342
decodeWith winEnc (WindowsString ba) = unsafePerformIO $ do
343-
r <- try @SomeException $ BS8.useAsCStringLen ba $ \fp -> GHC.peekCStringLen winEnc fp
343+
r <- trySafe @SomeException $ BS8.useAsCStringLen ba $ \fp -> GHC.peekCStringLen winEnc fp
344344
evaluate $ force $ first (flip EncodingError Nothing . displayException) r
345345
#else
346346
-- | Decode a 'PosixString' with the specified encoding.
@@ -350,7 +350,7 @@ decodeWith :: TextEncoding
350350
-> PLATFORM_STRING
351351
-> Either EncodingException String
352352
decodeWith unixEnc (PosixString ba) = unsafePerformIO $ do
353-
r <- try @SomeException $ BSP.useAsCStringLen ba $ \fp -> GHC.peekCStringLen unixEnc fp
353+
r <- trySafe @SomeException $ BSP.useAsCStringLen ba $ \fp -> GHC.peekCStringLen unixEnc fp
354354
evaluate $ force $ first (flip EncodingError Nothing . displayException) r
355355
#endif
356356

System/OsString/Encoding/Internal.hs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ module System.OsString.Encoding.Internal where
1010

1111
import qualified System.OsString.Data.ByteString.Short as BS8
1212
import qualified System.OsString.Data.ByteString.Short.Word16 as BS16
13+
import System.OsString.Internal.Exception
1314

1415
import GHC.Base
1516
import GHC.Real
@@ -282,13 +283,13 @@ peekPosixString' fp = getLocaleEncoding >>= \enc -> GHC.peekCStringLen enc fp
282283
-- | Decode with the given 'TextEncoding'.
283284
decodeWithTE :: TextEncoding -> BS8.ShortByteString -> Either EncodingException String
284285
decodeWithTE enc ba = unsafePerformIO $ do
285-
r <- try @SomeException $ BS8.useAsCStringLen ba $ \fp -> GHC.peekCStringLen enc fp
286+
r <- trySafe @SomeException $ BS8.useAsCStringLen ba $ \fp -> GHC.peekCStringLen enc fp
286287
evaluate $ force $ first (flip EncodingError Nothing . displayException) r
287288

288289
-- | Encode with the given 'TextEncoding'.
289290
encodeWithTE :: TextEncoding -> String -> Either EncodingException BS8.ShortByteString
290291
encodeWithTE enc str = unsafePerformIO $ do
291-
r <- try @SomeException $ GHC.withCStringLen enc str $ \cstr -> BS8.packCStringLen cstr
292+
r <- trySafe @SomeException $ GHC.withCStringLen enc str $ \cstr -> BS8.packCStringLen cstr
292293
evaluate $ force $ first (flip EncodingError Nothing . displayException) r
293294

294295
-- -----------------------------------------------------------------------------

System/OsString/Internal/Exception.hs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module System.OsString.Internal.Exception where
2+
3+
import Control.Exception ( catch, fromException, toException, throwIO, Exception, SomeAsyncException(..) )
4+
5+
-- | Like 'try', but rethrows async exceptions.
6+
trySafe :: Exception e => IO a -> IO (Either e a)
7+
trySafe ioA = catch action eHandler
8+
where
9+
action = do
10+
v <- ioA
11+
return (Right v)
12+
eHandler e
13+
| isAsyncException e = throwIO e
14+
| otherwise = return (Left e)
15+
16+
isAsyncException :: Exception e => e -> Bool
17+
isAsyncException e =
18+
case fromException (toException e) of
19+
Just (SomeAsyncException _) -> True
20+
Nothing -> False

os-string.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ library
5050
System.OsString.Encoding.Internal
5151
System.OsString
5252
System.OsString.Internal
53+
System.OsString.Internal.Exception
5354
System.OsString.Internal.Types
5455
System.OsString.Posix
5556
System.OsString.Windows

tests/encoding/EncodingSpec.hs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import Test.QuickCheck
1414
import Data.Either ( isRight )
1515
import qualified System.OsString.Data.ByteString.Short as BS8
1616
import qualified System.OsString.Data.ByteString.Short.Word16 as BS16
17+
import System.OsString.Internal.Exception
1718
import System.OsString.Encoding.Internal
1819
import GHC.IO (unsafePerformIO)
1920
import GHC.IO.Encoding ( setFileSystemEncoding )
@@ -154,21 +155,21 @@ padEven bs
154155

155156
decodeP' :: BS8.ShortByteString -> Either String String
156157
decodeP' ba = unsafePerformIO $ do
157-
r <- try @SomeException $ decodeWithBasePosix ba
158+
r <- trySafe @SomeException $ decodeWithBasePosix ba
158159
evaluate $ force $ first displayException r
159160

160161
encodeP' :: String -> Either String BS8.ShortByteString
161162
encodeP' str = unsafePerformIO $ do
162-
r <- try @SomeException $ encodeWithBasePosix str
163+
r <- trySafe @SomeException $ encodeWithBasePosix str
163164
evaluate $ force $ first displayException r
164165

165166
decodeW' :: BS16.ShortByteString -> Either String String
166167
decodeW' ba = unsafePerformIO $ do
167-
r <- try @SomeException $ decodeWithBaseWindows ba
168+
r <- trySafe @SomeException $ decodeWithBaseWindows ba
168169
evaluate $ force $ first displayException r
169170

170171
encodeW' :: String -> Either String BS8.ShortByteString
171172
encodeW' str = unsafePerformIO $ do
172-
r <- try @SomeException $ encodeWithBaseWindows str
173+
r <- trySafe @SomeException $ encodeWithBaseWindows str
173174
evaluate $ force $ first displayException r
174175

0 commit comments

Comments
 (0)