Merge lp:~gz/brz/unlazy_reg into lp:brz

Proposed by Martin Packman
Status: Merged
Approved by: Martin Packman
Approved revision: no longer in the source branch.
Merge reported by: The Breezy Bot
Merged at revision: not available
Proposed branch: lp:~gz/brz/unlazy_reg
Merge into: lp:brz
Diff against target: 231 lines (+13/-97)
7 files modified
breezy/__init__.py (+0/-12)
breezy/lazy_regex.py (+2/-39)
breezy/log.py (+5/-3)
breezy/plugins/grep/grep.py (+2/-5)
breezy/tests/test_lazy_regex.py (+4/-31)
breezy/tests/test_source.py (+0/-5)
brz (+0/-2)
To merge this branch: bzr merge lp:~gz/brz/unlazy_reg
Reviewer Review Type Date Requested Status
Jelmer Vernooij Approve
Review via email: mp+358919@code.launchpad.net

Commit message

Remove replacement of re.compile for lazy_regexp

Description of the change

Remove replacement of re.compile for lazy_regexp

On a typical invocation less than 10 regexp compilations are avoided
and most of those are in configobj. This is less than a millisecond
saving in general, which makes the complexity and other potential
issues with monkey patching probably not worth while.

Leave the LazyRegexp proxy object around which is used in various
places and generally harmless when expected.

