[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-dev] PRELIMINARY [PATCH 2/2] Remove a redundant instance declaration.



> We use the main repositories only for released versions.  FOr
> development branches, we use pesonal repositories.  Most people find
> that it's more convenient to use a service like github or gitorious or
> something to host their own branches, and then ask for review and
> merging when the branches are ready for review and merging.

> It's okay to send stuff to the list, but linking to a public branch
> and listing the commit IDs usually works as well as sending patches.

I don't have a server that I can physically control, and I'm trying to
avoid third party services.  Most of them use freedom-denying
software.  Moreover, companies might decide to shut them down for some
reason.  Imagine that I also won't be able to provde the needed code
in that case.  That would be very frustrating.

Lists are usually mirrored, plus subscribers might have local copies.
That's why I'd prefer to send patches to the list.  If anyone finds
that inconvenient, I'll try to find a workaround.

I'm attaching two preliminary patches.  I'm more confident about them
than the previous ones.  Still, I can't build the program or run the
tests, so you shouldn't expect them to work.

'0002-Remove-a-redundant-instance-declaration.patch' is
self-explanatory.  (I set 'network' to '>=2.4.0.0' because it's the
first version that derives 'Ord'.)

If you want to test the other patch, copy the functions to 'Test.hs'
and run the following:

$ ghc --version
The Glorious Glasgow Haskell Compilation System, version 7.6.3
$ ghci Test.hs
*Main> import Data.Dynamic (toDyn, fromDynamic)
*Main Data.Dynamic> E.tryJust syncExceptions $ E.throwIO E.DivideByZero
Left divide by zero
*Main Data.Dynamic> E.tryJust syncExceptions $ E.throwIO E.StackOverflow
*** Exception: stack overflow
*Main Data.Dynamic> ignoreJust syncExceptions $ E.throwIO E.StackOverflow
*** Exception: stack overflow
*Main Data.Dynamic> ignoreJust syncExceptions $ E.throwIO E.DivideByZero
*Main Data.Dynamic> showException [fromDynamic] $ E.toException $ toDyn "foo"
"foo"
*Main Data.Dynamic> showException [fromDynamic] $ E.toException E.DivideByZero
"divide by zero"
*Main Data.Dynamic> showException [fromDynamic, (return . show)] $ E.toException $ toDyn "foo"
"foo"
*Main Data.Dynamic> showException [fromDynamic, (return . show)] $ E.toException $ E.DivideByZero
"divide by zero"

If you want to test the original code (using GHC 6.10.4), you should
adjust some functions:

*Main> showException [fromDynamic] $ E.DynException $ toDyn "foo"
"foo"
*Main> showException [fromDynamic] $ E.ArithException E.DivideByZero
"divide by zero"
*Main> showException [fromDynamic, (return . show)] $ E.DynException $ toDyn "foo"
"foo"
*Main> showException [fromDynamic, (return . show)] $ E.ArithException E.DivideByZero
"divide by zero"

Did I forget any corner cases?

I tried to write tests for the above, but it didn't work well with
HUnit.  Should I try QuickCheck?  I guess we don't want to depend on
two testing libraries, do we?

If you apply the attached patches on top of [1] and [2], you should get
the following:

$ ./Setup.lhs configure --user
$ ./Setup.lhs build

[...]

[ 4 of 39] Compiling TorDNSEL.Util    ( dist/build/tordnsel/tordnsel-tmp/TorDNSEL/Util.hs, dist/build/tordnsel/tordnsel-tmp/TorDNSEL/Util.o )

src/TorDNSEL/Util.hsc:143:1: Warning:
    Module `GHC.IOBase' is deprecated: use GHC.IO instead

src/TorDNSEL/Util.hsc:380:47: Warning:
    In the use of `B.findSubstrings'
    (imported from Data.ByteString.Char8, but defined in Data.ByteString):
    Deprecated: "findSubstrings is deprecated in favour of breakSubstring."
[ 7 of 39] Compiling TorDNSEL.Random  ( src/TorDNSEL/Random.hs, dist/build/tordnsel/tordnsel-tmp/TorDNSEL/Random.o )

src/TorDNSEL/Random.hs:39:1:
    Unacceptable argument type in foreign declaration: CInt
    When checking declaration:
      foreign import ccall unsafe "static openssl/rand.h RAND_bytes" c_RAND_bytes
        :: Ptr Word8 -> CInt -> IO CInt

src/TorDNSEL/Random.hs:39:1:
    Unacceptable result type in foreign declaration: IO CInt
    When checking declaration:
      foreign import ccall unsafe "static openssl/rand.h RAND_bytes" c_RAND_bytes
        :: Ptr Word8 -> CInt -> IO CInt

src/TorDNSEL/Random.hs:59:1:
    Unacceptable argument type in foreign declaration: CInt
    When checking declaration:
      foreign import ccall unsafe "static openssl/rand.h RAND_seed" c_RAND_seed
        :: Ptr Word8 -> CInt -> IO ()

src/TorDNSEL/Random.hs:65:1:
    Unacceptable result type in foreign declaration: IO CInt
    When checking declaration:
      foreign import ccall unsafe "static openssl/rand.h RAND_status" c_RAND_status
        :: IO CInt

It's also necessary to fix exceptions in other modules (run 'grep -r
Exception src').

Any comments?

[1] http://lists.torproject.org/pipermail/tor-dev/attachments/20130622/3e84acaf/attachment.patch
[2] http://lists.torproject.org/pipermail/tor-dev/attachments/20130619/34418869/attachment.patch

