Browse Source

Move errors out of dulwich.errors.

Jelmer Vernooij 7 years ago
parent
commit
2bc4f19374
16 changed files with 217 additions and 231 deletions
  1. 7 0
      NEWS
  2. 7 12
      dulwich/_objects.c
  3. 5 5
      dulwich/_pack.c
  4. 8 4
      dulwich/client.py
  5. 30 156
      dulwich/errors.py
  6. 2 3
      dulwich/hooks.py
  7. 1 2
      dulwich/index.py
  8. 1 3
      dulwich/object_store.py
  9. 62 8
      dulwich/objects.py
  10. 8 4
      dulwich/pack.py
  11. 2 4
      dulwich/porcelain.py
  12. 41 4
      dulwich/protocol.py
  13. 8 4
      dulwich/refs.py
  14. 21 11
      dulwich/repo.py
  15. 6 8
      dulwich/server.py
  16. 8 3
      dulwich/walk.py

+ 7 - 0
NEWS

@@ -48,6 +48,13 @@
   * Clarified docstrings for Client.{send_pack,fetch_pack} implementations.
     (Jelmer Vernooij, #523)
 
+ CHANGES
+
+  * Errors have been moved out of ``dulwich.errors`` into their respective
+    modules. ``dulwich.errors`` also still provides them, but will in the future
+    print a deprecation warning (and at a further point, removed).
+    (Jelmer Vernooij)
+
 0.17.3	2017-03-20
 
  PLATFORM SUPPORT

+ 7 - 12
dulwich/_objects.c

@@ -264,7 +264,7 @@ static PyMethodDef py_objects_methods[] = {
 static PyObject *
 moduleinit(void)
 {
-	PyObject *m, *objects_mod, *errors_mod;
+	PyObject *m, *objects_mod;
 
 #if PY_MAJOR_VERSION >= 3
 	static struct PyModuleDef moduledef = {
@@ -286,22 +286,17 @@ moduleinit(void)
 		return NULL;
 	}
 
-	errors_mod = PyImport_ImportModule("dulwich.errors");
-	if (errors_mod == NULL) {
+	/* This is a circular import but should be safe since this module is
+	 * imported at at the very bottom of objects.py. */
+	objects_mod = PyImport_ImportModule("dulwich.objects");
+	if (objects_mod == NULL) {
 		return NULL;
 	}
 
 	object_format_exception_cls = PyObject_GetAttrString(
-		errors_mod, "ObjectFormatException");
-	Py_DECREF(errors_mod);
+		objects_mod, "ObjectFormatException");
 	if (object_format_exception_cls == NULL) {
-		return NULL;
-	}
-
-	/* This is a circular import but should be safe since this module is
-	 * imported at at the very bottom of objects.py. */
-	objects_mod = PyImport_ImportModule("dulwich.objects");
-	if (objects_mod == NULL) {
+		Py_DECREF(objects_mod);
 		return NULL;
 	}
 

+ 5 - 5
dulwich/_pack.c

@@ -262,7 +262,7 @@ static PyObject *
 moduleinit(void)
 {
 	PyObject *m;
-	PyObject *errors_module;
+	PyObject *pack_module;
 
 #if PY_MAJOR_VERSION >= 3
 	static struct PyModuleDef moduledef = {
@@ -278,12 +278,12 @@ moduleinit(void)
 	};
 #endif
 
-	errors_module = PyImport_ImportModule("dulwich.errors");
-	if (errors_module == NULL)
+	pack_module = PyImport_ImportModule("dulwich.pack");
+	if (pack_module == NULL)
 		return NULL;
 
-	PyExc_ApplyDeltaError = PyObject_GetAttrString(errors_module, "ApplyDeltaError");
-	Py_DECREF(errors_module);
+	PyExc_ApplyDeltaError = PyObject_GetAttrString(pack_module, "ApplyDeltaError");
+	Py_DECREF(pack_module);
 	if (PyExc_ApplyDeltaError == NULL)
 		return NULL;
 

+ 8 - 4
dulwich/client.py

@@ -60,13 +60,10 @@ except ImportError:
     import urllib.request as urllib2
     import urllib.parse as urlparse
 
-from dulwich.errors import (
+from dulwich.protocol import (
     GitProtocolError,
-    NotGitRepository,
     SendPackError,
     UpdateRefsError,
-    )
-from dulwich.protocol import (
     _RBUFSIZE,
     capability_agent,
     CAPABILITY_DELETE_REFS,
@@ -99,6 +96,13 @@ from dulwich.refs import (
     )
 
 
+class NotGitRepository(Exception):
+    """Indicates that no Git repository was found."""
+
+    def __init__(self, *args, **kwargs):
+        Exception.__init__(self, *args, **kwargs)
+
+
 def _fileno_can_read(fileno):
     """Check if a file descriptor is readable."""
     return len(select.select([fileno], [], [], 0)[0]) > 0

+ 30 - 156
dulwich/errors.py

@@ -21,160 +21,34 @@
 
 """Dulwich-related exception classes and utility functions."""
 
-import binascii
+from dulwich.hooks import HookError
+from dulwich.objects import (
+    ChecksumMismatch,
+    ObjectFormatException,
+    WrongObjectException,
+    NotBlobError,
+    NotCommitError,
+    NotTreeError,
+    NotTagError,
+    )
+from dulwich.pack import ApplyDeltaError
+from dulwich.protocol import (
+    GitProtocolError,
+    HangupException,
+    UnexpectedCommandError,
+    SendPackError,
+    UpdateRefsError,
+    )
+from dulwich.refs import (
+    PackedRefsException,
+    RefFormatError,
+    )
+from dulwich.repo import (
+    NotGitRepository,
+    NoIndexPresent,
+    CommitError,
+    )
+from dulwich.walk import (
+    MissingCommitError,
+    )
 
-
-class ChecksumMismatch(Exception):
-    """A checksum didn't match the expected contents."""
-
-    def __init__(self, expected, got, extra=None):
-        if len(expected) == 20:
-            expected = binascii.hexlify(expected)
-        if len(got) == 20:
-            got = binascii.hexlify(got)
-        self.expected = expected
-        self.got = got
-        self.extra = extra
-        if self.extra is None:
-            Exception.__init__(
-                self, "Checksum mismatch: Expected %s, got %s" %
-                (expected, got))
-        else:
-            Exception.__init__(
-                self, "Checksum mismatch: Expected %s, got %s; %s" %
-                (expected, got, extra))
-
-
-class WrongObjectException(Exception):
-    """Baseclass for all the _ is not a _ exceptions on objects.
-
-    Do not instantiate directly.
-
-    Subclasses should define a type_name attribute that indicates what
-    was expected if they were raised.
-    """
-
-    def __init__(self, sha, *args, **kwargs):
-        Exception.__init__(self, "%s is not a %s" % (sha, self.type_name))
-
-
-class NotCommitError(WrongObjectException):
-    """Indicates that the sha requested does not point to a commit."""
-
-    type_name = 'commit'
-
-
-class NotTreeError(WrongObjectException):
-    """Indicates that the sha requested does not point to a tree."""
-
-    type_name = 'tree'
-
-
-class NotTagError(WrongObjectException):
-    """Indicates that the sha requested does not point to a tag."""
-
-    type_name = 'tag'
-
-
-class NotBlobError(WrongObjectException):
-    """Indicates that the sha requested does not point to a blob."""
-
-    type_name = 'blob'
-
-
-class MissingCommitError(Exception):
-    """Indicates that a commit was not found in the repository"""
-
-    def __init__(self, sha, *args, **kwargs):
-        self.sha = sha
-        Exception.__init__(self, "%s is not in the revision store" % sha)
-
-
-class ObjectMissing(Exception):
-    """Indicates that a requested object is missing."""
-
-    def __init__(self, sha, *args, **kwargs):
-        Exception.__init__(self, "%s is not in the pack" % sha)
-
-
-class ApplyDeltaError(Exception):
-    """Indicates that applying a delta failed."""
-
-    def __init__(self, *args, **kwargs):
-        Exception.__init__(self, *args, **kwargs)
-
-
-class NotGitRepository(Exception):
-    """Indicates that no Git repository was found."""
-
-    def __init__(self, *args, **kwargs):
-        Exception.__init__(self, *args, **kwargs)
-
-
-class GitProtocolError(Exception):
-    """Git protocol exception."""
-
-    def __init__(self, *args, **kwargs):
-        Exception.__init__(self, *args, **kwargs)
-
-
-class SendPackError(GitProtocolError):
-    """An error occurred during send_pack."""
-
-    def __init__(self, *args, **kwargs):
-        Exception.__init__(self, *args, **kwargs)
-
-
-class UpdateRefsError(GitProtocolError):
-    """The server reported errors updating refs."""
-
-    def __init__(self, *args, **kwargs):
-        self.ref_status = kwargs.pop('ref_status')
-        Exception.__init__(self, *args, **kwargs)
-
-
-class HangupException(GitProtocolError):
-    """Hangup exception."""
-
-    def __init__(self):
-        Exception.__init__(
-            self, "The remote server unexpectedly closed the connection.")
-
-
-class UnexpectedCommandError(GitProtocolError):
-    """Unexpected command received in a proto line."""
-
-    def __init__(self, command):
-        if command is None:
-            command = 'flush-pkt'
-        else:
-            command = 'command %s' % command
-        GitProtocolError.__init__(self, 'Protocol got unexpected %s' % command)
-
-
-class FileFormatException(Exception):
-    """Base class for exceptions relating to reading git file formats."""
-
-
-class PackedRefsException(FileFormatException):
-    """Indicates an error parsing a packed-refs file."""
-
-
-class ObjectFormatException(FileFormatException):
-    """Indicates an error parsing an object."""
-
-
-class NoIndexPresent(Exception):
-    """No index is present."""
-
-
-class CommitError(Exception):
-    """An error occurred while performing a commit."""
-
-
-class RefFormatError(Exception):
-    """Indicates an invalid ref name."""
-
-
-class HookError(Exception):
-    """An error occurred while executing a hook."""

+ 2 - 3
dulwich/hooks.py

@@ -25,9 +25,8 @@ import subprocess
 import sys
 import tempfile
 
-from dulwich.errors import (
-    HookError,
-)
+class HookError(Exception):
+    """An error occurred while executing a hook."""
 
 
 class Hook(object):

+ 1 - 2
dulwich/index.py

@@ -593,8 +593,7 @@ def get_unstaged_changes(index, root_path):
             # This is actually a directory
             if os.path.exists(os.path.join(tree_path, '.git')):
                 # Submodule
-                from dulwich.errors import NotGitRepository
-                from dulwich.repo import Repo
+                from dulwich.repo import NotGitRepository, Repo
                 try:
                     if entry.sha != Repo(tree_path).head():
                         yield tree_path

+ 1 - 3
dulwich/object_store.py

@@ -35,11 +35,9 @@ from dulwich.diff_tree import (
     tree_changes,
     walk_trees,
     )
-from dulwich.errors import (
-    NotTreeError,
-    )
 from dulwich.file import GitFile
 from dulwich.objects import (
+    NotTreeError,
     Commit,
     ShaFile,
     Tag,

+ 62 - 8
dulwich/objects.py

@@ -31,14 +31,6 @@ import warnings
 import zlib
 from hashlib import sha1
 
-from dulwich.errors import (
-    ChecksumMismatch,
-    NotBlobError,
-    NotCommitError,
-    NotTagError,
-    NotTreeError,
-    ObjectFormatException,
-    )
 from dulwich.file import GitFile
 
 
@@ -63,6 +55,68 @@ _TAGGER_HEADER = b'tagger'
 S_IFGITLINK = 0o160000
 
 
+class ChecksumMismatch(Exception):
+    """A checksum didn't match the expected contents."""
+
+    def __init__(self, expected, got, extra=None):
+        if len(expected) == 20:
+            expected = binascii.hexlify(expected)
+        if len(got) == 20:
+            got = binascii.hexlify(got)
+        self.expected = expected
+        self.got = got
+        self.extra = extra
+        if self.extra is None:
+            Exception.__init__(
+                self, "Checksum mismatch: Expected %s, got %s" %
+                (expected, got))
+        else:
+            Exception.__init__(
+                self, "Checksum mismatch: Expected %s, got %s; %s" %
+                (expected, got, extra))
+
+
+class ObjectFormatException(Exception):
+    """Indicates an error parsing an object."""
+
+
+class WrongObjectException(Exception):
+    """Baseclass for all the _ is not a _ exceptions on objects.
+
+    Do not instantiate directly.
+
+    Subclasses should define a type_name attribute that indicates what
+    was expected if they were raised.
+    """
+
+    def __init__(self, sha, *args, **kwargs):
+        Exception.__init__(self, "%s is not a %s" % (sha, self.type_name))
+
+
+class NotCommitError(WrongObjectException):
+    """Indicates that the sha requested does not point to a commit."""
+
+    type_name = 'commit'
+
+
+class NotTreeError(WrongObjectException):
+    """Indicates that the sha requested does not point to a tree."""
+
+    type_name = 'tree'
+
+
+class NotTagError(WrongObjectException):
+    """Indicates that the sha requested does not point to a tag."""
+
+    type_name = 'tag'
+
+
+class NotBlobError(WrongObjectException):
+    """Indicates that the sha requested does not point to a blob."""
+
+    type_name = 'blob'
+
+
 def S_ISGITLINK(m):
     """Check if a mode indicates a submodule.
 

+ 8 - 4
dulwich/pack.py

@@ -72,15 +72,12 @@ else:
 if sys.platform == 'Plan9':
     has_mmap = False
 
-from dulwich.errors import (  # noqa: E402
-    ApplyDeltaError,
-    ChecksumMismatch,
-    )
 from dulwich.file import GitFile  # noqa: E402
 from dulwich.lru_cache import (  # noqa: E402
     LRUSizeCache,
     )
 from dulwich.objects import (  # noqa: E402
+    ChecksumMismatch,
     ShaFile,
     hex_to_sha,
     sha_to_hex,
@@ -97,6 +94,13 @@ DELTA_TYPES = (OFS_DELTA, REF_DELTA)
 DEFAULT_PACK_DELTA_WINDOW_SIZE = 10
 
 
+class ApplyDeltaError(Exception):
+    """Indicates that applying a delta failed."""
+
+    def __init__(self, *args, **kwargs):
+        Exception.__init__(self, *args, **kwargs)
+
+
 def take_msb_bytes(read, crc32=None):
     """Read bytes marked with most significant bit.
 

+ 2 - 4
dulwich/porcelain.py

@@ -76,10 +76,6 @@ from dulwich.diff_tree import (
     CHANGE_COPY,
     RENAME_CHANGE_TYPES,
     )
-from dulwich.errors import (
-    SendPackError,
-    UpdateRefsError,
-    )
 from dulwich.ignore import IgnoreFilterManager
 from dulwich.index import (
     blob_from_path_and_stat,
@@ -106,6 +102,8 @@ from dulwich.pack import (
     )
 from dulwich.patch import write_tree_diff
 from dulwich.protocol import (
+    SendPackError,
+    UpdateRefsError,
     Protocol,
     ZERO_SHA,
     )

+ 41 - 4
dulwich/protocol.py

@@ -28,10 +28,6 @@ from os import (
 import socket
 
 import dulwich
-from dulwich.errors import (
-    HangupException,
-    GitProtocolError,
-    )
 
 TCP_GIT_PORT = 9418
 
@@ -83,6 +79,47 @@ COMMAND_WANT = b'want'
 COMMAND_HAVE = b'have'
 
 
+class GitProtocolError(Exception):
+    """Git protocol exception."""
+
+    def __init__(self, *args, **kwargs):
+        Exception.__init__(self, *args, **kwargs)
+
+
+class SendPackError(GitProtocolError):
+    """An error occurred during send_pack."""
+
+    def __init__(self, *args, **kwargs):
+        Exception.__init__(self, *args, **kwargs)
+
+
+class UpdateRefsError(GitProtocolError):
+    """The server reported errors updating refs."""
+
+    def __init__(self, *args, **kwargs):
+        self.ref_status = kwargs.pop('ref_status')
+        Exception.__init__(self, *args, **kwargs)
+
+
+class HangupException(GitProtocolError):
+    """Hangup exception."""
+
+    def __init__(self):
+        Exception.__init__(
+            self, "The remote server unexpectedly closed the connection.")
+
+
+class UnexpectedCommandError(GitProtocolError):
+    """Unexpected command received in a proto line."""
+
+    def __init__(self, command):
+        if command is None:
+            command = 'flush-pkt'
+        else:
+            command = 'command %s' % command
+        GitProtocolError.__init__(self, 'Protocol got unexpected %s' % command)
+
+
 class ProtocolFile(object):
     """A dummy file for network ops that expect file-like objects."""
 

+ 8 - 4
dulwich/refs.py

@@ -26,10 +26,6 @@ import errno
 import os
 import sys
 
-from dulwich.errors import (
-    PackedRefsException,
-    RefFormatError,
-    )
 from dulwich.objects import (
     git_line,
     valid_hexsha,
@@ -47,6 +43,14 @@ BAD_REF_CHARS = set(b'\177 ~^:?*[')
 ANNOTATED_TAG_SUFFIX = b'^{}'
 
 
+class PackedRefsException(Exception):
+    """Indicates an error parsing a packed-refs file."""
+
+
+class RefFormatError(Exception):
+    """Indicates an invalid ref name."""
+
+
 def check_ref_format(refname):
     """Check if a refname is correctly formatted.
 

+ 21 - 11
dulwich/repo.py

@@ -34,17 +34,6 @@ import os
 import sys
 import stat
 
-from dulwich.errors import (
-    NoIndexPresent,
-    NotBlobError,
-    NotCommitError,
-    NotGitRepository,
-    NotTreeError,
-    NotTagError,
-    CommitError,
-    RefFormatError,
-    HookError,
-    )
 from dulwich.file import (
     GitFile,
     )
@@ -54,6 +43,10 @@ from dulwich.object_store import (
     ObjectStoreGraphWalker,
     )
 from dulwich.objects import (
+    NotCommitError,
+    NotBlobError,
+    NotTreeError,
+    NotTagError,
     check_hexsha,
     Blob,
     Commit,
@@ -63,6 +56,7 @@ from dulwich.objects import (
     )
 
 from dulwich.hooks import (
+    HookError,
     PreCommitShellHook,
     PostCommitShellHook,
     CommitMsgShellHook,
@@ -70,6 +64,7 @@ from dulwich.hooks import (
 
 from dulwich.refs import (  # noqa: F401
     check_ref_format,
+    RefFormatError,
     RefsContainer,
     DictRefsContainer,
     InfoRefsContainer,
@@ -106,6 +101,21 @@ BASE_DIRECTORIES = [
 DEFAULT_REF = b'refs/heads/master'
 
 
+class NoIndexPresent(Exception):
+    """No index is present."""
+
+
+class CommitError(Exception):
+    """An error occurred while performing a commit."""
+
+
+class NotGitRepository(Exception):
+    """Indicates that no Git repository was found."""
+
+    def __init__(self, *args, **kwargs):
+        Exception.__init__(self, *args, **kwargs)
+
+
 def parse_graftpoints(graftpoints):
     """Convert a list of graftpoints into a dict
 

+ 6 - 8
dulwich/server.py

@@ -52,24 +52,21 @@ try:
 except ImportError:
     import socketserver as SocketServer
 
-from dulwich.errors import (
-    ApplyDeltaError,
-    ChecksumMismatch,
-    GitProtocolError,
-    NotGitRepository,
-    UnexpectedCommandError,
-    ObjectFormatException,
-    )
 from dulwich import log_utils
 from dulwich.objects import (
+    ChecksumMismatch,
+    ObjectFormatException,
     Commit,
     valid_hexsha,
     )
 from dulwich.pack import (
+    ApplyDeltaError,
     write_pack_objects,
     )
 from dulwich.protocol import (  # noqa: F401
     BufferedPktLineWriter,
+    GitProtocolError,
+    UnexpectedCommandError,
     capability_agent,
     CAPABILITIES_REF,
     CAPABILITY_DELETE_REFS,
@@ -110,6 +107,7 @@ from dulwich.refs import (
     write_info_refs,
     )
 from dulwich.repo import (
+    NotGitRepository,
     Repo,
     )
 

+ 8 - 3
dulwich/walk.py

@@ -33,9 +33,6 @@ from dulwich.diff_tree import (
     tree_changes_for_merge,
     RenameDetector,
     )
-from dulwich.errors import (
-    MissingCommitError,
-    )
 from dulwich.objects import (
     Tag,
     )
@@ -49,6 +46,14 @@ ALL_ORDERS = (ORDER_DATE, ORDER_TOPO)
 _MAX_EXTRA_COMMITS = 5
 
 
+class MissingCommitError(Exception):
+    """Indicates that a commit was not found in the repository"""
+
+    def __init__(self, sha, *args, **kwargs):
+        self.sha = sha
+        Exception.__init__(self, "%s is not in the revision store" % sha)
+
+
 class WalkEntry(object):
     """Object encapsulating a single result from a walk."""