Hello community, here is the log from the commit of package ghc-exceptions for openSUSE:Factory checked in at 2018-07-24 17:18:29 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Comparing /work/SRC/openSUSE:Factory/ghc-exceptions (Old) and /work/SRC/openSUSE:Factory/.ghc-exceptions.new (New) ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Package is "ghc-exceptions" Tue Jul 24 17:18:29 2018 rev:11 rq:623767 version:0.10.0 Changes: -------- --- /work/SRC/openSUSE:Factory/ghc-exceptions/ghc-exceptions.changes 2018-05-30 12:25:36.573729699 +0200 +++ /work/SRC/openSUSE:Factory/.ghc-exceptions.new/ghc-exceptions.changes 2018-07-24 17:18:32.707004292 +0200 @@ -1,0 +2,32 @@ +Wed Jul 18 14:26:23 UTC 2018 - [email protected] + +- Cosmetic: replace tabs with blanks, strip trailing white space, + and update copyright headers with spec-cleaner. + +------------------------------------------------------------------- +Fri Jul 13 14:31:56 UTC 2018 - [email protected] + +- Update exceptions to version 0.10.0. + 0.10.0 + ------ + * Fix a regression in 0.9.0 whereby the non-IO effects in `bracket`'s `use` + action were not visible to the `release` action, and the non-IO effects in the + `release` action were not visible after the `bracket` call. + * The type of `generalBracket` was changed in order to restore those non-IO + effects, so if you are a library author that provides a `MonadMask` instance, + you will need to update your implementation of this method. + * Add `MonadMask` instance for `MaybeT` + * Add `onError` function whose action also runs on errors which are not + exceptions, such as a `Nothing` or a `Left`. + + 0.9.0 + ----- + * Add `generalBracket` to the `MonadMask` typeclass, allowing more + valid instances. + + Note that functions such as `bracket` and `finally` are now based off of + `generalBracket`, so if you are a library author that provides a `MonadMask` + instance, you will need to provide an implementation of this method. + * Add `MonadMask` instances for `ExceptT` and `ErrorT` + +------------------------------------------------------------------- @@ -62 +93,0 @@ - Old: ---- exceptions-0.8.3.tar.gz New: ---- exceptions-0.10.0.tar.gz ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Other differences: ------------------ ++++++ ghc-exceptions.spec ++++++ --- /var/tmp/diff_new_pack.wGF6RG/_old 2018-07-24 17:18:33.579005414 +0200 +++ /var/tmp/diff_new_pack.wGF6RG/_new 2018-07-24 17:18:33.579005414 +0200 @@ -1,7 +1,7 @@ # # spec file for package ghc-exceptions # -# Copyright (c) 2017 SUSE LINUX GmbH, Nuernberg, Germany. +# Copyright (c) 2018 SUSE LINUX GmbH, Nuernberg, Germany. # # All modifications and additions to the file contributed by third parties # remain the property of their copyright owners, unless otherwise agreed @@ -19,14 +19,14 @@ %global pkg_name exceptions %bcond_with tests Name: ghc-%{pkg_name} -Version: 0.8.3 +Version: 0.10.0 Release: 0 Summary: Extensible optionally-pure exceptions License: BSD-3-Clause Group: Development/Libraries/Haskell URL: https://hackage.haskell.org/package/%{pkg_name} Source0: https://hackage.haskell.org/package/%{pkg_name}-%{version}/%{pkg_name}-%{version}.tar.gz -Source1: https://hackage.haskell.org/package/%{pkg_name}-%{version}/revision/5.cabal#/%{pkg_name}.cabal +Source1: https://hackage.haskell.org/package/%{pkg_name}-%{version}/revision/1.cabal#/%{pkg_name}.cabal BuildRequires: ghc-Cabal-devel BuildRequires: ghc-mtl-devel BuildRequires: ghc-rpm-macros @@ -37,6 +37,7 @@ %if %{with tests} BuildRequires: ghc-QuickCheck-devel BuildRequires: ghc-test-framework-devel +BuildRequires: ghc-test-framework-hunit-devel BuildRequires: ghc-test-framework-quickcheck2-devel %endif ++++++ exceptions-0.8.3.tar.gz -> exceptions-0.10.0.tar.gz ++++++ diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/exceptions-0.8.3/.gitignore new/exceptions-0.10.0/.gitignore --- old/exceptions-0.8.3/.gitignore 2016-07-20 18:52:51.000000000 +0200 +++ new/exceptions-0.10.0/.gitignore 2018-03-11 15:52:28.000000000 +0100 @@ -1,4 +1,5 @@ dist +dist-newstyle docs wiki TAGS @@ -11,3 +12,21 @@ *.hi *~ *# +.stack-work/ +cabal-dev +*.chi +*.chs.h +*.dyn_o +*.dyn_hi +.hpc +.hsenv +.cabal-sandbox/ +cabal.sandbox.config +*.prof +*.aux +*.hp +*.eventlog +cabal.project.local +cabal.project.local~ +.HTF/ +.ghc.environment.* diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/exceptions-0.8.3/.travis.yml new/exceptions-0.10.0/.travis.yml --- old/exceptions-0.8.3/.travis.yml 2016-07-20 18:52:51.000000000 +0200 +++ new/exceptions-0.10.0/.travis.yml 2018-03-11 15:52:28.000000000 +0100 @@ -1,120 +1,146 @@ -# This file has been generated -- see https://github.com/hvr/multi-ghc-travis +# This Travis job script has been generated by a script via +# +# runghc make_travis_yml_2.hs '-o' '.travis.yml' '--irc-channel=irc.freenode.org#haskell-lens' '--no-no-tests-no-bench' '--no-installed' 'cabal.project' +# +# For more information, see https://github.com/hvr/multi-ghc-travis +# language: c sudo: false +git: + submodules: false # whether to recursively clone submodules + +notifications: + irc: + channels: + - "irc.freenode.org#haskell-lens" + skip_join: true + template: + - "\x0313exceptions\x03/\x0306%{branch}\x03 \x0314%{commit}\x03 %{build_url} %{message}" + cache: directories: - - $HOME/.cabsnap - $HOME/.cabal/packages + - $HOME/.cabal/store before_cache: - rm -fv $HOME/.cabal/packages/hackage.haskell.org/build-reports.log - - rm -fv $HOME/.cabal/packages/hackage.haskell.org/00-index.tar + # remove files that are regenerated by 'cabal update' + - rm -fv $HOME/.cabal/packages/hackage.haskell.org/00-index.* + - rm -fv $HOME/.cabal/packages/hackage.haskell.org/*.json + - rm -fv $HOME/.cabal/packages/hackage.haskell.org/01-index.cache + - rm -fv $HOME/.cabal/packages/hackage.haskell.org/01-index.tar + - rm -fv $HOME/.cabal/packages/hackage.haskell.org/01-index.tar.idx + + - rm -rfv $HOME/.cabal/packages/head.hackage matrix: include: - - env: CABALVER=1.18 GHCVER=7.0.4 - compiler: ": #GHC 7.0.4" - addons: {apt: {packages: [cabal-install-1.18,ghc-7.0.4], sources: [hvr-ghc]}} - - env: CABALVER=1.18 GHCVER=7.2.2 - compiler: ": #GHC 7.2.2" - addons: {apt: {packages: [cabal-install-1.18,ghc-7.2.2], sources: [hvr-ghc]}} - - env: CABALVER=1.18 GHCVER=7.4.2 - compiler: ": #GHC 7.4.2" - addons: {apt: {packages: [cabal-install-1.18,ghc-7.4.2], sources: [hvr-ghc]}} - - env: CABALVER=1.18 GHCVER=7.6.3 - compiler: ": #GHC 7.6.3" - addons: {apt: {packages: [cabal-install-1.18,ghc-7.6.3], sources: [hvr-ghc]}} - - env: CABALVER=1.18 GHCVER=7.8.4 - compiler: ": #GHC 7.8.4" - addons: {apt: {packages: [cabal-install-1.18,ghc-7.8.4], sources: [hvr-ghc]}} - - env: CABALVER=1.22 GHCVER=7.10.3 - compiler: ": #GHC 7.10.3" - addons: {apt: {packages: [cabal-install-1.22,ghc-7.10.3], sources: [hvr-ghc]}} - - env: CABALVER=1.24 GHCVER=8.0.1 - compiler: ": #GHC 8.0.1" - addons: {apt: {packages: [cabal-install-1.24,ghc-8.0.1], sources: [hvr-ghc]}} - - env: CABALVER=head GHCVER=head - compiler: ": #GHC head" - addons: {apt: {packages: [cabal-install-head,ghc-head], sources: [hvr-ghc]}} + - compiler: "ghc-7.0.4" + # env: TEST=--disable-tests BENCH=--disable-benchmarks + addons: {apt: {packages: [*apt_packages,cabal-install-2.0,ghc-7.0.4], sources: [hvr-ghc]}} + - compiler: "ghc-7.2.2" + # env: TEST=--disable-tests BENCH=--disable-benchmarks + addons: {apt: {packages: [*apt_packages,cabal-install-2.0,ghc-7.2.2], sources: [hvr-ghc]}} + - compiler: "ghc-7.4.2" + # env: TEST=--disable-tests BENCH=--disable-benchmarks + addons: {apt: {packages: [*apt_packages,cabal-install-2.0,ghc-7.4.2], sources: [hvr-ghc]}} + - compiler: "ghc-7.6.3" + # env: TEST=--disable-tests BENCH=--disable-benchmarks + addons: {apt: {packages: [*apt_packages,cabal-install-2.0,ghc-7.6.3], sources: [hvr-ghc]}} + - compiler: "ghc-7.8.4" + # env: TEST=--disable-tests BENCH=--disable-benchmarks + addons: {apt: {packages: [*apt_packages,cabal-install-2.0,ghc-7.8.4], sources: [hvr-ghc]}} + - compiler: "ghc-7.10.3" + # env: TEST=--disable-tests BENCH=--disable-benchmarks + addons: {apt: {packages: [*apt_packages,cabal-install-2.0,ghc-7.10.3], sources: [hvr-ghc]}} + - compiler: "ghc-8.0.2" + # env: TEST=--disable-tests BENCH=--disable-benchmarks + addons: {apt: {packages: [*apt_packages,cabal-install-2.0,ghc-8.0.2], sources: [hvr-ghc]}} + - compiler: "ghc-8.2.2" + # env: TEST=--disable-tests BENCH=--disable-benchmarks + addons: {apt: {packages: [*apt_packages,cabal-install-2.0,ghc-8.2.2], sources: [hvr-ghc]}} + - compiler: "ghc-8.4.1" + # env: TEST=--disable-tests BENCH=--disable-benchmarks + addons: {apt: {packages: [*apt_packages,cabal-install-2.2,ghc-8.4.1], sources: [hvr-ghc]}} + - compiler: "ghc-head" + env: GHCHEAD=true + addons: {apt: {packages: [*apt_packages,cabal-install-head,ghc-head], sources: [hvr-ghc]}} allow_failures: - - env: CABALVER=1.18 GHCVER=7.0.4 - - env: CABALVER=1.18 GHCVER=7.2.2 - - env: CABALVER=head GHCVER=head + - compiler: "ghc-7.0.4" + - compiler: "ghc-7.2.2" + - compiler: "ghc-head" before_install: - - unset CC - - export PATH=$HOME/.cabal/bin:/opt/ghc/$GHCVER/bin:/opt/cabal/$CABALVER/bin:$PATH + - HC=${CC} + - HCPKG=${HC/ghc/ghc-pkg} + - unset CC + - ROOTDIR=$(pwd) + - mkdir -p $HOME/.local/bin + - "PATH=/opt/ghc/bin:/opt/ghc-ppa-tools/bin:$HOME/local/bin:$PATH" + - HCNUMVER=$(( $(${HC} --numeric-version|sed -E 's/([0-9]+)\.([0-9]+)\.([0-9]+).*/\1 * 10000 + \2 * 100 + \3/') )) + - echo $HCNUMVER install: - - cabal --version - - echo "$(ghc --version) [$(ghc --print-project-git-commit-id 2> /dev/null || echo '?')]" - - if [ -f $HOME/.cabal/packages/hackage.haskell.org/00-index.tar.gz ]; - then - zcat $HOME/.cabal/packages/hackage.haskell.org/00-index.tar.gz > - $HOME/.cabal/packages/hackage.haskell.org/00-index.tar; - fi - - travis_retry cabal update -v - - sed -i 's/^jobs:/-- jobs:/' ${HOME}/.cabal/config - - cabal install --only-dependencies --enable-tests --dry -v > installplan.txt - - sed -i -e '1,/^Resolving /d' installplan.txt; cat installplan.txt - -# check whether current requested install-plan matches cached package-db snapshot - - if diff -u installplan.txt $HOME/.cabsnap/installplan.txt; - then - echo "cabal build-cache HIT"; - rm -rfv .ghc; - cp -a $HOME/.cabsnap/ghc $HOME/.ghc; - cp -a $HOME/.cabsnap/lib $HOME/.cabsnap/share $HOME/.cabsnap/bin $HOME/.cabal/; - else - echo "cabal build-cache MISS"; - rm -rf $HOME/.cabsnap; - mkdir -p $HOME/.ghc $HOME/.cabal/lib $HOME/.cabal/share $HOME/.cabal/bin; - cabal install -j --only-dependencies --enable-tests; - fi - -# snapshot package-db on cache miss - - if [ ! -d $HOME/.cabsnap ]; - then - echo "snapshotting package-db to build-cache"; - mkdir $HOME/.cabsnap; - cp -a $HOME/.ghc $HOME/.cabsnap/ghc; - cp -a $HOME/.cabal/lib $HOME/.cabal/share $HOME/.cabal/bin installplan.txt $HOME/.cabsnap/; - fi - -# Here starts the actual work to be performed for the package under -# test; any command which exits with a non-zero exit code causes the -# build to fail. -script: - # -v2 provides useful information for debugging - - cabal configure -v2 - - # this builds all libraries and executables - # (including tests/benchmarks) - - cabal build - - - cabal test --show-details=always - - # tests that a source-distribution can be generated - - cabal sdist - - # check that the generated source-distribution can be built & installed - - export SRC_TGZ=$(cabal info . | awk '{print $2 ".tar.gz";exit}') ; - cd dist/; - if [ -f "$SRC_TGZ" ]; then - cabal install --force-reinstalls "$SRC_TGZ"; - else - echo "expected '$SRC_TGZ' not found"; - exit 1; - fi + - cabal --version + - echo "$(${HC} --version) [$(${HC} --print-project-git-commit-id 2> /dev/null || echo '?')]" + - BENCH=${BENCH---enable-benchmarks} + - TEST=${TEST---enable-tests} + - HADDOCK=${HADDOCK-true} + - INSTALLED=${INSTALLED-true} + - GHCHEAD=${GHCHEAD-false} + - travis_retry cabal update -v + - "sed -i.bak 's/^jobs:/-- jobs:/' ${HOME}/.cabal/config" + - rm -fv cabal.project cabal.project.local + # Overlay Hackage Package Index for GHC HEAD: https://github.com/hvr/head.hackage + - | + if $GHCHEAD; then + sed -i.bak 's/-- allow-newer:.*/allow-newer: *:base, *:template-haskell, *:ghc, *:Cabal/' ${HOME}/.cabal/config + + echo 'repository head.hackage' >> ${HOME}/.cabal/config + echo ' url: http://head.hackage.haskell.org/' >> ${HOME}/.cabal/config + echo ' secure: True' >> ${HOME}/.cabal/config + echo ' root-keys: 07c59cb65787dedfaef5bd5f987ceb5f7e5ebf88b904bbd4c5cbdeb2ff71b740' >> ${HOME}/.cabal/config + echo ' 2e8555dde16ebd8df076f1a8ef13b8f14c66bad8eafefd7d9e37d0ed711821fb' >> ${HOME}/.cabal/config + echo ' 8f79fd2389ab2967354407ec852cbe73f2e8635793ac446d09461ffb99527f6e' >> ${HOME}/.cabal/config + echo ' key-threshold: 3' >> ${HOME}/.cabal.config + + cabal new-update head.hackage -v + fi + - grep -Ev -- '^\s*--' ${HOME}/.cabal/config | grep -Ev '^\s*$' + - "printf 'packages: \".\"\\n' > cabal.project" + - cat cabal.project + - if [ -f "./configure.ac" ]; then + (cd "." && autoreconf -i); + fi + - rm -f cabal.project.freeze + - cabal new-build -w ${HC} ${TEST} ${BENCH} --project-file="cabal.project" --dep -j2 all + - rm -rf "."/.ghc.environment.* "."/dist + - DISTDIR=$(mktemp -d /tmp/dist-test.XXXX) -notifications: - irc: - channels: - - "irc.freenode.org#haskell-lens" - skip_join: true - template: - - "\x0313exceptions\x0f/\x0306%{branch}\x0f \x0314%{commit}\x0f %{message} \x0302\x1f%{build_url}\x0f" +# Here starts the actual work to be performed for the package under test; +# any command which exits with a non-zero exit code causes the build to fail. +script: + # test that source-distributions can be generated + - (cd "." && cabal sdist) + - mv "."/dist/exceptions-*.tar.gz ${DISTDIR}/ + - cd ${DISTDIR} || false + - find . -maxdepth 1 -name '*.tar.gz' -exec tar -xvf '{}' \; + - "printf 'packages: exceptions-*/*.cabal\\n' > cabal.project" + - cat cabal.project + + + # build & run tests, build benchmarks + - cabal new-build -w ${HC} ${TEST} ${BENCH} all + - if [ "x$TEST" = "x--enable-tests" ]; then cabal new-test -w ${HC} ${TEST} ${BENCH} all; fi + + # cabal check + - (cd exceptions-* && cabal check) + + # haddock + - rm -rf ./dist-newstyle + - if $HADDOCK; then cabal new-haddock -w ${HC} ${TEST} ${BENCH} all; else echo "Skipping haddock generation";fi +# REGENDATA ["-o",".travis.yml","--irc-channel=irc.freenode.org#haskell-lens","--no-no-tests-no-bench","--no-installed","cabal.project"] # EOF diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/exceptions-0.8.3/CHANGELOG.markdown new/exceptions-0.10.0/CHANGELOG.markdown --- old/exceptions-0.8.3/CHANGELOG.markdown 2016-07-20 18:52:51.000000000 +0200 +++ new/exceptions-0.10.0/CHANGELOG.markdown 2018-03-11 15:52:28.000000000 +0100 @@ -1,3 +1,25 @@ +0.10.0 +------ +* Fix a regression in 0.9.0 whereby the non-IO effects in `bracket`'s `use` + action were not visible to the `release` action, and the non-IO effects in the + `release` action were not visible after the `bracket` call. +* The type of `generalBracket` was changed in order to restore those non-IO + effects, so if you are a library author that provides a `MonadMask` instance, + you will need to update your implementation of this method. +* Add `MonadMask` instance for `MaybeT` +* Add `onError` function whose action also runs on errors which are not + exceptions, such as a `Nothing` or a `Left`. + +0.9.0 +----- +* Add `generalBracket` to the `MonadMask` typeclass, allowing more + valid instances. + + Note that functions such as `bracket` and `finally` are now based off of + `generalBracket`, so if you are a library author that provides a `MonadMask` + instance, you will need to provide an implementation of this method. +* Add `MonadMask` instances for `ExceptT` and `ErrorT` + 0.8.3 ----- * `MonadCatch` and `MonadMask` instances for `Either SomeException` diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/exceptions-0.8.3/exceptions.cabal new/exceptions-0.10.0/exceptions.cabal --- old/exceptions-0.8.3/exceptions.cabal 2016-07-20 18:52:51.000000000 +0200 +++ new/exceptions-0.10.0/exceptions.cabal 2018-03-11 15:52:28.000000000 +0100 @@ -1,6 +1,6 @@ name: exceptions category: Control, Exceptions, Monad -version: 0.8.3 +version: 0.10.0 cabal-version: >= 1.8 license: BSD3 license-file: LICENSE @@ -12,9 +12,9 @@ copyright: Copyright (C) 2013-2015 Edward A. Kmett Copyright (C) 2012 Google Inc. build-type: Simple -tested-with: GHC == 7.4.2, GHC == 7.6.3, GHC == 7.8.4, GHC == 7.10.0.20150307 +tested-with: GHC == 7.0.4, GHC == 7.2.2, GHC == 7.4.2, GHC == 7.6.3, GHC == 7.8.4, GHC == 7.10.3, GHC == 8.0.2, GHC == 8.2.2, GHC == 8.4.1 synopsis: Extensible optionally-pure exceptions -description: Extensible optionally-pure exceptions +description: Extensible optionally-pure exceptions. extra-source-files: .travis.yml @@ -35,9 +35,9 @@ build-depends: base >= 4.3 && < 5, stm >= 2.2 && < 3, - template-haskell >= 2.2 && < 2.12, + template-haskell >= 2.2 && < 2.14, transformers >= 0.2 && < 0.6, - transformers-compat >= 0.3 && < 0.6, + transformers-compat >= 0.3 && < 0.7, mtl >= 2.0 && < 2.3 exposed-modules: @@ -49,7 +49,10 @@ test-suite exceptions-tests main-is: Tests.hs - other-modules: Control.Monad.Catch.Tests + other-modules: + Control.Monad.Catch + Control.Monad.Catch.Pure + Control.Monad.Catch.Tests hs-source-dirs: src, tests ghc-options: -Wall -fwarn-tabs type: exitcode-stdio-1.0 @@ -59,7 +62,8 @@ template-haskell, transformers, transformers-compat, - mtl, + mtl >= 2.2, test-framework >= 0.8 && < 0.9, + test-framework-hunit >= 0.3 && < 0.4, test-framework-quickcheck2 >= 0.3 && < 0.4, - QuickCheck >= 2.5 && < 2.10 + QuickCheck >= 2.5 && < 2.12 diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/exceptions-0.8.3/src/Control/Monad/Catch/Pure.hs new/exceptions-0.10.0/src/Control/Monad/Catch/Pure.hs --- old/exceptions-0.8.3/src/Control/Monad/Catch/Pure.hs 2016-07-20 18:52:51.000000000 +0200 +++ new/exceptions-0.10.0/src/Control/Monad/Catch/Pure.hs 2018-03-11 15:52:28.000000000 +0100 @@ -156,9 +156,25 @@ Right a -> return (Right a) -- | Note: This instance is only valid if the underlying monad has a single -- exit point! +-- +-- For example, @IO@ or @Either@ would be invalid base monads, but +-- @Reader@ or @State@ would be acceptable. instance Monad m => MonadMask (CatchT m) where mask a = a id uninterruptibleMask a = a id + generalBracket acquire release use = CatchT $ do + eresource <- runCatchT acquire + case eresource of + Left e -> return $ Left e + Right resource -> do + eb <- runCatchT (use resource) + case eb of + Left e -> runCatchT $ do + _ <- release resource (ExitCaseException e) + throwM e + Right b -> runCatchT $ do + c <- release resource (ExitCaseSuccess b) + return (b, c) instance MonadState s m => MonadState s (CatchT m) where get = lift get diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/exceptions-0.8.3/src/Control/Monad/Catch.hs new/exceptions-0.10.0/src/Control/Monad/Catch.hs --- old/exceptions-0.8.3/src/Control/Monad/Catch.hs 2016-07-20 18:52:51.000000000 +0200 +++ new/exceptions-0.10.0/src/Control/Monad/Catch.hs 2018-03-11 15:52:28.000000000 +0100 @@ -48,6 +48,7 @@ MonadThrow(..) , MonadCatch(..) , MonadMask(..) + , ExitCase(..) -- * Utilities -- $utilities @@ -66,6 +67,7 @@ , try , tryJust , onException + , onError , bracket , bracket_ , finally @@ -105,6 +107,10 @@ import Data.Monoid #endif +#if __GLASGOW_HASKELL__ < 710 +import Control.Applicative +#endif + ------------------------------------------------------------------------------ -- $mtl -- The mtl style typeclass @@ -148,23 +154,41 @@ -- 'ControlException.catch'. catch :: Exception e => m a -> (e -> m a) -> m a --- | A class for monads which provide for the ability to account for all --- possible exit points from a computation, and to mask asynchronous --- exceptions. Continuation-based monads, and stacks such as @ErrorT e IO@ --- which provide for multiple failure modes, are invalid instances of this --- class. --- --- Note that this package /does/ provide a @MonadMask@ instance for @CatchT@. --- This instance is /only/ valid if the base monad provides no ability to --- provide multiple exit. For example, @IO@ or @Either@ would be invalid base --- monads, but @Reader@ or @State@ would be acceptable. +-- | A class for monads which provide for the ability to account for +-- all possible exit points from a computation, and to mask +-- asynchronous exceptions. Continuation-based monads are invalid +-- instances of this class. -- -- Instances should ensure that, in the following code: -- --- > f `finally` g +-- > fg = f `finally` g -- -- The action @g@ is called regardless of what occurs within @f@, including --- async exceptions. +-- async exceptions. Some monads allow @f@ to abort the computation via other +-- effects than throwing an exception. For simplicity, we will consider aborting +-- and throwing an exception to be two forms of "throwing an error". +-- +-- If @f@ and @g@ both throw an error, the error thrown by @fg@ depends on which +-- errors we're talking about. In a monad transformer stack, the deeper layers +-- override the effects of the inner layers; for example, @ExceptT e1 (Except +-- e2) a@ represents a value of type @Either e2 (Either e1 a)@, so throwing both +-- an @e1@ and an @e2@ will result in @Left e2@. If @f@ and @g@ both throw an +-- error from the same layer, instances should ensure that the error from @g@ +-- wins. +-- +-- Effects other than throwing an error are also overriden by the deeper layers. +-- For example, @StateT s Maybe a@ represents a value of type @s -> Maybe (a, +-- s)@, so if an error thrown from @f@ causes this function to return @Nothing@, +-- any changes to the state which @f@ also performed will be erased. As a +-- result, @g@ will see the state as it was before @f@. Once @g@ completes, +-- @f@'s error will be rethrown, so @g@' state changes will be erased as well. +-- This is the normal interaction between effects in a monad transformer stack. +-- +-- By contrast, <https://hackage.haskell.org/package/lifted-base lifted-base>'s +-- version of 'finally' always discards all of @g@'s non-IO effects, and @g@ +-- never sees any of @f@'s non-IO effects, regardless of the layer ordering and +-- regardless of whether @f@ throws an error. This is not the result of +-- interacting effects, but a consequence of @MonadBaseControl@'s approach. class MonadCatch m => MonadMask m where -- | Runs an action with asynchronous exceptions disabled. The action is -- provided a method for restoring the async. environment to what it was @@ -179,6 +203,116 @@ -- and/or unkillable. uninterruptibleMask :: ((forall a. m a -> m a) -> m b) -> m b + -- | A generalized version of 'bracket' which uses 'ExitCase' to distinguish + -- the different exit cases, and returns the values of both the 'use' and + -- 'release' actions. In practice, this extra information is rarely needed, + -- so it is often more convenient to use one of the simpler functions which + -- are defined in terms of this one, such as 'bracket', 'finally', 'onError', + -- and 'bracketOnError'. + -- + -- This function exists because in order to thread their effects through the + -- execution of 'bracket', monad transformers need values to be threaded from + -- 'use' to 'release' and from 'release' to the output value. + -- + -- /NOTE/ This method was added in version 0.9.0 of this + -- library. Previously, implementation of functions like 'bracket' + -- and 'finally' in this module were based on the 'mask' and + -- 'uninterruptibleMask' functions only, disallowing some classes of + -- tranformers from having @MonadMask@ instances (notably + -- multi-exit-point transformers like 'ExceptT'). If you are a + -- library author, you'll now need to provide an implementation for + -- this method. The @StateT@ implementation demonstrates most of the + -- subtleties: + -- + -- @ + -- generalBracket acquire release use = StateT $ \s0 -> do + -- ((b, _s2), (c, s3)) <- generalBracket + -- (runStateT acquire s0) + -- (\(resource, s1) exitCase -> case exitCase of + -- ExitCaseSuccess (b, s2) -> runStateT (release resource (ExitCaseSuccess b)) s2 + -- + -- -- In the two other cases, the base monad overrides @use@'s state + -- -- changes and the state reverts to @s1@. + -- ExitCaseException e -> runStateT (release resource (ExitCaseException e)) s1 + -- ExitCaseAbort -> runStateT (release resource ExitCaseAbort) s1 + -- ) + -- (\(resource, s1) -> runStateT (use resource) s1) + -- return ((b, c), s3) + -- @ + -- + -- The @StateT s m@ implementation of @generalBracket@ delegates to the @m@ + -- implementation of @generalBracket@. The @acquire@, @use@, and @release@ + -- arguments given to @StateT@'s implementation produce actions of type + -- @StateT s m a@, @StateT s m b@, and @StateT s m c@. In order to run those + -- actions in the base monad, we need to call @runStateT@, from which we + -- obtain actions of type @m (a, s)@, @m (b, s)@, and @m (c, s)@. Since each + -- action produces the next state, it is important to feed the state produced + -- by the previous action to the next action. + -- + -- In the 'ExitCaseSuccess' case, the state starts at @s0@, flows through + -- @acquire@ to become @s1@, flows through @use@ to become @s2@, and finally + -- flows through @release@ to become @s3@. In the other two cases, @release@ + -- does not receive the value @s2@, so its action cannot see the state changes + -- performed by @use@. This is fine, because in those two cases, an error was + -- thrown in the base monad, so as per the usual interaction between effects + -- in a monad transformer stack, those state changes get reverted. So we start + -- from @s1@ instead. + -- + -- Finally, the @m@ implementation of @generalBracket@ returns the pairs + -- @(b, s)@ and @(c, s)@. For monad transformers other than @StateT@, this + -- will be some other type representing the effects and values performed and + -- returned by the @use@ and @release@ actions. The effect part of the @use@ + -- result, in this case @_s2@, usually needs to be discarded, since those + -- effects have already been incorporated in the @release@ action. + -- + -- The only effect which is intentionally not incorporated in the @release@ + -- action is the effect of throwing an error. In that case, the error must be + -- re-thrown. One subtlety which is easy to miss is that in the case in which + -- @use@ and @release@ both throw an error, the error from @release@ should + -- take priority. Here is an implementation for @ExceptT@ which demonstrates + -- how to do this. + -- + -- @ + -- generalBracket acquire release use = ExceptT $ do + -- (eb, ec) <- generalBracket + -- (runExceptT acquire) + -- (\eresource exitCase -> case eresource of + -- Left e -> return (Left e) -- nothing to release, acquire didn't succeed + -- Right resource -> case exitCase of + -- ExitCaseSuccess (Right b) -> runExceptT (release resource (ExitCaseSuccess b)) + -- ExitCaseException e -> runExceptT (release resource (ExitCaseException e)) + -- _ -> runExceptT (release resource ExitCaseAbort)) + -- (either (return . Left) (runExceptT . use)) + -- return $ do + -- -- The order in which we perform those two 'Either' effects determines + -- -- which error will win if they are both 'Left's. We want the error from + -- -- 'release' to win. + -- c <- ec + -- b <- eb + -- return (b, c) + -- @ + -- + -- @since 0.9.0 + generalBracket + :: m a + -- ^ acquire some resource + -> (a -> ExitCase b -> m c) + -- ^ release the resource, observing the outcome of the inner action + -> (a -> m b) + -- ^ inner action to perform with the resource + -> m (b, c) + +-- | A 'MonadMask' computation may either succeed with a value, abort with an +-- exception, or abort for some other reason. For example, in @ExceptT e IO@ +-- you can use 'throwM' to abort with an exception ('ExitCaseException') or +-- 'Control.Monad.Trans.Except.throwE' to abort with a value of type 'e' +-- ('ExitCaseAbort'). +data ExitCase a + = ExitCaseSuccess a + | ExitCaseException SomeException + | ExitCaseAbort + deriving Show + instance MonadThrow [] where throwM _ = [] instance MonadThrow Maybe where @@ -193,6 +327,13 @@ instance MonadMask IO where mask = ControlException.mask uninterruptibleMask = ControlException.uninterruptibleMask + generalBracket acquire release use = mask $ \unmasked -> do + resource <- acquire + b <- unmasked (use resource) `catch` \e -> do + _ <- release resource (ExitCaseException e) + throwM e + c <- release resource (ExitCaseSuccess b) + return (b, c) instance MonadThrow STM where throwM = STM.throwSTM @@ -213,6 +354,16 @@ mask f = f id uninterruptibleMask f = f id + generalBracket acquire release use = + case acquire of + Left e -> Left e + Right resource -> + case use resource of + Left e -> release resource (ExitCaseException e) >> Left e + Right b -> do + c <- release resource (ExitCaseSuccess b) + return (b, c) + instance MonadThrow m => MonadThrow (IdentityT m) where throwM e = lift $ throwM e instance MonadCatch m => MonadCatch (IdentityT m) where @@ -226,6 +377,12 @@ where q :: (m a -> m a) -> IdentityT m a -> IdentityT m a q u = IdentityT . u . runIdentityT + generalBracket acquire release use = IdentityT $ + generalBracket + (runIdentityT acquire) + (\resource exitCase -> runIdentityT (release resource exitCase)) + (\resource -> runIdentityT (use resource)) + instance MonadThrow m => MonadThrow (LazyS.StateT s m) where throwM e = lift $ throwM e instance MonadCatch m => MonadCatch (LazyS.StateT s m) where @@ -239,6 +396,21 @@ where q :: (m (a, s) -> m (a, s)) -> LazyS.StateT s m a -> LazyS.StateT s m a q u (LazyS.StateT b) = LazyS.StateT (u . b) + generalBracket acquire release use = LazyS.StateT $ \s0 -> do + -- This implementation is given as an example in the documentation of + -- 'generalBracket', so when changing it, remember to update the + -- documentation's copy as well + ((b, _s2), (c, s3)) <- generalBracket + (LazyS.runStateT acquire s0) + (\(resource, s1) exitCase -> case exitCase of + ExitCaseSuccess (b, s2) -> LazyS.runStateT (release resource (ExitCaseSuccess b)) s2 + -- In the two other cases, the base monad overrides @use@'s state + -- changes and the state reverts to @s1@. + ExitCaseException e -> LazyS.runStateT (release resource (ExitCaseException e)) s1 + ExitCaseAbort -> LazyS.runStateT (release resource ExitCaseAbort) s1) + (\(resource, s1) -> LazyS.runStateT (use resource) s1) + return ((b, c), s3) + instance MonadThrow m => MonadThrow (StrictS.StateT s m) where throwM e = lift $ throwM e instance MonadCatch m => MonadCatch (StrictS.StateT s m) where @@ -252,6 +424,18 @@ where q :: (m (a, s) -> m (a, s)) -> StrictS.StateT s m a -> StrictS.StateT s m a q u (StrictS.StateT b) = StrictS.StateT (u . b) + generalBracket acquire release use = StrictS.StateT $ \s0 -> do + ((b, _s2), (c, s3)) <- generalBracket + (StrictS.runStateT acquire s0) + (\(resource, s1) exitCase -> case exitCase of + ExitCaseSuccess (b, s2) -> StrictS.runStateT (release resource (ExitCaseSuccess b)) s2 + -- In the two other cases, the base monad overrides @use@'s state + -- changes and the state reverts to @s1@. + ExitCaseException e -> StrictS.runStateT (release resource (ExitCaseException e)) s1 + ExitCaseAbort -> StrictS.runStateT (release resource ExitCaseAbort) s1) + (\(resource, s1) -> StrictS.runStateT (use resource) s1) + return ((b, c), s3) + instance MonadThrow m => MonadThrow (ReaderT r m) where throwM e = lift $ throwM e instance MonadCatch m => MonadCatch (ReaderT r m) where @@ -265,6 +449,12 @@ where q :: (m a -> m a) -> ReaderT e m a -> ReaderT e m a q u (ReaderT b) = ReaderT (u . b) + generalBracket acquire release use = ReaderT $ \r -> + generalBracket + (runReaderT acquire r) + (\resource exitCase -> runReaderT (release resource exitCase) r) + (\resource -> runReaderT (use resource) r) + instance (MonadThrow m, Monoid w) => MonadThrow (StrictW.WriterT w m) where throwM e = lift $ throwM e instance (MonadCatch m, Monoid w) => MonadCatch (StrictW.WriterT w m) where @@ -278,6 +468,26 @@ where q :: (m (a, w) -> m (a, w)) -> StrictW.WriterT w m a -> StrictW.WriterT w m a q u b = StrictW.WriterT $ u (StrictW.runWriterT b) + generalBracket acquire release use = StrictW.WriterT $ do + ((b, _w12), (c, w123)) <- generalBracket + (StrictW.runWriterT acquire) + (\(resource, w1) exitCase -> case exitCase of + ExitCaseSuccess (b, w12) -> do + (c, w3) <- StrictW.runWriterT (release resource (ExitCaseSuccess b)) + return (c, mappend w12 w3) + -- In the two other cases, the base monad overrides @use@'s state + -- changes and the state reverts to @w1@. + ExitCaseException e -> do + (c, w3) <- StrictW.runWriterT (release resource (ExitCaseException e)) + return (c, mappend w1 w3) + ExitCaseAbort -> do + (c, w3) <- StrictW.runWriterT (release resource ExitCaseAbort) + return (c, mappend w1 w3)) + (\(resource, w1) -> do + (a, w2) <- StrictW.runWriterT (use resource) + return (a, mappend w1 w2)) + return ((b, c), w123) + instance (MonadThrow m, Monoid w) => MonadThrow (LazyW.WriterT w m) where throwM e = lift $ throwM e instance (MonadCatch m, Monoid w) => MonadCatch (LazyW.WriterT w m) where @@ -291,6 +501,26 @@ where q :: (m (a, w) -> m (a, w)) -> LazyW.WriterT w m a -> LazyW.WriterT w m a q u b = LazyW.WriterT $ u (LazyW.runWriterT b) + generalBracket acquire release use = LazyW.WriterT $ do + ((b, _w12), (c, w123)) <- generalBracket + (LazyW.runWriterT acquire) + (\(resource, w1) exitCase -> case exitCase of + ExitCaseSuccess (b, w12) -> do + (c, w3) <- LazyW.runWriterT (release resource (ExitCaseSuccess b)) + return (c, mappend w12 w3) + -- In the two other cases, the base monad overrides @use@'s state + -- changes and the state reverts to @w1@. + ExitCaseException e -> do + (c, w3) <- LazyW.runWriterT (release resource (ExitCaseException e)) + return (c, mappend w1 w3) + ExitCaseAbort -> do + (c, w3) <- LazyW.runWriterT (release resource ExitCaseAbort) + return (c, mappend w1 w3)) + (\(resource, w1) -> do + (a, w2) <- LazyW.runWriterT (use resource) + return (a, mappend w1 w2)) + return ((b, c), w123) + instance (MonadThrow m, Monoid w) => MonadThrow (LazyRWS.RWST r w s m) where throwM e = lift $ throwM e instance (MonadCatch m, Monoid w) => MonadCatch (LazyRWS.RWST r w s m) where @@ -304,6 +534,26 @@ where q :: (m (a, s, w) -> m (a, s, w)) -> LazyRWS.RWST r w s m a -> LazyRWS.RWST r w s m a q u (LazyRWS.RWST b) = LazyRWS.RWST $ \ r s -> u (b r s) + generalBracket acquire release use = LazyRWS.RWST $ \r s0 -> do + ((b, _s2, _w12), (c, s3, w123)) <- generalBracket + (LazyRWS.runRWST acquire r s0) + (\(resource, s1, w1) exitCase -> case exitCase of + ExitCaseSuccess (b, s2, w12) -> do + (c, s3, w3) <- LazyRWS.runRWST (release resource (ExitCaseSuccess b)) r s2 + return (c, s3, mappend w12 w3) + -- In the two other cases, the base monad overrides @use@'s state + -- changes and the state reverts to @s1@ and @w1@. + ExitCaseException e -> do + (c, s3, w3) <- LazyRWS.runRWST (release resource (ExitCaseException e)) r s1 + return (c, s3, mappend w1 w3) + ExitCaseAbort -> do + (c, s3, w3) <- LazyRWS.runRWST (release resource ExitCaseAbort) r s1 + return (c, s3, mappend w1 w3)) + (\(resource, s1, w1) -> do + (a, s2, w2) <- LazyRWS.runRWST (use resource) r s1 + return (a, s2, mappend w1 w2)) + return ((b, c), s3, w123) + instance (MonadThrow m, Monoid w) => MonadThrow (StrictRWS.RWST r w s m) where throwM e = lift $ throwM e instance (MonadCatch m, Monoid w) => MonadCatch (StrictRWS.RWST r w s m) where @@ -317,6 +567,26 @@ where q :: (m (a, s, w) -> m (a, s, w)) -> StrictRWS.RWST r w s m a -> StrictRWS.RWST r w s m a q u (StrictRWS.RWST b) = StrictRWS.RWST $ \ r s -> u (b r s) + generalBracket acquire release use = StrictRWS.RWST $ \r s0 -> do + ((b, _s2, _w12), (c, s3, w123)) <- generalBracket + (StrictRWS.runRWST acquire r s0) + (\(resource, s1, w1) exitCase -> case exitCase of + ExitCaseSuccess (b, s2, w12) -> do + (c, s3, w3) <- StrictRWS.runRWST (release resource (ExitCaseSuccess b)) r s2 + return (c, s3, mappend w12 w3) + -- In the two other cases, the base monad overrides @use@'s state + -- changes and the state reverts to @s1@ and @w1@. + ExitCaseException e -> do + (c, s3, w3) <- StrictRWS.runRWST (release resource (ExitCaseException e)) r s1 + return (c, s3, mappend w1 w3) + ExitCaseAbort -> do + (c, s3, w3) <- StrictRWS.runRWST (release resource ExitCaseAbort) r s1 + return (c, s3, mappend w1 w3)) + (\(resource, s1, w1) -> do + (a, s2, w2) <- StrictRWS.runRWST (use resource) r s1 + return (a, s2, mappend w1 w2)) + return ((b, c), s3, w123) + -- Transformers which are only instances of MonadThrow and MonadCatch, not MonadMask instance MonadThrow m => MonadThrow (ListT m) where throwM = lift . throwM @@ -329,6 +599,34 @@ -- | Catches exceptions from the base monad. instance MonadCatch m => MonadCatch (MaybeT m) where catch (MaybeT m) f = MaybeT $ catch m (runMaybeT . f) +-- | @since 0.10.0 +instance MonadMask m => MonadMask (MaybeT m) where + mask f = MaybeT $ mask $ \u -> runMaybeT $ f (q u) + where + q :: (m (Maybe a) -> m (Maybe a)) + -> MaybeT m a -> MaybeT m a + q u (MaybeT b) = MaybeT (u b) + uninterruptibleMask f = MaybeT $ uninterruptibleMask $ \u -> runMaybeT $ f (q u) + where + q :: (m (Maybe a) -> m (Maybe a)) + -> MaybeT m a -> MaybeT m a + q u (MaybeT b) = MaybeT (u b) + + generalBracket acquire release use = MaybeT $ do + (eb, ec) <- generalBracket + (runMaybeT acquire) + (\resourceMay exitCase -> case resourceMay of + Nothing -> return Nothing -- nothing to release, acquire didn't succeed + Just resource -> case exitCase of + ExitCaseSuccess (Just b) -> runMaybeT (release resource (ExitCaseSuccess b)) + ExitCaseException e -> runMaybeT (release resource (ExitCaseException e)) + _ -> runMaybeT (release resource ExitCaseAbort)) + (\resourceMay -> case resourceMay of + Nothing -> return Nothing + Just resource -> runMaybeT (use resource)) + -- The order in which we perform those two 'Maybe' effects doesn't matter, + -- since the error message is the same regardless. + return ((,) <$> eb <*> ec) -- | Throws exceptions into the base monad. instance (Error e, MonadThrow m) => MonadThrow (ErrorT e m) where @@ -336,6 +634,35 @@ -- | Catches exceptions from the base monad. instance (Error e, MonadCatch m) => MonadCatch (ErrorT e m) where catch (ErrorT m) f = ErrorT $ catch m (runErrorT . f) +instance (Error e, MonadMask m) => MonadMask (ErrorT e m) where + mask f = ErrorT $ mask $ \u -> runErrorT $ f (q u) + where + q :: (m (Either e a) -> m (Either e a)) + -> ErrorT e m a -> ErrorT e m a + q u (ErrorT b) = ErrorT (u b) + uninterruptibleMask f = ErrorT $ uninterruptibleMask $ \u -> runErrorT $ f (q u) + where + q :: (m (Either e a) -> m (Either e a)) + -> ErrorT e m a -> ErrorT e m a + q u (ErrorT b) = ErrorT (u b) + + generalBracket acquire release use = ErrorT $ do + (eb, ec) <- generalBracket + (runErrorT acquire) + (\eresource exitCase -> case eresource of + Left e -> return (Left e) -- nothing to release, acquire didn't succeed + Right resource -> case exitCase of + ExitCaseSuccess (Right b) -> runErrorT (release resource (ExitCaseSuccess b)) + ExitCaseException e -> runErrorT (release resource (ExitCaseException e)) + _ -> runErrorT (release resource ExitCaseAbort)) + (either (return . Left) (runErrorT . use)) + return $ do + -- The order in which we perform those two 'Either' effects determines + -- which error will win if they are both 'Left's. We want the error from + -- 'release' to win. + c <- ec + b <- eb + return (b, c) -- | Throws exceptions into the base monad. instance MonadThrow m => MonadThrow (ExceptT e m) where @@ -343,6 +670,39 @@ -- | Catches exceptions from the base monad. instance MonadCatch m => MonadCatch (ExceptT e m) where catch (ExceptT m) f = ExceptT $ catch m (runExceptT . f) +-- | @since 0.9.0 +instance MonadMask m => MonadMask (ExceptT e m) where + mask f = ExceptT $ mask $ \u -> runExceptT $ f (q u) + where + q :: (m (Either e a) -> m (Either e a)) + -> ExceptT e m a -> ExceptT e m a + q u (ExceptT b) = ExceptT (u b) + uninterruptibleMask f = ExceptT $ uninterruptibleMask $ \u -> runExceptT $ f (q u) + where + q :: (m (Either e a) -> m (Either e a)) + -> ExceptT e m a -> ExceptT e m a + q u (ExceptT b) = ExceptT (u b) + + generalBracket acquire release use = ExceptT $ do + -- This implementation is given as an example in the documentation of + -- 'generalBracket', so when changing it, remember to update the + -- documentation's copy as well + (eb, ec) <- generalBracket + (runExceptT acquire) + (\eresource exitCase -> case eresource of + Left e -> return (Left e) -- nothing to release, acquire didn't succeed + Right resource -> case exitCase of + ExitCaseSuccess (Right b) -> runExceptT (release resource (ExitCaseSuccess b)) + ExitCaseException e -> runExceptT (release resource (ExitCaseException e)) + _ -> runExceptT (release resource ExitCaseAbort)) + (either (return . Left) (runExceptT . use)) + return $ do + -- The order in which we perform those two 'Either' effects determines + -- which error will win if they are both 'Left's. We want the error from + -- 'release' to win. + c <- ec + b <- eb + return (b, c) instance MonadThrow m => MonadThrow (ContT r m) where throwM = lift . throwM @@ -367,6 +727,9 @@ -- | Catches all exceptions, and somewhat defeats the purpose of the extensible -- exception system. Use sparingly. +-- +-- /NOTE/ This catches all /exceptions/, but if the monad supports other ways of +-- aborting the computation, those other kinds of errors will not be caught. catchAll :: MonadCatch m => m a -> (SomeException -> m a) -> m a catchAll = catch @@ -437,36 +800,62 @@ -- | Run an action only if an exception is thrown in the main action. The -- exception is not caught, simply rethrown. +-- +-- /NOTE/ The action is only run if an /exception/ is thrown. If the monad +-- supports other ways of aborting the computation, the action won't run if +-- those other kinds of errors are thrown. See 'onError'. onException :: MonadCatch m => m a -> m b -> m a onException action handler = action `catchAll` \e -> handler >> throwM e +-- | Run an action only if an error is thrown in the main action. Unlike +-- 'onException', this works with every kind of error, not just exceptions. For +-- example, if @f@ is an 'ExceptT' computation which aborts with a 'Left', the +-- computation @onError f g@ will execute @g@, while @onException f g@ will not. +-- +-- This distinction is only meaningful for monads which have multiple exit +-- points, such as 'Except' and 'MaybeT'. For monads that only have a single +-- exit point, there is no difference between 'onException' and 'onError', +-- except that 'onError' has a more constrained type. +-- +-- @since 0.10.0 +onError :: MonadMask m => m a -> m b -> m a +onError action handler = bracketOnError (return ()) (const handler) (const action) + -- | Generalized abstracted pattern of safe resource acquisition and release --- in the face of exceptions. The first action \"acquires\" some value, which +-- in the face of errors. The first action \"acquires\" some value, which -- is \"released\" by the second action at the end. The third action \"uses\" -- the value and its result is the result of the 'bracket'. -- --- If an exception occurs during the use, the release still happens before the --- exception is rethrown. -bracket :: MonadMask m => m a -> (a -> m b) -> (a -> m c) -> m c -bracket acquire release use = mask $ \unmasked -> do - resource <- acquire - result <- unmasked (use resource) `onException` release resource - _ <- release resource - return result +-- If an error is thrown during the use, the release still happens before the +-- error is rethrown. +-- +-- Note that this is essentially a type-specialized version of +-- 'generalBracket'. This function has a more common signature (matching the +-- signature from "Control.Exception"), and is often more convenient to use. By +-- contrast, 'generalBracket' is more expressive, allowing us to implement +-- other functions like 'bracketOnError'. +bracket :: MonadMask m => m a -> (a -> m c) -> (a -> m b) -> m b +bracket acquire release = liftM fst . generalBracket + acquire + (\a _exitCase -> release a) -- | Version of 'bracket' without any value being passed to the second and -- third actions. -bracket_ :: MonadMask m => m a -> m b -> m c -> m c +bracket_ :: MonadMask m => m a -> m c -> m b -> m b bracket_ before after action = bracket before (const after) (const action) -- | Perform an action with a finalizer action that is run, even if an --- exception occurs. +-- error occurs. finally :: MonadMask m => m a -> m b -> m a finally action finalizer = bracket_ (return ()) finalizer action --- | Like 'bracket', but only performs the final action if there was an --- exception raised by the in-between computation. -bracketOnError :: MonadMask m => m a -> (a -> m b) -> (a -> m c) -> m c -bracketOnError acquire release use = mask $ \unmasked -> do - resource <- acquire - unmasked (use resource) `onException` release resource +-- | Like 'bracket', but only performs the final action if an error is +-- thrown by the in-between computation. +bracketOnError :: MonadMask m => m a -> (a -> m c) -> (a -> m b) -> m b +bracketOnError acquire release = liftM fst . generalBracket + acquire + (\a exitCase -> case exitCase of + ExitCaseSuccess _ -> return () + _ -> do + _ <- release a + return ()) diff -urN '--exclude=CVS' '--exclude=.cvsignore' '--exclude=.svn' '--exclude=.svnignore' old/exceptions-0.8.3/tests/Control/Monad/Catch/Tests.hs new/exceptions-0.10.0/tests/Control/Monad/Catch/Tests.hs --- old/exceptions-0.8.3/tests/Control/Monad/Catch/Tests.hs 2016-07-20 18:52:51.000000000 +0200 +++ new/exceptions-0.10.0/tests/Control/Monad/Catch/Tests.hs 2018-03-11 15:52:28.000000000 +0100 @@ -11,16 +11,21 @@ #endif import Control.Applicative ((<*>)) +import Control.Monad (unless) import Data.Data (Data, Typeable) +import Data.IORef (newIORef, writeIORef, readIORef) +import Control.Monad.Trans.Class (lift) import Control.Monad.Trans.Identity (IdentityT(..)) import Control.Monad.Reader (ReaderT(..)) import Control.Monad.List (ListT(..)) import Control.Monad.Trans.Maybe (MaybeT(..)) import Control.Monad.Error (ErrorT(..)) +import Control.Monad.Except (ExceptT(..), runExceptT) import Control.Monad.STM (STM, atomically) --import Control.Monad.Cont (ContT(..)) import Test.Framework (Test, testGroup) +import Test.Framework.Providers.HUnit (testCase) import Test.Framework.Providers.QuickCheck2 (testProperty) import Test.QuickCheck (Property, once) import Test.QuickCheck.Monadic (monadic, run, assert) @@ -40,20 +45,30 @@ instance Exception TestException -data MSpec = forall m. (MonadCatch m) => MSpec +data MSpec m = MSpec { mspecName :: String , mspecRunner :: (m Property -> Property) } -testMonadCatch :: MSpec -> Property -testMonadCatch MSpec { mspecRunner } = monadic mspecRunner $ +-- the @m Bool@ determines whether the @m ()@ has executed +data DetectableEffect m = DetectableEffect + { detectableEffectMSpec :: MSpec m + , detectableEffectEffectDetector :: m (m (), m Bool) + } + +data SomeMSpec = forall m. (MonadCatch m) => SomeMSpec (MSpec m) + +data SomeDetectableEffect = forall m. (MonadMask m) => SomeDetectableEffect (DetectableEffect m) + +testMonadCatch :: SomeMSpec -> Property +testMonadCatch (SomeMSpec MSpec { mspecRunner }) = monadic mspecRunner $ run $ catch failure handler where failure = throwM (TestException "foo") >> error "testMonadCatch" handler (_ :: TestException) = return () -testCatchJust :: MSpec -> Property -testCatchJust MSpec { mspecRunner } = monadic mspecRunner $ do +testCatchJust :: SomeMSpec -> Property +testCatchJust (SomeMSpec MSpec { mspecRunner }) = monadic mspecRunner $ do nice <- run $ catchJust testException posFailure posHandler assert $ nice == ("pos", True) bad <- run $ catch (catchJust testException negFailure posHandler) negHandler @@ -65,40 +80,153 @@ posFailure = throwM (TestException "pos") >> error "testCatchJust pos" negFailure = throwM (TestException "neg") >> error "testCatchJust neg" +testDetectableEffect :: SomeDetectableEffect -> Property +testDetectableEffect (SomeDetectableEffect (DetectableEffect mspec effectDetector)) = do + monadic (mspecRunner mspec) $ do + effectWasPerformed <- run $ do + (effect, detector) <- effectDetector + runExceptT $ ExceptT (return $ Left ()) `finally` lift effect + detector + assert effectWasPerformed + tests :: Test tests = testGroup "Control.Monad.Catch.Tests" $ - [ mkMonadCatch + ([ mkMonadCatch , mkCatchJust - ] <*> mspecs + ] <*> mspecs) ++ + ([ mkDetectableEffect + ] <*> detectableEffects) ++ + [ testCase "ExceptT+Left" exceptTLeft + , testCase "release error wins" releaseErrorWins + ] where mspecs = - [ MSpec "IO" io - , MSpec "IdentityT IO" $ io . runIdentityT - , MSpec "LazyState.StateT IO" $ io . flip LazyState.evalStateT () - , MSpec "StrictState.StateT IO" $ io . flip StrictState.evalStateT () - , MSpec "ReaderT IO" $ io . flip runReaderT () - , MSpec "LazyWriter.WriterT IO" $ io . fmap tfst . LazyWriter.runWriterT - , MSpec "StrictWriter.WriterT IO" $ io . fmap tfst . StrictWriter.runWriterT - , MSpec "LazyRWS.RWST IO" $ \m -> io $ fmap tfst $ LazyRWS.evalRWST m () () - , MSpec "StrictRWS.RWST IO" $ \m -> io $ fmap tfst $ StrictRWS.evalRWST m () () - - , MSpec "ListT IO" $ \m -> io $ fmap (\[x] -> x) (runListT m) - , MSpec "MaybeT IO" $ \m -> io $ fmap (maybe undefined id) (runMaybeT m) - , MSpec "ErrorT IO" $ \m -> io $ fmap (either error id) (runErrorT m) - , MSpec "STM" $ io . atomically - --, MSpec "ContT IO" $ \m -> io $ runContT m return + [ SomeMSpec mspecIO + , SomeMSpec mspecIdentityTIO + , SomeMSpec mspecLazyStateTIO + , SomeMSpec mspecStrictStateTIO + , SomeMSpec mspecReaderTIO + , SomeMSpec mspecLazyWriterTIO + , SomeMSpec mspecStrictWriterTIO + , SomeMSpec mspecLazyRWSTIO + , SomeMSpec mspecStrictRWSTIO + + , SomeMSpec mspecListTIO + , SomeMSpec mspecMaybeTIO + , SomeMSpec mspecErrorTIO + , SomeMSpec mspecSTM + --, SomeMSpec mspecContTIO - , MSpec "CatchT Identity" $ fromRight . runCatch - , MSpec "Either SomeException" fromRight + , SomeMSpec mspecCatchTIdentity + , SomeMSpec mspecEitherSomeException ] + mspecIO :: MSpec IO + mspecIO = MSpec "IO" io + + mspecIdentityTIO :: MSpec (IdentityT IO) + mspecIdentityTIO = MSpec "IdentityT IO" $ io . runIdentityT + + mspecLazyStateTIO :: MSpec (LazyState.StateT Bool IO) + mspecLazyStateTIO = MSpec "LazyState.StateT IO" $ io . flip LazyState.evalStateT False + + mspecStrictStateTIO :: MSpec (StrictState.StateT Bool IO) + mspecStrictStateTIO = MSpec "StrictState.StateT IO" $ io . flip StrictState.evalStateT False + + mspecReaderTIO :: MSpec (ReaderT () IO) + mspecReaderTIO = MSpec "ReaderT IO" $ io . flip runReaderT () + + mspecLazyWriterTIO :: MSpec (LazyWriter.WriterT () IO) + mspecLazyWriterTIO = MSpec "LazyWriter.WriterT IO" $ io . fmap tfst . LazyWriter.runWriterT + + mspecStrictWriterTIO :: MSpec (StrictWriter.WriterT () IO) + mspecStrictWriterTIO = MSpec "StrictWriter.WriterT IO" $ io . fmap tfst . StrictWriter.runWriterT + + mspecLazyRWSTIO :: MSpec (LazyRWS.RWST () () Bool IO) + mspecLazyRWSTIO = MSpec "LazyRWS.RWST IO" $ \m -> io $ fmap tfst $ LazyRWS.evalRWST m () False + + mspecStrictRWSTIO :: MSpec (StrictRWS.RWST () () Bool IO) + mspecStrictRWSTIO = MSpec "StrictRWS.RWST IO" $ \m -> io $ fmap tfst $ StrictRWS.evalRWST m () False + + mspecListTIO :: MSpec (ListT IO) + mspecListTIO = MSpec "ListT IO" $ \m -> io $ fmap (\[x] -> x) (runListT m) + + mspecMaybeTIO :: MSpec (MaybeT IO) + mspecMaybeTIO = MSpec "MaybeT IO" $ \m -> io $ fmap (maybe undefined id) (runMaybeT m) + + mspecErrorTIO :: MSpec (ErrorT String IO) + mspecErrorTIO = MSpec "ErrorT IO" $ \m -> io $ fmap (either error id) (runErrorT m) + + mspecSTM :: MSpec STM + mspecSTM = MSpec "STM" $ io . atomically + + --mspecContTIO :: MSpec (ContT () IO) + --mspecContTIO = MSpec "ContT IO" $ \m -> io $ runContT m return + + mspecCatchTIdentity :: MSpec Catch + mspecCatchTIdentity = MSpec "Catch" $ fromRight . runCatch + + mspecEitherSomeException :: MSpec (Either SomeException) + mspecEitherSomeException = MSpec "Either SomeException" fromRight + tfst :: (Property, ()) -> Property = fst fromRight (Left _) = error "fromRight" fromRight (Right a) = a io = morallyDubiousIOProperty - mkMonadCatch = mkTestType "MonadCatch" testMonadCatch - mkCatchJust = mkTestType "catchJust" testCatchJust + detectableEffects = + [ SomeDetectableEffect $ detectableEffectIO + , SomeDetectableEffect detectableEffectLazyStateTIO + , SomeDetectableEffect detectableEffectStrictStateTIO + , SomeDetectableEffect detectableEffectLazyRWSTIO + , SomeDetectableEffect detectableEffectStrictRWSTIO + ] - mkTestType name test = \spec -> - testProperty (name ++ " " ++ mspecName spec) $ once $ test spec + detectableEffectIO :: DetectableEffect IO + detectableEffectIO = DetectableEffect mspecIO $ do + ref <- newIORef False + return (writeIORef ref True, readIORef ref) + + detectableEffectLazyStateTIO :: DetectableEffect (LazyState.StateT Bool IO) + detectableEffectLazyStateTIO = DetectableEffect mspecLazyStateTIO $ do + LazyState.put False + return (LazyState.put True, LazyState.get) + + detectableEffectStrictStateTIO :: DetectableEffect (StrictState.StateT Bool IO) + detectableEffectStrictStateTIO = DetectableEffect mspecStrictStateTIO $ do + StrictState.put False + return (StrictState.put True, StrictState.get) + + detectableEffectLazyRWSTIO :: DetectableEffect (LazyRWS.RWST () () Bool IO) + detectableEffectLazyRWSTIO = DetectableEffect mspecLazyRWSTIO $ do + LazyRWS.put False + return (LazyRWS.put True, LazyRWS.get) + + detectableEffectStrictRWSTIO :: DetectableEffect (StrictRWS.RWST () () Bool IO) + detectableEffectStrictRWSTIO = DetectableEffect mspecStrictRWSTIO $ do + StrictRWS.put False + return (StrictRWS.put True, StrictRWS.get) + + mkMonadCatch = mkMSpecTest "MonadCatch" testMonadCatch + mkCatchJust = mkMSpecTest "catchJust" testCatchJust + mkDetectableEffect = mkDetectableEffectTest "effect during release" testDetectableEffect + + mkMSpecTest :: String -> (SomeMSpec -> Property) -> SomeMSpec -> Test + mkMSpecTest name test = \someMSpec@(SomeMSpec spec) -> + testProperty (name ++ " " ++ mspecName spec) $ once $ test someMSpec + + mkDetectableEffectTest :: String -> (SomeDetectableEffect -> Property) -> SomeDetectableEffect -> Test + mkDetectableEffectTest name test = \someDetectableEffect@(SomeDetectableEffect detectableEffect) -> + let testName = name ++ " " ++ mspecName (detectableEffectMSpec detectableEffect) + in testProperty testName $ once $ test someDetectableEffect + + exceptTLeft = do + ref <- newIORef False + Left () <- runExceptT $ ExceptT (return $ Left ()) `finally` lift (writeIORef ref True) + val <- readIORef ref + unless val $ error "Looks like cleanup didn't happen" + + -- if both 'use' and 'release' abort, the 'release' error should win + releaseErrorWins = do + Left val <- runExceptT $ ExceptT (return $ Left False) `finally` ExceptT (return $ Left True) + unless val $ error "Looks like the 'use' error won" ++++++ exceptions.cabal ++++++ --- /var/tmp/diff_new_pack.wGF6RG/_old 2018-07-24 17:18:33.635005486 +0200 +++ /var/tmp/diff_new_pack.wGF6RG/_new 2018-07-24 17:18:33.635005486 +0200 @@ -1,7 +1,7 @@ name: exceptions category: Control, Exceptions, Monad -version: 0.8.3 -x-revision: 5 +version: 0.10.0 +x-revision: 1 cabal-version: >= 1.8 license: BSD3 license-file: LICENSE @@ -13,9 +13,9 @@ copyright: Copyright (C) 2013-2015 Edward A. Kmett Copyright (C) 2012 Google Inc. build-type: Simple -tested-with: GHC == 7.4.2, GHC == 7.6.3, GHC == 7.8.4, GHC == 7.10.0.20150307 +tested-with: GHC == 7.0.4, GHC == 7.2.2, GHC == 7.4.2, GHC == 7.6.3, GHC == 7.8.4, GHC == 7.10.3, GHC == 8.0.2, GHC == 8.2.2, GHC == 8.4.1 synopsis: Extensible optionally-pure exceptions -description: Extensible optionally-pure exceptions +description: Extensible optionally-pure exceptions. extra-source-files: .travis.yml @@ -36,7 +36,7 @@ build-depends: base >= 4.3 && < 5, stm >= 2.2 && < 3, - template-haskell >= 2.2 && < 2.14, + template-haskell >= 2.2 && < 2.15, transformers >= 0.2 && < 0.6, transformers-compat >= 0.3 && < 0.7, mtl >= 2.0 && < 2.3 @@ -50,7 +50,10 @@ test-suite exceptions-tests main-is: Tests.hs - other-modules: Control.Monad.Catch.Tests + other-modules: + Control.Monad.Catch + Control.Monad.Catch.Pure + Control.Monad.Catch.Tests hs-source-dirs: src, tests ghc-options: -Wall -fwarn-tabs type: exitcode-stdio-1.0 @@ -60,7 +63,8 @@ template-haskell, transformers, transformers-compat, - mtl, + mtl >= 2.2, test-framework >= 0.8 && < 0.9, + test-framework-hunit >= 0.3 && < 0.4, test-framework-quickcheck2 >= 0.3 && < 0.4, QuickCheck >= 2.5 && < 2.12