To post a comment you must log in.
Revision history for this message
Jelmer Vernooij (jelmer) :
review: Approve
Revision history for this message
The Breezy Bot (the-breezy-bot) wrote :
Revision history for this message
The Breezy Bot (the-breezy-bot) wrote :

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'breezy/__init__.py'
2--- breezy/__init__.py 2018-11-11 04:08:32 +0000
3+++ breezy/__init__.py 2018-11-16 19:10:57 +0000
4@@ -112,18 +112,6 @@
5 return main_version + sub_string
6
7
8-# lazy_regex import must be done after _format_version_tuple definition
9-# to avoid "no attribute '_format_version_tuple'" error when using
10-# deprecated_function in the lazy_regex module.
11-if getattr(sys, '_brz_lazy_regex', False):
12- # The 'brz' executable sets _brz_lazy_regex. We install the lazy regex
13- # hack as soon as possible so that as much of the standard library can
14- # benefit, including the 'string' module.
15- del sys._brz_lazy_regex
16- import breezy.lazy_regex
17- breezy.lazy_regex.install_lazy_compile()
18-
19-
20 __version__ = _format_version_tuple(version_info)
21 version_string = __version__
22
23
24=== modified file 'breezy/lazy_regex.py'
25--- breezy/lazy_regex.py 2018-11-16 18:59:44 +0000
26+++ breezy/lazy_regex.py 2018-11-16 19:10:57 +0000
27@@ -54,7 +54,7 @@
28 __slots__ = ['_real_regex', '_regex_args', '_regex_kwargs',
29 ] + _regex_attributes_to_copy
30
31- def __init__(self, args=(), kwargs={}):
32+ def __init__(self, args, kwargs):
33 """Create a new proxy object, passing in the args to pass to re.compile
34
35 :param args: The `*args` to pass to re.compile
36@@ -74,7 +74,7 @@
37 def _real_re_compile(self, *args, **kwargs):
38 """Thunk over to the original re.compile"""
39 try:
40- return _real_re_compile(*args, **kwargs)
41+ return re.compile(*args, **kwargs)
42 except re.error as e:
43 # raise InvalidPattern instead of re.error as this gives a
44 # cleaner message to the user.
45@@ -111,40 +111,3 @@
46 :return: a LazyRegex proxy object.
47 """
48 return LazyRegex(args, kwargs)
49-
50-
51-def install_lazy_compile():
52- """Make lazy_compile the default compile mode for regex compilation.
53-
54- This overrides re.compile with lazy_compile. To restore the original
55- functionality, call reset_compile().
56- """
57- re.compile = lazy_compile
58-
59-
60-def reset_compile():
61- """Restore the original function to re.compile().
62-
63- It is safe to call reset_compile() multiple times, it will always
64- restore re.compile() to the value that existed at import time.
65- Though the first call will reset back to the original (it doesn't
66- track nesting level)
67- """
68- re.compile = _real_re_compile
69-
70-
71-_real_re_compile = re.compile
72-if _real_re_compile is lazy_compile:
73- raise AssertionError(
74- "re.compile has already been overridden as lazy_compile, but this "
75- "would cause infinite recursion")
76-
77-
78-# Some libraries calls re.finditer which fails it if receives a LazyRegex.
79-if getattr(re, 'finditer', False):
80- def finditer_public(pattern, string, flags=0):
81- if isinstance(pattern, LazyRegex):
82- return pattern.finditer(string)
83- else:
84- return _real_re_compile(pattern, flags).finditer(string)
85- re.finditer = finditer_public
86
87=== modified file 'breezy/log.py'
88--- breezy/log.py 2018-11-16 18:33:17 +0000
89+++ breezy/log.py 2018-11-16 19:10:57 +0000
90@@ -65,6 +65,7 @@
91 controldir,
92 diff,
93 foreign,
94+ lazy_regex,
95 revision as _mod_revision,
96 )
97 from breezy.i18n import gettext, ngettext
98@@ -907,7 +908,8 @@
99 """
100 if not match:
101 return log_rev_iterator
102- searchRE = [(k, [re.compile(x, re.IGNORECASE) for x in v])
103+ # Use lazy_compile so mapping to InvalidPattern error occurs.
104+ searchRE = [(k, [lazy_regex.lazy_compile(x, re.IGNORECASE) for x in v])
105 for k, v in match.items()]
106 return _filter_re(searchRE, log_rev_iterator)
107
108@@ -928,14 +930,14 @@
109 }
110 strings[''] = [item for inner_list in strings.values()
111 for item in inner_list]
112- for (k, v) in searchRE:
113+ for k, v in searchRE:
114 if k in strings and not _match_any_filter(strings[k], v):
115 return False
116 return True
117
118
119 def _match_any_filter(strings, res):
120- return any(re.search(s) for re in res for s in strings)
121+ return any(r.search(s) for r in res for s in strings)
122
123
124 def _make_delta_filter(branch, generate_delta, search, log_rev_iterator,
125
126=== modified file 'breezy/plugins/grep/grep.py'
127--- breezy/plugins/grep/grep.py 2018-11-16 18:33:17 +0000
128+++ breezy/plugins/grep/grep.py 2018-11-16 19:10:57 +0000
129@@ -120,14 +120,11 @@
130
131
132 def compile_pattern(pattern, flags=0):
133- patternc = None
134 try:
135- # use python's re.compile as we need to catch re.error in case of bad pattern
136- lazy_regex.reset_compile()
137- patternc = re.compile(pattern, flags)
138+ return re.compile(pattern, flags)
139 except re.error as e:
140 raise errors.BzrError("Invalid pattern: '%s'" % pattern)
141- return patternc
142+ return None
143
144
145 def is_fixed_string(s):
146
147=== modified file 'breezy/tests/test_lazy_regex.py'
148--- breezy/tests/test_lazy_regex.py 2018-11-11 04:08:32 +0000
149+++ breezy/tests/test_lazy_regex.py 2018-11-16 19:10:57 +0000
150@@ -48,8 +48,7 @@
151 return super(InstrumentedLazyRegex, self).__getattr__(attr)
152
153 def _real_re_compile(self, *args, **kwargs):
154- self._actions.append(('_real_re_compile',
155- args, kwargs))
156+ self._actions.append(('_real_re_compile', args, kwargs))
157 return super(InstrumentedLazyRegex, self)._real_re_compile(
158 *args, **kwargs)
159
160@@ -61,7 +60,7 @@
161 actions = []
162 InstrumentedLazyRegex.use_actions(actions)
163
164- pattern = InstrumentedLazyRegex(args=('foo',))
165+ pattern = InstrumentedLazyRegex(args=('foo',), kwargs={})
166 actions.append(('created regex', 'foo'))
167 # This match call should compile the regex and go through __getattr__
168 pattern.match('foo')
169@@ -135,31 +134,5 @@
170 lazy_pattern = lazy_regex.lazy_compile('[,;]+')
171 pickled = pickle.dumps(lazy_pattern)
172 unpickled_lazy_pattern = pickle.loads(pickled)
173- self.assertEqual(['x', 'y', 'z'],
174- unpickled_lazy_pattern.split('x,y;z'))
175-
176-
177-class TestInstallLazyCompile(tests.TestCase):
178- """Tests for lazy compiled regexps.
179-
180- Other tests, and breezy in general, count on the lazy regexp compiler
181- being installed, and this is done by loading breezy. So these tests
182- assume it is installed, and leave it installed when they're done.
183- """
184-
185- def test_install(self):
186- # Don't count on it being present
187- lazy_regex.install_lazy_compile()
188- pattern = re.compile('foo')
189- self.assertIsInstance(pattern, lazy_regex.LazyRegex)
190-
191- def test_reset(self):
192- lazy_regex.reset_compile()
193- self.addCleanup(lazy_regex.install_lazy_compile)
194- pattern = re.compile('foo')
195- self.assertFalse(isinstance(pattern, lazy_regex.LazyRegex),
196- 'lazy_regex.reset_compile() did not restore the original'
197- ' compile() function %s' % (type(pattern),))
198- # but the returned object should still support regex operations
199- m = pattern.match('foo')
200- self.assertEqual('foo', m.group())
201+ self.assertEqual(
202+ ['x', 'y', 'z'], unpickled_lazy_pattern.split('x,y;z'))
203
204=== modified file 'breezy/tests/test_source.py'
205--- breezy/tests/test_source.py 2018-11-16 18:59:44 +0000
206+++ breezy/tests/test_source.py 2018-11-16 19:10:57 +0000
207@@ -325,11 +325,6 @@
208 self.fail('\n\n'.join(problems))
209
210 def test_flake8(self):
211- # Disable lazy_regex, since flake8 uses sre_compile which can't handle
212- # lazy_regex compile objects.
213- from .. import lazy_regex
214- lazy_regex.reset_compile()
215- self.addCleanup(lazy_regex.install_lazy_compile)
216 self.requireFeature(features.flake8)
217 # Older versions of flake8 don't support the 'paths'
218 # variable
219
220=== modified file 'brz'
221--- brz 2018-03-24 15:16:49 +0000
222+++ brz 2018-11-16 19:10:57 +0000
223@@ -58,8 +58,6 @@
224 sys._brz_default_fs_enc = "utf8"
225
226
227-# instruct breezy/__init__.py to install lazy_regex
228-sys._brz_lazy_regex = True
229 try:
230 import breezy
231 except ImportError as e:

Subscribers

People subscribed via source and target branches