Rev 3645: Change from using AssertionError to using DirstateCorrupt in a few places in http://bzr.arbash-meinel.com/branches/bzr/1.7-dev/dirstate_segv_186014

John Arbash Meinel john at arbash-meinel.com
Tue Sep 2 19:51:04 BST 2008


At http://bzr.arbash-meinel.com/branches/bzr/1.7-dev/dirstate_segv_186014

------------------------------------------------------------
revno: 3645
revision-id: john at arbash-meinel.com-20080902185103-camvrjyw7a9efpno
parent: john at arbash-meinel.com-20080902183801-1u7pk8k03hx8aw1o
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: dirstate_segv_186014
timestamp: Tue 2008-09-02 13:51:03 -0500
message:
  Change from using AssertionError to using DirstateCorrupt in a few places
-------------- next part --------------
=== modified file 'bzrlib/_dirstate_helpers_c.pyx'
--- a/bzrlib/_dirstate_helpers_c.pyx	2008-09-02 18:38:01 +0000
+++ b/bzrlib/_dirstate_helpers_c.pyx	2008-09-02 18:51:03 +0000
@@ -19,6 +19,7 @@
 This is the python implementation for DirState functions.
 """
 
+from bzrlib import errors
 from bzrlib.dirstate import DirState
 
 
@@ -477,6 +478,7 @@
 cdef class Reader:
     """Maintain the current location, and return fields as you parse them."""
 
+    cdef object state # The DirState object
     cdef object text # The overall string object
     cdef char *text_cstr # Pointer to the beginning of text
     cdef int text_size # Length of text
@@ -485,7 +487,8 @@
     cdef char *cur_cstr # Pointer to the current record
     cdef char *next # Pointer to the end of this record
 
-    def __init__(self, text):
+    def __init__(self, text, state):
+        self.state = state
         self.text = text
         self.text_cstr = PyString_AsString(text)
         self.text_size = PyString_Size(text)
@@ -506,7 +509,8 @@
         self.cur_cstr = <char*>memchr(next, c'\0', self.end_cstr - next)
         if self.cur_cstr == NULL:
             extra_len = self.end_cstr - next
-            raise AssertionError('failed to find trailing NULL (\\0).'
+            raise errors.DirstateCorrupt(self.state,
+                'failed to find trailing NULL (\\0).'
                 ' Trailing garbage: %r'
                 % safe_string_from_size(next, extra_len))
         size[0] = self.cur_cstr - next
@@ -640,13 +644,13 @@
         # marker.
         trailing = self.get_next(&cur_size)
         if cur_size != 1 or trailing[0] != c'\n':
-            raise AssertionError(
+            raise errors.DirstateCorrupt(self.state,
                 'Bad parse, we expected to end on \\n, not: %d %s: %s'
                 % (cur_size, safe_string_from_size(trailing, cur_size),
                    ret))
         return ret
 
-    def _parse_dirblocks(self, state):
+    def _parse_dirblocks(self):
         """Parse all dirblocks in the state file."""
         cdef int num_trees
         cdef object current_block
@@ -656,14 +660,15 @@
         cdef int expected_entry_count
         cdef int entry_count
 
-        num_trees = state._num_present_parents() + 1
-        expected_entry_count = state._num_entries
+        num_trees = self.state._num_present_parents() + 1
+        expected_entry_count = self.state._num_entries
 
         # Ignore the first record
         self._init()
 
         current_block = []
-        state._dirblocks = [('', current_block), ('', [])]
+        dirblocks = [('', current_block), ('', [])]
+        self.state._dirblocks = dirblocks
         obj = ''
         current_dirname = <void*>obj
         new_block = 0
@@ -681,15 +686,16 @@
             if new_block:
                 # new block - different dirname
                 current_block = []
-                PyList_Append(state._dirblocks,
+                PyList_Append(dirblocks,
                               (<object>current_dirname, current_block))
             PyList_Append(current_block, entry)
             entry_count = entry_count + 1
         if entry_count != expected_entry_count:
-            raise AssertionError('We read the wrong number of entries.'
+            raise errors.DirstateCorrupt(self.state,
+                    'We read the wrong number of entries.'
                     ' We expected to read %s, but read %s'
                     % (expected_entry_count, entry_count))
-        state._split_root_dirblock_into_contents()
+        self.state._split_root_dirblock_into_contents()
 
 
 def _read_dirblocks_c(state):
@@ -708,7 +714,7 @@
     text = state._state_file.read()
     # TODO: check the crc checksums. crc_measured = zlib.crc32(text)
 
-    reader = Reader(text)
+    reader = Reader(text, state)
 
-    reader._parse_dirblocks(state)
+    reader._parse_dirblocks()
     state._dirblock_state = DirState.IN_MEMORY_UNMODIFIED

=== modified file 'bzrlib/_dirstate_helpers_py.py'
--- a/bzrlib/_dirstate_helpers_py.py	2008-09-02 18:38:01 +0000
+++ b/bzrlib/_dirstate_helpers_py.py	2008-09-02 18:51:03 +0000
@@ -20,6 +20,7 @@
 
 # We cannot import the dirstate module, because it loads this module
 # All we really need is the IN_MEMORY_MODIFIED constant
+from bzrlib import errors
 from bzrlib.dirstate import DirState
 
 
@@ -201,8 +202,8 @@
     # Remove the last blank entry
     trailing = fields.pop()
     if trailing != '':
-        raise AssertionError("dirstate file has trailing garbage: %r"
-            % (trailing,))
+        raise errors.DirstateCorrupt(state,
+            'trailing garbage: %r' % (trailing,))
     # consider turning fields into a tuple.
 
     # skip the first field which is the trailing null from the header.
@@ -220,7 +221,7 @@
     field_count = len(fields)
     # this checks our adjustment, and also catches file too short.
     if field_count - cur != expected_field_count:
-        raise AssertionError(
+        raise errors.DirstateCorrupt(state,
             'field count incorrect %s != %s, entry_size=%s, '\
             'num_entries=%s fields=%r' % (
             field_count - cur, expected_field_count, entry_size,

=== modified file 'bzrlib/errors.py'
--- a/bzrlib/errors.py	2008-08-03 00:36:46 +0000
+++ b/bzrlib/errors.py	2008-09-02 18:51:03 +0000
@@ -224,6 +224,16 @@
         self.message = message
 
 
+class DirstateCorrupt(BzrError):
+
+    _fmt = "The dirstate file (%(state)s) appears to be corrupt: %(msg)s"
+
+    def __init__(self, state, msg):
+        BzrError.__init__(self)
+        self.state = state
+        self.msg = msg
+
+
 class DisabledMethod(InternalBzrError):
 
     _fmt = "The smart server method '%(class_name)s' is disabled."

=== modified file 'bzrlib/tests/test__dirstate_helpers.py'
--- a/bzrlib/tests/test__dirstate_helpers.py	2008-09-02 18:38:01 +0000
+++ b/bzrlib/tests/test__dirstate_helpers.py	2008-09-02 18:51:03 +0000
@@ -21,6 +21,7 @@
 
 from bzrlib import (
     dirstate,
+    errors,
     tests,
     )
 from bzrlib.tests import test_dirstate
@@ -700,7 +701,8 @@
             f.write('bogus\n')
         finally:
             f.close()
-        e = self.assertRaises(AssertionError, state._read_dirblocks_if_needed)
+        e = self.assertRaises(errors.DirstateCorrupt,
+                              state._read_dirblocks_if_needed)
         # Make sure we mention the bogus characters in the error
         self.assertContainsRe(str(e), 'bogus')
 

=== modified file 'bzrlib/tests/test_errors.py'
--- a/bzrlib/tests/test_errors.py	2008-07-30 04:34:59 +0000
+++ b/bzrlib/tests/test_errors.py	2008-09-02 18:51:03 +0000
@@ -45,6 +45,13 @@
             "Error: the reason why",
             str(error))
 
+    def test_dirstate_corrupt(self):
+        error = errors.DirstateCorrupt('.bzr/checkout/dirstate',
+                                       'trailing garbage: "x"')
+        self.assertEqualDiff("The dirstate file (.bzr/checkout/dirstate)"
+            " appears to be corrupt: trailing garbage: \"x\"",
+            str(error))
+
     def test_disabled_method(self):
         error = errors.DisabledMethod("class name")
         self.assertEqualDiff(



More information about the bazaar-commits mailing list