[diffoscope] 01/02: comparators/haskell.py: Tidy

Chris Lamb chris at chris-lamb.co.uk
Fri Jan 20 03:55:08 CET 2017


This is an automated email from the git hooks/post-receive script.

lamby pushed a commit to branch master
in repository diffoscope.

commit 6e67a13d0f31c41fb2cbd9618692c86f985bf6d7
Author: Chris Lamb <lamby at debian.org>
Date:   Fri Jan 20 13:51:50 2017 +1100

    comparators/haskell.py: Tidy
    
    Gbp-Dch: ignore
    
    Signed-off-by: Chris Lamb <lamby at debian.org>
---
 diffoscope/comparators/haskell.py | 65 ++++++++++++++++++++++++++-------------
 1 file changed, 43 insertions(+), 22 deletions(-)

diff --git a/diffoscope/comparators/haskell.py b/diffoscope/comparators/haskell.py
index 21cd724..09e9b80 100644
--- a/diffoscope/comparators/haskell.py
+++ b/diffoscope/comparators/haskell.py
@@ -30,6 +30,14 @@ from diffoscope.difference import Difference
 from .utils.file import File
 from .utils.command import Command
 
+HI_MAGIC_32 = struct.pack('>I', 0x1face)
+HI_MAGIC_64 = struct.pack('>I', 0x1face64)
+
+if platform.architecture()[0] == '32bit':
+    HI_MAGIC = HI_MAGIC_32
+else:
+    HI_MAGIC = HI_MAGIC_64
+
 logger = logging.getLogger(__name__)
 
 
@@ -38,17 +46,10 @@ class ShowIface(Command):
     def cmdline(self):
         return ['ghc', '--show-iface', self.path]
 
-
-HI_MAGIC_32 = struct.pack('>I', 0x1face)
-HI_MAGIC_64 = struct.pack('>I', 0x1face64)
-if platform.architecture()[0] == '32bit':
-    HI_MAGIC = HI_MAGIC_32
-else:
-    HI_MAGIC = HI_MAGIC_64
-
 class HiFile(File):
     """
     Here is how an example .hi file starts:
+
     % hexdump -C tests/data/test1.hi | head -n 1
     00000000  01 fa ce 64 00 00 00 00  00 00 00 00 04 00 00 00  |...d............|
               ~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~ ~~ ~~~~~~~~~~
@@ -64,9 +65,8 @@ class HiFile(File):
                   int('1')     int('0')    int('3')
 
 
-    So the version of this file has 4 characters, and it's 7103.
-    Note how all this information is stored as big endian.
-
+    So the version of this file has 4 characters, and it's 7103. Note how all
+    this information is stored as big endian.
     """
     RE_FILE_EXTENSION = re.compile(r'\.(p_|dyn_)?hi$')
 
@@ -74,32 +74,45 @@ class HiFile(File):
     def recognizes(file):
         if not HiFile.RE_FILE_EXTENSION.search(file.name):
             return False
+
         if not hasattr(HiFile, 'hi_version'):
             try:
                 with profile('command', 'ghc'):
-                    output = subprocess.check_output(['ghc', '--numeric-version'], shell=False)
-                major, minor, patch = map(int, output.decode('utf-8').strip().split('.'))
-                HiFile.hi_version = "%d%02d%d" % (major, minor, patch)
-                logger.debug('Found .hi version %s', HiFile.hi_version)
+                    output = subprocess.check_output(
+                        ['ghc', '--numeric-version'],
+                    )
+                major, minor, patch = [
+                    int(x) for x in output.decode('utf-8').strip().split('.')
+                ]
+                HiFile.hi_version = '%d%02d%d' % (major, minor, patch)
+                logger.debug("Found .hi version %s", HiFile.hi_version)
             except OSError:
                 HiFile.hi_version = None
-                logger.debug('Unable to read GHC version')
+                logger.debug("Unable to read GHC version")
+
         if HiFile.hi_version is None:
             return False
 
         with open(file.path, 'rb') as fp:
-            # read magic
+            # Read magic
             buf = fp.read(4)
             if buf != HI_MAGIC:
-                logger.debug('Haskell interface magic mismatch. Found %r instead of %r or %r', buf, HI_MAGIC_32, HI_MAGIC_64)
+                logger.debug(
+                    "Haskell interface magic mismatch. "
+                    "Found %r instead of %r or %r",
+                    buf, HI_MAGIC_32, HI_MAGIC_64,
+                )
                 return False
-            # skip some old descriptor thingy that has varying size
+
+            # Skip some old descriptor thingy that has varying size
             if buf == HI_MAGIC_32:
                 fp.read(4)
             elif buf == HI_MAGIC_64:
                 fp.read(8)
+
             # Read version, which is [Char]
             buf = fp.read(1)
+
             # Small list optimisation - anything less than 0xff has its length
             # in a single byte; everything else is 0xff followed by the 32-bit
             # length (big-endian).
@@ -108,12 +121,20 @@ class HiFile(File):
                 length = struct.unpack('>I', buf)[0]
             else:
                 length = buf[0]
+
             # Now read characters; each is 32-bit big-endian.
-            version_found = ''.join([chr(struct.unpack('>I', fp.read(4))[0]) for _ in range(length)])
+            version_found = ''.join(
+                chr(struct.unpack('>I', fp.read(4))[0]) for _ in range(length)
+            )
+
             if version_found != HiFile.hi_version:
-                logger.debug('Haskell version mismatch. Found %s instead of %s.',
-                             version_found, HiFile.hi_version)
+                logger.debug(
+                    "Haskell version mismatch; found %s instead of %s.",
+                    version_found,
+                    HiFile.hi_version,
+                )
                 return False
+
             return True
 
     def compare_details(self, other, source=None):

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/reproducible/diffoscope.git


More information about the diffoscope mailing list