Rev 6004: (mbp) use explicit lazy regexps when appropriate (bug 608054) (Martin Pool) in file:///home/pqm/archives/thelove/bzr/%2Btrunk/

Canonical.com Patch Queue Manager pqm at pqm.ubuntu.com
Thu Jun 30 16:48:14 UTC 2011


At file:///home/pqm/archives/thelove/bzr/%2Btrunk/

------------------------------------------------------------
revno: 6004 [merge]
revision-id: pqm at pqm.ubuntu.com-20110630164811-kpfgfqyzdzxnn8q6
parent: pqm at pqm.ubuntu.com-20110630160619-3022zmfchft893nt
parent: mbp at canonical.com-20110628223941-rnxnukj963t1goft
committer: Canonical.com Patch Queue Manager <pqm at pqm.ubuntu.com>
branch nick: +trunk
timestamp: Thu 2011-06-30 16:48:11 +0000
message:
  (mbp) use explicit lazy regexps when appropriate (bug 608054) (Martin Pool)
modified:
  bzrlib/config.py               config.py-20051011043216-070c74f4e9e338e8
  bzrlib/globbing.py             glob.py-20061113075651-q63o2v35fm2ydk9x-1
  bzrlib/lazy_regex.py           lazy_regex.py-20061009091222-fyettq6z5qomdl9e-1
  bzrlib/log.py                  log.py-20050505065812-c40ce11702fe5fb1
  bzrlib/tests/test_lazy_regex.py test_lazy_regex.py-20061009091222-fyettq6z5qomdl9e-2
=== modified file 'bzrlib/config.py'
--- a/bzrlib/config.py	2011-06-22 12:23:03 +0000
+++ b/bzrlib/config.py	2011-06-30 16:48:11 +0000
@@ -88,6 +88,7 @@
     bzrdir,
     debug,
     errors,
+    lazy_regex,
     lockdir,
     mail_client,
     mergetools,
@@ -2943,9 +2944,10 @@
             raise errors.NoSuchConfigOption(name)
 
     def _show_matching_options(self, name, directory, scope):
-        name = re.compile(name)
+        name = lazy_regex.lazy_compile(name)
         # We want any error in the regexp to be raised *now* so we need to
-        # avoid the delay introduced by the lazy regexp.
+        # avoid the delay introduced by the lazy regexp.  But, we still do
+        # want the nicer errors raised by lazy_regex.
         name._compile_and_collapse()
         cur_conf_id = None
         cur_section = None

=== modified file 'bzrlib/globbing.py'
--- a/bzrlib/globbing.py	2010-07-21 10:17:11 +0000
+++ b/bzrlib/globbing.py	2011-06-28 22:25:28 +0000
@@ -1,4 +1,4 @@
-# Copyright (C) 2006-2010 Canonical Ltd
+# Copyright (C) 2006-2011 Canonical Ltd
 
 # This program is free software; you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
@@ -22,7 +22,10 @@
 
 import re
 
