Rev 2540: Some restructuring. in http://bzr.arbash-meinel.com/branches/bzr/0.17-dev/dirstate_pyrex
John Arbash Meinel
john at arbash-meinel.com
Thu Jul 12 17:34:22 BST 2007
At http://bzr.arbash-meinel.com/branches/bzr/0.17-dev/dirstate_pyrex
------------------------------------------------------------
revno: 2540
revision-id: john at arbash-meinel.com-20070712163402-lp91q157w5etslrj
parent: john at arbash-meinel.com-20070712052601-n0bcu3r5nlu1skj4
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: dirstate_pyrex
timestamp: Thu 2007-07-12 11:34:02 -0500
message:
Some restructuring.
Move bisect_path_* to private functions
Move cmp_path_by_dirblock to a private function,
since it is only used by the bisect_path functions.
Add tests that the compiled versions are actually used.
This catches cases when the import fails for the wrong reason.
Move some code around to make it closer to sorted by name.
modified:
bzrlib/_dirstate_helpers_c.pyx dirstate_helpers.pyx-20070503201057-u425eni465q4idwn-3
bzrlib/_dirstate_helpers_py.py _dirstate_helpers_py-20070710145033-90nz6cqglsk150jy-1
bzrlib/dirstate.py dirstate.py-20060728012006-d6mvoihjb3je9peu-1
bzrlib/tests/test__dirstate_helpers.py test_dirstate_helper-20070504035751-jsbn00xodv0y1eve-2
-------------- next part --------------
=== modified file 'bzrlib/_dirstate_helpers_c.pyx'
--- a/bzrlib/_dirstate_helpers_c.pyx 2007-07-11 21:57:05 +0000
+++ b/bzrlib/_dirstate_helpers_c.pyx 2007-07-12 16:34:02 +0000
@@ -178,13 +178,16 @@
PyString_Size(path2))
-def cmp_path_by_dirblock_c(path1, path2):
+def _cmp_path_by_dirblock_c(path1, path2):
"""Compare two paths based on what directory they are in.
This generates a sort order, such that all children of a directory are
sorted together, and grandchildren are in the same order as the
children appear. But all grandchildren come after all children.
+ In other words, all entries in a directory are sorted together, and
+ directorys are sorted in cmp_by_dirs order.
+
:param path1: first path
:param path2: the second path
:return: positive number if ``path1`` comes first,
@@ -274,7 +277,7 @@
return PyString_FromStringAndSize(s, length)
-def bisect_path_left_c(paths, path):
+def _bisect_path_left_c(paths, path):
"""Return the index where to insert path into paths.
This uses a path-wise comparison so we get::
@@ -324,7 +327,7 @@
return _lo
-def bisect_path_right_c(paths, path):
+def _bisect_path_right_c(paths, path):
"""Return the index where to insert path into paths.
This uses a path-wise comparison so we get::
=== modified file 'bzrlib/_dirstate_helpers_py.py'
--- a/bzrlib/_dirstate_helpers_py.py 2007-07-12 05:26:01 +0000
+++ b/bzrlib/_dirstate_helpers_py.py 2007-07-12 16:34:02 +0000
@@ -23,7 +23,7 @@
from bzrlib.dirstate import DirState
-def bisect_path_left_py(paths, path):
+def _bisect_path_left_py(paths, path):
"""Return the index where to insert path into paths.
This uses the dirblock sorting. So all children in a directory come before
@@ -62,14 +62,14 @@
mid = (lo + hi) // 2
# Grab the dirname for the current dirblock
cur = paths[mid]
- if cmp_path_by_dirblock_py(cur, path) < 0:
+ if _cmp_path_by_dirblock_py(cur, path) < 0:
lo = mid + 1
else:
hi = mid
return lo
-def bisect_path_right_py(paths, path):
+def _bisect_path_right_py(paths, path):
"""Return the index where to insert path into paths.
This uses a path-wise comparison so we get::
@@ -93,7 +93,7 @@
mid = (lo+hi)//2
# Grab the dirname for the current dirblock
cur = paths[mid]
- if cmp_path_by_dirblock_py(path, cur) < 0:
+ if _cmp_path_by_dirblock_py(path, cur) < 0:
hi = mid
else:
lo = mid + 1
@@ -131,6 +131,46 @@
return lo
+def cmp_by_dirs_py(path1, path2):
+ """Compare two paths directory by directory.
+
+ This is equivalent to doing::
+
+ cmp(path1.split('/'), path2.split('/'))
+
+ The idea is that you should compare path components separately. This
+ differs from plain ``cmp(path1, path2)`` for paths like ``'a-b'`` and
+ ``a/b``. "a-b" comes after "a" but would come before "a/b" lexically.
+
+ :param path1: first path
+ :param path2: second path
+ :return: positive number if ``path1`` comes first,
+ 0 if paths are equal,
+ and negative number if ``path2`` sorts first
+ """
+ return cmp(path1.split('/'), path2.split('/'))
+
+
+def _cmp_path_by_dirblock_py(path1, path2):
+ """Compare two paths based on what directory they are in.
+
+ This generates a sort order, such that all children of a directory are
+ sorted together, and grandchildren are in the same order as the
+ children appear. But all grandchildren come after all children.
+
+ :param path1: first path
+ :param path2: the second path
+ :return: positive number if ``path1`` comes first,
+ 0 if paths are equal
+ and a negative number if ``path2`` sorts first
+ """
+ dirname1, basename1 = os.path.split(path1)
+ key1 = (dirname1.split('/'), basename1)
+ dirname2, basename2 = os.path.split(path2)
+ key2 = (dirname2.split('/'), basename2)
+ return cmp(key1, key2)
+
+
def _read_dirblocks_py(state):
"""Read in the dirblocks for the given DirState object.
@@ -233,42 +273,3 @@
# state._dirblocks = sorted(state._dirblocks)
state._dirblock_state = DirState.IN_MEMORY_UNMODIFIED
-
-def cmp_by_dirs_py(path1, path2):
- """Compare two paths directory by directory.
-
- This is equivalent to doing::
-
- cmp(path1.split('/'), path2.split('/'))
-
- The idea is that you should compare path components separately. This
- differs from plain ``cmp(path1, path2)`` for paths like ``'a-b'`` and
- ``a/b``. "a-b" comes after "a" but would come before "a/b" lexically.
-
- :param path1: first path
- :param path2: second path
- :return: positive number if ``path1`` comes first,
- 0 if paths are equal,
- and negative number if ``path2`` sorts first
- """
- return cmp(path1.split('/'), path2.split('/'))
-
-
-def cmp_path_by_dirblock_py(path1, path2):
- """Compare two paths based on what directory they are in.
-
- This generates a sort order, such that all children of a directory are
- sorted together, and grandchildren are in the same order as the
- children appear. But all grandchildren come after all children.
-
- :param path1: first path
- :param path2: the second path
- :return: positive number if ``path1`` comes first,
- 0 if paths are equal
- and a negative number if ``path2`` sorts first
- """
- dirname1, basename1 = os.path.split(path1)
- key1 = (dirname1.split('/'), basename1)
- dirname2, basename2 = os.path.split(path2)
- key2 = (dirname2.split('/'), basename2)
- return cmp(key1, key2)
=== modified file 'bzrlib/dirstate.py'
--- a/bzrlib/dirstate.py 2007-07-11 23:45:20 +0000
+++ b/bzrlib/dirstate.py 2007-07-12 16:34:02 +0000
@@ -555,7 +555,7 @@
first_path = first_fields[1] + '/' + first_fields[2]
else:
first_path = first_fields[2]
- first_loc = bisect_path_left(cur_files, first_path)
+ first_loc = _bisect_path_left(cur_files, first_path)
# These exist before the current location
pre = cur_files[:first_loc]
@@ -582,7 +582,7 @@
last_path = last_fields[1] + '/' + last_fields[2]
else:
last_path = last_fields[2]
- last_loc = bisect_path_right(post, last_path)
+ last_loc = _bisect_path_right(post, last_path)
middle_files = post[:last_loc]
post = post[last_loc:]
@@ -2282,15 +2282,15 @@
from bzrlib._dirstate_helpers_c import (
_read_dirblocks_c as _read_dirblocks,
bisect_dirblock_c as bisect_dirblock,
- bisect_path_left_c as bisect_path_left,
- bisect_path_right_c as bisect_path_right,
+ _bisect_path_left_c as _bisect_path_left,
+ _bisect_path_right_c as _bisect_path_right,
cmp_by_dirs_c as cmp_by_dirs,
)
except ImportError:
from bzrlib._dirstate_helpers_py import (
_read_dirblocks_py as _read_dirblocks,
bisect_dirblock_py as bisect_dirblock,
- bisect_path_left_py as bisect_path_left,
- bisect_path_right_py as bisect_path_right,
+ _bisect_path_left_py as _bisect_path_left,
+ _bisect_path_right_py as _bisect_path_right,
cmp_by_dirs_py as cmp_by_dirs,
)
=== modified file 'bzrlib/tests/test__dirstate_helpers.py'
--- a/bzrlib/tests/test__dirstate_helpers.py 2007-07-12 05:14:26 +0000
+++ b/bzrlib/tests/test__dirstate_helpers.py 2007-07-12 16:34:02 +0000
@@ -41,15 +41,15 @@
class TestBisectPathMixin(object):
- """Test that bisect_path_*() returns the expected values.
+ """Test that _bisect_path_*() returns the expected values.
- bisect_path_* is intended to work like bisect.bisect_*() except it
+ _bisect_path_* is intended to work like bisect.bisect_*() except it
knows it is working on paths that are sorted by ('path', 'to', 'foo')
chunks rather than by raw 'path/to/foo'.
"""
def get_bisect_path(self):
- """Return an implementation of bisect_path_*"""
+ """Return an implementation of _bisect_path_*"""
raise NotImplementedError
def get_bisect(self):
@@ -194,15 +194,15 @@
class TestBisectPathLeft(tests.TestCase, TestBisectPathMixin):
def get_bisect_path(self):
- from bzrlib._dirstate_helpers_py import bisect_path_left_py
- return bisect_path_left_py
+ from bzrlib._dirstate_helpers_py import _bisect_path_left_py
+ return _bisect_path_left_py
def get_bisect(self):
return bisect.bisect_left, 0
class TestCompiledBisectPathLeft(TestBisectPathLeft):
- """Test that bisect_path_left() returns the expected values.
+ """Test that _bisect_path_left() returns the expected values.
This runs all the normal tests that TestBisectDirblock did, but uses the
compiled version.
@@ -211,22 +211,22 @@
_test_needs_features = [CompiledDirstateHelpersFeature]
def get_bisect_path(self):
- from bzrlib._dirstate_helpers_c import bisect_path_left_c
- return bisect_path_left_c
+ from bzrlib._dirstate_helpers_c import _bisect_path_left_c
+ return _bisect_path_left_c
class TestBisectPathRight(tests.TestCase, TestBisectPathMixin):
def get_bisect_path(self):
- from bzrlib._dirstate_helpers_py import bisect_path_right_py
- return bisect_path_right_py
+ from bzrlib._dirstate_helpers_py import _bisect_path_right_py
+ return _bisect_path_right_py
def get_bisect(self):
return bisect.bisect_right, -1
class TestCompiledBisectPathRight(TestBisectPathRight):
- """Test that bisect_path_right() returns the expected values.
+ """Test that _bisect_path_right() returns the expected values.
This runs all the normal tests that TestBisectDirblock did, but uses the
compiled version.
@@ -235,8 +235,8 @@
_test_needs_features = [CompiledDirstateHelpersFeature]
def get_bisect_path(self):
- from bzrlib._dirstate_helpers_c import bisect_path_right_c
- return bisect_path_right_c
+ from bzrlib._dirstate_helpers_c import _bisect_path_right_c
+ return _bisect_path_right_c
class TestBisectDirblock(tests.TestCase):
@@ -459,9 +459,9 @@
class TestCmpPathByDirblock(tests.TestCase):
def get_cmp_path_by_dirblock(self):
- """Get a specific implementation of cmp_path_by_dirblock."""
- from bzrlib._dirstate_helpers_py import cmp_path_by_dirblock_py
- return cmp_path_by_dirblock_py
+ """Get a specific implementation of _cmp_path_by_dirblock."""
+ from bzrlib._dirstate_helpers_py import _cmp_path_by_dirblock_py
+ return _cmp_path_by_dirblock_py
def assertCmpPathByDirblock(self, paths):
"""Compare all paths and make sure they evaluate to the correct order.
@@ -563,17 +563,17 @@
class TestCompiledCmpPathByDirblock(TestCmpPathByDirblock):
- """Test the pyrex implementation of cmp_path_by_dirblock"""
+ """Test the pyrex implementation of _cmp_path_by_dirblock"""
_test_needs_features = [CompiledDirstateHelpersFeature]
def get_cmp_by_dirs(self):
- from bzrlib._dirstate_helpers_c import cmp_path_by_dirblock_c
- return cmp_path_by_dirblock_c
+ from bzrlib._dirstate_helpers_c import _cmp_path_by_dirblock_c
+ return _cmp_path_by_dirblock_c
class TestMemRChr(tests.TestCase):
- """Test the pyrex implementation of cmp_path_by_dirblock"""
+ """Test memrchr functionality"""
_test_needs_features = [CompiledDirstateHelpersFeature]
@@ -641,3 +641,53 @@
def get_read_dirblocks(self):
from bzrlib._dirstate_helpers_c import _read_dirblocks_c
return _read_dirblocks_c
+
+
+class TestUsingCompiledIfAvailable(tests.TestCase):
+ """Check that any compiled functions that are available are the default.
+
+ It is possible to have typos, etc in the import line, such that
+ _dirstate_helpers_c is actually available, but the compiled functions are
+ not being used.
+ """
+
+ def test_bisect_dirblock(self):
+ if CompiledDirstateHelpersFeature.available():
+ from bzrlib._dirstate_helpers_c import bisect_dirblock_c
+ self.assertIs(bisect_dirblock_c, dirstate.bisect_dirblock)
+ else:
+ from bzrlib._dirstate_helpers_py import bisect_dirblock_py
+ self.assertIs(bisect_dirblock_py, dirstate.bisect_dirblock)
+
+ def test__bisect_path_left(self):
+ if CompiledDirstateHelpersFeature.available():
+ from bzrlib._dirstate_helpers_c import _bisect_path_left_c
+ self.assertIs(_bisect_path_left_c, dirstate._bisect_path_left)
+ else:
+ from bzrlib._dirstate_helpers_py import _bisect_path_left_py
+ self.assertIs(_bisect_path_left_py, dirstate._bisect_path_left)
+
+ def test__bisect_path_right(self):
+ if CompiledDirstateHelpersFeature.available():
+ from bzrlib._dirstate_helpers_c import _bisect_path_right_c
+ self.assertIs(_bisect_path_right_c, dirstate._bisect_path_right)
+ else:
+ from bzrlib._dirstate_helpers_py import _bisect_path_right_py
+ self.assertIs(_bisect_path_right_py, dirstate._bisect_path_right)
+
+ def test_cmp_by_dirs(self):
+ if CompiledDirstateHelpersFeature.available():
+ from bzrlib._dirstate_helpers_c import cmp_by_dirs_c
+ self.assertIs(cmp_by_dirs_c, dirstate.cmp_by_dirs)
+ else:
+ from bzrlib._dirstate_helpers_py import cmp_by_dirs_py
+ self.assertIs(cmp_by_dirs_py, dirstate.cmp_by_dirs)
+
+ def test__read_dirblocks(self):
+ if CompiledDirstateHelpersFeature.available():
+ from bzrlib._dirstate_helpers_c import _read_dirblocks_c
+ self.assertIs(_read_dirblocks_c, dirstate._read_dirblocks)
+ else:
+ from bzrlib._dirstate_helpers_py import _read_dirblocks_py
+ self.assertIs(_read_dirblocks_py, dirstate._read_dirblocks)
+
More information about the bazaar-commits
mailing list