Browse Source

Add 'flakes' target invoking pyflakes, and fix some pyflakes warnings.

Jelmer Vernooij 10 years ago
parent
commit
4265383895

+ 4 - 0
Makefile

@@ -1,4 +1,5 @@
 PYTHON = python
+PYFLAKES = pyflakes
 SETUP = $(PYTHON) setup.py
 PYDOCTOR ?= pydoctor
 ifeq ($(shell $(PYTHON) -c "import sys; print(sys.version_info >= (2, 7))"),True)
@@ -47,3 +48,6 @@ check-all: check check-pypy check-noextensions
 clean::
 	$(SETUP) clean --all
 	rm -f dulwich/*.so
+
+flakes:
+	$(PYFLAKES) dulwich

+ 4 - 4
dulwich/file.py

@@ -37,7 +37,7 @@ def fancy_rename(oldname, newname):
     if not os.path.exists(newname):
         try:
             os.rename(oldname, newname)
-        except OSError as e:
+        except OSError:
             raise
         return
 
@@ -46,17 +46,17 @@ def fancy_rename(oldname, newname):
         (fd, tmpfile) = tempfile.mkstemp(".tmp", prefix=oldname+".", dir=".")
         os.close(fd)
         os.remove(tmpfile)
-    except OSError as e:
+    except OSError:
         # either file could not be created (e.g. permission problem)
         # or could not be deleted (e.g. rude virus scanner)
         raise
     try:
         os.rename(newname, tmpfile)
-    except OSError as e:
+    except OSError:
         raise   # no rename occurred
     try:
         os.rename(oldname, newname)
-    except OSError as e:
+    except OSError:
         os.rename(tmpfile, newname)
         raise
     os.remove(tmpfile)

+ 1 - 1
dulwich/index.py

@@ -105,7 +105,7 @@ def read_cache_entry(f):
     name = f.read((flags & 0x0fff))
     # Padding:
     real_size = ((f.tell() - beginoffset + 8) & ~7)
-    data = f.read((beginoffset + real_size) - f.tell())
+    f.read((beginoffset + real_size) - f.tell())
     return IndexEntry(name, ctime, mtime, dev, ino, mode, uid, gid, size,
                       sha_to_hex(sha), flags & ~0x0fff)
 

+ 1 - 1
dulwich/objects.py

@@ -398,7 +398,7 @@ class ShaFile(object):
             obj._needs_serialization = True
             obj._file = f
             return obj
-        except (IndexError, ValueError) as e:
+        except (IndexError, ValueError):
             raise ObjectFormatException("invalid object header")
 
     @staticmethod

+ 0 - 1
dulwich/repo.py

@@ -30,7 +30,6 @@ local disk (Repo).
 from io import BytesIO
 import errno
 import os
-import stat
 
 from dulwich.errors import (
     NoIndexPresent,

+ 2 - 2
dulwich/server.py

@@ -716,7 +716,7 @@ class ReceivePackHandler(Handler):
             # TODO: more informative error messages than just the exception string
             try:
                 recv = getattr(self.proto, "recv", None)
-                p = self.repo.object_store.add_thin_pack(self.proto.read, recv)
+                self.repo.object_store.add_thin_pack(self.proto.read, recv)
                 status.append(('unpack', 'ok'))
             except all_exceptions as e:
                 status.append(('unpack', str(e).replace('\n', '')))
@@ -917,7 +917,7 @@ def serve_command(handler_cls, argv=sys.argv, backend=None, inf=sys.stdin,
 def generate_info_refs(repo):
     """Generate an info refs file."""
     refs = repo.get_refs()
-    return write_info_refs(repo.get_refs(), repo.object_store)
+    return write_info_refs(refs, repo.object_store)
 
 
 def generate_objects_info_packs(repo):

+ 3 - 7
dulwich/tests/__init__.py

@@ -27,13 +27,9 @@ import sys
 import tempfile
 
 
-if sys.version_info >= (2, 7):
-    # If Python itself provides an exception, use that
-    import unittest
-    from unittest import SkipTest, skipIf, TestCase as _TestCase
-else:
-    import unittest2 as unittest
-    from unittest2 import SkipTest, skipIf, TestCase as _TestCase
+# If Python itself provides an exception, use that
+import unittest
+from unittest import TestCase as _TestCase
 
 
 def get_safe_env(env=None):

+ 0 - 2
dulwich/tests/compat/server_utils.py

@@ -21,11 +21,9 @@
 
 import errno
 import os
-import select
 import shutil
 import socket
 import tempfile
-import threading
 
 from dulwich.repo import Repo
 from dulwich.objects import hex_to_sha

+ 1 - 1
dulwich/tests/compat/test_client.py

@@ -32,6 +32,7 @@ import tarfile
 import tempfile
 import threading
 import urllib
+from unittest import SkipTest
 
 from dulwich import (
     client,
@@ -44,7 +45,6 @@ from dulwich import (
     )
 from dulwich.tests import (
     get_safe_env,
-    SkipTest,
     )
 
 from dulwich.tests.compat.utils import (

+ 2 - 1
dulwich/tests/compat/test_utils.py

@@ -19,8 +19,9 @@
 
 """Tests for git compatibility utilities."""
 
+from unittest import SkipTest
+
 from dulwich.tests import (
-    SkipTest,
     TestCase,
     )
 from dulwich.tests.compat import utils

+ 3 - 3
dulwich/tests/compat/test_web.py

@@ -25,14 +25,14 @@ warning: these tests should be fairly stable, but when writing/debugging new
 """
 
 import threading
+from unittest import (
+    SkipTest,
+    )
 from wsgiref import simple_server
 
 from dulwich.server import (
     DictBackend,
     )
-from dulwich.tests import (
-    SkipTest,
-    )
 from dulwich.web import (
     make_wsgi_chain,
     HTTPGitApplication,

+ 1 - 1
dulwich/tests/compat/utils.py

@@ -25,13 +25,13 @@ import socket
 import subprocess
 import tempfile
 import time
+from unittest import SkipTest
 
 from dulwich.repo import Repo
 from dulwich.protocol import TCP_GIT_PORT
 
 from dulwich.tests import (
     get_safe_env,
-    SkipTest,
     TestCase,
     )
 

+ 2 - 2
dulwich/tests/test_client.py

@@ -206,7 +206,7 @@ class GitClientTests(TestCase):
             return {}
 
         f = BytesIO()
-        empty_pack = write_pack_objects(f, {})
+        write_pack_objects(f, {})
         self.client.send_pack('/', determine_wants, generate_pack_contents)
         self.assertIn(
             self.rout.getvalue(),
@@ -248,7 +248,7 @@ class GitClientTests(TestCase):
             return [(commit, None), (tree, ''), ]
 
         f = BytesIO()
-        pack = write_pack_objects(f, generate_pack_contents(None, None))
+        write_pack_objects(f, generate_pack_contents(None, None))
         self.client.send_pack('/', determine_wants, generate_pack_contents)
         self.assertIn(
             self.rout.getvalue(),

+ 0 - 1
dulwich/tests/test_config.py

@@ -22,7 +22,6 @@ from io import BytesIO
 from dulwich.config import (
     ConfigDict,
     ConfigFile,
-    OrderedDict,
     StackedConfig,
     _check_section_name,
     _check_variable_name,

+ 0 - 1
dulwich/tests/test_diff_tree.py

@@ -872,7 +872,6 @@ class RenameDetectionTest(DiffTestCase):
         blob_c2 = make_object(Blob, data='a\nb\nc\ne\n')
         tree1 = self.commit_tree([('a', blob_a1), ('b', blob_b)])
         tree2 = self.commit_tree([('c', blob_c2), ('b', blob_b)])
-        detector = RenameDetector(self.store)
         self.assertEqual(
           [TreeChange(CHANGE_RENAME, ('a', F, blob_a1.id),
                       ('c', F, blob_c2.id))],

+ 1 - 1
dulwich/tests/test_fastexport.py

@@ -19,6 +19,7 @@
 
 from io import BytesIO
 import stat
+from unittest import SkipTest
 
 
 from dulwich.object_store import (
@@ -33,7 +34,6 @@ from dulwich.repo import (
     MemoryRepo,
     )
 from dulwich.tests import (
-    SkipTest,
     TestCase,
     )
 from dulwich.tests.utils import (

+ 5 - 3
dulwich/tests/test_file.py

@@ -17,15 +17,15 @@
 # MA  02110-1301, USA.
 
 import errno
+import io
 import os
 import shutil
 import sys
 import tempfile
-import io
+from unittest import SkipTest
 
 from dulwich.file import GitFile, fancy_rename
 from dulwich.tests import (
-    SkipTest,
     TestCase,
     )
 
@@ -59,7 +59,7 @@ class FancyRenameTests(TestCase):
         new_f = open(self.bar, 'rb')
         self.assertEqual('foo contents', new_f.read())
         new_f.close()
-         
+
     def test_dest_exists(self):
         self.create(self.bar, 'bar contents')
         fancy_rename(self.foo, self.bar)
@@ -158,6 +158,8 @@ class GitFileTests(TestCase):
             self.fail()
         except OSError as e:
             self.assertEqual(errno.EEXIST, e.errno)
+        else:
+            f2.close()
         f1.write(' contents')
         f1.close()
 

+ 2 - 1
dulwich/tests/test_greenthreads.py

@@ -23,7 +23,6 @@ import time
 
 from dulwich.tests import (
     TestCase,
-    skipIf,
     )
 from dulwich.object_store import (
     MemoryObjectStore,
@@ -36,6 +35,8 @@ from dulwich.objects import (
     parse_timezone,
     )
 
+from unittest import skipIf
+
 try:
     import gevent
     gevent_support = True

+ 0 - 1
dulwich/tests/test_hooks.py

@@ -21,7 +21,6 @@ import os
 import stat
 import shutil
 import tempfile
-import warnings
 
 from dulwich import errors
 

+ 1 - 1
dulwich/tests/test_index.py

@@ -319,7 +319,7 @@ class BuildIndexTests(TestCase):
             sorted(os.listdir(os.path.join(repo.path, 'c'))))
 
 
-class BuildIndexTests(TestCase):
+class GetUnstagedChangesTests(TestCase):
 
     def test_get_unstaged_changes(self):
         """Unit test for get_unstaged_changes."""

+ 2 - 2
dulwich/tests/test_lru_cache.py

@@ -102,7 +102,7 @@ class TestLRUCache(TestCase):
         def cleanup_func(key, val):
             cleanup_called.append((key, val))
 
-        cache = lru_cache.LRUCache(max_cache=2)
+        cache = lru_cache.LRUCache(max_cache=2, after_cleanup_count=2)
 
         cache.add('baz', '1', cleanup=cleanup_func)
         cache.add('foo', '2', cleanup=cleanup_func)
@@ -188,7 +188,7 @@ class TestLRUCache(TestCase):
         # By default _after_cleanup_size is 80% of the normal size
         self.assertEqual(4, cache._after_cleanup_count)
 
-    def test_cleanup(self):
+    def test_cleanup_2(self):
         cache = lru_cache.LRUCache(max_cache=5, after_cleanup_count=2)
 
         # Add these in order

+ 0 - 4
dulwich/tests/test_objectspec.py

@@ -24,9 +24,6 @@
 
 from dulwich.objects import (
     Blob,
-    Commit,
-    Tag,
-    Tree,
     )
 from dulwich.objectspec import (
     parse_object,
@@ -38,7 +35,6 @@ from dulwich.tests import (
     )
 from dulwich.tests.utils import (
     build_commit_graph,
-    make_object,
     )
 
 

+ 4 - 8
dulwich/tests/test_pack.py

@@ -37,7 +37,6 @@ from dulwich.object_store import (
     MemoryObjectStore,
     )
 from dulwich.objects import (
-    Blob,
     hex_to_sha,
     sha_to_hex,
     Commit,
@@ -47,7 +46,6 @@ from dulwich.objects import (
 from dulwich.pack import (
     OFS_DELTA,
     REF_DELTA,
-    DELTA_TYPES,
     MemoryPackIndex,
     Pack,
     PackData,
@@ -60,7 +58,6 @@ from dulwich.pack import (
     write_pack_header,
     write_pack_index_v1,
     write_pack_index_v2,
-    SHA1Writer,
     write_pack_object,
     write_pack,
     unpack_object,
@@ -187,7 +184,7 @@ class TestPackData(PackTests):
     """Tests getting the data from the packfile."""
 
     def test_create_pack(self):
-        p = self.get_pack_data(pack1_sha)
+        self.get_pack_data(pack1_sha)
 
     def test_from_file(self):
         path = os.path.join(self.datadir, 'pack-%s.pack' % pack1_sha)
@@ -408,7 +405,7 @@ class TestPack(PackTests):
         self.assertRaises(ChecksumMismatch, lambda:
                           bad_pack.check_length_and_checksum())
 
-    def test_iterobjects(self):
+    def test_iterobjects_2(self):
         p = self.get_pack(pack1_sha)
         objs = dict((o.id, o) for o in p.iterobjects())
         self.assertEqual(3, len(objs))
@@ -490,7 +487,6 @@ class WritePackTests(TestCase):
 
         f.write('x')  # unpack_object needs extra trailing data.
         f.seek(offset)
-        comp_len = len(f.getvalue()) - offset - 1
         unpacked, unused = unpack_object(f.read, compute_crc32=True)
         self.assertEqual(Blob.type_num, unpacked.pack_type_num)
         self.assertEqual(Blob.type_num, unpacked.obj_type_num)
@@ -998,7 +994,7 @@ class DeltaChainIteratorTests(TestCase):
     def test_bad_ext_ref_non_thin_pack(self):
         blob, = self.store_blobs(['blob'])
         f = BytesIO()
-        entries = build_pack(f, [(REF_DELTA, (blob.id, 'blob1'))],
+        build_pack(f, [(REF_DELTA, (blob.id, 'blob1'))],
                              store=self.store)
         pack_iter = self.make_pack_iter(f, thin=False)
         try:
@@ -1010,7 +1006,7 @@ class DeltaChainIteratorTests(TestCase):
     def test_bad_ext_ref_thin_pack(self):
         b1, b2, b3 = self.store_blobs(['foo', 'bar', 'baz'])
         f = BytesIO()
-        entries = build_pack(f, [
+        build_pack(f, [
           (REF_DELTA, (1, 'foo99')),
           (REF_DELTA, (b1.id, 'foo1')),
           (REF_DELTA, (b2.id, 'bar2')),

+ 1 - 1
dulwich/tests/test_patch.py

@@ -19,6 +19,7 @@
 """Tests for patch.py."""
 
 from io import BytesIO
+from unittest import SkipTest
 
 from dulwich.objects import (
     Blob,
@@ -37,7 +38,6 @@ from dulwich.patch import (
     write_tree_diff,
     )
 from dulwich.tests import (
-    SkipTest,
     TestCase,
     )
 

+ 1 - 2
dulwich/tests/test_porcelain.py

@@ -271,7 +271,6 @@ class SymbolicRefTests(PorcelainTestCase):
             [3, 1, 2]])
         self.repo.refs["HEAD"] = c3.id
 
-        outstream = BytesIO()
         self.assertRaises(ValueError, porcelain.symbolic_ref, self.repo.path, 'foobar')
 
     def test_set_force_wrong_symbolic_ref(self):
@@ -374,7 +373,7 @@ class TagTests(PorcelainTestCase):
 
         tags = self.repo.refs.as_dict("refs/tags")
         self.assertEqual(tags.keys(), ["tryme"])
-        tag = self.repo['refs/tags/tryme']
+        self.repo['refs/tags/tryme']
         self.assertEqual(tags.values(), [self.repo.head()])
 
 

+ 2 - 1
dulwich/tests/test_repository.py

@@ -502,7 +502,8 @@ exit 1
 
         warnings.simplefilter("always", UserWarning)
         self.addCleanup(warnings.resetwarnings)
-        warnings_list = setup_warning_catcher()
+        warnings_list, restore_warnings = setup_warning_catcher()
+        self.addCleanup(restore_warnings)
 
         commit_sha2 = r.do_commit(
             'empty commit',

+ 1 - 3
dulwich/tests/test_walk.py

@@ -23,10 +23,8 @@ from itertools import (
     )
 
 from dulwich.diff_tree import (
-    CHANGE_ADD,
     CHANGE_MODIFY,
     CHANGE_RENAME,
-    CHANGE_COPY,
     TreeChange,
     RenameDetector,
     )
@@ -404,7 +402,7 @@ class WalkerTest(TestCase):
         #   \          /
         #    \-y3--y4-/--y5
         # Due to skew, y5 is the oldest commit.
-        c1, x2, y3, y4, y5, m6 = cs = self.make_commits(
+        c1, x2, y3, y4, y5, m6 = self.make_commits(
           [[1], [2, 1], [3, 1], [4, 3], [5, 4], [6, 2, 4]],
           times=[2, 3, 4, 5, 1, 6])
         self.assertWalkYields([m6, y4, y3, x2, c1], [m6.id])

+ 1 - 1
dulwich/tests/test_web.py

@@ -480,7 +480,7 @@ class GunzipTestCase(HTTPGitApplicationTestCase):
         self.assertEqual(self._environ['HTTP_CONTENT_ENCODING'], 'gzip')
         self._environ['CONTENT_LENGTH'] = zlength
         self._environ['wsgi.input'] = zstream
-        app_output = self._app(self._environ, None)
+        self._app(self._environ, None)
         buf = self._environ['wsgi.input']
         self.assertIsNot(buf, zstream)
         buf.seek(0)

+ 8 - 4
dulwich/tests/utils.py

@@ -26,6 +26,9 @@ import shutil
 import tempfile
 import time
 import types
+from unittest import (
+    SkipTest,
+    )
 import warnings
 
 from dulwich.index import (
@@ -46,9 +49,6 @@ from dulwich.pack import (
     create_delta,
     )
 from dulwich.repo import Repo
-from dulwich.tests import (
-    SkipTest,
-    )
 
 # Plain files are very frequently used in tests, so let the mode be very short.
 F = 0o100644  # Shorthand mode for Files.
@@ -323,4 +323,8 @@ def setup_warning_catcher():
         caught_warnings.append(args[0])
 
     warnings.showwarning = custom_showwarning
-    return caught_warnings
+
+    def restore_showwarning():
+        warnings.showwarning = original_showwarning
+
+    return caught_warnings, restore_showwarning