-from bzrlib import errors
+from bzrlib import (
+    errors,
+    lazy_regex,
+    )
 from bzrlib.trace import (
     mutter,
     warning,
@@ -38,7 +41,7 @@
     must not contain capturing groups.
     """
 
-    _expand = re.compile(ur'\\&')
+    _expand = lazy_regex.lazy_compile(ur'\\&')
 
     def __init__(self, source=None):
         self._pat = None
@@ -74,7 +77,7 @@
 
     def __call__(self, text):
         if not self._pat:
-            self._pat = re.compile(
+            self._pat = lazy_regex.lazy_compile(
                     u'|'.join([u'(%s)' % p for p in self._pats]),
                     re.UNICODE)
         return self._pat.sub(self._do_sub, text)
@@ -217,9 +220,13 @@
 
     def _add_patterns(self, patterns, translator, prefix=''):
         while patterns:
-            grouped_rules = ['(%s)' % translator(pat) for pat in patterns[:99]]
+            grouped_rules = [
+                '(%s)' % translator(pat) for pat in patterns[:99]]
             joined_rule = '%s(?:%s)$' % (prefix, '|'.join(grouped_rules))
-            self._regex_patterns.append((re.compile(joined_rule, re.UNICODE),
+            # Explicitly use lazy_compile here, because we count on its
+            # nicer error reporting.
+            self._regex_patterns.append((
+                lazy_regex.lazy_compile(joined_rule, re.UNICODE),
                 patterns[:99]))
             patterns = patterns[99:]
 
@@ -275,7 +282,7 @@
         translator = Globster.pattern_info[Globster.identify(pattern)]["translator"]
         tpattern = '(%s)' % translator(pattern)
         try:
-            re_obj = re.compile(tpattern, re.UNICODE)
+            re_obj = lazy_regex.lazy_compile(tpattern, re.UNICODE)
             re_obj.search("") # force compile
         except errors.InvalidPattern, e:
             result = False
@@ -334,7 +341,7 @@
                 Globster.pattern_info[t]["prefix"])
 
 
-_slashes = re.compile(r'[\\/]+')
+_slashes = lazy_regex.lazy_compile(r'[\\/]+')
 def normalize_pattern(pattern):
     """Converts backslashes in path patterns to forward slashes.
 

=== modified file 'bzrlib/lazy_regex.py'
--- a/bzrlib/lazy_regex.py	2011-05-18 16:42:48 +0000
+++ b/bzrlib/lazy_regex.py	2011-06-13 22:39:49 +0000
@@ -16,8 +16,12 @@
 
 """Lazily compiled regex objects.
 
-This module defines a class which creates proxy objects for regex compilation.
-This allows overriding re.compile() to return lazily compiled objects.
+This module defines a class which creates proxy objects for regex
+compilation.  This allows overriding re.compile() to return lazily compiled
+objects.  
+
+We do this rather than just providing a new interface so that it will also
+be used by existing Python modules that create regexs.
 """
 
 import re

=== modified file 'bzrlib/log.py'
--- a/bzrlib/log.py	2011-06-30 13:18:18 +0000
+++ b/bzrlib/log.py	2011-06-30 16:48:11 +0000
@@ -79,6 +79,7 @@
 """)
 
 from bzrlib import (
+    lazy_regex,
     registry,
     )
 from bzrlib.osutils import (
@@ -863,7 +864,7 @@
     """
     if search is None:
         return log_rev_iterator
-    searchRE = re.compile(search, re.IGNORECASE)
+    searchRE = lazy_regex.lazy_compile(search, re.IGNORECASE)
     return _filter_message_re(searchRE, log_rev_iterator)
 
 

=== modified file 'bzrlib/tests/test_lazy_regex.py'
--- a/bzrlib/tests/test_lazy_regex.py	2011-05-13 12:51:05 +0000
+++ b/bzrlib/tests/test_lazy_regex.py	2011-06-28 22:39:41 +0000
@@ -1,4 +1,4 @@
-# Copyright (C) 2006 Canonical Ltd
+# Copyright (C) 2006, 2011 Canonical Ltd
 #
 # This program is free software; you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
@@ -41,7 +41,8 @@
     def _real_re_compile(self, *args, **kwargs):
         self._actions.append(('_real_re_compile',
                                                args, kwargs))
-        return super(InstrumentedLazyRegex, self)._real_re_compile(*args, **kwargs)
+        return super(InstrumentedLazyRegex, self)._real_re_compile(
+            *args, **kwargs)
 
 
 class TestLazyRegex(tests.TestCase):
@@ -116,23 +117,26 @@
 
 
 class TestInstallLazyCompile(tests.TestCase):
+    """Tests for lazy compiled regexps.
 
-    def setUp(self):
-        super(TestInstallLazyCompile, self).setUp()
-        self.addCleanup(lazy_regex.reset_compile)
+    Other tests, and bzrlib in general, count on the lazy regexp compiler
+    being installed, and this is done by loading bzrlib.  So these tests
+    assume it is installed, and leave it installed when they're done.
+    """
 
     def test_install(self):
+        # Don't count on it being present
         lazy_regex.install_lazy_compile()
         pattern = re.compile('foo')
         self.assertIsInstance(pattern, lazy_regex.LazyRegex)
 
     def test_reset(self):
-        lazy_regex.install_lazy_compile()
         lazy_regex.reset_compile()
+        self.addCleanup(lazy_regex.install_lazy_compile)
         pattern = re.compile('foo')
         self.assertFalse(isinstance(pattern, lazy_regex.LazyRegex),
-                    'lazy_regex.reset_compile() did not restore the original'
-                    ' compile() function %s' % (type(pattern),))
+            'lazy_regex.reset_compile() did not restore the original'
+            ' compile() function %s' % (type(pattern),))
         # but the returned object should still support regex operations
         m = pattern.match('foo')
         self.assertEqual('foo', m.group())




More information about the bazaar-commits mailing list