From a91d70a90e354d569e00d93305dcda51b05eca16 Mon Sep 17 00:00:00 2001
From: Nikita Karetnikov <nikita@xxxxxxxxxxxxxx>
Date: Mon, 8 Jul 2013 09:06:21 +0000
Subject: [PATCH 1/2] Replace 'TorDNSEL.Compat.Exception' with
 'Control.Exception'.

* src/TorDNSEL/Util.hsc (ignoreJust, syncExceptions, showException):
  Adjust to work with 'Control.Exception'.
---
 src/TorDNSEL/Util.hsc |   21 ++++++++++++---------
 1 file changed, 12 insertions(+), 9 deletions(-)

diff --git a/src/TorDNSEL/Util.hsc b/src/TorDNSEL/Util.hsc
index 92c7ca3..5d38468 100644
--- a/src/TorDNSEL/Util.hsc
+++ b/src/TorDNSEL/Util.hsc
@@ -101,7 +101,7 @@ module TorDNSEL.Util (
   ) where
 
 import Control.Arrow ((&&&), first, second)
-import qualified TorDNSEL.Compat.Exception as E
+import qualified Control.Exception as E
 import Control.Monad.Error
   (Error(..), MonadError(..), MonadTrans(..), MonadIO(..))
 import qualified Control.Monad.State as State
@@ -328,13 +328,14 @@ split :: Int -> ByteString -> [ByteString]
 split x = takeWhile (not . B.null) . map (B.take x) . iterate (B.drop x)
 
 -- | Catch and discard exceptions matching the predicate.
-ignoreJust :: (E.Exception -> Maybe a) -> IO () -> IO ()
+ignoreJust :: (E.SomeException -> Maybe a) -> IO () -> IO ()
 ignoreJust p = E.handleJust p . const . return $ ()
 
 -- | A predicate matching synchronous exceptions.
-syncExceptions :: E.Exception -> Maybe E.Exception
-syncExceptions (E.AsyncException _) = Nothing
-syncExceptions e                    = Just e
+syncExceptions :: E.SomeException -> Maybe E.SomeException
+syncExceptions e = case E.fromException e :: Maybe E.AsyncException of
+  Nothing -> Just e
+  Just _  -> Nothing
 
 -- | Print a usage message to the given handle and exit with the given code.
 exitUsage :: Handle -> ExitCode -> IO a
@@ -385,10 +386,12 @@ splitByDelimiter delimiter bs = subst (-len : B.findSubstrings delimiter bs)
 
 -- | Convert an exception to a string given a list of functions for displaying
 -- dynamically typed exceptions.
-showException :: [Dynamic -> Maybe String] -> E.Exception -> String
-showException fs (E.DynException dyn)
-  | str:_ <- mapMaybe ($ dyn) fs = str
-showException _ e                = show e
+showException :: [Dynamic -> Maybe String] -> E.SomeException -> String
+showException fs e
+  | Just e' <- E.fromException e :: Maybe Dynamic
+  , str:_   <- mapMaybe ($ e') fs
+  = str
+  | otherwise = show e
 
 -- | Convert a 'UTCTime' to a string in ISO 8601 format.
 showUTCTime :: UTCTime -> String
-- 
1.7.9.5

From 18eb751288df992f37367134f5d9f63eafd37b45 Mon Sep 17 00:00:00 2001
From: Nikita Karetnikov <nikita@xxxxxxxxxxxxxx>
Date: Mon, 8 Jul 2013 09:04:36 +0000
Subject: [PATCH 2/2] Remove a redundant instance declaration.

* tordnsel.cabal (Build-Depends): Change the required version of 'network'.
* src/TorDNSEL/Util.hsc (instance Ord SockAddr): Remove it.
---
 src/TorDNSEL/Util.hsc |    9 ---------
 tordnsel.cabal        |    2 +-
 2 files changed, 1 insertion(+), 10 deletions(-)

diff --git a/src/TorDNSEL/Util.hsc b/src/TorDNSEL/Util.hsc
index 5d38468..0ba7365 100644
--- a/src/TorDNSEL/Util.hsc
+++ b/src/TorDNSEL/Util.hsc
@@ -440,15 +440,6 @@ bindListeningUnixDomainStreamSocket sockPath mode = do
     listen sock sOMAXCONN
     return sock
 
-instance Ord SockAddr where
-  SockAddrInet port1 addr1 `compare` SockAddrInet port2 addr2 =
-    case addr1 `compare` addr2 of
-      EQ    -> port1 `compare` port2
-      other -> other
-  SockAddrUnix path1 `compare` SockAddrUnix path2 = path1 `compare` path2
-  SockAddrInet _ _ `compare` SockAddrUnix _ = LT
-  SockAddrUnix _ `compare` SockAddrInet _ _ = GT
-
 --------------------------------------------------------------------------------
 -- Monads
 
diff --git a/tordnsel.cabal b/tordnsel.cabal
index 09a273e..6268544 100644
--- a/tordnsel.cabal
+++ b/tordnsel.cabal
@@ -22,7 +22,7 @@ Flag Devel
   Default:     False
 
 Executable         tordnsel
-  Build-Depends:   base>=2.0, network>=2.0, mtl>=1.0, unix>=1.0, stm>=2.0,
+  Build-Depends:   base>=2.0, network>=2.4.0.0, mtl>=1.0, unix>=1.0, stm>=2.0,
     time>=1.0, binary>=0.4, bytestring>=0.9, array>=0.1,
     directory>=1.0, containers>=0.1
   Main-Is:         tordnsel.hs
-- 
1.7.9.5

Attachment: pgpL0J1R5QvKl.pgp
Description: PGP signature

_______________________________________________
tor-dev mailing list
tor-dev@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-dev