Rev 2493: Moving check_header into C shows no appreciable advantage, even though in http://bzr.arbash-meinel.com/branches/bzr/0.17-dev/knit_index_pyrex

John Arbash Meinel john at arbash-meinel.com
Wed May 9 21:01:53 BST 2007


At http://bzr.arbash-meinel.com/branches/bzr/0.17-dev/knit_index_pyrex

------------------------------------------------------------
revno: 2493
revision-id: john at arbash-meinel.com-20070509200141-6cnhjp8ldduugjxr
parent: john at arbash-meinel.com-20070509194537-3h1zslf0rmozzgsq
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: knit_index_pyrex
timestamp: Wed 2007-05-09 15:01:41 -0500
message:
  Moving check_header into C shows no appreciable advantage, even though
  it means we remove a f.read() call.
modified:
  bzrlib/knit_c.pyx              knit_c.pyx-20070509143944-u42gy8w387a10m0j-1
-------------- next part --------------
=== modified file 'bzrlib/knit_c.pyx'
--- a/bzrlib/knit_c.pyx	2007-05-09 19:45:37 +0000
+++ b/bzrlib/knit_c.pyx	2007-05-09 20:01:41 +0000
@@ -16,7 +16,7 @@
 
 """Pyrex extensions to knit parsing."""
 
-import sys
+from bzrlib import errors
 
 cdef extern from "stdlib.h":
     long int strtol(char *nptr, char **endptr, int base)
@@ -269,12 +269,44 @@
 
         return self.process_one_record(start, last)
 
+    cdef int check_header(self) except -1:
+        """Check the header string is valid."""
+        cdef char *end
+        cdef char *header_str
+        cdef int header_len
+        cdef int disk_header_len
+        cdef int invalid
+
+        header = self.kndx.HEADER
+        header_str = PyString_AsString(header)
+        header_len = PyString_Size(header)
+
+        invalid = 1
+        end = strchr(self.cur_str, c'\n')
+        if end != NULL:
+            disk_header_len = end - self.cur_str + 1
+            if (disk_header_len == header_len
+                and strncmp(self.cur_str, header_str, header_len) == 0):
+                invalid = 0
+        if invalid == 1:
+            line = PyString_FromStringAndSize(self.cur_str, disk_header_len)
+            raise errors.KnitHeaderError(badline=line,
+                              filename=self.kndx._transport.abspath(
+                                                    self.kndx._filename))
+        # If the header is correct, we move past it.
+        self.cur_str = end
+        return 0
+
     def read(self):
         cdef int text_size
 
         self.validate()
 
-        self.kndx.check_header(self.fp)
+        text = self.fp.read()
+        if PyString_Size(text) == 0:
+            # An empty file can actually be treated as though the file doesn't
+            # exist yet.
+            raise errors.NoSuchFile(self.kndx._full_path())
 
         # We read the whole thing at once
         # TODO: jam 2007-05-09 Consider reading incrementally rather than
@@ -284,11 +316,11 @@
         #       The other possibility is to avoid a Python String here
         #       completely. However self.fp may be a 'file-like' object
         #       it is not guaranteed to be a real file.
-        text = self.fp.read()
         text_size = PyString_Size(text)
         self.cur_str = PyString_AsString(text)
         # This points to the last character in the string
         self.end_str = self.cur_str + text_size
+        self.check_header()
 
         while self.cur_str < self.end_str:
             self.process_next_record()



More information about the bazaar-commits mailing list